File size: 4,838 Bytes
d4035c1 |
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 |
function [matching] = Hungarian(map)
% Hungarian
% Algorithm is from:
% http://www.public.iastate.edu/~ddoty/HungarianAlgorithm.html
matching = zeros(size(map));
x_con = find(sum(~isinf(map), 2) ~= 0);
y_con = find(sum(~isinf(map), 1) ~= 0);
C_size = max(length(x_con),length(y_con));
C_mat = zeros(C_size);
C_mat(1:length(x_con), 1:length(y_con)) = map(x_con, y_con);
if isempty(C_mat)
return
end
stepnum = 1;
while true
switch stepnum
case 1
C_mat = step1(C_mat);
stepnum = 2;
case 2
[covered_row, covered_col, M] = step2(C_mat);
stepnum = 3;
case 3
[covered_col, stepnum] = step3(M, C_size);
case 4
[M, covered_row, covered_col, Z_row, Z_col, stepnum] = step4(C_mat,covered_row,covered_col,M);
case 5
[M, covered_row, covered_col] = step5(M,Z_row,Z_col,covered_row,covered_col);
stepnum = 3;
case 6
C_mat = step6(C_mat, covered_row, covered_col);
stepnum = 4;
case 7
break;
end
end
matching(x_con, y_con) = M(1:length(x_con), 1:length(y_con));
end
function C_mat = step1(C_mat)
for i = 1:length(C_mat)
min_val = min(C_mat(i, :));
C_mat(i,:) = C_mat(i,:) - min_val;
end
end
function [covered_row, covered_col, M] = step2(C_mat)
n = length(C_mat);
covered_row = zeros(n, 1);
covered_col = zeros(n, 1);
M = zeros(n, n);
for i = 1:n
for j = 1:n
if C_mat(i,j) == 0 && covered_row(i) == 0 && covered_col(j) == 0
M(i,j) = 1;
covered_row(i) = 1;
covered_col(j) = 1;
end
end
end
covered_row(:) = 0;
covered_col(:) = 0;
end
function [covered_col,stepnum] = step3(M, C_size)
covered_col = sum(M, 1);
if sum(covered_col) == C_size
stepnum = 7;
else
stepnum = 4;
end
end
function [M, covered_row, covered_col, Z_row, Z_col, stepnum] = step4(C_mat, covered_row, covered_col, M)
function [row, col] = find_a_zero()
row = 0;
col = 0;
i = 1;
done2 = false;
while ~done2
j = 1;
while true
if C_mat(i,j) == 0 && covered_row(i) == 0 && covered_col(j) == 0
row = i;
col = j;
done2 = true;
end
j = j + 1;
if j > n
break;
end
end
i = i + 1;
if i > n
done2 = true;
end
end
end
n = length(C_mat);
done = false;
while ~done
[row, col] = find_a_zero();
if row == 0
stepnum = 6;
done = true;
Z_row = 0;
Z_col = 0;
else
M(row,col) = 2;
if sum(find(M(row,:)==1)) ~= 0
covered_row(row) = 1;
zcol = find(M(row,:)==1);
covered_col(zcol) = 0;
else
stepnum = 5;
done = true;
Z_row = row;
Z_col = col;
end
end
end
end
function [M,covered_row,covered_col] = step5(M, Z_row, Z_col, covered_row, covered_col)
done = false;
count = 1;
while ~done
rindex = find(M(:,Z_col(count))==1);
if rindex > 0
count = count + 1;
Z_row(count,1) = rindex;
Z_col(count,1) = Z_col(count - 1);
else
done = true;
end
if ~done
cindex = find(M(Z_row(count),:)==2);
count = count + 1;
Z_row(count, 1) = Z_row(count - 1);
Z_col(count, 1) = cindex;
end
end
for i = 1:count
if M(Z_row(i), Z_col(i)) == 1
M(Z_row(i), Z_col(i)) = 0;
else
M(Z_row(i), Z_col(i)) = 1;
end
end
covered_row(:) = 0;
covered_col(:) = 0;
M(M == 2) = 0;
end
function C_mat = step6(C_mat, covered_row, covered_col)
from_row = find(covered_row == 0);
from_col = find(covered_col == 0);
minval = min(min(C_mat(from_row, from_col)));
C_mat(find(covered_row == 1), :) = C_mat(find(covered_row == 1), :) + minval;
C_mat(:, find(covered_col == 0)) = C_mat(:,find(covered_col == 0)) - minval;
end
function cnum = min_line_cover(Edge)
[covered_row, covered_col, M] = step2(Edge);
covered_col = step3(M, length(Edge));
[M, covered_row, covered_col, Z_row, Z_col] = step4(Edge,covered_row,covered_col,M);
cnum = length(Edge) - sum(covered_row) - sum(covered_col);
end |