Skip to content

Commit 3749be6

Browse files
♻️ refactor(blossom): Extract blossomLeaves.
1 parent fbaf3e5 commit 3749be6

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

src/core/blossom/blossom.js

+7-23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import checkDelta3 from './checkDelta3';
77
import statistics from './statistics';
88
import endpoints from './endpoints';
99
import neighbours from './neighbours';
10+
import blossomLeaves from './blossomLeaves';
1011

1112
// Adapted from http://jorisvr.nl/maximummatching.html
1213
// All credit for the implementation goes to Joris van Rantwijk [http://jorisvr.nl].
@@ -193,23 +194,6 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
193194
return dualvar[i] + dualvar[j] - 2 * wt;
194195
};
195196

196-
// Generate the leaf vertices of a blossom.
197-
const blossomLeaves = function (b, fn) {
198-
if (b < nvertex) {
199-
if (fn(b)) return true;
200-
} else {
201-
let i;
202-
let t;
203-
const length_ = blossomchilds[b].length;
204-
for (i = 0; i < length_; ++i) {
205-
t = blossomchilds[b][i];
206-
if (t < nvertex) {
207-
if (fn(t)) return true;
208-
} else if (blossomLeaves(t, fn)) return true;
209-
}
210-
}
211-
};
212-
213197
// Assign label t to the top-level blossom containing vertex w
214198
// and record the fact that w was reached through the edge with
215199
// remote endpoint p.
@@ -225,7 +209,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
225209
bestedge[b] = -1;
226210
if (t === 1) {
227211
// B became an S-vertex/blossom; add it(s vertices) to the queue.
228-
blossomLeaves(b, function (v) {
212+
blossomLeaves(nvertex, blossomchilds, b, function (v) {
229213
queue.push(v);
230214
});
231215
console.debug('DEBUG: PUSH ' + queue);
@@ -379,7 +363,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
379363
// Set dual variable to zero.
380364
dualvar[b] = 0;
381365
// Relabel vertices.
382-
blossomLeaves(b, function (v) {
366+
blossomLeaves(nvertex, blossomchilds, b, function (v) {
383367
if (label[inblossom[v]] === 2) {
384368
// This T-vertex now turns into an S-vertex because it becomes
385369
// part of an S-blossom; add it to the queue.
@@ -403,7 +387,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
403387
// This subblossom does not have a list of least-slack edges;
404388
// get the information from the vertices.
405389
nblists = [];
406-
blossomLeaves(bv, function (v) {
390+
blossomLeaves(nvertex, blossomchilds, bv, function (v) {
407391
j = neighbend[v].length;
408392
temporary_ = new Array(j);
409393
while (j--) {
@@ -499,7 +483,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
499483
// Recursively expand this sub-blossom.
500484
expandBlossom(s, endstage);
501485
} else {
502-
blossomLeaves(s, function (v) {
486+
blossomLeaves(nvertex, blossomchilds, s, function (v) {
503487
inblossom[v] = s;
504488
});
505489
}
@@ -569,7 +553,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
569553
continue;
570554
}
571555

572-
blossomLeaves(bv, function (v) {
556+
blossomLeaves(nvertex, blossomchilds, bv, function (v) {
573557
if (label[v] !== 0) {
574558
// If the sub-blossom contains a reachable vertex, assign
575559
// label T to the sub-blossom.
@@ -875,7 +859,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
875859
nvertex,
876860
edges,
877861
blossomparent,
878-
blossomLeaves,
862+
blossomchilds,
879863
neighbend,
880864
label,
881865
endpoint,

src/core/blossom/blossomLeaves.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Generate the leaf vertices of a blossom.
3+
*/
4+
const blossomLeaves = (nvertex, nodes, b, fn) => {
5+
if (b < nvertex) {
6+
if (fn(b)) return true;
7+
} else {
8+
const length_ = nodes[b].length;
9+
for (let i = 0; i < length_; ++i) {
10+
const t = nodes[b][i];
11+
if (t < nvertex) {
12+
if (fn(t)) return true;
13+
} else if (blossomLeaves(nvertex, nodes, t, fn)) return true;
14+
}
15+
}
16+
};
17+
18+
export default blossomLeaves;

src/core/blossom/checkDelta3.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import assert from 'assert';
2+
import blossomLeaves from './blossomLeaves';
23

34
// Check optimized delta3 against a trivial computation.
45
const checkDelta3 = ({
56
nvertex,
67
edges,
78
blossomparent,
8-
blossomLeaves,
9+
blossomchilds,
910
neighbend,
1011
label,
1112
endpoint,
@@ -19,7 +20,7 @@ const checkDelta3 = ({
1920
let tbd = null;
2021
for (let b = 0; b < 2 * nvertex; ++b) {
2122
if (blossomparent[b] === -1 && label[b] === 1) {
22-
blossomLeaves(b, function (v) {
23+
blossomLeaves(nvertex, blossomchilds, b, function (v) {
2324
for (let x = 0; x < neighbend[v].length; ++x) {
2425
const p = neighbend[v][x];
2526
const k = Math.floor(p / 2);

0 commit comments

Comments
 (0)