1
0
Fork 0
Python code for Maximum Weighted Matching in general graphs
Go to file
Joris van Rantwijk d5d8a43a3b fix 2024-07-28 11:29:41 +02:00
cpp Add corner cases to C++ unit test 2023-07-07 22:33:33 +02:00
doc fix 2024-07-28 11:29:41 +02:00
python Add testcases to force big values for dual/slack 2024-07-26 19:41:57 +02:00
tests Add benchmark script 2024-07-10 21:01:21 +02:00
.gitignore C++ command line tool to run matching 2023-07-07 22:33:33 +02:00
README.md Fix Algorithm.doc 2024-07-26 23:48:45 +02:00
pylintrc Pylint cleanups 2023-03-12 12:16:29 +01:00
test_cpp.sh Separate C++ test script 2024-07-10 20:52:59 +02:00
test_python.sh Test script for Python code 2024-07-09 21:10:38 +02:00
tox.ini Add tox.ini for Python testing 2024-07-10 20:19:12 +02:00

README.md

Maximum Weighted Matching

This repository contains implementations of maximum weighted matching for general graphs.

In graph theory, a matching is a subset of edges that does not use any vertex more than once. For an edge-weighted graph, a maximum weight matching is a matching that achieves the largest possible sum of weights of matched edges.

The code in this repository is based on a variant of the blossom algorithm that runs in O(n m log n) steps. See the file Algorithm.md for a detailed description.

Python

The folder python/ contains a Python 3 package mwmatching that implements maximum weighted matching. The Python code is self-contained -- it has no dependencies outside the standard library.

To use the package, set your PYTHONPATH to the location of the package mwmatching. Alternatively, you can install the package into your Python environment as follows:

cd python
pip install .

Using the package is easy. You describe the input graph by listing its edges. Each edge is represented as a tuple of vertex indices and the weight of the edge. The example below finds a matching in a graph with 5 vertices and 5 edges. The maximum weight matching contains two edges and has total weight 11.

from mwmatching import maximum_weight_matching
edges = [(0, 1, 3), (1, 2, 8), (1, 4, 6), (2, 3, 5), (2, 4, 7)]
matching = maximum_weight_matching(edges)
print(matching)  # prints [(1, 4), (2, 3)]

C++

The folder cpp/ contains a header-only C++ implementation of maximum weighted matching.

NOTE: The C++ code currently implements a slower algorithm that runs in O(n3) steps. I plan to eventually update the C++ code to implement the faster O(n m log n) algorithm.

The C++ code is self-contained and can easily be linked into an application. It is also reasonably efficient.

For serious use cases, LEMON may be a better choice. LEMON is a C++ library that provides a very fast and robust implementation of maximum weighted matching and many other graph algorithms. To my knowledge, it is the only free software library that provides a high-quality matching algorithm.

Repository structure

    python/
        mwmatching/            : Python package for maximum weight matching
            __init__.py
            algorithm.py       : Algorithm implementation
            datastruct.py      : Internal data structures
        tests/
            test_algorithm     : Unit tests for the algorithm
            test_datastruct.py : Unit tests for data structures
        run_matching.py        : Command-line program to run the matching algorithm 

    cpp/
        mwmatching.h           : C++ implementation of maximum weight matching
        test_mwmatching.cpp    : Unit tests
        run_matching.cpp       : Command-line program to run the matching algorithm

    tests/
        generate/              : Python programs to generate graphs
        graphs/                : Collection of graphs and matching outputs
        lemon/                 : C++ program to run LEMON's matching algorithm
        run_test.py            : Command-line program to test/benchmark matching solvers

    doc/
        Algorithm.md           : Description of the algorithm

Status of this software

The code is finished and tested and it works.

I ran extensive tests on random graphs and compared the results against another solver. It seems to me that most bugs would have been found through these tests. But it is definitely still possible that I overlooked something.

This repository is just a side-project for me. I don't have any plans to develop this into a polished, full-featured, supported software package.

Credits

The code in this repository was written by me, but it implements an algorithm that has been known for decades. This algorithm is entirely the work of other people. I did not invent any part of it, and deserve no credit for it.

My implementation follows the description of the algorithm as it appears in the paper "Efficient Algorithms for Finding Maximum Matching in Graphs" by Zvi Galil (1986). However, earlier versions of the algorithm were invented and improved by several other scientists. See the file Algorithm.md for links to the most important papers.

I used some ideas from the source code of the MaxWeightedMatching class in LEMON: the technique to implement lazy updates of vertex dual variables, and the approach to re-use alternating trees after augmenting the matching.

I used Fortran programs hardcard.f, t.f and tt.f by R. B. Mattingly and N. Ritchey to generate test graphs. These programs are part of the DIMACS Network Flows and Matching implementation challenge. They can be found in the DIMACS Netflow archive.

To check the correctness of my results, I used other maximum weight matching solvers: the MaxWeightedMatching module in LEMON, and the program wmatch by Edward Rothberg.

License

The following license applies to the software in this repository, excluding the folder doc. This license is sometimes called the MIT License or the Expat License:

Copyright (c) 2023-2024 Joris van Rantwijk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The following applies to the documentation in the folder doc:

Written in 2023 by Joris van Rantwijk.
This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License.