edu.princeton.cs.algs4
Class AssignmentProblem
- java.lang.Object
-
- edu.princeton.cs.algs4.AssignmentProblem
-
public class AssignmentProblem extends java.lang.ObjectTheAssignmentProblemclass represents a data type for computing an optimal solution to an n-by-n assignment problem. The assignment problem is to find a minimum weight matching in an edge-weighted complete bipartite graph.The data type supplies methods for determining the optimal solution and the corresponding dual solution.
This implementation uses the successive shortest paths algorithm. The order of growth of the running time in the worst case is O(n^3 log n) to solve an n-by-n instance.
For additional documentation, see Section 6.5 Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
Constructor Summary
Constructors Constructor and Description AssignmentProblem(double[][] weight)Determines an optimal solution to the assignment problem.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description doubledualCol(int j)Returns the dual optimal value for the specified column.doubledualRow(int i)Returns the dual optimal value for the specified row.static voidmain(java.lang.String[] args)Unit tests theAssignmentProblemdata type.intsol(int i)Returns the column associated with the specified row in the optimal solution.doubleweight()Returns the total weight of the optimal solution
-
-
-
Constructor Detail
-
AssignmentProblem
public AssignmentProblem(double[][] weight)
Determines an optimal solution to the assignment problem.- Parameters:
weight- the n-by-n matrix of weights- Throws:
java.lang.IllegalArgumentException- unless all weights are nonnegativejava.lang.IllegalArgumentException- ifweightisnull
-
-
Method Detail
-
dualRow
public double dualRow(int i)
Returns the dual optimal value for the specified row.- Parameters:
i- the row index- Returns:
- the dual optimal value for row
i - Throws:
java.lang.IllegalArgumentException- unless0 <= i < n
-
dualCol
public double dualCol(int j)
Returns the dual optimal value for the specified column.- Parameters:
j- the column index- Returns:
- the dual optimal value for column
j - Throws:
java.lang.IllegalArgumentException- unless0 <= j < n
-
sol
public int sol(int i)
Returns the column associated with the specified row in the optimal solution.- Parameters:
i- the row index- Returns:
- the column matched to row
iin the optimal solution - Throws:
java.lang.IllegalArgumentException- unless0 <= i < n
-
weight
public double weight()
Returns the total weight of the optimal solution- Returns:
- the total weight of the optimal solution
-
main
public static void main(java.lang.String[] args)
Unit tests theAssignmentProblemdata type. Takes a command-line argument n; creates a random n-by-n matrix; solves the n-by-n assignment problem; and prints the optimal solution.- Parameters:
args- the command-line arguments
-
-
DataMelt 3.0 © DataMelt by jWork.ORG