Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/graphAlgorithms/maxFlo/dfsPath.cpp
00001 #include "Adt.h"
00002 #include "dfsPath.h"
00003 
00004 using namespace grafalgo;
00005 
00006 dfsPath::dfsPath(Flograph& fg1, int& floVal) : augPath(fg1,floVal) {
00007 // Find maximum flow in fg using the shortest augment path algorithm.
00008         floVal = 0;
00009         while(true) {
00010                 for (vertex u = 1; u <= fg->n(); u++) pEdge[u] = 0;
00011                 if (!findPath()) break;
00012                 floVal += augment(); 
00013         }
00014 }
00015 
00016 bool dfsPath::findPath(vertex u) {
00017 // Find a shortest path with unused residual capacity.
00018 // This version uses depth-first search
00019         vertex u,v; edge e;
00020 
00021         if (u == fg->snk()) return true;
00022 
00023         for (e = fg->firstAt(u); e != 0; e = fg->nextAt(u,e)) {
00024                 if (e == pEdge[u] || fg->res(u,e) == 0) continue;
00025                 vertex v = fg->mate(u,e);
00026                 if (v != fg->src() && pEdge[v] == 0) {
00027                         pEdge[v] = e;
00028                         if (findPath(v)) return true;
00029                 }
00030         }
00031         return false;
00032 }
 All Classes Files Functions Variables Typedefs Friends