Ticket #9676: trac_9676-cosmetics.patch

File trac_9676-cosmetics.patch, 2.7 KB (added by ncohen, 11 years ago)

Cosmetics on top of Edward's patch

  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1282531258 -28800
    # Node ID 1e8732a7e9ed4e94c47e3f5ae995a70f08d8afc6
    # Parent  23f8a43c7f411fb03e9c384f480ea7bea94b873a
    trac 9676 -- cosmetic improvements to RandomTree Graph generator
    
    diff -r 23f8a43c7f41 -r 1e8732a7e9ed sage/graphs/graph_generators.py
    a b  
    41554155        set `\{0,1,...,n-1\}`. This constructor chooses one of these uniformly
    41564156        at random.
    41574157
     4158        ALGORITHM:
     4159
    41584160        The algoritm works by generating an `(n-2)`-long
    41594161        random sequence of numbers chosen independently and uniformly
    41604162        from `\{0,1,\ldots,n-1\}` and then applies an inverse
     
    41694171            sage: G = graphs.RandomTree(10)
    41704172            sage: G.is_tree()
    41714173            True
    4172             sage: G.show() # long time
    4173 
     4174            sage: G.show() # long
     4175
     4176        TESTS:
     4177
     4178        Ensuring that we encounter no unexpected surprise ::
     4179
     4180            sage: all( graphs.RandomTree(10).is_tree()
     4181            ...        for i in range(100) )
     4182            True
    41744183
    41754184        """
    41764185        from sage.misc.prandom import randint
     
    41794188        # create random Prufer code
    41804189        code = [ randint(0,n-1) for i in xrange(n-2) ]
    41814190
    4182         # flags to show which vertices are available (unused)
    4183         avail = [ True for i in xrange(n) ]
    4184 
    41854191        # We count the number of symbols of each type.
    41864192        # count[k] is the no. of times k appears in code
     4193        #
     4194        # (count[k] is set to -1 when the corresponding vertex is not
     4195        # available anymore)
    41874196        count = [ 0 for i in xrange(n) ]   
    4188         for k in xrange(n-2):
    4189             count[code[k]] += 1
     4197        for k in code:
     4198            count[k] += 1
    41904199
    41914200        g.add_vertices(range(n))
    41924201
    4193         idx = 0
    4194 
    4195         while idx < len(code):
    4196             xlist = [k for k in range(n) if avail[k] and count[k]==0 ]
    4197             if len(xlist)==0: break
    4198             x = xlist[0]
    4199             avail[x] = False
    4200             s = code[idx]
     4202        for s in code:
     4203            for x in range(n):
     4204                if count[x] == 0:
     4205                    break
     4206
     4207            count[x] = -1
    42014208            g.add_edge(x,s)
    42024209            count[s] -= 1
    4203             idx += 1
    4204 
    4205         last_edge = [ v for v in range(n) if avail[v] ]
    4206 
     4210
     4211        # Adding as an edge the last two available vertices
     4212        last_edge = [ v for v in range(n) if count[v] != -1 ]
    42074213        g.add_edge(last_edge)
    42084214
    42094215        return g
    42104216
    4211 
    4212 
    42134217    def RandomTreePowerlaw(self, n, gamma=3, tries=100, seed=None):
    42144218        """
    42154219        Returns a tree with a power law degree distribution. Returns False