Skip to content

Commit 661d3ed

Browse files
✨ feat: Improve API for maximum cardinality matching.
1 parent 6f84ace commit 661d3ed

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ See [docs](https://aureooms.github.io/js-maximum-matching/index.html).
99
import maximumMatching, {iter} from '@aureooms/js-maximum-matching';
1010
const edges = [[1, 2, 10], [2, 3, 11]] ;
1111
const matching = maximumMatching(edges) ; // [-1, -1, 3, 2]
12-
iter(matching); // [ [2, 3] ]
12+
[...iter(matching)]; // [ [2, 3] ]
13+
14+
import maximumCardinalityMatching from '@aureooms/js-maximum-matching/cardinality';
15+
for (const edge of iter(maximumCardinalityMatching([[1, 2], [2, 3], [3, 4]]))) {
16+
console.log(edge);
17+
}
18+
// [1,2]
19+
// [3,4]
1320
```
1421

1522
[![License](https://img.shields.io/github/license/aureooms/js-maximum-matching.svg)](https://raw.githubusercontent.com/aureooms/js-maximum-matching/master/LICENSE)

src/addDefaultWeight.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const addDefaultWeight = (edges) => edges.map(([u, v, w]) => [u, v, w || 1]);
2+
export default addDefaultWeight;

src/cardinality/opt/general.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import blossomNoChecks from '../../core/blossomNoChecks';
2+
import addDefaultWeight from '../../addDefaultWeight';
23

3-
const general = (edges) => blossomNoChecks(edges, true);
4+
const general = (edges) => blossomNoChecks(addDefaultWeight(edges), true);
45

56
export default general;

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import cardinality from './cardinality';
22
import core from './core';
33
import weight from './weight';
44
import iter from './iter';
5+
import addDefaultWeight from './addDefaultWeight';
56

67
export default weight;
78

8-
export {cardinality, core, weight, iter};
9+
export {cardinality, core, weight, iter, addDefaultWeight};

test/src/cardinality.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'ava';
22
import {enumerate} from '@aureooms/js-itertools';
33

44
import maximumCardinalityMatching from '../../src/cardinality';
5+
import {addDefaultWeight} from '../../src';
56
import blossom from '../../src/core/blossom';
67

78
const macro = (t, algorithm, edges, expected) => {
@@ -16,6 +17,15 @@ macro.title = (title, algorithm, edges, expected) =>
1617
`${algorithm.name}(${JSON.stringify(edges)}) = ${JSON.stringify(expected)}`;
1718

1819
const tests = {
20+
withoutWeights: {
21+
edges: [
22+
[1, 2],
23+
[2, 3],
24+
[3, 4]
25+
],
26+
expected: [-1, 2, 1, 4, 3]
27+
},
28+
1929
test14_maxcard: {
2030
edges: [
2131
[1, 2, 5],
@@ -42,8 +52,8 @@ const bdflt = blossom();
4252

4353
const algorithms = [
4454
maximumCardinalityMatching,
45-
(edges) => btt(edges, true),
46-
(edges) => bdflt(edges, true)
55+
(edges) => btt(addDefaultWeight(edges), true),
56+
(edges) => bdflt(addDefaultWeight(edges), true)
4757
];
4858

4959
for (const [i, algorithm] of enumerate(algorithms))

test/src/readme.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import test from 'ava';
22

33
import maximumMatching, {iter} from '../../src';
4+
import maximumCardinalityMatching from '../../src/cardinality';
45

5-
test('README', (t) => {
6+
test('weight', (t) => {
67
const edges = [
78
[1, 2, 10],
89
[2, 3, 11]
@@ -11,3 +12,19 @@ test('README', (t) => {
1112
t.deepEqual([-1, -1, 3, 2], matching);
1213
t.deepEqual([[2, 3]], [...iter(matching)]);
1314
});
15+
16+
test('cardinality', (t) => {
17+
const edges = [
18+
[1, 2],
19+
[2, 3],
20+
[3, 4]
21+
];
22+
const result = [...iter(maximumCardinalityMatching(edges))];
23+
t.deepEqual(
24+
[
25+
[1, 2],
26+
[3, 4]
27+
],
28+
result
29+
);
30+
});

0 commit comments

Comments
 (0)