maximum_weight_matching#
- graph_tool.structure.maximum_weight_matching(g, weight, bipartite=False, minimize=False, heuristic=False, edges=False, brute_force=False)[git][source]#
Find a matching of maximum total weight in the graph.
- Parameters:
- g
Graph Graph to be used. Edge directions are ignored.
- weight
EdgePropertyMap Edge weights.
- bipartite
boolorVertexPropertyMap(optional, default:False) If
True, the graph will be assumed to be bipartite, and the algorithm for bipartite graphs will be used. If aVertexPropertyMapis passed, it should correspond to an existing bipartition.- minimize
bool(optional, default:False) If
True, a matching of minimum total weight among those of maximum cardinality will be returned instead (see below).- heuristic
bool(optional, default:False) If
True, an approximate matching will be found by a random greedy heuristic, which runs in linear time.- edges
bool(optional, default:False) If
True, an edge property map will be returned, instead of a vertex property map. If a matched pair of vertices is connected by parallel edges, only one of them is marked, the one with the best weight.- brute_force
bool(optional, default:False) If
True, andheuristicisFalse, all matchings are enumerated, which is feasible only for very small graphs.
- g
- Returns:
- match
VertexPropertyMap Vertex property map with the vertex matched to each vertex (or the largest value of
int64_tfor the unmatched ones). Ifedges == Truea boolean-valued edge property map is returned instead.
- match
See also
maximum_cardinality_matchingMatching with the largest number of edges
approx_minimum_maximal_matchingApproximate minimum maximal matching
Notes
A matching is a set of edges no two of which share an endpoint. With
minimize == False, the matching \(M\) returned maximizes the total weight \(\sum_{e\in M}w_e\) among all matchings. It contains no edges of negative weight, and it is in general not a maximum cardinality matching (seemaximum_cardinality_matching()), since an edge may be heavier than two edges that share its endpoints.With
minimize == True, the matching returned has the smallest total weight among the matchings of maximum cardinality, since otherwise the empty matching would be the minimum for nonnegative weights. It is obtained by maximizing the total weight with the weights \(C - w_e\), where \(C\) is larger than twice the sum of \(|w_e|\), so that every matching with more edges has a larger total. For floating-point weights, matchings whose total weights differ by less than the rounding error of \(C\) may not be distinguished.The weighted blossom algorithm of Edmonds [edmonds-maximum-1965] [galil-efficient-1986] [boost-max-weighted-matching] is used, which takes time \(O(V^3)\). If
bipartiteis given, the Hungarian method [kuhn-hungarian-1955] is used instead. Withbrute_force == True, all the matchings are enumerated, in time \(O(2^E)\). Self-loops are ignored, and of the parallel edges between two vertices only the one with the largest weight (the smallest withminimize == True) is used.If
heuristic == True, the vertices are visited in random order, and every unmatched vertex is matched to an unmatched neighbor along an edge of largest weight (smallest withminimize == True), with ties broken at random [matching-heuristic]. The result is a maximal matching, i.e. no edge can be added to it, which only approximates the optimal one, in time \(O(V + E)\).References
[edmonds-maximum-1965]Jack Edmonds, “Maximum matching and a polyhedron with 0,1-vertices”, Journal of Research of the National Bureau of Standards B 69, 125-130 (1965), DOI: 10.6028/jres.069B.013 [sci-hub, @tor]
[galil-efficient-1986]Zvi Galil, “Efficient algorithms for finding maximum matching in graphs”, ACM Computing Surveys 18, 23-38 (1986), DOI: 10.1145/6462.6502 [sci-hub, @tor]
[kuhn-hungarian-1955]Harold W. Kuhn, “The Hungarian method for the assignment problem”, Naval Research Logistics Quarterly 2, 83-97 (1955), DOI: 10.1002/nav.3800020109 [sci-hub, @tor]
[matching-heuristic]B. Hendrickson and R. Leland. “A Multilevel Algorithm for Partitioning Graphs.” In S. Karin, editor, Proc. Supercomputing ’95, San Diego. ACM Press, New York, 1995, DOI: 10.1145/224170.224228 [sci-hub, @tor]
Examples
The matching of maximum weight can have fewer edges than a maximum cardinality matching, as in the following example, where the weights are the differences between the indices of the endpoints:
>>> g = gt.collection.data["karate"] >>> w = g.new_ep("double", vals=[abs(int(e.source()) - int(e.target())) ... for e in g.edges()]) >>> m = gt.maximum_weight_matching(g, w, edges=True) >>> print(m.a.sum(), (m.a * w.a).sum()) 11 177.0 >>> m = gt.maximum_weight_matching(g, w, minimize=True, edges=True) >>> print(m.a.sum(), (m.a * w.a).sum()) 13 96.0 >>> print(gt.maximum_cardinality_matching(g, edges=True).a.sum()) 13