Ticket #7600: trac_7600.patch

File trac_7600.patch, 4.1 KB (added by ncohen, 10 years ago)
  • sage/graphs/graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1249573009 25200
    # Node ID c9d86f25e6d5c00bf4875dc7afda761ea83cb923
    # Parent  cb0894c1710b4fa00c935a87adfce9153263533b
    Graph.vertex_cover
    
    diff -r cb0894c1710b -r c9d86f25e6d5 sage/graphs/graph.py
    a b  
    32543254            v = pv
    32553255        return B, C
    32563256
     3257    def vertex_cover(self,algorithm="Cliquer",value_only=False,log=0):
     3258        r"""
     3259        Returns a minimum vertex cover of the graph
     3260        ( cf. http://en.wikipedia.org/wiki/Vertex_cover )
     3261        represented by the list of its vertices.
     3262
     3263        A minimum vertex cover of a graph is a set `S` of
     3264        its vertices such that each edge is incident to at least
     3265        one element of `S`, and such that `S` is of minimum
     3266        cardinality.
     3267
     3268        A vertex cover is also defined as the complement of an
     3269        independent set.
     3270
     3271        As an optimization problem, it can be expressed as :
     3272
     3273        .. MATH::
     3274            \mbox{Minimize : }&\sum_{v\in G} b_v\\
     3275            \mbox{Such that : }&\forall (u,v) \in G.edges(), b_u+b_v\geq 1\\
     3276            &\forall x\in G, b_x\mbox{ is a binary variable}
     3277   
     3278        INPUT:
     3279       
     3280        - ``algorithm`` -- Select an algorithm
     3281       
     3282            - ``"Cliquer"`` (default) will return a minimum vertex cover
     3283              using the algorithm Cliquer.
     3284
     3285            - ``"MILP"`` will return a minimum vertex cover through a Mixed
     3286              Integer Linear Program ( requires packages GLPK or CBC )
     3287
     3288        - ``value_only`` --
     3289            - If ``value_only = True``, only the cardinality of a
     3290              minimum vertex cover is returned.
     3291            - If ``value_only = False`` ( default ), a minimum vertex cover
     3292              is returned as the list of its vertices
     3293   
     3294        - ``log`` ( integer ) --
     3295          As vertex cover is an `NP`-complete problem, its solving may take some time
     3296          depending on the graph. Use ``log`` to define the level of verbosity you want
     3297          from the linear program solver.
     3298
     3299          By default ``log=0``, meaning that there will be no message printed by the solver.
     3300
     3301          Only useful if ``algorithm="MILP"``.
     3302   
     3303        EXAMPLES:
     3304
     3305        On a ``PappusGraph`` ::
     3306       
     3307           sage: g=graphs.PappusGraph()
     3308           sage: g.vertex_cover(value_only=True)
     3309           9
     3310
     3311        Obviously, the two methods return the same results ::
     3312
     3313           sage: g=graphs.RandomGNP(10,.5)
     3314           sage: vc1 = g.vertex_cover(algorithm="MILP")     # optional requires GLPK or CBC
     3315           sage: vc2 = g.vertex_cover(algorithm="Cliquer")  # optional requires GLPK or CBC
     3316           sage: len(vc1) == len(vc2) # optional requires GLPK or CBC
     3317           True
     3318        """
     3319
     3320
     3321        if algorithm=="Cliquer":
     3322            independent = self.independent_set()
     3323           
     3324            if value_only:
     3325                return self.order()-len(independent)
     3326            else:
     3327                return set(self.vertices()).difference(set(independent))
     3328
     3329        elif algorithm=="MILP":
     3330
     3331            from sage.numerical.mip import MixedIntegerLinearProgram
     3332            g=self
     3333            p=MixedIntegerLinearProgram(maximization=False)
     3334            b=p.new_variable()
     3335   
     3336            # minimizes the number of vertices in the set
     3337            p.set_objective(sum([b[v] for v in g.vertices()]))
     3338   
     3339            # an edge contains at least one vertex of the minimum vertex cover
     3340            [p.add_constraint(b[u]+b[v],min=1) for (u,v) in g.edges(labels=None)]
     3341           
     3342            p.set_binary(b)
     3343   
     3344            if value_only:
     3345                return p.solve(objective_only=True,log=log)
     3346            else:
     3347                p.solve()
     3348                b=p.get_values(b)
     3349                return set([v for v in g.vertices() if b[v]==1])
     3350        else:
     3351            raise ValueError("Only two algorithms are available : Cliquer and MILP.")
     3352   
    32573353    ### Vertex handlers
    32583354
    32593355    def add_vertex(self, name=None):