所以,我试图在有向图中找到一个使用DFS的循环。现在,我知道如果图的拓扑排序是不可能的,那么图就包含一个循环。我为拓扑排序做了以下算法。我不知道应该在哪里修改此代码,以便return true
或false
以及检查我的图是否包含循环。这里是我使用的算法:使用DFS检测有向图中的周期?
DFS-Loop (Graph G)
1. Mark all vertices as unexplored.
2. Set label = n // number of vertices
3. For each vertex V (belonging to) G
-- If V not yet explored,
-- DFS (G,V)
4. Set f(V) = current_label // here, f(V) is basically the position of V in the ordering
5. current_label--
Where DFS(G,V) will be something like:
1. Mark V as explored.
2. For every vertex K belonging to Adj.(V)
-- If K is not explored,
-- Call DFS(G,K)
我应该在哪里添加此检查是否包含循环?
谢谢!
顺便说一句,为什么C++标签? – Petr