/src/git/linear-assignment.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Based on: Jonker, R., & Volgenant, A. (1987). <i>A shortest augmenting path |
3 | | * algorithm for dense and sparse linear assignment problems</i>. Computing, |
4 | | * 38(4), 325-340. |
5 | | */ |
6 | | #include "git-compat-util.h" |
7 | | #include "linear-assignment.h" |
8 | | |
9 | 0 | #define COST(column, row) cost[(column) + column_count * (row)] |
10 | | |
11 | | /* |
12 | | * The parameter `cost` is the cost matrix: the cost to assign column j to row |
13 | | * i is `cost[j + column_count * i]. |
14 | | */ |
15 | | void compute_assignment(int column_count, int row_count, int *cost, |
16 | | int *column2row, int *row2column) |
17 | 0 | { |
18 | 0 | int *v, *d; |
19 | 0 | int *free_row, free_count = 0, saved_free_count, *pred, *col; |
20 | 0 | int i, j, phase; |
21 | |
|
22 | 0 | if (column_count < 2) { |
23 | 0 | memset(column2row, 0, sizeof(int) * column_count); |
24 | 0 | memset(row2column, 0, sizeof(int) * row_count); |
25 | 0 | return; |
26 | 0 | } |
27 | | |
28 | 0 | memset(column2row, -1, sizeof(int) * column_count); |
29 | 0 | memset(row2column, -1, sizeof(int) * row_count); |
30 | 0 | ALLOC_ARRAY(v, column_count); |
31 | | |
32 | | /* column reduction */ |
33 | 0 | for (j = column_count - 1; j >= 0; j--) { |
34 | 0 | int i1 = 0; |
35 | |
|
36 | 0 | for (i = 1; i < row_count; i++) |
37 | 0 | if (COST(j, i1) > COST(j, i)) |
38 | 0 | i1 = i; |
39 | 0 | v[j] = COST(j, i1); |
40 | 0 | if (row2column[i1] == -1) { |
41 | | /* row i1 unassigned */ |
42 | 0 | row2column[i1] = j; |
43 | 0 | column2row[j] = i1; |
44 | 0 | } else { |
45 | 0 | if (row2column[i1] >= 0) |
46 | 0 | row2column[i1] = -2 - row2column[i1]; |
47 | 0 | column2row[j] = -1; |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | | /* reduction transfer */ |
52 | 0 | ALLOC_ARRAY(free_row, row_count); |
53 | 0 | for (i = 0; i < row_count; i++) { |
54 | 0 | int j1 = row2column[i]; |
55 | 0 | if (j1 == -1) |
56 | 0 | free_row[free_count++] = i; |
57 | 0 | else if (j1 < -1) |
58 | 0 | row2column[i] = -2 - j1; |
59 | 0 | else { |
60 | 0 | int min = COST(!j1, i) - v[!j1]; |
61 | 0 | for (j = 1; j < column_count; j++) |
62 | 0 | if (j != j1 && min > COST(j, i) - v[j]) |
63 | 0 | min = COST(j, i) - v[j]; |
64 | 0 | v[j1] -= min; |
65 | 0 | } |
66 | 0 | } |
67 | |
|
68 | 0 | if (free_count == |
69 | 0 | (column_count < row_count ? row_count - column_count : 0)) { |
70 | 0 | free(v); |
71 | 0 | free(free_row); |
72 | 0 | return; |
73 | 0 | } |
74 | | |
75 | | /* augmenting row reduction */ |
76 | 0 | for (phase = 0; phase < 2; phase++) { |
77 | 0 | int k = 0; |
78 | |
|
79 | 0 | saved_free_count = free_count; |
80 | 0 | free_count = 0; |
81 | 0 | while (k < saved_free_count) { |
82 | 0 | int u1, u2; |
83 | 0 | int j1 = 0, j2, i0; |
84 | |
|
85 | 0 | i = free_row[k++]; |
86 | 0 | u1 = COST(j1, i) - v[j1]; |
87 | 0 | j2 = -1; |
88 | 0 | u2 = INT_MAX; |
89 | 0 | for (j = 1; j < column_count; j++) { |
90 | 0 | int c = COST(j, i) - v[j]; |
91 | 0 | if (u2 > c) { |
92 | 0 | if (u1 < c) { |
93 | 0 | u2 = c; |
94 | 0 | j2 = j; |
95 | 0 | } else { |
96 | 0 | u2 = u1; |
97 | 0 | u1 = c; |
98 | 0 | j2 = j1; |
99 | 0 | j1 = j; |
100 | 0 | } |
101 | 0 | } |
102 | 0 | } |
103 | 0 | if (j2 < 0) { |
104 | 0 | j2 = j1; |
105 | 0 | u2 = u1; |
106 | 0 | } |
107 | |
|
108 | 0 | i0 = column2row[j1]; |
109 | 0 | if (u1 < u2) |
110 | 0 | v[j1] -= u2 - u1; |
111 | 0 | else if (i0 >= 0) { |
112 | 0 | j1 = j2; |
113 | 0 | i0 = column2row[j1]; |
114 | 0 | } |
115 | |
|
116 | 0 | if (i0 >= 0) { |
117 | 0 | if (u1 < u2) |
118 | 0 | free_row[--k] = i0; |
119 | 0 | else |
120 | 0 | free_row[free_count++] = i0; |
121 | 0 | } |
122 | 0 | row2column[i] = j1; |
123 | 0 | column2row[j1] = i; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | /* augmentation */ |
128 | 0 | saved_free_count = free_count; |
129 | 0 | ALLOC_ARRAY(d, column_count); |
130 | 0 | ALLOC_ARRAY(pred, column_count); |
131 | 0 | ALLOC_ARRAY(col, column_count); |
132 | 0 | for (free_count = 0; free_count < saved_free_count; free_count++) { |
133 | 0 | int i1 = free_row[free_count], low = 0, up = 0, last, k; |
134 | 0 | int min, c, u1; |
135 | |
|
136 | 0 | for (j = 0; j < column_count; j++) { |
137 | 0 | d[j] = COST(j, i1) - v[j]; |
138 | 0 | pred[j] = i1; |
139 | 0 | col[j] = j; |
140 | 0 | } |
141 | |
|
142 | 0 | j = -1; |
143 | 0 | do { |
144 | 0 | last = low; |
145 | 0 | min = d[col[up++]]; |
146 | 0 | for (k = up; k < column_count; k++) { |
147 | 0 | j = col[k]; |
148 | 0 | c = d[j]; |
149 | 0 | if (c <= min) { |
150 | 0 | if (c < min) { |
151 | 0 | up = low; |
152 | 0 | min = c; |
153 | 0 | } |
154 | 0 | col[k] = col[up]; |
155 | 0 | col[up++] = j; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | for (k = low; k < up; k++) |
159 | 0 | if (column2row[col[k]] == -1) |
160 | 0 | goto update; |
161 | | |
162 | | /* scan a row */ |
163 | 0 | do { |
164 | 0 | int j1 = col[low++]; |
165 | |
|
166 | 0 | i = column2row[j1]; |
167 | 0 | u1 = COST(j1, i) - v[j1] - min; |
168 | 0 | for (k = up; k < column_count; k++) { |
169 | 0 | j = col[k]; |
170 | 0 | c = COST(j, i) - v[j] - u1; |
171 | 0 | if (c < d[j]) { |
172 | 0 | d[j] = c; |
173 | 0 | pred[j] = i; |
174 | 0 | if (c == min) { |
175 | 0 | if (column2row[j] == -1) |
176 | 0 | goto update; |
177 | 0 | col[k] = col[up]; |
178 | 0 | col[up++] = j; |
179 | 0 | } |
180 | 0 | } |
181 | 0 | } |
182 | 0 | } while (low != up); |
183 | 0 | } while (low == up); |
184 | | |
185 | 0 | update: |
186 | | /* updating of the column pieces */ |
187 | 0 | for (k = 0; k < last; k++) { |
188 | 0 | int j1 = col[k]; |
189 | 0 | v[j1] += d[j1] - min; |
190 | 0 | } |
191 | | |
192 | | /* augmentation */ |
193 | 0 | do { |
194 | 0 | if (j < 0) |
195 | 0 | BUG("negative j: %d", j); |
196 | 0 | i = pred[j]; |
197 | 0 | column2row[j] = i; |
198 | 0 | SWAP(j, row2column[i]); |
199 | 0 | } while (i1 != i); |
200 | 0 | } |
201 | | |
202 | 0 | free(col); |
203 | 0 | free(pred); |
204 | 0 | free(d); |
205 | 0 | free(v); |
206 | 0 | free(free_row); |
207 | 0 | } |