Last active
May 11, 2016 05:50
-
-
Save yzarubin/3a7e91079688b2760db038a0c96df3e9 to your computer and use it in GitHub Desktop.
Max Bipartite Match implementation in JavaScript
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
| // Modified Ford-Fulkerson algorithm | |
| function mbp(matrix, isConnected) { | |
| var N = matrix.length; | |
| var M = matrix[0].length; | |
| var visited = new Array(M); | |
| var matches = new Array(M); | |
| var res = 0; | |
| isConnected = isConnected || function(x) {return x;}; | |
| function hasMatch(u) { | |
| for (var v = 0; v < M; v++) { | |
| if (isConnected(matrix[u][v]) && !visited[v]) { | |
| visited[v] = 1; | |
| if (matches[v] === -1 || hasMatch(matches[v])) { | |
| matches[v] = u; | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| for (var i = 0; i < M; i++) matches[i] = -1; // initialize | |
| for (var u = 0; u < N; u++) { | |
| for (var k = 0; k < M; k++) visited[k] = 0; // reset | |
| if (hasMatch(u)) res++; | |
| } | |
| return res; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment