annotate _FullBNT/KPMtools/optimalMatching.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 % MATCH - Solves the weighted bipartite matching (or assignment)
matthiasm@8 2 % problem.
matthiasm@8 3 %
matthiasm@8 4 % Usage: a = match(C);
matthiasm@8 5 %
matthiasm@8 6 % Arguments:
matthiasm@8 7 % C - an m x n cost matrix; the sets are taken to be
matthiasm@8 8 % 1:m and 1:n; C(i, j) gives the cost of matching
matthiasm@8 9 % items i (of the first set) and j (of the second set)
matthiasm@8 10 %
matthiasm@8 11 % Returns:
matthiasm@8 12 %
matthiasm@8 13 % a - an m x 1 assignment vector, which gives the
matthiasm@8 14 % minimum cost assignment. a(i) is the index of
matthiasm@8 15 % the item of 1:n that was matched to item i of
matthiasm@8 16 % 1:m. If item i (of 1:m) was not matched to any
matthiasm@8 17 % item of 1:n, then a(i) is zero.
matthiasm@8 18
matthiasm@8 19 % Copyright (C) 2002 Mark A. Paskin
matthiasm@8 20 %
matthiasm@8 21 % This program is free software; you can redistribute it and/or modify
matthiasm@8 22 % it under the terms of the GNU General Public License as published by
matthiasm@8 23 % the Free Software Foundation; either version 2 of the License, or
matthiasm@8 24 % (at your option) any later version.
matthiasm@8 25 %
matthiasm@8 26 % This program is distributed in the hope that it will be useful, but
matthiasm@8 27 % WITHOUT ANY WARRANTY; without even the implied warranty of
matthiasm@8 28 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
matthiasm@8 29 % General Public License for more details.
matthiasm@8 30 %
matthiasm@8 31 % You should have received a copy of the GNU General Public License
matthiasm@8 32 % along with this program; if not, write to the Free Software
matthiasm@8 33 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
matthiasm@8 34 % USA.
matthiasm@8 35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@8 36
matthiasm@8 37 function [a] = optimalMatching(C)
matthiasm@8 38
matthiasm@8 39 % Trivial cases:
matthiasm@8 40 [p, q] = size(C);
matthiasm@8 41 if (p == 0)
matthiasm@8 42 a = [];
matthiasm@8 43 return;
matthiasm@8 44 elseif (q == 0)
matthiasm@8 45 a = zeros(p, 1);
matthiasm@8 46 return;
matthiasm@8 47 end
matthiasm@8 48
matthiasm@8 49
matthiasm@8 50 if 0
matthiasm@8 51 % First, reduce the problem by making easy optimal matches. If two
matthiasm@8 52 % elements agree that they are the best match, then match them up.
matthiasm@8 53 [x, a] = min(C, [], 2);
matthiasm@8 54 [y, b] = min(C, [], 1);
matthiasm@8 55 u = find(1:p ~= b(a(:)));
matthiasm@8 56 a(u) = 0;
matthiasm@8 57 v = find(1:q ~= a(b(:))');
matthiasm@8 58 C = C(u, v);
matthiasm@8 59 if (isempty(C)) return; end
matthiasm@8 60 end
matthiasm@8 61
matthiasm@8 62 % Get the (new) size of the two sets, u and v.
matthiasm@8 63 [m, n] = size(C);
matthiasm@8 64
matthiasm@8 65 %mx = realmax;
matthiasm@8 66 mx = 2*max(C(:));
matthiasm@8 67 mn = -2*min(C(:));
matthiasm@8 68 % Pad the affinity matrix to be square
matthiasm@8 69 if (m < n)
matthiasm@8 70 C = [C; mx * ones(n - m, n)];
matthiasm@8 71 elseif (n < m)
matthiasm@8 72 C = [C, mx * ones(m, m - n)];
matthiasm@8 73 end
matthiasm@8 74
matthiasm@8 75 % Run the Hungarian method. First replace infinite values by the
matthiasm@8 76 % largest (or smallest) finite values.
matthiasm@8 77 C(find(isinf(C) & (C > 0))) = mx;
matthiasm@8 78 C(find(isinf(C) & (C < 0))) = mn;
matthiasm@8 79 %fprintf('running hungarian\n');
matthiasm@8 80 [b, cost] = hungarian(C');
matthiasm@8 81
matthiasm@8 82 % Extract only the real assignments
matthiasm@8 83 ap = b(1:m)';
matthiasm@8 84 ap(find(ap > n)) = 0;
matthiasm@8 85
matthiasm@8 86 a = ap;
matthiasm@8 87 %% Incorporate this sub-assignment into the complete assignment
matthiasm@8 88 % k = find(ap);
matthiasm@8 89 % a(u(k)) = v(ap(k));
matthiasm@8 90