munkres-cpp
munkres-cpp copied to clipboard
do this code return the minimum assignment or the max one?
sometimes i get the max return: d2: -0.851823 -0.0848411 0.997337 0.519324 0.752464 0.05258 0.971547 0.304817 -0.311632 dd: -1 -1 0 -1 0 -1 0 -1 -1 pair 0 is :0,2 pair 1 is :1,1 pair 2 is :2,0
sometimes i get the min one: d2: 0.0511896 -0.0754626 -0.831758 0.541599 0.0293286 -0.483856 0.436718 -0.553582 0.285733 dd: -1 -1 0 0 -1 -1 -1 0 -1 pair 0 is :0,2 pair 1 is :1,0 pair 2 is :2,1
why is that? and how can i fix that?
Hello @zhuzy-2018,
-0.851823 -0.0848411 0.997337 0.519324 0.752464 0.05258 0.971547 0.304817 -0.311632
Please note that the algorithm requires as input parameters a nonnegative cost matrix (https://en.wikipedia.org/wiki/Hungarian_algorithm#The_problem).
and how can i fix that?
I would suggest preparing a cost matrix with negative values in the following way:
- Find the minimal negative value in the original input matrix. In your matrix, it is
-0.851823
. - Add a modulo of this value
0.851823
to all matrix elements. - Pass the modified nonnegative matrix into the algorithm.
0 0.7669819 1.84916
1.371147 1.604287 0.904403
1.82337 1.15664 0.540191
The solution will be:
0 -1 -1
-1 -1 0
-1 0 -1
That means that for the original matrix cost -0.851823 + 0.05258 + 0.304817 = -0.494426
is minimal one.
Please don't hesitate to ask additional questions if it doesn't help.