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