HungarianAlgorithm

An implementation of the Hungarian algorithm for solving the assignment problem. An instance of the assignment problem consists of a number of workers along with a number of jobs and a cost matrix which gives the cost of assigning the i'th worker to the j'th job at position (i, j). The goal is to find an assignment of workers to jobs so that no job is assigned more than one worker and so that no worker is assigned to more than one job in such a manner so as to minimize the total cost of completing the jobs. <p>

An assignment for a cost matrix that has more workers than jobs will necessarily include unassigned workers, indicated by an assignment value of -1; in no other circumstance will there be unassigned workers. Similarly, an assignment for a cost matrix that has more jobs than workers will necessarily include unassigned jobs; in no other circumstance will there be unassigned jobs. For completeness, an assignment for a square cost matrix will give exactly one unique worker to each job.

This version of the Hungarian algorithm runs in time O(n^3), where n is the maximum among the number of workers and the number of jobs.

@author Kevin L. Stern

Ported to D language, Oct 8 2012 V.1.2, by leonardo maffi.

Members

Functions

computeInitialFeasibleSolution
void computeInitialFeasibleSolution()

Compute an initial feasible solution by assigning zero labels to the workers and by assigning to each job a label equal to the minimum cost among its incident edges.

executePhase
void executePhase()

Execute a single phase of the algorithm. A phase of the Hungarian algorithm consists of building a set of committed workers and a set of committed jobs from a root unmatched worker by following alternating unmatched/matched zero-slack edges. If an unmatched job is encountered, then an augmenting path has been found and the matching is grown. If the connected zero-slack edges have been exhausted, the labels of committed workers are increased by the minimum slack among committed workers and non-committed jobs to create more zero-slack edges (the labels of committed jobs are simultaneously decreased by the same amount in order to maintain a feasible labeling).

fetchUnmatchedWorker
int fetchUnmatchedWorker()

@return the first unmatched worker or {@link #dim} if none.

greedyMatch
void greedyMatch()

Find a valid matching by greedily selecting among zero-cost matchings. This is a heuristic to jump-start the augmentation algorithm.

initializePhase
void initializePhase(int w)

Initialize the next phase of the algorithm by clearing the committed workers and jobs sets and by initializing the slack arrays to the values corresponding to the specified root worker.

match
void match(int w, int j)

Helper method to record a matching between worker w and job j.

reduce
void reduce()

Reduce the cost matrix by subtracting the smallest element of each row from all elements of the row as well as the smallest element of each column from all elements of the column. Note that an optimal assignment for a reduced cost matrix is optimal for the original cost matrix.

updateLabeling
void updateLabeling(T slack)

Update labels with the specified slack by adding the slack value for committed workers and by subtracting the slack value for committed jobs. In addition, update the minimum slack values appropriately.

Static functions

copyOf
T[] copyOf(T[] input, size_t size)
Undocumented in source. Be warned that the author may not have intended to support it.
opCall
int[] opCall(T[][] costMatrix_)

Construct an instance of the algorithm and Execute the algorithm.

Variables

infinity
enum T infinity;
Undocumented in source.
infinity
enum T infinity;
Undocumented in source.

Meta