-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0017_MaximumBipartiteMatching.cc
263 lines (227 loc) · 7.52 KB
/
0017_MaximumBipartiteMatching.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "../Headers/0003_Graph/0017_MaximumBipartiteMatching.h"
#include<climits>
#include<queue>
using namespace std;
namespace MaximumBipartiteMatching
{
// Graph Private Member Methods
void Graph::ResolveAntiParallelEdges()
{
int countParallelEdges = 0;
for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if (this->_adjMatrix[i][j] > 0 && this->_adjMatrix[j][i] > 0)
{
countParallelEdges++;
}
}
}
// As i->j and j->i both edges has been counted, actual count is count = count / 2
countParallelEdges /= 2;
this->_flagParallelEdges = countParallelEdges > 0;
// If there are no anti-parallel edges, no need to modify the adjMatrix
if (!this->_flagParallelEdges)
{
return;
}
int newNoOfVertices = this->_noOfVertices + countParallelEdges;
// Modifying the adjMatrix
for (auto& edge : this->_adjMatrix)
{
edge.resize(newNoOfVertices, 0);
}
int k = this->_noOfVertices;
this->_visited.resize(newNoOfVertices, false);
this->_parent.resize(newNoOfVertices, -1);
this->_adjMatrix.resize(newNoOfVertices, vector<int>(newNoOfVertices, 0));
// Removing the anti-parallel edges by adding new nodes
for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if (this->_adjMatrix[i][j] > 0 && this->_adjMatrix[j][i] > 0)
{
this->_adjMatrix[i][k] = this->_adjMatrix[i][j];
this->_adjMatrix[k][j] = this->_adjMatrix[i][j];
this->_adjMatrix[i][j] = 0;
k++;
}
}
}
// Updating the total no of vertices after modifying the adjMatrix
this->_noOfVertices = newNoOfVertices;
}
// This method is used to color the vertices of the graph to determine if the given graph is bipartite or not
void Graph::ColorGraph()
{
// Color of all the vertices are initialised to WHITE
fill(this->_color.begin(), this->_color.end(), WHITE);
// Queue to hold the vertices
queue<int> nodeQueue;
for (int node = 0; node < this->_noOfVertices; node++)
{
// Check if the node is already not colored
if (this->_color[node] == WHITE)
{
// The color of the node is set to RED
this->_color[node] = RED;
// The node is inserted into the queue
nodeQueue.push(node);
// Using BFS method to color all the vertices
while (!nodeQueue.empty())
{
int nodeU = nodeQueue.front();
nodeQueue.pop();
// Iterating over G.Adj[nodeU]
for (int nodeV = 0; nodeV < this->_noOfVertices; nodeV++)
{
// As there are no self loops, continue
if (nodeU == nodeV)
{
continue;
}
// Check if there is an edge u --> v and nodeV is not colored yet
else if (this->_residualGraph[nodeU][nodeV] != 0 && this->_color[nodeV] == WHITE)
{
// Set the color of nodeV opposite of nodeU
this->_color[nodeV] = 1 - this->_color[nodeU];
// Insert the nodeV into the queue
nodeQueue.push(nodeV);
}
// Check if there is an edge u --> v and nodeV is of same color as nodeU
else if (this->_residualGraph[nodeU][nodeV] != 0 && this->_color[nodeV] == this->_color[nodeU])
{
// Set the _isBipartite flag to false and return
this->_isBipartite = false;
return;
}
}
}
}
}
// If the above operation completes without returning yet that indicates the graph is bipartite
// Set the _isBipartite flag to true and return
this->_isBipartite = true;
return;
}
// This method is used to create the additional edges
// from the source vertex to the RED colored vertices and
// from the BLUE colored vertices to the sink vertex
void Graph::AddAdditionalEdges()
{
// Resizing the residual graph to accomodate space for the new edges
for (auto& edge : this->_residualGraph)
{
edge.resize(this->_noOfVertices, 0);
}
this->_parent.resize(this->_noOfVertices, -1);
this->_visited.resize(this->_noOfVertices, false);
this->_color.resize(this->_noOfVertices, WHITE);
this->_residualGraph.resize(this->_noOfVertices, vector<int>(this->_noOfVertices, 0));
// Creating the additional edges
for (int node = 0; node < this->_source; node++)
{
// From source vertex --> RED colored vertices
if (this->_color[node] == RED)
{
this->_residualGraph[this->_source][node] = 1;
}
// From BLUE colored vertices --> sink vertex
else if (this->_color[node] == BLUE)
{
this->_residualGraph[node][this->_sink] = 1;
}
}
}
// Implementation of BreadthFirstSearch for EdmondsKarp algorithm to find the path from source to sink
bool Graph::BreadthFirstSearch()
{
// Resetting the visited values
fill(this->_visited.begin(), this->_visited.end(), false);
// Resetting the parent values
fill(this->_parent.begin(), this->_parent.end(), -1);
queue<int> nodeQueue;
nodeQueue.push(this->_source);
this->_visited[this->_source] = true;
while (!nodeQueue.empty())
{
int nodeU = nodeQueue.front();
nodeQueue.pop();
for (int nodeV = 0; nodeV < this->_noOfVertices; nodeV++)
{
if (!this->_visited[nodeV] && this->_residualGraph[nodeU][nodeV] > 0)
{
this->_parent[nodeV] = nodeU;
this->_visited[nodeV] = true;
nodeQueue.push(nodeV);
}
}
}
// Returning the visited value of the sink vertex, initially it was set to false
return this->_visited[this->_sink];
}
// Graph Public Member Methods
void Graph::CreateGraph(int noOfVertices)
{
this->_noOfVertices = noOfVertices;
this->_maximumFlow = 0;
this->_flagParallelEdges = false;
this->_adjMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, 0));
this->_parent = vector<int>(this->_noOfVertices, -1);
this->_visited = vector<bool>(this->_noOfVertices, false);
this->_color = vector<int>(this->_noOfVertices, WHITE);
}
void Graph::PushDirectedEdge(int valueU, int valueV)
{
this->_adjMatrix[valueU][valueV] = 1;
}
int Graph::FindMaximumBipartiteMatching()
{
// Resolving all the parallel edges if present
this->ResolveAntiParallelEdges();
this->_residualGraph = this->_adjMatrix;
this->ColorGraph();
this->_source = this->_noOfVertices;
this->_noOfVertices++;
this->_sink = this->_noOfVertices;
this->_noOfVertices++;
this->AddAdditionalEdges();
// While there exists a path p from source to sink in the residual network G'
while (this->BreadthFirstSearch())
{
int augmentedPathFlow = 1;
// No need to find the minimum amount of augmentedPathFlow as like standard EdmondsKarp algorithm
// as here capacity of each edges is 1
for (int nodeV = this->_sink; nodeV != this->_source; nodeV = this->_parent[nodeV])
{
int nodeU = this->_parent[nodeV];
this->_residualGraph[nodeU][nodeV] -= augmentedPathFlow;
this->_residualGraph[nodeV][nodeU] += augmentedPathFlow;
}
this->_maximumFlow += augmentedPathFlow;
}
return this->_maximumFlow;
}
// This method is used for finding the matchings
vector<vector<int>> Graph::GetMatchings()
{
for (int nodeU = 0; nodeU < this->_adjMatrix.size(); nodeU++)
{
for (int nodeV = 0; nodeV < this->_adjMatrix.size(); nodeV++)
{
// Check if the nodeU and nodeV are not source or sink
// and there is a flow of 1 unit from nodeU --> nodeV
// which means nodeU --> nodeV is being used for the maximum flow (maximum matching)
if ((nodeU != this->_source || nodeU != this->_sink || nodeV != this->_source || nodeV != this->_sink)
&&
(this->_adjMatrix[nodeU][nodeV] - this->_residualGraph[nodeU][nodeV]) == 1)
{
this->_matchings.push_back({ nodeU, nodeV });
}
}
}
return this->_matchings;
}
}