Skip to content

Instantly share code, notes, and snippets.

@skhokhlov
Created December 19, 2015 19:23
Show Gist options
  • Save skhokhlov/cd2cd2e9a35458bfac8a to your computer and use it in GitHub Desktop.
Save skhokhlov/cd2cd2e9a35458bfac8a to your computer and use it in GitHub Desktop.
'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