Hi,
hope it’s okay that I bring this very old post back up, but my issue seems like a follow-up.
I found something that is a little unclear between the docs and the behavior of max_cardinality_matching when edge weights are passed. The Notes say:
If the parameter weight is provided, a matching with maximum cardinality and maximum weight is returned.
These two requirements can sometimes not be fulfilled at the same time, and I observed the following:
Setup
MWE
import graph_tool.all as gt
g = gt.Graph(directed=False)
g.add_vertex(3)
weights = g.new_ep("double")
for u, v, w in [(0, 1, 1), (1, 2, 100), (2, 3, 1)]:
e = g.add_edge(u, v)
weights[e] = w
# with weights:
m1 = gt.max_cardinality_matching(g, weight=weights, minimize=False, edges=True)
matching1 = [(int(e.source()), int(e.target()), weights[e]) for e in g.edges() if m1[e]]
print("MWMCM:", matching1)
print("Cardinality:", len(matching1))
print("Total weight:", sum(w for _, _, w in matching1))
# without weights:
m2 = gt.max_cardinality_matching(g, minimize=False, edges=True)
matching2 = [(int(e.source()), int(e.target()), weights[e]) for e in g.edges() if m2[e]]
print("MCM:", matching2)
print("Cardinality:", len(matching2))
print("Total weight:", sum(w for _, _, w in matching2))
Output:
MWMCM: [(1, 2, 100.0)]
Cardinality: 1
Total weight: 100.0
MCM: [(0, 1, 1.0), (2, 3, 1.0)]
Cardinality: 2
Total weight: 2.0
With weights, the result has cardinality 1. But a matching of cardinality 2 exists, as the unweighted call shows. Going by the docs, I’d expect [(0, 1), (2, 3)] with total weight 2. So it seems the function finds a maximum-weight matching, not a maximum-cardinality one. That’s perfectly reasonable behavior, but I did not really expect it based on the docs.
Two small things I noticed in the same docstring, in case they’re useful:
-
The minimize parameter is documented as default: True, but the signature has minimize=False.
-
The Boost reference links (http://www.boost.org/libs/graph/doc/maximum_matching.html and .../maximum_weighted_matching.html) now return 404.
Thanks for your work on this!
Best,
Anna