aryagraph.algorithms.matching.max_weight_matching#

aryagraph.algorithms.matching.max_weight_matching(g: Graph, weight: str | Callable[[Hashable, Hashable, dict], float] | None = 'weight', maxcardinality: bool = False) → set[tuple[Hashable, Hashable]][source]#

Maximum-weight matching of a general graph (Edmonds’ blossom algorithm, exact).

Parameters:
  • weight – Edge-attribute name (missing ⇒ 1), None (all 1, giving a maximum cardinality matching), or f(u, v, attrs) (None hides the edge).

  • maxcardinality – If True, the heaviest among the matchings of maximum cardinality.

Notes

Primal–dual method with blossom shrinking (Galil 1986, after van Rantwijk’s formulation), O(n³). With integer weights all arithmetic is integral and the optimum is exact; with floats it is exact up to round-off. Edges of weight ≤ 0 are never needed unless maxcardinality.

Examples

>>> g = Graph([(1, 2, 6), (1, 3, 2), (2, 3, 1), (2, 4, 7), (3, 5, 9), (4, 5, 3)])
>>> sorted(max_weight_matching(g))
[(2, 4), (3, 5)]