-
Notifications
You must be signed in to change notification settings - Fork 3
/
hopcroft_karp.cpp
128 lines (101 loc) · 2.1 KB
/
hopcroft_karp.cpp
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
/// Hopcroft-Karp Algorithm ///
#include<bits/stdc++.h>
using namespace std;
list<int> *G;
bool bfs(int *setU, int *setV, int *dist, int m)
{
queue<int> que;
for (int u=1; u<=m; u++)
{
if (setU[u]==0)
{
dist[u] = 0;
que.push(u);
}
else dist[u] = INT_MAX;
}
dist[0] = INT_MAX;
while (!que.empty())
{
int u = que.front();
que.pop();
if (dist[u] < dist[0])
{
list<int>::iterator i;
for (i=G[u].begin(); i!=G[u].end(); ++i)
{
int v = *i;
if (dist[setV[v]] == INT_MAX)
{
dist[setV[v]] = dist[u] + 1;
que.push(setV[v]);
}
}
}
}
return (dist[0] != INT_MAX);
}
bool dfs(int u, int *setU, int *setV, int *dist)
{
if (u != 0)
{
list<int>::iterator i;
for (i=G[u].begin(); i!=G[u].end(); ++i) {
int v = *i;
if ((dist[setV[v]] == dist[u]+1) && dfs(setV[v], setU, setV, dist))
{
setU[u] = v;
setV[v] = u;
return true;
}
}
dist[u] = INT_MAX;
return false;
}
return true;
}
void hopcroft_karp(int m, int n)
{
int setU[m+1], setV[n+1], dist[m+1];
for (int u=0; u<=m; u++)
setU[u] = 0;
for (int v=0; v<=n; v++)
setV[v] = 0;
int cnt = 0;
while (bfs(setU, setV, dist, m)) {
for (int u=1; u<=m; u++) {
if (!setU[u] && dfs(u, setU, setV, dist))
cnt++;
}
}
cout << "\nMaximum Matching:" << endl;
for(int i=1; i<cnt+1; i++)
cout << "u" << setU[i] << " - v" << setV[i] << endl;
cout << "\nCount: " << cnt << endl;
}
int main()
{
int n;
cout << "Enter no. of edges: " << flush;
cin >> n;
G = new list<int>[n+1];
cout << "Enter space seperated end-points for each edge (start at 1) \n" << endl;
cout << "U V" << endl;
int u_max=0, v_max=0;
for(int i=0; i<n; i++){
int u, v;
cin >> u >> v;
if(u>u_max) u_max = u;
if(v>v_max) v_max = v;
G[u].push_back(v);
}
hopcroft_karp(u_max, v_max);
// Example 1 Example 2
// u1 - v2 u1 - v1
// u1 - v3 u1 - v3
// u2 - v1 u2 - v3
// u3 - v2 u3 - v4
// u4 - v2 u4 - v3
// u4 - v4 u4 - v2
return 0;
}