Repository navigation
Expand file tree
/
Copy pathhopcroftKarp.cpp
More file actions
117 lines (95 loc) · 1.94 KB
/
Copy pathhopcroftKarp.cpp
File metadata and controls
117 lines (95 loc) · 1.94 KB
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
#include <bits/stdc++.h>
using namespace std;
list <int>Adj[100];
int n;
int distX[100];
int distY[100];
int mateX[100];
int mateY[100]; // O(E*sqrt(V))
void resetDist(){
for(int i=1;i<=n;i++){
distX[i]=0;
distY[i]=0;
}
return;
}
void resetMate(){
for(int i=1;i<=n;i++){
mateX[i]=-1;
mateY[i]=-1;
}
return;
}
// checking if augmenting path with start vertex 'u' exists or not using distX & distY.
// setting up mateX & mateY recursively
bool dfs(int u){
for(auto it=Adj[u].begin();it!=Adj[u].end();it++){
int v=*(it);
if(distY[v]==distX[u]+1){
distY[v]=0; //* frees v from getting selected again
if(mateY[v]==-1 || dfs(mateY[v])){
mateX[u]=v;
mateY[v]=u;
return true;
}
}
}
return false;
}
// checking if any augmenting pth exists. If yes then setting up distX,distY for retracing later.
bool bfs(){
bool found=false;
queue<int> Q;
for(int i=1;i<=n;i++){
if(mateX[i]==-1){
Q.push(i);
}
}
resetDist();
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(auto it=Adj[u].begin();it!=Adj[u].end();it++){
int v=*(it);
// distY[v]>0 suggests having a mateY[v]!=-1 or already having a mate in X side set in the surrent bfs()
// distY[v]==0 can still means that it can have a mate in X set cz of resetDist() called prior to while loop
if(distY[v]==0){
distY[v]=distX[u]+1;
if(mateY[v]==-1){found=true;}
else{
distX[mateY[v]]=distY[v]+1;
Q.push(mateY[v]);
}
}
}
}
return found;
}
int hopcroftKarp(){
int res=0;
while(bfs()){
for(int i=1;i<=n;i++){
if(mateX[i]==-1){
if(dfs(i)){res++;}
}
}
}
return res;
}
void addEdge(int a,int b){
Adj[a].push_back(b);
return;
}
int main(){
n=8;
addEdge(1, 6);
addEdge(1, 7);
addEdge(2, 5);
addEdge(3, 6);
addEdge(4, 6);
addEdge(4, 8);
resetDist();
resetMate();
cout<<hopcroftKarp()<<"\n";
return 0;
}