Grafalgo
Library of useful data structures and algorithms
|
00001 #include "Flograph.h" 00002 #include "capScale.h" 00003 00004 using namespace grafalgo; 00005 00006 capScale::capScale(Flograph& fg1, int& floVal) : augPath(fg1,floVal) { 00007 // Find maximum flow in fg using the shortest augment path algorithm. 00008 // initialize scale factor to largest power of 2 00009 // that is <= (max edge capacity)/2 00010 edge e; int maxCap = 0; 00011 for (e = fg->first(); e != 0; e = fg->next(e)) 00012 maxCap = max(maxCap,fg->cap(fg->tail(e),e)); 00013 for (scale = 1; scale <= maxCap/2; scale *= 2) {} 00014 floVal = 0; 00015 while(findPath()) floVal += augment(); 00016 } 00017 00018 bool capScale::findPath() { 00019 // Find a path with sufficient unused residual capacity. 00020 vertex u,v; edge e; 00021 List queue(fg->n()); 00022 00023 while (scale > 0) { 00024 for (u = 1; u <= fg->n(); u++) pEdge[u] = 0; 00025 queue.addLast(fg->src()); 00026 while (!queue.empty()) { 00027 u = queue.first(); queue.removeFirst(); 00028 for (e = fg->firstAt(u); e != 0; e=fg->nextAt(u,e)) { 00029 v = fg->mate(u,e); 00030 if (fg->res(u,e) >= scale && pEdge[v] == 0 00031 && v != fg->src()) { 00032 pEdge[v] = e; 00033 if (v == fg->snk()) return true; 00034 queue.addLast(v); 00035 } 00036 } 00037 } 00038 scale /= 2; 00039 } 00040 return false; 00041 }