1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use num_traits::{Bounded, Signed, Zero};
use fixedbitset::FixedBitSet;
use matrix::Matrix;
use std::iter::Sum;
pub trait Weights<C> {
fn rows(&self) -> usize;
fn columns(&self) -> usize;
fn at(&self, row: usize, col: usize) -> C;
fn neg(&self) -> Self
where
Self: Sized,
C: Signed;
}
impl<C: Copy> Weights<C> for Matrix<C> {
fn rows(&self) -> usize {
self.rows
}
fn columns(&self) -> usize {
self.columns
}
fn at(&self, row: usize, col: usize) -> C {
self[&(row, col)]
}
fn neg(&self) -> Self
where
C: Signed,
{
-self.clone()
}
}
pub fn kuhn_munkres<C, W>(weights: &W) -> (C, Vec<usize>)
where
C: Bounded + Sum<C> + Signed + Zero + Ord + Copy,
W: Weights<C>,
{
let nx = weights.rows();
let ny = weights.columns();
assert!(
nx <= ny,
"number of rows must not be larger than number of columns"
);
let mut xy: Vec<Option<usize>> = vec![None; nx];
let mut yx: Vec<Option<usize>> = vec![None; ny];
let mut lx: Vec<C> = (0..nx)
.map(|row| (0..ny).map(|col| weights.at(row, col)).max().unwrap())
.collect::<Vec<_>>();
let mut ly: Vec<C> = vec![Zero::zero(); ny];
let mut s = FixedBitSet::with_capacity(nx);
let mut alternating = Vec::with_capacity(ny);
let mut slack = vec![Zero::zero(); ny];
let mut slackx = Vec::with_capacity(ny);
for root in 0..nx {
alternating.clear();
alternating.resize(ny, None);
let mut y = {
s.clear();
s.insert(root);
for y in 0..ny {
slack[y] = lx[root] + ly[y] - weights.at(root, y);
}
slackx.clear();
slackx.resize(ny, root);
Some(loop {
let mut delta = Bounded::max_value();
let mut x = 0;
let mut y = 0;
for yy in 0..ny {
if alternating[yy].is_none() && slack[yy] < delta {
delta = slack[yy];
x = slackx[yy];
y = yy;
}
}
debug_assert!(s.contains(x));
if delta > Zero::zero() {
for x in s.ones() {
lx[x] = lx[x] - delta;
}
for y in 0..ny {
if alternating[y].is_some() {
ly[y] = ly[y] + delta;
} else {
slack[y] = slack[y] - delta;
}
}
}
debug_assert!(lx[x] + ly[y] == weights.at(x, y));
alternating[y] = Some(x);
if yx[y].is_none() {
break y;
}
let x = yx[y].unwrap();
debug_assert!(!s.contains(x));
s.insert(x);
for y in 0..ny {
if alternating[y].is_none() {
let alternate_slack = lx[x] + ly[y] - weights.at(x, y);
if slack[y] > alternate_slack {
slack[y] = alternate_slack;
slackx[y] = x;
}
}
}
})
};
while y.is_some() {
let x = alternating[y.unwrap()].unwrap();
let prec = xy[x];
yx[y.unwrap()] = Some(x);
xy[x] = y;
y = prec;
}
}
(
lx.into_iter().sum::<C>() + ly.into_iter().sum(),
xy.into_iter().map(|v| v.unwrap()).collect::<Vec<_>>(),
)
}
pub fn kuhn_munkres_min<C, W>(weights: &W) -> (C, Vec<usize>)
where
C: Bounded + Sum<C> + Zero + Signed + Ord + Copy,
W: Weights<C>,
{
let (total, assignments) = kuhn_munkres(&weights.neg());
(-total, assignments)
}