Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #include "ppHiLab.h" 00010 00019 ppHiLab::ppHiLab(Flograph& fg1, int& floVal, bool batch) : prePush(fg1,floVal) { 00020 doit(batch); floVal = flowValue(); 00021 } 00022 00034 ppHiLab::ppHiLab(Flograph& fg1, int& floVal, bool batch, string& stats) 00035 : prePush(fg1,floVal) { 00036 doit(batch); floVal = flowValue(); 00037 stringstream ss; 00038 ss << satCount << " " << nonSatCount << " " << newDistCount 00039 << " " << relabCount; 00040 stats = ss.str(); 00041 } 00042 00048 void ppHiLab::doit(bool batch) { 00049 // ubVec[d] is an unbalanced vertex with a distance label of d 00050 // or 0 if there is no unbalanced vertex with a distance label of d 00051 // unbal partitions the vertices into circular lists, where all 00052 // vertices on the same list are unbalanced and have the same 00053 // distance label; balanced vertices each form singleton lists 00054 ubVec = new int[2*fg->n()]; 00055 unbal = new ClistSet(fg->n()); 00056 00057 // initialization 00058 for (int i = 0; i < 2*fg->n(); i++) ubVec[i] = 0; 00059 top = 0; 00060 vertex s = fg->src(); 00061 for (edge e = fg->firstOut(s); e != 0; e = fg->nextOut(s,e)) { 00062 vertex v = fg->head(e); 00063 if (v != fg->snk()) addUnbal(v); 00064 } 00065 00066 vertex u; 00067 if (!batch) { // incremental relabeling 00068 while (top > 0) { 00069 u = removeUnbal(); 00070 if (!balance(u)) { 00071 d[u] = 1 + minlabel(u); 00072 nextedge[u] = fg->firstAt(u); 00073 addUnbal(u); 00074 relabCount++; 00075 } 00076 } 00077 } else { // batch relabeling 00078 while (top > 0) { 00079 while (top > 0) { 00080 u = removeUnbal(); 00081 balance(u); 00082 } 00083 initdist(); 00084 for (u = 1; u <= fg->n(); u++) { 00085 if (u == fg->src() || u == fg->snk()) continue; 00086 nextedge[u] = fg->firstAt(u); 00087 if (excess[u] > 0) addUnbal(u); 00088 } 00089 } 00090 } 00091 delete unbal; delete [] ubVec; 00092 } 00093 00097 void ppHiLab::newUnbal(vertex u) { 00098 if (unbal->suc(u) == u && ubVec[d[u]] != u) addUnbal(u); 00099 } 00100 00105 void ppHiLab::addUnbal(vertex u) { 00106 if (ubVec[d[u]] == 0) ubVec[d[u]] = u; 00107 else unbal->join(ubVec[d[u]],u); 00108 top = max(top,d[u]); 00109 } 00110 00117 vertex ppHiLab::removeUnbal() { 00118 vertex u = ubVec[top]; 00119 vertex v = unbal->suc(u); 00120 unbal->remove(u); 00121 if (v != u) { 00122 ubVec[top] = v; 00123 } else { 00124 ubVec[top] = 0; 00125 while (top > 0 && ubVec[top] == 0) top--; 00126 } 00127 return u; 00128 } 00129