Ticket #9676: trac_9676.patch

File trac_9676.patch, 3.1 KB (added by edward.scheinerman, 11 years ago)

New, improved, repaired RandomTree graph constructor

  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Ed Scheinerman <ers@jhu.edu>
    # Date 1281909194 14400
    # Node ID e781fca6b570aa348cf5b47df56d72643422e4ba
    # Parent  5b338f2e484f2065d3d30d47bc204d6e9ed13d12
    trac 9676 - New version of RandomTree that corrects mistakes in the algorithm and has much better documentation.
    
    diff -r 5b338f2e484f -r e781fca6b570 sage/graphs/graph_generators.py
    a b  
    145145- :meth:`RandomNewmanWattsStrogatz <GraphGenerators.RandomNewmanWattsStrogatz>`
    146146- :meth:`RandomRegular <GraphGenerators.RandomRegular>`
    147147- :meth:`RandomShell <GraphGenerators.RandomShell>`
     148- :meth:`RandomTree <GraphGenerators.RandomTree>`
    148149- :meth:`RandomTreePowerlaw <GraphGenerators.RandomTreePowerlaw>`
    149150
    150151
     
    196197- Harald Schilly and Yann Laigle-Chapuy (2010-03-24): added Fibonacci Tree
    197198
    198199- Jason Grout (2010-06-04): cospectral_graphs
     200
     201- Edward Scheinerman (2010-08-11): RandomTree
    199202"""
    200203
    201204###########################################################################
     
    41444147        import networkx
    41454148        return graph.Graph(networkx.random_lobster(n, p, q, seed=seed))
    41464149
     4150    def RandomTree(self, n):
     4151        """
     4152        Returns a random tree on `n` nodes numbered `0` through `n-1`.
     4153
     4154        By Cayley's theorem, there are `n^{n-2}` trees with vertex
     4155        set `\{0,1,...,n-1\}`. This constructor chooses one of these uniformly
     4156        at random.
     4157
     4158        The algoritm works by generating an `(n-2)`-long
     4159        random sequence of numbers chosen independently and uniformly
     4160        from `\{0,1,\ldots,n-1\}` and then applies an inverse
     4161        Prufer transformation.
     4162
     4163        INPUT:
     4164
     4165        -  ``n`` - number of vertices in the tree
     4166
     4167        EXAMPLE::
     4168
     4169            sage: G = graphs.RandomTree(10)
     4170            sage: G.is_tree()
     4171            True
     4172            sage: G.show() # long time
     4173
     4174
     4175        """
     4176        from sage.misc.prandom import randint
     4177        g = graph.Graph()
     4178
     4179        # create random Prufer code
     4180        code = [ randint(0,n-1) for i in xrange(n-2) ]
     4181
     4182        # flags to show which vertices are available (unused)
     4183        avail = [ True for i in xrange(n) ]
     4184
     4185        # We count the number of symbols of each type.
     4186        # count[k] is the no. of times k appears in code
     4187        count = [ 0 for i in xrange(n) ]   
     4188        for k in xrange(n-2):
     4189            count[code[k]] += 1
     4190
     4191        g.add_vertices(range(n))
     4192
     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]
     4201            g.add_edge(x,s)
     4202            count[s] -= 1
     4203            idx += 1
     4204
     4205        last_edge = [ v for v in range(n) if avail[v] ]
     4206
     4207        g.add_edge(last_edge)
     4208
     4209        return g
     4210
     4211
     4212
    41474213    def RandomTreePowerlaw(self, n, gamma=3, tries=100, seed=None):
    41484214        """
    41494215        Returns a tree with a power law degree distribution. Returns False