Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/graphAlgorithms/maxFlo/check.cpp
00001 
00014 #include "stdinc.h"
00015 #include "Flograph.h"
00016 #include "List.h"
00017 
00018 using namespace grafalgo;
00019 
00020 int main() {
00021         vertex u,v; edge e; int sum;
00022         Flograph fg; cin >> fg;
00023 
00024         // verify that capacity constraints are respected
00025         for (e = fg.first(); e != 0; e = fg.next(e)) {
00026                 u = fg.tail(e); v = fg.head(e); string s;
00027                 if (fg.f(u,e) < 0)
00028                         cout << "Negative flow on edge " 
00029                              << e << "=" << fg.edge2string(e,s) << endl;
00030                 if (fg.f(u,e) > fg.cap(u,e))
00031                         cout << "Flow exceeds capacity on edge "
00032                              << e << "=" << fg.edge2string(e,s) << endl;
00033         }
00034 
00035         // verify that flow at each node is balanced
00036         for (u = 1; u <= fg.n(); u++) { 
00037                 if (u == fg.src() || u == fg.snk()) continue;
00038                 sum = 0;
00039                 for (e = fg.firstAt(u); e != 0; e = fg.nextAt(u,e)) {
00040                         sum -= fg.f(u,e);
00041                 }
00042                 if (sum != 0) cout << "Vertex " << u << " is not balanced\n";
00043         }
00044 
00045         // Verify that the flow is maximum by computing hop-count distance
00046         // over all edges that are not saturated. If sink ends up with a
00047         // distance smaller than n, then there is a path to sink with
00048         // non-zero residual capacity.
00049         int *d = new int[fg.n()+1];
00050         for (u = 1; u <= fg.n(); u++) d[u] = fg.n();
00051         d[fg.src()] = 0;
00052         List q(fg.n()); q.addLast(fg.src());
00053         while (!q.empty()) {
00054                 u = q.first(); q.removeFirst();
00055                 for (e = fg.firstAt(u); e != 0; e = fg.nextAt(u,e)) {
00056                         v = fg.mate(u,e);
00057                         if (fg.res(u,e) > 0 && d[v] > d[u] + 1) {
00058                                 d[v] = d[u] + 1; q.addLast(v);
00059                         }
00060                 }
00061         }
00062         if (d[fg.snk()] < fg.n()) printf("Not a maximum flow\n");
00063 }
 All Classes Files Functions Variables Typedefs Friends