void DirectedDFS::dfs(DirGraph& G, int v){ marked[v] = true; Bag *TempBag = G.Iterator(v); TempBag->BeginIter(); int iter; while(TempBag->HasNext()) if(!marked[(iter = TempBag->Next())]) dfs(G,iter); }
void DepthFirstOrder::dfs(DirGraph &G, int v){ pre.push_back(v); marked[v] = true; Bag *Temp = G.Iterator(v); Temp->BeginIter(); int nextVert; while(Temp->HasNext()){ nextVert = Temp->Next(); if(!marked[nextVert]) dfs(G,nextVert); } post.push_back(v); reversePost.push_front(v); }
void DirectedCycle::dfs(DirGraph& G, int v){ onStack[v] = true; marked[v] = true; Bag *Temp = G.Iterator(v); Temp->BeginIter(); int w; while(Temp->HasNext()){ w = Temp->Next(); if(hasCycle()) return; else{ if(!marked[w]){ edgeTo[w] = v; dfs(G,w); } else if (onStack[w]){ for(int x = v; x != w; x = edgeTo[x]) cycle.push_front(x); cycle.push_front(w); cycle.push_front(v); } } } onStack[v] = false; }