stable-marriage

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i stable-marriage

Weekly Downloads

5

Version

1.0.2

License

MIT

Unpacked Size

34.7 kB

Total Files

8

Last publish

Collaborators

  • evetbox