Created
December 19, 2015 19:23
-
-
Save skhokhlov/cd2cd2e9a35458bfac8a to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
(function Kuhn(k, n, g) { | |
var m = new Array(k), | |
used = new Array(n); | |
function pair(v) { | |
// used[v] = true; | |
// for (let i = 0; i < n; i++) { | |
// let to = g[v][i]; | |
// if (m[to] == null || Kuhn(m[to])) { | |
// m[to] = v; | |
// | |
// return true; | |
// } | |
// } | |
let q = []; | |
q.push(0); | |
while (q.length) { | |
const t = q.pop(); | |
used[t] = true; | |
for (let i = 0; i < g[v].length; i++) { | |
let to = g[v][i]; | |
if(m[to] === undefined) { | |
m[to] = v; | |
return true; | |
} else { | |
q.unshift(i); | |
} | |
} | |
} | |
return false; | |
} | |
for (let i = 0; i < n; i++) { | |
used.fill(false); | |
pair(i); | |
} | |
for (let i = 0; i < k; i++) { | |
console.log((m[i] !== undefined) ? (m[i]+1) + ' ' + (i+1) : false); | |
} | |
})(4,4,[ | |
[0,2,3], | |
[0,1,2], | |
[0,2,3], | |
[0,1,2] | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment