Skip to content

Commit

Permalink
✨ feat: Improve API for maximum cardinality matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Sep 24, 2020
1 parent 6f84ace commit 661d3ed
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ See [docs](https://aureooms.github.io/js-maximum-matching/index.html).
import maximumMatching, {iter} from '@aureooms/js-maximum-matching';
const edges = [[1, 2, 10], [2, 3, 11]] ;
const matching = maximumMatching(edges) ; // [-1, -1, 3, 2]
iter(matching); // [ [2, 3] ]
[...iter(matching)]; // [ [2, 3] ]

import maximumCardinalityMatching from '@aureooms/js-maximum-matching/cardinality';
for (const edge of iter(maximumCardinalityMatching([[1, 2], [2, 3], [3, 4]]))) {
console.log(edge);
}
// [1,2]
// [3,4]
```

[![License](https://img.shields.io/github/license/aureooms/js-maximum-matching.svg)](https://raw.githubusercontent.com/aureooms/js-maximum-matching/master/LICENSE)
Expand Down
2 changes: 2 additions & 0 deletions src/addDefaultWeight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const addDefaultWeight = (edges) => edges.map(([u, v, w]) => [u, v, w || 1]);
export default addDefaultWeight;
3 changes: 2 additions & 1 deletion src/cardinality/opt/general.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import blossomNoChecks from '../../core/blossomNoChecks';
import addDefaultWeight from '../../addDefaultWeight';

const general = (edges) => blossomNoChecks(edges, true);
const general = (edges) => blossomNoChecks(addDefaultWeight(edges), true);

export default general;
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import cardinality from './cardinality';
import core from './core';
import weight from './weight';
import iter from './iter';
import addDefaultWeight from './addDefaultWeight';

export default weight;

export {cardinality, core, weight, iter};
export {cardinality, core, weight, iter, addDefaultWeight};
14 changes: 12 additions & 2 deletions test/src/cardinality.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava';
import {enumerate} from '@aureooms/js-itertools';

import maximumCardinalityMatching from '../../src/cardinality';
import {addDefaultWeight} from '../../src';
import blossom from '../../src/core/blossom';

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

const tests = {
withoutWeights: {
edges: [
[1, 2],
[2, 3],
[3, 4]
],
expected: [-1, 2, 1, 4, 3]
},

test14_maxcard: {
edges: [
[1, 2, 5],
Expand All @@ -42,8 +52,8 @@ const bdflt = blossom();

const algorithms = [
maximumCardinalityMatching,
(edges) => btt(edges, true),
(edges) => bdflt(edges, true)
(edges) => btt(addDefaultWeight(edges), true),
(edges) => bdflt(addDefaultWeight(edges), true)
];

for (const [i, algorithm] of enumerate(algorithms))
Expand Down
19 changes: 18 additions & 1 deletion test/src/readme.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import test from 'ava';

import maximumMatching, {iter} from '../../src';
import maximumCardinalityMatching from '../../src/cardinality';

test('README', (t) => {
test('weight', (t) => {
const edges = [
[1, 2, 10],
[2, 3, 11]
Expand All @@ -11,3 +12,19 @@ test('README', (t) => {
t.deepEqual([-1, -1, 3, 2], matching);
t.deepEqual([[2, 3]], [...iter(matching)]);
});

test('cardinality', (t) => {
const edges = [
[1, 2],
[2, 3],
[3, 4]
];
const result = [...iter(maximumCardinalityMatching(edges))];
t.deepEqual(
[
[1, 2],
[3, 4]
],
result
);
});

0 comments on commit 661d3ed

Please sign in to comment.