# Ticket #8166: trac_8166.patch

File trac_8166.patch, 5.8 KB (added by ncohen, 11 years ago)
• ## sage/graphs/generic_graph.py

# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1273950179 14400
# Node ID 65fc9f7cd99e2b92e69727fd5bf63ba6fec4b984
# Parent  45fc4178e0672a4996a4e6b9083b05800d51db04
trac #8166 - max matching

diff -r 45fc4178e067 -r 65fc9f7cd99e sage/graphs/generic_graph.py
 a return paths def matching(self,value_only=False, use_edge_labels=True, solver=None, verbose=0): def matching(self,value_only=False, algorithm = "Edmonds", use_edge_labels=True, solver=None, verbose=0): r""" Returns a maximum weighted matching of the graph represented by the list of its edges. For more information, see the &\forall x\in G, b_x\mbox{ is a binary variable} INPUT: - algorithm (string) - "Edmonds" selects Edmonds' algorithm as implemented in NetworkX - "LP" uses a Linear Program formulation of the matching problem By default, Emonds' algorithm is used. - value_only (boolean) function of MixedIntegerLinearProgram. See the documentation  of MixedIntegerLinearProgram.solve for more informations. Only useful when algorithm == "LP". - verbose (integer) -- sets the level of verbosity. Set to 0 by default (quiet). EXAMPLE:: Only useful when algorithm == "LP". ALGORITHM: The problem is solved using Edmond's algorithm inplemented in NetworkX, or using Linear Programming depending on the value of algorithm. EXAMPLE: Maximum matching in a Pappus Graph:: sage: g=graphs.PappusGraph() sage: g.matching(value_only=True) # optional - requires Glpk or COIN-OR/CBC sage: g.matching(value_only=True) 9.0 """ from sage.numerical.mip import MixedIntegerLinearProgram g=self # returns the weight of an edge considering it may not be # weighted ... Same test with the Linear Program formulation :: sage: g=graphs.PappusGraph() sage: g.matching(algorithm = "LP", value_only=True) # optional - requires GLPK CBC or CPLEX 9.0 If algorithm is set to anything different from "Edmonds" or "LP", an exception is raised :: sage: g.matching(algorithm="somethingdifferent") ... ValueError: Algorithm must be set to either "Edmonds" or "LP" """ from sage.rings.real_mpfr import RR weight=lambda x: x if x in RR else 1 p=MixedIntegerLinearProgram(maximization=True) b=p.new_variable(dim=2) p.set_objective(sum([weight(w)*b[min(u,v)][max(u,v)] for (u,v,w) in g.edges()])) # for any vertex v, there is at most one edge incident to v in the maximum matching for v in g.vertices(): p.add_constraint(sum([b[min(u,v)][max(u,v)] for u in g.neighbors(v)]),max=1) p.set_binary(b) if value_only: return p.solve(objective_only=True, solver = solver, log = verbose) else: p.solve(solver = solver, log = verbose) b=p.get_values(b) return [(u,v,w) for (u,v,w) in g.edges() if b[min(u,v)][max(u,v)] == 1] if algorithm == "Edmonds": import networkx if use_edge_labels: g = networkx.Graph() for u,v,l in self.edges(): g.add_edge(u,v, attr_dict={"weight":weight(l)}) else: g = self.networkx_graph(copy=False) d =  networkx.max_weight_matching(g) if value_only: return sum([ weight(self.edge_label(u,v)) for u,v in d.iteritems()])*0.5 else: return [(u,v,self.edge_label(u,v)) for u,v in d.iteritems() if u
• ## sage/graphs/graph.py

diff -r 45fc4178e067 -r 65fc9f7cd99e sage/graphs/graph.py
 a return classes_b def max_weight_matching(self): import networkx return networkx.max_weight_matching(self.networkx_graph(copy=False)) def compare_edges(x, y): """ Compare edge x to edge y, return -1 if x y, 1 if x y, else 0.