Ticket #9058: trac_9058.patch

File trac_9058.patch, 2.8 KB (added by ncohen, 10 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1280798449 -28800
    # Node ID ef4ea6b9d33a657e96957eee6b1e16b6dd749f9c
    # Parent  0b12ec9add58ab64d979b10238f140e1ee19c287
    trac 9058 -- Computing the k-core of a graph
    
    diff -r 0b12ec9add58 -r ef4ea6b9d33a sage/graphs/generic_graph.py
    a b  
    86188618   
    86198619    ### Cores
    86208620   
    8621     def cores(self, with_labels=False):
     8621    def cores(self, k = None, with_labels=False):
    86228622        """
    86238623        Returns the core number for each vertex in an ordered list.
    86248624       
     
    86478647
    86488648        INPUT:
    86498649       
     8650        - ``k`` (integer) --
     8651
     8652            * If ``k = None`` (default), returns the core number for
     8653              each vertex, according to the value of ``with_labels``.
     8654
     8655            * Otherwise, returns a pair ``(ordering, core)``, where
     8656              ``core`` is the list of vertices in the `k`-core of
     8657              ``self``, and ``ordering`` is an elimination order for
     8658              the others vertices such that each vertex is of degree
     8659              strictly less than `k` when it is to be eliminated from
     8660              the graph.
    86508661       
    86518662        -  ``with_labels`` - default False returns list as
    86528663           described above. True returns dict keyed by vertex labels.
     
    86818692            {0: 3, 1: 3, 2: 3, 3: 3, 4: 2, 5: 2, 6: 3, 7: 1, 8: 3, 9: 3, 10: 3, 11: 3, 12: 3, 13: 3, 14: 2, 15: 3, 16: 3, 17: 3, 18: 3, 19: 3}
    86828693            sage: [v for v,c in cores.items() if c>=2] # the vertices in the 2-core
    86838694            [0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
     8695
     8696        Checking the 2-core of a random lobster is indeed the empty set::
     8697
     8698            sage: g = graphs.RandomLobster(20,.5,.5)
     8699            sage: ordering, core = g.cores(2)
     8700            sage: len(core) == 0
     8701            True
    86848702        """
    86858703        # compute the degrees of each vertex
    86868704        degrees=self.degree(labels=True)
     
    87018719        nbrs=dict((v,set(self.neighbors(v))) for v in self)
    87028720        # form vertex core building up from smallest
    87038721        for v in verts:
     8722           
     8723            # If all the vertices have a degree larger than k, we can
     8724            # return our answer if k != None
     8725            if k is not None and core[v] >= k:
     8726                return verts[:vert_pos[v]], verts[vert_pos[v]:]
     8727
    87048728            for u in nbrs[v]:
    87058729                if core[u] > core[v]:
    87068730                    nbrs[u].remove(v)
     
    87188742                    bin_boundaries[core[u]]+=1
    87198743                    core[u] -= 1
    87208744
     8745        if k is not None:
     8746            return verts, []
     8747
    87218748        if with_labels:
    87228749            return core
    87238750        else: