'Hungarian algorithm in Python

Is there good implementation of Hungarian algorithm in standard python libraries?



Solution 1:[1]

I just tried:


pip install munkres

and it worked. Here you can find a short explanation on how to use it.

I got an error trying to install "hungarian".

Solution 2:[2]

There are multiple Options:

pip install munkres

Documentation here

pip install hungarian

Documentation here

pip install scipy
scipy.optimize.linear_sum_assignment

Documentation here

Solution 3:[3]

Check this munkres out

Solution 4:[4]

Munkres is the Hungarian algorithm in Python.

  1. How to install it:

    pip install munkres
    
  2. How to use it:

    from munkres import Munkres
    mnkrs = Munkres()
    matrix = []
    
    for i, val1 in enumerate(data1):
        for j, val2 in enumerate(data2):        
           if len(matrix) < i+1:
              matrix.append([])
           if len(matrix[i]) < j+1:
              matrix[i].append([])
           matrix[i][j] = val2 - val1 # or whatever you want to compare
    
    if matrix:
        best_idxs = mnkrs.compute(matrix)
        for (k, l) in best_idxs:
           print(data1[k], data2[l])
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 federicober
Solution 2
Solution 3 krico
Solution 4 tsveti_iko