# Ticket #8166: reviewer.diff

File reviewer.diff, 10.1 KB (added by mvngu, 11 years ago)

diff patch showing changes proposed by reviewer

• ## sage/graphs/generic_graph.py

diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
 a Please use this method only if you need to copy but change the underlying implementation.  Otherwise simply do copy(g) instead of idoing g.copy(). instead of doing g.copy(). EXAMPLES:: def spanning_trees_count(self, root_vertex=None): """ Returns the number of spanning trees in a graph. In the case of a digraph, couts the number of spanning out-trees rooted in digraph, counts the number of spanning out-trees rooted in root_vertex. Default is to set first vertex as root. .. [AMOZ06] Asahiro, Y. and Miyano, E. and Ono, H. and Zenmyo, K. Graph orientation algorithms to minimize the maximum outdegree Proceedings of the 12th Computing: The Australasian Theroy Symposium Proceedings of the 12th Computing: The Australasian Theory Symposium Volume 51, page 20 Australian Computer Society, Inc. 2006 """ return paths def matching(self,value_only=False, algorithm = "Edmonds", 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 maximum weight such that no two edges of S are incident with each other. As an optimization problem, it can be expressed as : As an optimization problem, it can be expressed as: .. math:: \mbox{Maximize : }&\sum_{e\in G.edges()} w_e b_e\\ \mbox{Such that : }&\forall v \in G, \sum_{(u,v)\in G.edges()} b_{(u,v)}\leq 1\\ &\forall x\in G, b_x\mbox{ is a binary variable} INPUT: - algorithm (string) INPUT: - value_only -- boolean (default: False). When set to True, only the cardinal (or the weight) of the matching is returned. - algorithm -- string (default: "Edmonds") - "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) - When set to True, only the cardinal ( or the weight ) of the the matching is returned - use_edge_labels (boolean) - When set to True, computes a weighted matching where each edge is weighted by its label. ( if an edge has no label, 1 is assumed ) when set to False, each edge has weight 1 - solver -- Specify a Linear Program solver to be used. If set to None, the default one is used. function of MixedIntegerLinearProgram. See the documentation  of MixedIntegerLinearProgram.solve for more informations. - use_edge_labels -- boolean (default: True) - When set to True, computes a weighted matching where each edge is weighted by its label. (If an edge has no label, 1 is assumed.) - When set to False, each edge has weight 1. - solver -- (default: None) Specify a Linear Program (LP) solver to be used. If set to None, the default one is used. For more information on LP solvers and which default solver is used, see the method :meth:solve  of the class :class:MixedIntegerLinearProgram . - verbose -- integer (default: 0). Sets the level of verbosity. Set to 0 by default, which means quiet. Only useful when algorithm == "LP". - verbose (integer) -- sets the level of verbosity. Set to 0 by default (quiet). 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: The problem is solved using Edmond's algorithm implemented in NetworkX, or using Linear Programming depending on the value of algorithm. EXAMPLES: Maximum matching in a Pappus Graph:: sage: g=graphs.PappusGraph() sage: g = graphs.PappusGraph() sage: g.matching(value_only=True) 9.0 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 Same test with the Linear Program formulation:: sage: g = graphs.PappusGraph() sage: g.matching(algorithm="LP", value_only=True) 9.0 If algorithm is set to anything different from "Edmonds" or "LP", an exception is raised :: TESTS: If algorithm is set to anything different from "Edmonds" or "LP", an exception is raised:: sage: g = graphs.PappusGraph() sage: g.matching(algorithm="somethingdifferent") Traceback (most recent call last): ... ValueError: Algorithm must be set to either "Edmonds" or "LP" """ 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 weight = lambda x: x if x in RR else 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)}) 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) 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