/** Find a negative cost cycle in the residual graph. * @return some vertex on the cycle, or 0 if no negative * cycle is present in the residual graph; The edges in the * cycle are found by traversing the pEdge pointers, starting * at pEdge[returnedVertex]. */ vertex mcfCycRed::findCyc() { vertex u,v,last; edge e; int c[wfg->n()+1]; List q(wfg->n()); for (u = 1; u <= wfg->n(); u++) { pEdge[u] = 0; c[u] = 0; q.addLast(u); } last = q.last(); // each pass completes when last removed from q while (!q.empty()) { u = q.first(); q.removeFirst(); for (e = wfg->firstAt(u); e != 0; e = wfg->nextAt(u,e)) { if (wfg->res(u,e) == 0) continue; v = wfg->mate(u,e); if (c[v] > c[u] + wfg->cost(u,e)) { pEdge[v] = e; c[v] = c[u] + wfg->cost(u,e); if (!q.member(v)) q.addLast(v); } } if (u == last) { v = cycleCheck(); if (v != 0) return v; last = q.last(); } } return 0; }
void DFS_M::dfs(int iRoot) { Stack2< Pair<int,int> > fringe; //Queue< Pair<int,int> > fringe; fringe.push(Pair<int,int>(-1,iRoot)); int precount = 0; // pre-order counter int poscount = 0; // post-order counter int inocount = 0; // in-order counter int P = -1; int forest=0; int cyclecount = 0; // number of cycles found so far while (precount < _graph.getVertexSize()) { while ( !fringe.empty() ) { Pair<int,int> pair = fringe.getTop(); fringe.pop(); int v0 = pair.first(); int v1 = pair.second(); if (v0==-2) { _postorder[v1]=poscount; ++poscount; continue; } if ( !_visited[v1] ) { // first time we see this vertex, push a dummy edge on the fringe // before pushing all its children. When the dummy edge will be popped // that means all the subtrees rooted under this vertex v1, will have been // explored completely, and we can assign the postorder index to the vertex fringe.push(Pair<int,int>(-2,v1)); _preorder[v1]=precount; ++precount; _visited[v1]=1; _parent[v1]=v0; _forest[v1]=forest; if ( v0 > -1 ) { _matrix[v0][v1]='T'; } //std::cout << "(" << v1 << ")" << std::endl; for ( GraphEdgeIter it = _graph.getEdgeIter(v1); !it.end(); ++it ) { int v2 = *it; if (!_visited[v2]) { fringe.push(Pair<int,int>(v1,v2)); } else { // already visited char edgeType = tagEdge(v1,v2); cycleCheck(edgeType, v1, v2, cyclecount); } } } else { // already visited char edgeType = tagEdge(v0,v1); cycleCheck(edgeType, v0, v1, cyclecount); } } if (fringe.empty() && precount<_graph.getVertexSize()) { for (int i=0; i<_graph.getVertexSize(); ++i) { if (!_visited[i]) { fringe.push(Pair<int,int>(-1,i)); break; } } ++forest; } } }