View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

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

4.7 | 17 ratings Rate this file 110 Downloads (last 30 days) File Size: 3.27 KB File ID: #20652 Version: 1.4

Hungarian Algorithm for Linear Assignment Problems (V2.3)


Yi Cao (view profile)


10 Jul 2008 (Updated )

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

| Watch this File

File Information

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


Assignprob.Zip inspired this file.

This file inspired Smooth Point Set Registration Using Neighboring Constraints, Tactics Toolbox, Simple Tracker, Eigenshuffle, Hungarian Based Particle Linking, Lapjv Jonker Volgenant Algorithm For Linear Assignment Problem V3.0, and Minimum Cost Constrained Input Output And Control Configuration Co Design Problem.

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 (38)
22 Nov 2016 Rupert Thomas

27 Oct 2016 David

David (view profile)

Hi people, who know the maximal number this file can deal with?

Comment only
22 Oct 2016 Roy Sung

22 Sep 2016 Prakhar Sinha

08 Dec 2015 Biaobin Jiang

21 Apr 2015 arnaud ensberg

be careful it gives completely false results for 40*40 matrix !

31 Oct 2014 Matteo

Matteo (view profile)

Hi people,
I a question about the hungarian algorithm. this algorithm is optimal algorithm for the assignment problem, and the time complexity is O(n^3), right?
But , if the input is the multidimensional matrix, it's possible to use the hungarian algorithm? how does it change the algorithm and the time complexity ?

Comment only
21 Aug 2014 Kui

Kui (view profile)

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?

Comment only
21 Aug 2014 Kui

Kui (view profile)

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%.

Comment only
22 May 2014 Alexander Farley


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

Comment only
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

Comment only
29 Mar 2012 Wasit Limprasert

>> munkres([1 0])

ans =

1 0

Could you fix this problem?

23 Sep 2011 Syed

Syed (view profile)

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

Comment only
21 Sep 2011 Yi Cao

Yi Cao (view profile)

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.

Comment only
21 Sep 2011 Syed

Syed (view profile)

Thanks for a nice implementation
I used the code for following matrix
I have the following matrix
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

gives me:
Result= 3 4 5 1 6 2

I change the Matrix to
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

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

Comment only
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)


Comment only
03 Mar 2011 DQ LIU

DQ LIU (view profile)

Great work, Thanks a lot~~
BTW, I compared your implementation with Niclas Borlin's implementation ( 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?

Comment only
16 Apr 2010 Turkay YILDIZ

09 Mar 2010 Yi Cao

Yi Cao (view profile)


Thanks for pointing out the JV algorithm. You may wish to know that I have implemented a Matlab version of the JV algorithm in
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.


Comment only
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 ''. This solves the same problem and appeared way faster.

Comment only
01 Mar 2010 Yi Cao

Yi Cao (view profile)

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.


Comment only
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

James (view profile)

05 Apr 2009 V. Poor

30 Mar 2009 Oliver Woodford


17 Feb 2009 John D'Errico

John D'Errico (view profile)


21 Dec 2008 Yi Cao

Yi Cao (view profile)

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.

Comment only
21 Dec 2008 ek de

ek de (view profile)

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.
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');
assign1=assign1(assign1~=0);m1 = length(assign1);
assign2=assign2(assign2~=0);m2 = length(assign2);
if m1 ~= m2
disp('Assignment Vecs not compatible');
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));

Comment only
21 Dec 2008 Yi Cao

Yi Cao (view profile)

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.

Comment only
19 Dec 2008 ek de

ek de (view profile)

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

Yi Cao (view profile)

Thanks Immanuel. The bug has been fixed.

Comment only
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.

Comment only
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.

Comment only
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)

Comment only
16 Dec 2008 1.1

Bug fix

01 Mar 2010 1.2

Update to improve efficiency further.

11 Sep 2011 1.3

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

15 Sep 2011 1.4

a bug fixed

Contact us