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