Coder Social home page Coder Social logo

stable-marriage's Introduction

stable-marriage

Node.js implementation of the Stable Marriage Problem (SMP). Generates a random instance of the problem (default size: 20 persons of each gender) and solves it using the Gale-Shapley algorithm.

The script logs the process of matching partners in a dialogue-style manner, for presenting it to an audience and, well, for the funsies. Like this:

Screenshot of the Mac terminal

Remark

I am very well aware of how stereotypical the man/woman terminology is. The SMP has been known for decades, and well, times were different. For reasons of clear data structuring and because the problem is best known under the man/woman couple analogy, I stick to the outmoded labels, whilst strongly supporting diversity in any way. <3

Usage

Start the script by hitting npm start in the project directory.

If you don't pass any arguments, the script will generate a random SMP instance of size n=20.

Generating a random instance of size n

You can adjust the default size by passing an optional argument n=<size>, where <size> is the number of couples to be generated. For example

$ npm start n=25

will generate an instance with 25 women and 25 men, each of them having their own priority list with 25 entries, resulting in 25 couples.

Careful! Time complexity of the current implementation is exponential. I'd recommend not using any n > 400.

Passing a specific instance via a JSON file

If you want to precisely specify the instance to be matched, you can define it in a separate .json file and pass it to the script via the argument f=<path-to-json>, where <path-to-json> is the relative path from the entry point of your script (usually the stable-marriage/ folder).

The .json file must match the following structure:

{
  "men": {
    <m1>: [ <wx>, <wy>, ... ],
    <m2>: ...,
    ...
  },
  "women": {
    <w1>: [ <mx>, <my>, ... ],
    <w2>: ...,
    ...
  }
}

where

  • <m1>, <m2>, ... are IDs of men
  • <w1>, <w2>, ... are IDs of women
  • [ <wx>, <wy>, ... ] are priority lists consisting of women's IDs
  • [ <mx>, <my>, ... ] are priority lists consisting of men's IDs.

Naturally, the "men" and "women" Objects as well as all priority lists must be of the same size. IDs must be unique and priority lists may only contain IDs that (I) exist and (II) are assigned to persons of the opposite sex.

A working example and a fucked up example can be found in the ./assets/ folder. A working call would be e.g.:

$ npm start f=./assets/example-input.json

Requires

  • Node.js version 6.2.2
  • a terminal that supports emojis

stable-marriage's People

Contributors

mgschoen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.