Crates.io | hungarian |
lib.rs | hungarian |
version | 1.1.1 |
source | src |
created_at | 2018-03-14 09:05:59.977054 |
updated_at | 2020-06-15 02:21:41.813084 |
description | A simple, fast implementation of the Hungarian (Kuhn-Munkres) algorithm. |
homepage | |
repository | https://github.com/nwtnni/hungarian |
max_upload_size | |
id | 55549 |
size | 36,208 |
IMPORTANT: The pathfinding
crate has a significantly
faster implementation of this
algorithm (benchmarks below), uses traits to abstract over cost matrices, and is also better maintained.
I recommend using it instead.
A simple Rust implementation of the Hungarian (or Kuhn–Munkres) algorithm.
Should run in O(n^3)
time and take O(m*n)
space, given an m * n
rectangular
matrix (represented as a 1D slice).
Derived and modified from this great explanation.
Add the following to your Cargo.toml
file:
[dependencies]
hungarian = "1.1.1"
Add the following to the top of your binary or library:
extern crate hungarian;
use hungarian::minimize;
And you should be good to go! For more information, check out the documentation.
pathfinding
redirect appears on crates.io
.num-trait
to take generic integer weightsndarray
to scale better with larger inputshungarian
function to minimize
Vec<Option<Usize>>
to handle when not all columns are assigned to rowsInstead of using splitting logic across files and helper functions, I tried to simplify and condense the above explanation into a single, simple function while maintaining correctness. After trawling the web for test cases, I'm reasonably confident that my implementation works, even though the end result looks fairly different.
Please let me know if you find any bugs!
Benchmarks were obtained using Criterion.rs, with the following two types of cost matrices:
Worst Case | Generic Case
|
------------- | -------------
| 1 | 2 | 3 | ... | | 1 | 2 | 3 |
------------- | -------------
| 2 | 4 | 6 | ... | | 4 | 5 | 6 |
------------- | -------------
| 3 | 6 | 9 | ... | | 7 | 8 | 9 |
------------- | -------------
. . . |
. . . |
. . . |
|
C(i, j) = (i + 1)(j + 1) | C(i, j) = (i * width) + j
Cost Matrix | Matrix Size | hungarian Average Runtime |
pathfinding Average Runtime |
---|---|---|---|
Worst-Case | 5 x 5 | 2.42 us | 1.19 us |
Worst-Case | 10 x 10 | 20.38 us | 4.24 us |
Worst-Case | 25 x 25 | 546.88 us | 59.66 us |
Worst-Case | 50 x 50 | 6.97 ms | 422.05 us |
Generic | 5 x 5 | 1.75 us | 871.24 ns |
Generic | 10 x 10 | 7.49 us | 3.50 us |
Generic | 25 x 25 | 86.33 us | 33.91 us |
Generic | 50 x 50 | 556.48 us | 285.69 us |
Generic | 100 x 100 | 3.97 ms | 1.93 ms |
Measured on a quad-core 2.6GHz Intel(R) i7-6700HQ with 16GB RAM; using Ubuntu 16.04 Linux x86_64 4.8.0-53-generic