|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcommon.BipartiteMatcher
public class BipartiteMatcher
An engine for finding the maximum-weight matching in a complete bipartite graph. Suppose we have two sets S and T, both of size n. For each i in S and j in T, we have a weight wij. A perfect matching X is a subset of S x T such that each i in S occurs in exactly one element of X, and each j in T occurs in exactly one element of X. Thus, X can be thought of as a one-to-one function from S to T. The weight of X is the sum, over (i, j) in X, of wij. A BipartiteMatcher takes the number n and the weights wij, and finds a perfect matching of maximum weight. It uses the Hungarian algorithm of Kuhn (1955), as improved and presented by E. L. Lawler in his book Combinatorial Optimization: Networks and Matroids (Holt, Rinehart and Winston, 1976, p. 205-206). The running time is O(n3). The weights can be any finite real numbers; Lawler's algorithm assumes positive weights, so if necessary we add a constant c to all the weights before running the algorithm. This increases the weight of every perfect matching by nc, which doesn't change which perfect matchings have maximum weight. If a weight is set to Double.NEGATIVE_INFINITY, then the algorithm will behave as if that edge were not in the graph. If all the edges incident on a given node have weight Double.NEGATIVE_INFINITY, then the final result will not be a perfect matching, and an exception will be thrown.
Constructor Summary | |
---|---|
BipartiteMatcher()
Creates a BipartiteMatcher without specifying the graph size. |
|
BipartiteMatcher(int n)
Creates a BipartiteMatcher and prepares it to run on an n x n graph. |
Method Summary | |
---|---|
int[] |
getMatching()
Returns a maximum-weight perfect matching relative to the weights specified with setWeight. |
static void |
main(java.lang.String[] args)
|
void |
reset(int n)
Resets the BipartiteMatcher to run on an n x n graph. |
void |
setWeight(int i,
int j,
double w)
Sets the weight wij to the given value w. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BipartiteMatcher()
public BipartiteMatcher(int n)
Method Detail |
---|
public void reset(int n)
public void setWeight(int i, int j, double w)
java.lang.IllegalArgumentException
- if i or j is outside the range [0, n).public int[] getMatching()
public static void main(java.lang.String[] args)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |