HungarianAlgorithm icon indicating copy to clipboard operation
HungarianAlgorithm copied to clipboard

Never ending loop when number rows > cols in matrix

Open keesschollaart81 opened this issue 4 years ago • 2 comments

I stumbled upon a never ending loop, easy to reproduce like:

var matrix1 = new int[,]{
   {0,0,0},
   {0,0,0}
};
var matrix2 = new int[,]{
   {0,0},
   {0,0},
   {0,0}
};
var assignments = matrix1.FindAssignments();
Console.WriteLine($"Found {assignments.Length} assignments in matrix 1");

var assignments2 = matrix2.FindAssignments();
Console.WriteLine($"Found {assignments.Length} assignments in matrix 2"); // we never get here

Run live from dotnetfiddle.

I'm not really sure how to fix this but I assume it is unexpected behaviour?

keesschollaart81 avatar Jul 04 '21 06:07 keesschollaart81

I will try to reproduce it.

vivet avatar Jul 06 '21 06:07 vivet

Hi Sorry for the long delay, but I haven't forgotten the issue. I have reproduced the problem, and I recall from many years ago I had the same problem. I am not an expert in the algorithm, and I haven't written the original source-code. I used to handle it by using the Array extension SquareArray, before invoking the algorithm.

I am not able to fix it, but feel free to submit a PR

vivet avatar Sep 21 '22 06:09 vivet

I also met this problem and found out one way to resolve it. See the pull request :)

Mrstarr avatar May 18 '24 15:05 Mrstarr

I will check it out Good work fixing it 😀

vivet avatar May 21 '24 09:05 vivet

I have merged the PR. Also I removed the SquareArray method, as it should not be needed anymore.

vivet avatar May 22 '24 04:05 vivet