1+ #include " ../Headers/0003_Graph/0019_MaximumFlowRelabelToFront.h"
2+ #include < climits>
3+ #include < iterator>
4+ using namespace std ;
5+
6+ namespace MaximumFlowRelabelToFront
7+ {
8+ // Graph Private Member Methods
9+
10+ // Initializes Pre-Flow in the given Flow Network
11+ void Graph::InitializePreflow ()
12+ {
13+ // The height of source is set to highest possible height value
14+ this ->_height [this ->_source ] = this ->_noOfVertices ;
15+
16+ // Iterating over all the vertices
17+ for (int i = 0 ; i < this ->_noOfVertices ; i++)
18+ {
19+ // For the all the edges (source, v)
20+ if (this ->_residualGraph [this ->_source ][i] > 0 )
21+ {
22+ // v.excessFlow = capacity(source, v)
23+ this ->_excessFlow [i] = this ->_residualGraph [this ->_source ][i];
24+
25+ // source.excessFlow = source.excessFlow - capacity(source, v)
26+ this ->_excessFlow [this ->_source ] = this ->_excessFlow [this ->_source ] - this ->_residualGraph [this ->_source ][i];
27+
28+ // Adjusting the flow and reverse flow along source->v and v->source respectively
29+ this ->_residualGraph [i][this ->_source ] = this ->_residualGraph [this ->_source ][i];
30+ this ->_residualGraph [this ->_source ][i] = 0 ;
31+ }
32+ }
33+ }
34+
35+ void Graph::Discharge (int nodeU)
36+ {
37+ while (this ->_excessFlow [nodeU] > 0 )
38+ {
39+ bool hasPushed = false ;
40+ for (int nodeV = 0 ; nodeV < this ->_noOfVertices ; nodeV++)
41+ {
42+ if (this ->_residualGraph [nodeU][nodeV] > 0 && this ->_height [nodeU] == 1 + this ->_height [nodeV])
43+ {
44+ this ->Push (nodeU, nodeV);
45+ hasPushed = true ;
46+ if (this ->_excessFlow [nodeU] == 0 )
47+ {
48+ break ;
49+ }
50+ }
51+ }
52+
53+ if (!hasPushed)
54+ {
55+ this ->Relabel (nodeU);
56+ }
57+ }
58+ }
59+
60+ // Pushes the flow from nodeU to its neighbour vertices
61+ void Graph::Push (int nodeU, int nodeV)
62+ {
63+ // Calculate the flow amount to be added along the edge and excess flow subtracted from nodeU
64+ int minimumFlow = min (this ->_residualGraph [nodeU][nodeV], this ->_excessFlow [nodeU]);
65+
66+ // Adjust the flow and the reverse flow along (nodeU, nodeV)
67+ this ->_residualGraph [nodeU][nodeV] = this ->_residualGraph [nodeU][nodeV] - minimumFlow;
68+ this ->_residualGraph [nodeV][nodeU] = this ->_residualGraph [nodeV][nodeU] + minimumFlow;
69+
70+ // Adjust the excess flows in nodeU and nodeV
71+ this ->_excessFlow [nodeU] = this ->_excessFlow [nodeU] - minimumFlow;
72+ this ->_excessFlow [nodeV] = this ->_excessFlow [nodeV] + minimumFlow;
73+
74+ }
75+
76+ // Relabels height of vertex nodeU when there are outgoing non-saturated edges available
77+ void Graph::Relabel (int nodeU)
78+ {
79+ int minimumHeight = INT_MAX;
80+
81+ // Iterating over all the vertices
82+ for (int nodeV = 0 ; nodeV < this ->_noOfVertices ; nodeV++)
83+ {
84+ // For G'.Adj[nodeU] select for which nodeV, height[nodeU] <= height[nodeV]
85+ if (this ->_residualGraph [nodeU][nodeV] > 0 && this ->_height [nodeU] <= this ->_height [nodeV])
86+ {
87+ // Get the minimum height among all these G'.Adj[nodeU]
88+ minimumHeight = min (minimumHeight, this ->_height [nodeV]);
89+ }
90+ }
91+
92+ // Set height[nodeU]
93+ this ->_height [nodeU] = minimumHeight + 1 ;
94+ }
95+
96+
97+ // Graph Public Member Methods
98+ void Graph::CreateGraph (int noOfVertices)
99+ {
100+ this ->_noOfVertices = noOfVertices;
101+ this ->_source = 0 ;
102+ this ->_sink = this ->_noOfVertices - 1 ;
103+ this ->_maximumFlow = 0 ;
104+ this ->_adjMatrix = vector<vector<int >>(this ->_noOfVertices , vector<int >(this ->_noOfVertices , 0 ));
105+ this ->_excessFlow = vector<int >(this ->_noOfVertices , 0 );
106+ this ->_height = vector<int >(this ->_noOfVertices , 0 );
107+ this ->_visited = vector<bool >(this ->_noOfVertices , false );
108+ }
109+
110+ void Graph::PushDirectedEdge (int valueU, int valueV, int capacity)
111+ {
112+ this ->_adjMatrix [valueU][valueV] = capacity;
113+ }
114+
115+ int Graph::FindMaximumFlowRelabelToFront ()
116+ {
117+ this ->_residualGraph = this ->_adjMatrix ;
118+
119+ // Initialize Pre-flow
120+ this ->InitializePreflow ();
121+
122+ for (int i = 0 ; i < this ->_noOfVertices ; i++)
123+ {
124+ if (i != this ->_source && i != this ->_sink )
125+ {
126+ this ->_nodeList .push_back (i);
127+ }
128+ }
129+
130+ list<int >::iterator nodeUiterator = this ->_nodeList .begin ();
131+
132+ while (nodeUiterator != this ->_nodeList .end ())
133+ {
134+ int oldHeight = this ->_height [*nodeUiterator];
135+ this ->Discharge (*nodeUiterator);
136+ if (this ->_height [*nodeUiterator] > oldHeight)
137+ {
138+ this ->_nodeList .splice (this ->_nodeList .begin (), this ->_nodeList , nodeUiterator);
139+ nodeUiterator++;
140+ }
141+ else
142+ {
143+ nodeUiterator++;
144+ }
145+ }
146+
147+ // Return the excess flow in the sink vertex which is actually the maximum flow along the given flow network
148+ return this ->_excessFlow [this ->_sink ];
149+ }
150+ }
0 commit comments