├── Hungarian.asv ├── Hungarian.m ├── findMin.m ├── fit.asv └── fit.m /Hungarian.asv: -------------------------------------------------------------------------------- 1 | clc, clear; 2 | % c = [2, 3, 5, 7; 3, 5, 2, 8; 9, 5, 7, 8; 2, 2, 3, 9]; 3 | c = [12, 7, 9, 7, 9; 8, 9, 6, 6, 6; 7, 17, 12, 14, 12; 15, 14, 6, 6, 10; 4, 10, 7, 10, 6 ]; 4 | % c = [4,8,7,15,12;7,9,17,14,10;6,9,12,8,7;6,7,14,6,10;6,9,12,10,6]; % 5 | %function result = Hungarian(c) 6 | size = length(c); 7 | %pre-operation 8 | line = min((c')); 9 | line = repmat(line',1, size); 10 | c = c - line; 11 | column = min(c); 12 | column = repmat(column, size, 1); 13 | c = c - column; 14 | 15 | %% 16 | [axis_line, axis_column] = find(c == 0); 17 | %C = [axis_line axis_column]; 18 | result = [axis_line, axis_column]; 19 | % [x, y] = meshgrid(temp_line, temp_column); 20 | % result = [x(:), y(:)]; 21 | %get the left position 22 | 23 | %decode the matrix to three dimension 24 | %test = cell(size, 1); 25 | z = zeros(size, size); 26 | for i=1:size 27 | x = find(result(:, 2) == i); 28 | temp_size = length(x); 29 | z(i, 1) = temp_size; 30 | for j = 2:temp_size+1 31 | test = result(x(j - 1),1); 32 | z(i, j) = test; 33 | end 34 | end 35 | disp(result) 36 | disp('das') 37 | disp(z) 38 | disp('asf') 39 | 40 | 41 | %% 42 | %mark 43 | %flag = zeros(size, size); 44 | %this loop is aimed to finding the answer for sereral iteration 45 | tempresult = result; 46 | while true 47 | %mark 48 | markedLine = zeros(1, size); 49 | markedColumn = zeros(1, size); 50 | [tempAnswer, all] = fit(z, 1, (1:size), zeros(1, size), []); 51 | disp(tempAnswer) 52 | left = setdiff((1:size), tempAnswer); 53 | if ~isempty(all) 54 | disp('all rolution'); 55 | for i = 1:length(all(:, 1)) 56 | fprintf('the %dth solution', i); 57 | disp(all(i, :)); 58 | end 59 | break; 60 | end 61 | %mark the row 62 | %flag(left, :) = 1; 63 | markedLine(left) = 1; 64 | %this loop is aimed to find the possible zero 65 | %find all marked lines 66 | temp_marked = find(markedLine == 1); 67 | for i = 1:length(temp_marked) 68 | tmp = temp_marked(i) 69 | while true 70 | %% 71 | %step1 72 | %find the line(subscript) 73 | disp(markedLine) 74 | disp(tmp) 75 | tmp = find(tempresult(:, 1) == tmp); 76 | erase = tmp; 77 | %find the column 78 | disp(tmp) 79 | disp(tempresult) 80 | tmp = tempresult(tmp, 2); 81 | %find weather it is terminate 82 | if isempty(tmp) 83 | break; 84 | end 85 | %mark the column 86 | %flag(:,tmp) = 1; 87 | markedColumn(tmp) = 1; 88 | %erase the line 89 | tempresult(erase, :) = []; 90 | 91 | %% 92 | %step2 93 | %find the line(subscript) 94 | tmp = find(tempresult(:, 2) == tmp); 95 | erase = tmp; 96 | %find the line 97 | tmp = tempresult(tmp, 1); 98 | %find weather it is terminate 99 | if isempty(tmp) 100 | break; 101 | end 102 | %mark the line 103 | %flag(tmp, :) = 1; 104 | markedLine(tmp) = 1; 105 | %erase the column 106 | tempresult(erase, :) = []; 107 | end 108 | end 109 | 110 | %% 111 | %find the minimal number 112 | line = markedLine; 113 | column = not(markedColumn); 114 | flag = (line')*column; 115 | subscript = find(flag == 1); 116 | min = findMin(c, subscript); 117 | %add and sub 118 | templine = find(markedLine == 1); 119 | tempcolumn = find(markedColumn == 1); 120 | c(templine, :) = c(templine, :) - min; 121 | c(:, tempcolumn) = c(:, tempcolumn) + min; 122 | temp = find(c(subscript) == 0); 123 | temp = subscript(temp); 124 | %for i = 1:length(temp) 125 | y = fix((temp-1)/5) + 1; 126 | x = temp - (y-1)*5; 127 | %end 128 | disp('------') 129 | disp(tempresult) 130 | disp([x, y]) 131 | tempresult = [result; [x, y]]; 132 | %check the answer 133 | for i = 1:length(x) 134 | z(x(i), 1) = z(x(i), 1) + 1; 135 | z(x(i), z(x(i), 1)+1) = y(i); 136 | end 137 | end 138 | result = all; -------------------------------------------------------------------------------- /Hungarian.m: -------------------------------------------------------------------------------- 1 | clc, clear; 2 | % c = [2, 3, 5, 7; 3, 5, 2, 8; 9, 5, 7, 8; 2, 2, 3, 9]; 3 | c = [12, 7, 9, 7, 9; 8, 9, 6, 6, 6; 7, 17, 12, 14, 12; 15, 14, 6, 6, 10; 4, 10, 7, 10, 6 ]; 4 | % c = [4,8,7,15,12;7,9,17,14,10;6,9,12,8,7;6,7,14,6,10;6,9,12,10,6]; % end up with endless loop 5 | %function result = Hungarian(c) 6 | size = length(c); 7 | %pre-operation 8 | line = min((c')); 9 | line = repmat(line',1, size); 10 | c = c - line; 11 | column = min(c); 12 | column = repmat(column, size, 1); 13 | c = c - column; 14 | 15 | %% 16 | [axis_line, axis_column] = find(c == 0); 17 | %C = [axis_line axis_column]; 18 | result = [axis_line, axis_column]; 19 | % [x, y] = meshgrid(temp_line, temp_column); 20 | % result = [x(:), y(:)]; 21 | %get the left position 22 | 23 | %decode the matrix to three dimension 24 | %test = cell(size, 1); 25 | z = zeros(size, size); 26 | for i=1:size 27 | x = find(result(:, 2) == i); 28 | temp_size = length(x); 29 | z(i, 1) = temp_size; 30 | for j = 2:temp_size+1 31 | test = result(x(j - 1),1); 32 | z(i, j) = test; 33 | end 34 | end 35 | 36 | 37 | %% 38 | %mark 39 | %flag = zeros(size, size); 40 | %this loop is aimed to finding the answer for sereral iteration 41 | tempresult = result; 42 | while true 43 | %mark 44 | markedLine = zeros(1, size); 45 | markedColumn = zeros(1, size); 46 | [tempAnswer, all] = fit(z, 1, (1:size), zeros(1, size), []); 47 | left = setdiff((1:size), tempAnswer); 48 | if ~isempty(all) 49 | disp('all rolution'); 50 | for i = 1:length(all(:, 1)) 51 | fprintf('the %dth solution', i); 52 | disp(all(i, :)); 53 | end 54 | break; 55 | end 56 | %mark the row 57 | %flag(left, :) = 1; 58 | markedLine(left) = 1; 59 | %this loop is aimed to find the possible zero 60 | %find all marked lines 61 | temp_marked = find(markedLine == 1); 62 | for i = 1:length(temp_marked) 63 | tmp = temp_marked(i); 64 | while true 65 | %% 66 | %step1 67 | %find the line(subscript) 68 | tmp = find(tempresult(:, 1) == tmp); 69 | erase = tmp; 70 | %find the colum 71 | tmp = tempresult(tmp, 2); 72 | %find weather it is terminate 73 | if isempty(tmp) 74 | break; 75 | end 76 | %mark the column 77 | %flag(:,tmp) = 1; 78 | markedColumn(tmp) = 1; 79 | %erase the line 80 | tempresult(erase, :) = []; 81 | 82 | %% 83 | %step2 84 | %find the line(subscript) 85 | tmp = find(tempresult(:, 2) == tmp); 86 | erase = tmp; 87 | %find the line 88 | tmp = tempresult(tmp, 1); 89 | %find weather it is terminate 90 | if isempty(tmp) 91 | break; 92 | end 93 | %mark the line 94 | %flag(tmp, :) = 1; 95 | markedLine(tmp) = 1; 96 | %erase the column 97 | tempresult(erase, :) = []; 98 | end 99 | end 100 | 101 | %% 102 | %find the minimal number 103 | line = markedLine; 104 | column = not(markedColumn); 105 | flag = (line')*column; 106 | subscript = find(flag == 1); 107 | min = findMin(c, subscript); 108 | %add and sub 109 | templine = find(markedLine == 1); 110 | tempcolumn = find(markedColumn == 1); 111 | c(templine, :) = c(templine, :) - min; 112 | c(:, tempcolumn) = c(:, tempcolumn) + min; 113 | temp = find(c(subscript) == 0); 114 | temp = subscript(temp); 115 | %for i = 1:length(temp) 116 | y = fix((temp-1)/5) + 1; 117 | x = temp - (y-1)*5; 118 | %end 119 | tempresult = [result; [x, y]]; 120 | %check the answer 121 | for i = 1:length(x) 122 | z(x(i), 1) = z(x(i), 1) + 1; 123 | z(x(i), z(x(i), 1)+1) = y(i); 124 | end 125 | end 126 | result = all; -------------------------------------------------------------------------------- /findMin.m: -------------------------------------------------------------------------------- 1 | function min = findMin(c, subscript) 2 | min = inf; 3 | for i = 1:length(subscript) 4 | if c(subscript(i)) < min 5 | min = c(subscript(i)); 6 | end 7 | end -------------------------------------------------------------------------------- /fit.asv: -------------------------------------------------------------------------------- 1 | function result = fit(c, dimension, left, result) 2 | if isempty(left) 3 | return 4 | end 5 | for i = 1:c(dimension) 6 | nextCandidate =c(dimension, i+1); 7 | if ismember(nextCandidate, left) 8 | result(dimension) = nextCandidate; 9 | %wheather or not finding the solution 10 | if(dimension == length(c)) 11 | disp(result); 12 | return 13 | end 14 | result = fit(c, dimension+1, setdiff(left, nextCandidate), result); 15 | if isempty(setdiff([s:size], result)) 16 | break; 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /fit.m: -------------------------------------------------------------------------------- 1 | function [result, all] = fit(c, dimension, left, result, all) 2 | if isempty(left) 3 | return 4 | end 5 | for i = 1:c(dimension) 6 | nextCandidate =c(dimension, i+1); 7 | if ismember(nextCandidate, left) 8 | result(dimension) = nextCandidate; 9 | %wheather or not finding the solution 10 | if(dimension == length(c)) 11 | all = [all; result]; 12 | return 13 | end 14 | [result, all] = fit(c, dimension+1, setdiff(left, nextCandidate), result, all); 15 | end 16 | end 17 | --------------------------------------------------------------------------------