/HungarianAlgorithm

Hungarian Algorithm Implementation

Primary LanguageC#MIT LicenseMIT

Hungarian Algorithm

Build status NuGet NuGet

The Hungarian algorithm is a combinatorial optimization method, that solves the assignment problem in polynomial time, and which anticipated later primal-dual methods. In other words, based on a matrix of possible combinations of costs, the algorithm returns an ordered collcetion of matches, having the lowest combined cost, thus being the most optimal assignment.


The Algorithm

The example below, shows how to use and apply the algorithm.
It defines a two-dimensional array, passes it to algorithm, and recieves a result of an array of matched columns for each row (x) passed.

int[,] costs = new int[x,y]();
int[] result = HungarianAlgorithm.FindAssignments(costs);

Square Input Array

The algorithm doesn't always produce the most optimal result, when the input contains more columns (y) than rows (x). To overcome this issue, square the dimensions of the input before passing it to the algorithm, as shown below.

int[,] costs = new int[x,y]();
int[,] squared = costs.SquareArray(costs)

Original source-code posted by Alex Regueiro in 2010 (Link)