From 661d3ed0307551f5471d9335397ee9608f48daaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Thu, 24 Sep 2020 13:55:24 +0200 Subject: [PATCH] :sparkles: feat: Improve API for maximum cardinality matching. --- README.md | 9 ++++++++- src/addDefaultWeight.js | 2 ++ src/cardinality/opt/general.js | 3 ++- src/index.js | 3 ++- test/src/cardinality.js | 14 ++++++++++++-- test/src/readme.js | 19 ++++++++++++++++++- 6 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/addDefaultWeight.js diff --git a/README.md b/README.md index ca6ab27..ff7149f 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/addDefaultWeight.js b/src/addDefaultWeight.js new file mode 100644 index 0000000..9f4f36c --- /dev/null +++ b/src/addDefaultWeight.js @@ -0,0 +1,2 @@ +const addDefaultWeight = (edges) => edges.map(([u, v, w]) => [u, v, w || 1]); +export default addDefaultWeight; diff --git a/src/cardinality/opt/general.js b/src/cardinality/opt/general.js index abb54a6..0d39c92 100644 --- a/src/cardinality/opt/general.js +++ b/src/cardinality/opt/general.js @@ -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; diff --git a/src/index.js b/src/index.js index f0dde99..4a3e251 100644 --- a/src/index.js +++ b/src/index.js @@ -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}; diff --git a/test/src/cardinality.js b/test/src/cardinality.js index d370ae4..89fcd15 100644 --- a/test/src/cardinality.js +++ b/test/src/cardinality.js @@ -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) => { @@ -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], @@ -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)) diff --git a/test/src/readme.js b/test/src/readme.js index e7aafb7..81a9e51 100644 --- a/test/src/readme.js +++ b/test/src/readme.js @@ -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] @@ -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 + ); +});