所以,我試圖在有向圖中找到一個使用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