public class HungarianAlgorithm extends Object
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.
Constructor and Description |
---|
HungarianAlgorithm(double[][] costMatrix)
Construct an instance of the algorithm.
|
Modifier and Type | Method and Description |
---|---|
protected 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.
|
int[] |
execute()
Execute the algorithm.
|
protected void |
executePhase()
Execute a single phase of the algorithm.
|
protected int |
fetchUnmatchedWorker() |
protected void |
greedyMatch()
Find a valid matching by greedily selecting among zero-cost matchings.
|
protected 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.
|
protected void |
match(int w,
int j)
Helper method to record a matching between worker
w and job j . |
protected 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.
|
protected void |
updateLabeling(double slack)
Update labels with the specified slack by adding the slack value for committed workers and by
subtracting the slack value for committed jobs.
|
public HungarianAlgorithm(double[][] costMatrix)
costMatrix
- the cost matrix, where matrix[i][j] holds the cost of assigning worker i to
job j, for all i, j. The cost matrix must not be irregular in the sense that all rows
must be the same length.protected void computeInitialFeasibleSolution()
public int[] execute()
protected void executePhase()
The runtime of a single phase of the algorithm is O(n^2), where n is the dimension of the internal square cost matrix, since each edge is visited at most once and since increasing the labeling is accomplished in time O(n) by maintaining the minimum slack values among non-committed jobs. When a phase completes, the matching will have increased in size.
protected int fetchUnmatchedWorker()
protected void greedyMatch()
protected void initializePhase(int w)
w
- the worker at which to root the next phase.protected void match(int w, int j)
w
and job j
.w
- the workerj
- the jobprotected void reduce()
protected void updateLabeling(double slack)
slack
- the slack.Copyright © 2010–2023 Owen Feehan, ETH Zurich, University of Zurich, Hoffmann-La Roche. All rights reserved.