Skip to content

Commit ad80fa3

Browse files
♻️ refactor(blossom): Exctract alternating path toggle logic.
1 parent c534356 commit ad80fa3

File tree

1 file changed

+40
-47
lines changed

1 file changed

+40
-47
lines changed

src/core/blossom/blossom.js

+40-47
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,6 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
649649
// single vertices. The augmenting path runs through edge k, which
650650
// connects a pair of S vertices.
651651
const augmentMatching = function (k) {
652-
let bs;
653-
let t;
654-
let bt;
655-
let j;
656-
657652
const v = edges[k][0];
658653
const w = edges[k][1];
659654

@@ -662,50 +657,48 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
662657
);
663658
console.debug('DEBUG: PAIR ' + v + ' ' + w + ' (k=' + k + ')');
664659

665-
[
666-
[v, 2 * k + 1],
667-
[w, 2 * k]
668-
].forEach(function (edge) {
669-
let s = edge[0];
670-
let p = edge[1];
671-
// Match vertex s to remote endpoint p. Then trace back from s
672-
// until we find a single vertex, swapping matched and unmatched
673-
// edges as we go.
674-
// eslint-disable-next-line no-constant-condition
675-
while (true) {
676-
bs = inblossom[s];
677-
assert(label[bs] === 1);
678-
assert(labelend[bs] === mate[blossombase[bs]]);
679-
// Augment through the S-blossom from s to base.
680-
if (bs >= nvertex) augmentBlossom(bs, s);
681-
// Update mate[s]
682-
mate[s] = p;
683-
// Trace one step back.
684-
if (labelend[bs] === -1) {
685-
// Reached single vertex; stop.
686-
break;
687-
}
660+
matchVerticesAndFix(v, 2 * k + 1);
661+
matchVerticesAndFix(w, 2 * k);
662+
};
688663

689-
t = endpoint[labelend[bs]];
690-
bt = inblossom[t];
691-
assert(label[bt] === 2);
692-
// Trace one step back.
693-
assert(labelend[bt] >= 0);
694-
s = endpoint[labelend[bt]];
695-
j = endpoint[labelend[bt] ^ 1];
696-
// Augment through the T-blossom from j to base.
697-
assert(blossombase[bt] === t);
698-
if (bt >= nvertex) augmentBlossom(bt, j);
699-
// Update mate[j]
700-
mate[j] = labelend[bt];
701-
// Keep the opposite endpoint;
702-
// it will be assigned to mate[s] in the next step.
703-
p = labelend[bt] ^ 1;
704-
console.debug(
705-
'DEBUG: PAIR ' + s + ' ' + t + ' (k=' + Math.floor(p / 2) + ')'
706-
);
664+
const matchVerticesAndFix = (s, p) => {
665+
// Match vertex s to remote endpoint p. Then trace back from s
666+
// until we find a single vertex, swapping matched and unmatched
667+
// edges as we go.
668+
// eslint-disable-next-line no-constant-condition
669+
while (true) {
670+
const bs = inblossom[s];
671+
assert(label[bs] === 1);
672+
assert(labelend[bs] === mate[blossombase[bs]]);
673+
// Augment through the S-blossom from s to base.
674+
if (bs >= nvertex) augmentBlossom(bs, s);
675+
// Update mate[s]
676+
mate[s] = p;
677+
// Trace one step back.
678+
if (labelend[bs] === -1) {
679+
// Reached single vertex; stop.
680+
break;
707681
}
708-
});
682+
683+
const t = endpoint[labelend[bs]];
684+
const bt = inblossom[t];
685+
assert(label[bt] === 2);
686+
// Trace one step back.
687+
assert(labelend[bt] >= 0);
688+
s = endpoint[labelend[bt]];
689+
const j = endpoint[labelend[bt] ^ 1];
690+
// Augment through the T-blossom from j to base.
691+
assert(blossombase[bt] === t);
692+
if (bt >= nvertex) augmentBlossom(bt, j);
693+
// Update mate[j]
694+
mate[j] = labelend[bt];
695+
// Keep the opposite endpoint;
696+
// it will be assigned to mate[s] in the next step.
697+
p = labelend[bt] ^ 1;
698+
console.debug(
699+
'DEBUG: PAIR ' + s + ' ' + t + ' (k=' + Math.floor(p / 2) + ')'
700+
);
701+
}
709702
};
710703

711704
let b;

0 commit comments

Comments
 (0)