maximum_cardinality_matching#
- graph_tool.structure.maximum_cardinality_matching(g, bipartite=False, init_match='extra_greedy', heuristic=False, edges=False)[git][source]#
Find a maximum cardinality matching in the graph.
- Parameters:
- g
Graph Graph to be used. Edge directions are ignored.
- 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. AValueErroris raised if the graph is not bipartite, or if the bipartition is not valid.- init_match
string(optional, default:"extra_greedy") Initial matching strategy. Can be one of: “empty”, “greedy”, or “extra_greedy”. Ignored if
heuristic == Trueor ifbipartiteis given.- heuristic
bool(optional, default:False) If
True, a maximal matching is found by a random greedy heuristic, which runs in linear time, instead of a maximum one.- 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.
- 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_weight_matchingMatching of maximum total weight
approx_minimum_maximal_matchingApproximate minimum maximal matching
minimum_edge_coverMinimum edge cover
bipartite_vertex_coverMinimum vertex cover of a bipartite graph
k_factorSpanning subgraph in which every vertex has degree k
Notes
A matching is a set of edges no two of which share an endpoint, and a maximum cardinality matching is one with the largest number of edges. It is found with Edmonds’ blossom algorithm [edmonds-paths-1965] [boost-max-matching], starting from the matching given by
init_match, in time \(O(EV\alpha(E,V))\), where \(\alpha(E,V)\) is the inverse Ackermann function, which is at most 4 for any feasible input. Ifbipartiteis given, the algorithm of Hopcroft and Karp [hopcroft-n52-1973] is used instead, which augments the matching in phases along a maximal set of vertex-disjoint shortest augmenting paths, found by a breadth-first search followed by depth-first searches, and needs \(O(\sqrt{V})\) phases of time \(O(V + E)\), i.e. time \(O(E\sqrt{V})\) in total. Self-loops are ignored in both cases, since they cannot be part of a matching.If
heuristic == True, the vertices are visited in random order, and every unmatched vertex is matched to a randomly chosen unmatched neighbor, if there is one [matching-heuristic]. The result is a maximal matching, i.e. no edge can be added to it, which approximates the maximum one within a factor two, since every edge of a maximum matching shares an endpoint with an edge of any maximal matching. This takes time \(O(V + E)\), andbipartiteis then ignored.References
[edmonds-paths-1965]Jack Edmonds, “Paths, trees, and flowers”, Canadian Journal of Mathematics 17, 449-467 (1965), DOI: 10.4153/CJM-1965-045-4 [sci-hub, @tor]
[hopcroft-n52-1973]John E. Hopcroft and Richard M. Karp, “An \(n^{5/2}\) algorithm for maximum matchings in bipartite graphs”, SIAM Journal on Computing 2, 225-231 (1973), DOI: 10.1137/0202019 [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
>>> g = gt.GraphView(gt.price_network(300), directed=False) >>> w = gt.maximum_cardinality_matching(g, edges=True) >>> gt.graph_draw(g, edge_color=w, edge_pen_width=w.t(lambda x: 2*x + 1), ... vertex_fill_color="grey", ecmap=matplotlib.cm.tab10, ... output="max_card_match.pdf") <...>
Edges belonging to the matching are drawn in orange.#
For a bipartite graph, such as a square lattice, the algorithm for bipartite graphs can be used, which gives a matching of the same size:
>>> g = gt.lattice([30, 30]) >>> m = gt.maximum_cardinality_matching(g, bipartite=True, edges=True) >>> print(m.a.sum(), gt.maximum_cardinality_matching(g, edges=True).a.sum()) 450 450