Finding a cycle in an undirected graph vs finding one in a directed graph

前端 未结 1 817
猫巷女王i
猫巷女王i 2021-02-04 07:18

So I\'m reading Robert Sedgewick\'s Algorithms 4th ed. book and the methods for finding a cycle in a directed graph is different than the one for finding a cycle in an undirecte

1条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 07:42

    You can't use the same algorithm: The algorithm above simply explores all connected components of the graph. If you encounter an already marked vertex, there must be two different paths to reach it, and in an undirected graph there must be a cycle. If not, you can continue with the next connected component - no need to clean up the component you just finished.

    On the other hand, if you have a directed graph, two different paths to the same vertex don't make a cycle. So you need a different algorithm (for example, you may need to clean up any steps you back track.)

    0 讨论(0)
提交回复
热议问题