Detecting all circles in a graph

后端 未结 2 627
太阳男子
太阳男子 2021-02-10 07:49

I have a directed graph stored in a Map data structure, where the key is the node\'s ID and the [value] is the array of the nodeIds of the nodes which are pointed by the key no

2条回答
  •  臣服心动
    2021-02-10 08:44

    Since a circle in a directed graph represents a strongly connected component, you can use any of the algorithms referenced on the wikipedia page for finding strongly connected components https://en.wikipedia.org/wiki/Strongly_connected_component - for instance Tarjan's algorithm which is based on depth-first search: https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm

    Edit: It is possible that a strongly connected component contains multiple circles that share nodes with each other. So, a manual checking of each component must follow, but should be easy.

提交回复
热议问题