Skip to main content

A Python implementation of the celebrated Gale-Shapley Algorithm.

Project description

gale-shapley-algorithm

A Python implementation of the celebrated Gale-Shapley (a.k.a. the Deferred Acceptance) Algorithm.

Time complexity is O(n^2), space complexity is O(n).

CI Docs Docker PyPI Python Ruff uv License: MIT

GUI with Docker

The easiest way to try the algorithm is with the interactive web GUI:

docker pull oedokumaci/gale-shapley-algorithm
docker run --rm -p 8000:8000 oedokumaci/gale-shapley-algorithm

Then open http://localhost:8000 in your browser.

Or build locally for development:

docker build -t gale-shapley-algorithm .
docker run --rm -p 8000:8000 gale-shapley-algorithm

The GUI lets you:

  • Add and remove people on each side (proposers and responders)
  • Set preferences by drag-and-drop reordering
  • Randomize all preferences with one click
  • Run the matching and see results in a table with stability info
  • Animate step-by-step to watch proposals, rejections, and tentative matches unfold round by round in an SVG visualization
  • Upload images for each person to personalize the visualization
  • Toggle dark/light mode

Installation

pip install gale-shapley-algorithm

With CLI support:

pip install "gale-shapley-algorithm[cli]"

Quick Start

As a Library

import gale_shapley_algorithm as gsa

result = gsa.create_matching(
    proposer_preferences={
        "alice": ["bob", "charlie"],
        "dave": ["charlie", "bob"],
    },
    responder_preferences={
        "bob": ["alice", "dave"],
        "charlie": ["dave", "alice"],
    },
)
print(result.matches)  # {'alice': 'bob', 'dave': 'charlie'}

As a CLI

The CLI uses interactive prompts -- no config files needed:

# Interactive mode: enter names and rank preferences
uvx --from "gale-shapley-algorithm[cli]" python -m gale_shapley_algorithm

# Random mode: auto-generate names and preferences
uvx --from "gale-shapley-algorithm[cli]" python -m gale_shapley_algorithm --random

# Swap proposers and responders
uvx --from "gale-shapley-algorithm[cli]" python -m gale_shapley_algorithm --swap-sides

Interactive mode example:

$ python -m gale_shapley_algorithm

  Gale-Shapley Algorithm

Enter proposer side name [Proposers]: Men
Enter responder side name [Responders]: Women

Enter names for Men (comma-separated): Will, Hampton
Enter names for Women (comma-separated): April, Summer

Ranking preferences for Men...

  Available for Will:
  1. April
  2. Summer
  Enter ranking for Will (e.g. 1,2): 1,2
  -> Will: April > Summer

  Available for Hampton:
  1. April
  2. Summer
  Enter ranking for Hampton (e.g. 1,2): 2,1
  -> Hampton: Summer > April

Ranking preferences for Women...
  ...

┌──────── Matching Result ────────┐
│ Men     │ Women                 │
├─────────┼───────────────────────┤
│ Will    │ April                 │
│ Hampton │ Summer                │
└─────────┴───────────────────────┘
Completed in 1 round. Stable: Yes

Random mode example:

$ python -m gale_shapley_algorithm --random

  Gale-Shapley Algorithm

Enter proposer side name [Proposers]: Cats
Enter responder side name [Responders]: Dogs
Number of Cats [3]: 3
Number of Dogs [3]: 3

  ... (random preferences generated and displayed) ...

Completed in 2 rounds. Stable: Yes

Development

This project is managed with uv and uses taskipy for task running.

git clone https://github.com/oedokumaci/gale-shapley-algorithm
cd gale-shapley-algorithm
uvx --from taskipy task setup   # Install dependencies
uvx --from taskipy task run     # Run the application
uvx --from taskipy task fix     # Auto-format + lint fix
uvx --from taskipy task ci      # Run all CI checks
uvx --from taskipy task test    # Run tests
uvx --from taskipy task docs    # Serve docs locally

Install pre-commit hooks:

uv run pre-commit install

Documentation

Full documentation is available at oedokumaci.github.io/gale-shapley-algorithm.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gale_shapley_algorithm-1.5.4.tar.gz (458.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gale_shapley_algorithm-1.5.4-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file gale_shapley_algorithm-1.5.4.tar.gz.

File metadata

  • Download URL: gale_shapley_algorithm-1.5.4.tar.gz
  • Upload date:
  • Size: 458.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gale_shapley_algorithm-1.5.4.tar.gz
Algorithm Hash digest
SHA256 84856b2f9ade04f66f877241efed303883aeb8d54af8f2bbcec766d928800c58
MD5 e537c278f6a916a04b2bde37f355d7b1
BLAKE2b-256 b84b1ae92890f5403d0ff819304d021badcee3bb0a91ecf5032eb498fcffca73

See more details on using hashes here.

File details

Details for the file gale_shapley_algorithm-1.5.4-py3-none-any.whl.

File metadata

  • Download URL: gale_shapley_algorithm-1.5.4-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gale_shapley_algorithm-1.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cdf18233947efb10ecf37e3a8724804c41d89eaff463c0b6800c88f572c2cf50
MD5 325600d01feae23bae7fd49adbc48f38
BLAKE2b-256 679abd4c31b9b1d83b9f38e266f7ffb93e32111e9ab83fb5617a41cef6d628f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page