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 b  
    434434
    435435           Please use this method only if you need to copy but change the
    436436           underlying implementation.  Otherwise simply do ``copy(g)``
    437            instead of idoing ``g.copy()``.
     437           instead of doing ``g.copy()``.
    438438
    439439        EXAMPLES::
    440440       
     
    19411941    def spanning_trees_count(self, root_vertex=None):
    19421942        """
    19431943        Returns the number of spanning trees in a graph. In the case of a
    1944         digraph, couts the number of spanning out-trees rooted in
     1944        digraph, counts the number of spanning out-trees rooted in
    19451945        ``root_vertex``.
    19461946        Default is to set first vertex as root.
    19471947
     
    20562060       
    20572061        .. [AMOZ06] Asahiro, Y. and Miyano, E. and Ono, H. and Zenmyo, K.
    20582062          Graph orientation algorithms to minimize the maximum outdegree
    2059           Proceedings of the 12th Computing: The Australasian Theroy Symposium
     2063          Proceedings of the 12th Computing: The Australasian Theory Symposium
    20602064          Volume 51, page 20
    20612065          Australian Computer Society, Inc. 2006
    20622066        """
     
    38413862
    38423863        return paths
    38433864
    3844 
    3845     def matching(self,value_only=False, algorithm = "Edmonds", use_edge_labels=True, solver=None, verbose=0):
     3865    def matching(self, value_only=False, algorithm="Edmonds", use_edge_labels=True, solver=None, verbose=0):
    38463866        r"""
    38473867        Returns a maximum weighted matching of the graph
    38483868        represented by the list of its edges. For more information, see the
     
    38543874        maximum weight such that no two edges of `S` are incident
    38553875        with each other.
    38563876
    3857         As an optimization problem, it can be expressed as :
     3877        As an optimization problem, it can be expressed as:
    38583878
    38593879        .. math::
     3880
    38603881            \mbox{Maximize : }&\sum_{e\in G.edges()} w_e b_e\\
    38613882            \mbox{Such that : }&\forall v \in G, \sum_{(u,v)\in G.edges()} b_{(u,v)}\leq 1\\
    38623883            &\forall x\in G, b_x\mbox{ is a binary variable}
    3863    
    3864         INPUT:
    3865 
    3866         - ``algorithm`` (string)
     3884
     3885        INPUT:
     3886
     3887        - ``value_only`` -- boolean (default: ``False``). When set to
     3888          ``True``, only the cardinal (or the weight) of the matching is
     3889          returned.
     3890
     3891        - ``algorithm`` -- string (default: ``"Edmonds"``)
    38673892
    38683893          - ``"Edmonds"`` selects Edmonds' algorithm as implemented in NetworkX
     3894
    38693895          - ``"LP"`` uses a Linear Program formulation of the matching problem
    38703896
    3871           By default, Emonds' algorithm is used.
    3872    
    3873         - ``value_only`` (boolean)
    3874 
    3875             - When set to ``True``, only the cardinal
    3876               ( or the weight ) of the the matching
    3877               is returned
    3878 
    3879         - ``use_edge_labels`` (boolean)
    3880 
    3881             - When set to ``True``, computes a weighted matching
    3882               where each edge is weighted by its label. ( if
    3883               an edge has no label, `1` is assumed )
    3884               when set to ``False``, each edge has weight `1`
    3885 
    3886         - ``solver`` -- Specify a Linear Program solver to be used.
    3887           If set to ``None``, the default one is used.
    3888           function of ``MixedIntegerLinearProgram``. See the documentation  of ``MixedIntegerLinearProgram.solve``
    3889           for more informations.
    3890 
     3897        - ``use_edge_labels`` -- boolean (default: ``True``)
     3898
     3899          - When set to ``True``, computes a weighted matching
     3900            where each edge is weighted by its label. (If
     3901            an edge has no label, `1` is assumed.)
     3902
     3903          - When set to ``False``, each edge has weight `1`.
     3904
     3905        - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     3906          solver to be used. If set to ``None``, the default one is used. For
     3907          more information on LP solvers and which default solver is used, see
     3908          the method
     3909          :meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
     3910          of the class
     3911          :class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.
     3912
     3913        - ``verbose`` -- integer (default: ``0``). Sets the level of
     3914          verbosity. Set to 0 by default, which means quiet.
    38913915          Only useful when ``algorithm == "LP"``.
    38923916
    3893         - ``verbose`` (integer) -- sets the level of verbosity. Set to 0
    3894           by default (quiet).
    3895 
    3896           Only useful when ``algorithm == "LP"``.
    3897 
    38983917        ALGORITHM:
    38993918
    3900         The problem is solved using Edmond's algorithm inplemented in NetworkX,
    3901         or using Linear Programming depending on the value of ``algorithm``.
    3902    
    3903         EXAMPLE:
     3919        The problem is solved using Edmond's algorithm implemented in
     3920        NetworkX, or using Linear Programming depending on the value of
     3921        ``algorithm``.
     3922
     3923        EXAMPLES:
    39043924
    39053925        Maximum matching in a Pappus Graph::
    3906        
    3907            sage: g=graphs.PappusGraph()
     3926
     3927           sage: g = graphs.PappusGraph()
    39083928           sage: g.matching(value_only=True)
    39093929           9.0
    39103930
    3911         Same test with the Linear Program formulation ::
    3912 
    3913            sage: g=graphs.PappusGraph()
    3914            sage: g.matching(algorithm = "LP", value_only=True) # optional - requires GLPK CBC or CPLEX
     3931        Same test with the Linear Program formulation::
     3932
     3933           sage: g = graphs.PappusGraph()
     3934           sage: g.matching(algorithm="LP", value_only=True)
    39153935           9.0
    39163936
    3917         If ``algorithm`` is set to anything different from ``"Edmonds"`` or ``"LP"``, an
    3918         exception is raised ::
    3919 
     3937        TESTS:
     3938
     3939        If ``algorithm`` is set to anything different from ``"Edmonds"`` or
     3940        ``"LP"``, an exception is raised::
     3941
     3942           sage: g = graphs.PappusGraph()
    39203943           sage: g.matching(algorithm="somethingdifferent")
     3944           Traceback (most recent call last):
    39213945           ...
    3922            ValueError: Algorithm must be set to either "Edmonds" or "LP"
    3923         """
    3924 
     3946           ValueError: Algorithm must be set to either "Edmonds" or "LP".
     3947        """
    39253948        from sage.rings.real_mpfr import RR
    3926         weight=lambda x: x if x in RR else 1
     3949        weight = lambda x: x if x in RR else 1
    39273950
    39283951        if algorithm == "Edmonds":
    3929 
    39303952            import networkx
    3931 
    39323953            if use_edge_labels:
    39333954                g = networkx.Graph()
    3934                 for u,v,l in self.edges():
    3935                     g.add_edge(u,v, attr_dict={"weight":weight(l)})
     3955                for u, v, l in self.edges():
     3956                    g.add_edge(u, v, attr_dict={"weight": weight(l)})
    39363957            else:
    39373958                g = self.networkx_graph(copy=False)
    3938 
    3939             d =  networkx.max_weight_matching(g)
    3940 
     3959            d = networkx.max_weight_matching(g)
    39413960            if value_only:
    3942                 return sum([ weight(self.edge_label(u,v)) for u,v in d.iteritems()])*0.5
    3943 
    3944             else:
    3945                 return [(u,v,self.edge_label(u,v)) for u,v in d.iteritems() if u<v]
    3946            
     3961                return sum([weight(self.edge_label(u, v))
     3962                            for u, v in d.iteritems()]) * 0.5
     3963            else:
     3964                return [(u, v, self.edge_label(u, v))
     3965                        for u, v in d.iteritems() if u < v]
     3966
    39473967        elif algorithm == "LP":
    3948 
    39493968            from sage.numerical.mip import MixedIntegerLinearProgram
    3950             g=self
    3951    
     3969            g = self
    39523970            # returns the weight of an edge considering it may not be
    39533971            # weighted ...
    3954    
    3955             p=MixedIntegerLinearProgram(maximization=True)
    3956    
    3957             b=p.new_variable(dim=2)
    3958             p.set_objective(sum([weight(w)*b[min(u,v)][max(u,v)] for (u,v,w) in g.edges()]))
    3959    
    3960    
    3961             # for any vertex v, there is at most one edge incident to v in the maximum matching
    3962             for v in g.vertices():
    3963                 p.add_constraint(sum([b[min(u,v)][max(u,v)] for u in g.neighbors(v)]),max=1)
    3964    
     3972            p = MixedIntegerLinearProgram(maximization=True)
     3973            b = p.new_variable(dim=2)
     3974            p.set_objective(
     3975                sum([weight(w) * b[min(u, v)][max(u, v)]
     3976                     for u, v, w in g.edges()]))
     3977            # for any vertex v, there is at most one edge incident to v in
     3978            # the maximum matching
     3979            for v in g.vertex_iterator():
     3980                p.add_constraint(
     3981                    sum([b[min(u, v)][max(u, v)]
     3982                         for u in g.neighbors(v)]), max=1)
    39653983            p.set_binary(b)
    3966        
    39673984            if value_only:
    3968                 return p.solve(objective_only=True, solver = solver, log = verbose)
    3969             else:
    3970                 p.solve(solver = solver, log = verbose)
    3971                 b=p.get_values(b)
    3972                 return [(u,v,w) for (u,v,w) in g.edges() if b[min(u,v)][max(u,v)] == 1]
    3973 
    3974         else:
    3975             raise ValueError("Algorithm must be set to either \"Edmonds\" or \"LP\"")
     3985                return p.solve(objective_only=True, solver=solver, log=verbose)
     3986            else:
     3987                p.solve(solver=solver, log=verbose)
     3988                b = p.get_values(b)
     3989                return [(u, v, w) for u, v, w in g.edges()
     3990                        if b[min(u, v)][max(u, v)] == 1]
     3991
     3992        else:
     3993            raise ValueError(
     3994                'Algorithm must be set to either "Edmonds" or "LP".')
    39763995
    39773996    def dominating_set(self, independent=False, value_only=False, solver=None, verbose=0):
    39783997        r"""
     
    1011010137
    1011110138    def _keys_for_vertices(self):
    1011210139        """
    10113         Returns a function mapping each vertex to a unique and hopefuly readable string
     10140        Returns a function mapping each vertex to a unique and hopefully readable string
    1011410141        """
    1011510142        from sage.graphs.dot2tex_utils import key, key_with_hash
    1011610143        if len(set(key(v) for v in self)) < len(self.vertices()):
     
    1013610163        INPUT:
    1013710164
    1013810165        - ``labels`` - "string" or "latex" (default: "string"). If labels is
    10139           string latex command are not interepreted. This option stands for both
     10166          string latex command are not interpreted. This option stands for both
    1014010167          vertex labels and edge labels.
    1014110168
    1014210169        - ``vertex_labels`` - boolean (default: True) whether to add the labels