Code covered by the BSD License  

Highlights from
Hungarian Algorithm for Linear Assignment Problems (V2.3)

4.91667

4.9 | 12 ratings Rate this file 105 Downloads (last 30 days) File Size: 3.27 KB File ID: #20652

Hungarian Algorithm for Linear Assignment Problems (V2.3)

by

 

10 Jul 2008 (Updated )

An extremely fast implementation of the Hungarian algorithm on a native Matlab code.

| Watch this File

File Information
Description

This is an extremely fast implementation of the famous Hungarian algorithm (aslo known as Munkres' algorithm). It can solve a 1000 x 1000 problem in about 20 seconds in a Core Duo (T2500 @ 2.00GHz) XP laptop with Matlab 2008a, which is about 2.5 times faster than the mex code "assignmentoptimal" in FEX ID 6543, about 6 times faster than the author's first version in FEX ID 20328, and at least 30 times faster than other Matlab implementations in the FEX.

The code can also handle rectangular prolems and problems with forbiden allocations.

The new version (V2.3)is able to conduct a partial assignment if a full assignment is not feasible.

For more details of the Hungarian algorithm, visit http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html

Acknowledgements

Assignprob.Zip inspired this file.

This file inspired Smooth Point Set Registration Using Neighboring Constraints, Simple Tracker, Tactics Toolbox, Eigenshuffle, Hungarian Based Particle Linking, and Lapjv Jonker Volgenant Algorithm For Linear Assignment Problem V3.0.

MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (31)
21 Aug 2014 Kui

Note that only after I posted did I see the discussion about JIT and profiler results... I got my numbers by looking at the profiler and not with tic/toc or external timer. Perhaps the profiler is misleading?

21 Aug 2014 Kui

I have found a code optimization for the outerplus function using bsxfun. The following code is equivalent but about an order of magnitude faster:

function [minval,rIdx,cIdx]=outerplus(M,x,y)

xPlusYc = bsxfun(@plus, x, y);
M = M - xPlusYc;
minval = min(M(:));
[rIdx, cIdx] = find(M == minval);

For my 60x60 test cases, this reduces the runtime of the overall algorithm by about 30%.

22 May 2014 Alexander Farley  
11 May 2014 MUHAMMAD UZAIR

kindly help me by writing short and generalized code for assignment method of scheduling...

07 May 2014 Shengjie Guo  
24 Apr 2012 Arun Kumar Chithanar

I was wrong about the above comment. It completes, but takes a very long time. For instance, it took 287s for a 100 x 983 matrix. This matrix was from my specific problem. All the weights were real and there were no zero weights.

On the other hand, on the same system it completes a 400 x 400 random in 4 seconds.

Could the specific values in the input matrix cause such a drastic difference in performance?

I checked

29 Mar 2012 Wasit Limprasert

>> munkres([1 0])

ans =

1 0

Could you fix this problem?

23 Sep 2011 Syed

I am thankful to you for pointing this out as I was comparing your method with another algorithm and it was giving output columnwise.
regards

21 Sep 2011 Yi Cao

Well, I can see what you try to do is to increase the cost of selected assignment then to find next best assignment. However, you made a wrong change. The assignment results in dicated row 1 assigned with colume 3, but you miss understood as column 1 assigned with row 3. Wish this helps.

21 Sep 2011 Syed

Thanks for a nice implementation
I used the code for following matrix
I have the following matrix
M=
67 98 65 95 79 82
50 40 91 66 57 35
61 74 85 112 39 79
41 63 72 97 39 56
56 55 83 91 59 50
66 34 98 70 69 54

Result=munkres(M);
gives me:
Result= 3 4 5 1 6 2

I change the Matrix to
Mp=
67 98 65 205 79 82
50 40 91 66 57 145
171 74 85 112 39 79
41 173 72 97 39 56
56 55 193 91 59 50
66 34 98 70 179 54

Result=munkres(Mp);
gives me:
Result= 3 4 5 1 6 2
cost is also same in both cases.
could u please tell me the reason.

18 Aug 2011 Ricky Wong

Hi, Yi. Thanks for your effort.
However, the function as below is missing
taht prevents me from using your code on Matlab...

bsxfun(@minus, dMat, minR)

Ricky

03 Mar 2011 DQ LIU

Great work, Thanks a lot~~
BTW, I compared your implementation with Niclas Borlin's implementation (http://www.mathworks.com/matlabcentral/fileexchange/94-assignprob-zip). The results are not the same. Yours seems better. Given his is a classic implementation, do you know why? or did you did some improvements over the classic ones?

16 Apr 2010 Turkay YILDIZ  
09 Mar 2010 Yi Cao

Anoop,

Thanks for pointing out the JV algorithm. You may wish to know that I have implemented a Matlab version of the JV algorithm in
http://www.mathworks.com/matlabcentral/fileexchange/26836
Certainly, it will not be as fast as the mex code, but it has no limitation on the cost matrix to be integer. If you test it, please let me know how it works with your application.

Yi

02 Mar 2010 Anoop Korattikara Balan

Thanks... You might be right about the memory part. Although I was cleaning the workspace, I was using a MEX code which might have a memory leak (dont know if this is possible).

I ended up using the Jonker-Volgenant shortest augmenting path algorithm from 'http://boguslawobara.net/index.php?page=software.php'. This solves the same problem and appeared way faster.

01 Mar 2010 Yi Cao

Hi Anoop Balan,

Thanks for commenting on the code. The algorithm is polynomial. This means the computation time will growth about n^p with n the size of the problem and p some constant depending on CPU speed, memory and software implementation. On my PC, Intel Core2 Quad CPU (Q9300) 2.5 GHz with 4 GB RAM, with XP and Matlab 2009b, I got the following results:

n = 1000, cpu time = 18 sec
n = 2000, cpu time = 150 sec
n = 4000, cpu time = 1216 sec

Another machin

n = 1000, cpu time = 128 sec
n = 2000, cpu time = 1024 sec

So roughly, p = 3 (2^3 = 8). I do not know why your machin has so large p (>5). When you solve a large size problem, make sure you start with a clean workspace to avoid any unnecessary overhead for swaping memory with hard disk.

Yi

28 Feb 2010 Anoop Korattikara Balan

Thanks a lot for for sharing this code!

This works very well for sizes around 1000x1000 (around 35 secs). However, for a 1500x1500 problem it takes around 260 secs and for a 2000x2000 problem it takes around 1670 seconds.

My problem sets are of size 3000x3000 - 4000x4000. Would you know of any approximation algorithms that work well with such sizes?

02 Nov 2009 James  
05 Apr 2009 V. Poor  
30 Mar 2009 Oliver Woodford

Excellent

17 Feb 2009 John D'Errico

splendid!

21 Dec 2008 Yi Cao

Oh, yes. It was my mistake. A=-PROFIT gives the maximum of sum(PROFIT), but A=1./PROFIT results in the maximum of 1/sum(1./PROFIT), which is different from sum(PROFIT). Sorry for this.

21 Dec 2008 ek de

Hi Yi,

Using COST = 1./PROFIT doesn't seem to give the same results as with COST = -PROFIT. Try out the code below. In some cases the solutions result with different profits.
%%
clc
clear
for i = 1:100
m = ceil(rand*20)+1;
n = ceil(rand*20)+1;
a = rand(m,n)+eps;
[assign1 cost1] = munkres(-a);
[assign2 cost2] = munkres(1./a);
if ~all(assign1 == assign2)
disp('Different assignments');
if ~all(sort(assign1) == sort(assign2))
disp('Assignment vectors do not agree on permutations');
disp([assign1;assign2]);
end
assign1=assign1(assign1~=0);m1 = length(assign1);
assign2=assign2(assign2~=0);m2 = length(assign2);
if m1 ~= m2
disp('Assignment Vecs not compatible');
continue;
end
cost1 = sum(sum((a.*accumarray([(1:m1)' assign1'],ones(m1,1),[m n]))));
cost2 = sum(sum((a.*accumarray([(1:m1)' assign2'],ones(m1,1),[m n]))));
disp(sprintf('Cost difference = %f',abs(cost1 - cost2)/cost1));
end
end

21 Dec 2008 Yi Cao

Yes, the code works with negative cost. You can either use negative cost or use reciprocal, i.e. COST = 1./PROFIT if you do not have zero profit elements.

19 Dec 2008 ek de

Hi Yi,

Will this work if I want to solve the maximum weight matching? That is, if we have a profit matrix rather than a cost, and we want to maximize the profit rather than minimize the cost. I assume that negating the cost matrix should work but was wondering if you could confirm this.
BTW, this is really fast!

19 Dec 2008 Immanuel Weber

Hey Yi
that was a fast bugfix for your fast algorithm.
switched back to it.
Thank you!

16 Dec 2008 Yi Cao

Thanks Immanuel. The bug has been fixed.

15 Dec 2008 Immanuel Weber

Hi Yi, I used your algorithm and it did good work until I encountered a possible bug.
As long as the input matrix ist big enough it works fine, but when the matrix consists of only 2 entries the algorithm creates a wrong match. For example the matrix [1 0], the matching should be 1->2, a return of [2], but the algorithm returns 1->1 ([1]).
Caused by lack of time I switched for the moment to another algorithm, but it would be nice if you could post a bugfix.
In addition, I use it on R2006b, I can't test it on newer versions, so maybe this is related to the old one.

21 Sep 2008 search search

cool, really fast. Thank you.
I test it using Matlab 2007a, will 2008a faster?

12 Jul 2008 Yi Cao

Hi, Laszlo:

Thanks for comments.
The outerplus function uses JIT acceleration. If you use an old version Matlab, which does not support JIT, then you have to convert it to a vectorized version. Note, if you use profile view, this function will always be slower than vectorized. One way to do vectorization is as you described. Alternatively, you can use bsxfun, which should be faster than repmat.

11 Jul 2008 Laszlo Sragner

Hi, good stuff, but it is much slower on my machine. It seems outerplus() is the culprit.
Is it possible that this implementation has some 64bit issues?

This helped me a bit:

function [minval,rIdx,cIdx]=outerplus3(M,x,y)
M=M-repmat(x,1,numel(y))-repmat(y,numel(x),1);
minval=min(M(:));
[rIdx,cIdx]=find(M==minval);

Updates
16 Dec 2008

Bug fix

01 Mar 2010

Update to improve efficiency further.

11 Sep 2011

The new version implements particial assignment if a full assignment is not feasible.

15 Sep 2011

a bug fixed

Contact us