Created
August 25, 2012 11:24
-
-
Save irpap/3464112 to your computer and use it in GitHub Desktop.
Maximum Bipartite Matching using DFS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int m, n; | |
boolean[][] graph; | |
boolean seen[]; | |
int matchL[]; //What left vertex i is matched to (or -1 if unmatched) | |
int matchR[]; //What right vertex j is matched to (or -1 if unmatched) | |
int maximumMatching() { | |
//Read input and populate graph[][] | |
//Set m to be the size of L, n to be the size of R | |
Arrays.fill(matchL, -1); | |
Arrays.fill(matchR, -1); | |
int count = 0; | |
for (int i = 0; i < m; i++) { | |
Arrays.fill(seen, false); | |
if (bpm(i)) count++; | |
} | |
return count; | |
} | |
boolean bpm(int u) { | |
//try to match with all vertices on right side | |
for (int v = 0; v < n; v++) { | |
if (!graph[u][v] || seen[v]) continue; | |
seen[v] = true; | |
//match u and v, if v is unassigned, or if v's match on the left side can be reassigned to another right vertex | |
if (matchR[v] == -1 || bpm(matchR[v])) { | |
matchL[u] = v; | |
matchR[v] = u; | |
return true; | |
} | |
} | |
return false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment