Skip to content

Instantly share code, notes, and snippets.

@irpap
Created August 25, 2012 11:24
Show Gist options
  • Save irpap/3464112 to your computer and use it in GitHub Desktop.
Save irpap/3464112 to your computer and use it in GitHub Desktop.
Maximum Bipartite Matching using DFS
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