stable-marriage
v1.0.2
Published
A javascript implementation of stable marriage problem
Downloads
5
Maintainers
Readme
stable-marriage-problem
A javascript implementation of stable marriage problem, influenced by algorithm-archive
Installation
Use npm:
npm install stable-marriage
Or use yarn:
yarn add stable-marriage
Usage
const { Person, stableMarriage, shuffle } = require('stable-marriage')
const boys = [...'ABCD'].map((name) => new Person(name))
const girls = [...'abcd'].map((name) => new Person(name))
console.log('boys')
for (const boy of boys) {
boy.generatePreferences(shuffle(girls))
console.log(`${boy.name}: ${boy.preferences.map((p) => p.name).join()}`)
}
console.log('\nGirls')
for (const girl of girls) {
girl.generatePreferences(shuffle(boys))
console.log(`${girl.name}: ${girl.preferences.map((p) => p.name).join()}`)
}
stableMarriage(boys)
console.log('\nPairings')
for (const boy of boys) {
if (boy.fiance) {
console.log(`${boy.name}: ${boy.fiance.name}`)
}
}
Run the example code and your terminal should log like this:
boys
A: c,b,d,a
B: b,d,c,a
C: b,c,a,d
D: d,c,a,b
Girls
a: B,A,D,C
b: B,D,C,A
c: C,D,A,B
d: A,C,B,D
Pairings
A: d
B: b
C: c
D: a