Skip to content

Commit e0ad468

Browse files
🐛 fix: Extract independent code, simplify, and potentially fix bug.
1 parent e33458c commit e0ad468

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/exact/blossom.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import assert from 'assert';
1818
// A C program for maximum weight matching by Ed Rothberg was used extensively
1919
// to validate this new code.
2020

21+
const min = (a, i, j) => {
22+
let o = a[i];
23+
for (++i; i < j; ++i) if (a[i] < o) o = a[i];
24+
return o;
25+
};
26+
2127
export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
2228
// Check delta2/delta3 computation after every substage;
2329
// only works on integer weights, slows down the algorithm to O(n^4).
@@ -26,25 +32,6 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
2632
// Check optimality of solution before returning; only works on integer weights.
2733
if (CHECK_OPTIMUM === undefined) CHECK_OPTIMUM = true;
2834

29-
// Compatibility
30-
31-
const min = function (a, i, j) {
32-
let o = a[i];
33-
34-
while (--j > i) {
35-
if (a[j] < o) o = a[j];
36-
}
37-
38-
return o;
39-
};
40-
41-
const zip = function (a, fn) {
42-
const shortest = a[0].length < a[1].length ? a[0] : a[1];
43-
shortest.map((_, i) => fn(...a.map((array) => array[i])));
44-
};
45-
46-
// <end>
47-
4835
const maxWeightMatching = function (edges, maxcardinality) {
4936
let i;
5037
let j;
@@ -824,10 +811,14 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
824811
jblossoms.push(blossomparent[jblossoms[jblossoms.length - 1]]);
825812
iblossoms.reverse();
826813
jblossoms.reverse();
827-
zip([iblossoms, jblossoms], function (bi, bj) {
828-
if (bi !== bj) return true;
814+
const length = Math.min(iblossoms.length, jblossoms.length);
815+
for (let x = 0; x < length; ++x) {
816+
const bi = iblossoms[x];
817+
const bj = jblossoms[x];
818+
if (bi !== bj) break;
829819
s += 2 * dualvar[bi];
830-
});
820+
}
821+
831822
assert(s >= 0);
832823
if (Math.floor(mate[i] / 2) === k || Math.floor(mate[j] / 2) === k) {
833824
assert(

0 commit comments

Comments
 (0)