2 * Finding the maximum matching in the given bipartite graph using
3 * Hopcroft-Karp algorithm.
8 #define VNIL (MNODES - 1) /* the special null vertex */
11 static int bfs(int **adj
, int *pair
, int *pair2
, int *dist
, int n1
)
16 for (i
= 0; i
< n1
; i
++) {
17 if (adj
[i
][0] >= 0 && pair
[i
] == VNIL
) {
27 for (i
= 0; adj
[v
][i
] >= 0; i
++) {
29 if (dist
[pair2
[u
]] == INF
) {
30 dist
[pair2
[u
]] = dist
[v
] + 1;
35 return dist
[VNIL
] != INF
;
38 static int dfs(int **adj
, int *pair
, int *pair2
, int *dist
, int v
)
43 for (i
= 0; adj
[v
][i
] >= 0; i
++) {
45 if (dist
[pair2
[u
]] == dist
[v
] + 1) {
46 if (dfs(adj
, pair
, pair2
, dist
, pair2
[u
])) {
58 * Find the maximum matching in the given bipartite graph. adj
59 * is the adjacency list of the first partition.
61 int matching(int **adj
, int n1
, int n2
)
66 int pair
[MPARTS
]; /* vertices matched to the first partition */
67 int pair2
[MPARTS
]; /* vertices matched to the second partition */
69 for (i
= 0; i
< n1
; i
++)
71 for (i
= 0; i
< n2
; i
++)
74 while (bfs(adj
, pair
, pair2
, dist
, n1
))
75 for (i
= 0; i
< n1
; i
++)
76 if (pair
[i
] == VNIL
&& dfs(adj
, pair
, pair2
, dist
, i
))