forked from NVIDIA-AI-IOT/deepstream_pose_estimation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
munkres_algorithm.cpp
244 lines (227 loc) · 5.27 KB
/
munkres_algorithm.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "pair_graph.hpp"
#include "cover_table.hpp"
#include <stdio.h>
#include <vector>
#include <array>
#include <queue>
#include <cmath>
template <class T>
using Vec1D = std::vector<T>;
template <class T>
using Vec2D = std::vector<Vec1D<T>>;
template <class T>
using Vec3D = std::vector<Vec2D<T>>;
// Helper method to subtract the minimum row from cost_graph
void subtract_minimum_row(Vec2D<float> &cost_graph, int nrows, int ncols)
{
for (int i = 0; i < nrows; i++)
{
// Iterate the find the minimum
float min = cost_graph[i][0];
for (int j = 0; j < ncols; j++)
{
float val = cost_graph[i][j];
if (val < min)
{
min = val;
}
}
// Subtract the Minimum
for (int j = 0; j < ncols; j++)
{
cost_graph[i][j] -= min;
}
}
}
// Helper method to subtract the minimum col from cost_graph
void subtract_minimum_column(Vec2D<float> &cost_graph, int nrows, int ncols)
{
for (int j = 0; j < ncols; j++)
{
// Iterate and find the minimum
float min = cost_graph[0][j];
for (int i = 0; i < nrows; i++)
{
float val = cost_graph[i][j];
if (val < min)
{
min = val;
}
}
// Subtract the minimum
for (int i = 0; i < nrows; i++)
{
cost_graph[i][j] -= min;
}
}
}
void munkresStep1(Vec2D<float> &cost_graph, PairGraph &star_graph, int nrows,
int ncols)
{
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
if (!star_graph.isRowSet(i) && !star_graph.isColSet(j) && (cost_graph[i][j] == 0))
{
star_graph.set(i, j);
}
}
}
}
// Exits if '1' is returned
bool munkresStep2(const PairGraph &star_graph, CoverTable &cover_table)
{
int k =
star_graph.nrows < star_graph.ncols ? star_graph.nrows : star_graph.ncols;
int count = 0;
for (int j = 0; j < star_graph.ncols; j++)
{
if (star_graph.isColSet(j))
{
cover_table.coverCol(j);
count++;
}
}
return count >= k;
}
bool munkresStep3(Vec2D<float> &cost_graph, const PairGraph &star_graph,
PairGraph &prime_graph, CoverTable &cover_table, std::pair<int, int> &p,
int nrows, int ncols)
{
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
if (cost_graph[i][j] == 0 && !cover_table.isCovered(i, j))
{
prime_graph.set(i, j);
if (star_graph.isRowSet(i))
{
cover_table.coverRow(i);
cover_table.uncoverCol(star_graph.colForRow(i));
}
else
{
p.first = i;
p.second = j;
return 1;
}
}
}
}
return 0;
};
void munkresStep4(PairGraph &star_graph, PairGraph &prime_graph,
CoverTable &cover_table, std::pair<int, int> &p)
{
// This process should be repeated until no star is found in prime's column
while (star_graph.isColSet(p.second))
{
// First find and reset any star found in the prime's columns
std::pair<int, int> s = {star_graph.rowForCol(p.second), p.second};
star_graph.reset(s.first, s.second);
// Set this prime to a star
star_graph.set(p.first, p.second);
// Repeat the same process for prime in cleared star's row
p = {s.first, prime_graph.colForRow(s.first)};
}
star_graph.set(p.first, p.second);
cover_table.clear();
prime_graph.clear();
}
void munkresStep5(Vec2D<float> &cost_graph, const CoverTable &cover_table,
int nrows, int ncols)
{
bool valid = false;
float min;
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
if (!cover_table.isCovered(i, j))
{
if (!valid)
{
min = cost_graph[i][j];
valid = true;
}
else if (cost_graph[i][j] < min)
{
min = cost_graph[i][j];
}
}
}
}
for (int i = 0; i < nrows; i++)
{
if (cover_table.isRowCovered(i))
{
for (int j = 0; j < ncols; j++)
{
cost_graph[i][j] += min;
}
}
}
for (int j = 0; j < ncols; j++)
{
if (!cover_table.isColCovered(j))
{
for (int i = 0; i < nrows; i++)
{
cost_graph[i][j] -= min;
}
}
}
}
void munkres_algorithm(Vec2D<float> &cost_graph, PairGraph &star_graph, int nrows,
int ncols)
{
PairGraph prime_graph(nrows, ncols);
CoverTable cover_table(nrows, ncols);
prime_graph.clear();
cover_table.clear();
star_graph.clear();
int step = 0;
if (ncols >= nrows)
{
subtract_minimum_row(cost_graph, nrows, ncols);
}
if (ncols > nrows)
{
step = 1;
}
std::pair<int, int> p;
bool done = false;
while (!done)
{
switch (step)
{
case 0:
subtract_minimum_column(cost_graph, nrows, ncols);
case 1:
munkresStep1(cost_graph, star_graph, nrows, ncols);
case 2:
if (munkresStep2(star_graph, cover_table))
{
done = true;
break;
}
case 3:
if (!munkresStep3(cost_graph, star_graph, prime_graph, cover_table, p,
nrows, ncols))
{
step = 5;
break;
}
case 4:
munkresStep4(star_graph, prime_graph, cover_table, p);
step = 2;
break;
case 5:
munkresStep5(cost_graph, cover_table, nrows, ncols);
step = 3;
break;
}
}
}