# 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 set `\{0,1,...,n-1\}`. This constructor chooses one of these uniformly at random. ALGORITHM: The algoritm works by generating an `(n-2)`-long random sequence of numbers chosen independently and uniformly from `\{0,1,\ldots,n-1\}` and then applies an inverse sage: G = graphs.RandomTree(10) sage: G.is_tree() True sage: G.show() # long time sage: G.show() # long TESTS: Ensuring that we encounter no unexpected surprise :: sage: all( graphs.RandomTree(10).is_tree() ...        for i in range(100) ) True """ from sage.misc.prandom import randint # create random Prufer code code = [ randint(0,n-1) for i in xrange(n-2) ] # flags to show which vertices are available (unused) avail = [ True for i in xrange(n) ] # We count the number of symbols of each type. # count[k] is the no. of times k appears in code # # (count[k] is set to -1 when the corresponding vertex is not # available anymore) count = [ 0 for i in xrange(n) ] for k in xrange(n-2): count[code[k]] += 1 for k in code: count[k] += 1 g.add_vertices(range(n)) idx = 0 while idx < len(code): xlist = [k for k in range(n) if avail[k] and count[k]==0 ] if len(xlist)==0: break x = xlist[0] avail[x] = False s = code[idx] for s in code: for x in range(n): if count[x] == 0: break count[x] = -1 g.add_edge(x,s) count[s] -= 1 idx += 1 last_edge = [ v for v in range(n) if avail[v] ] # Adding as an edge the last two available vertices last_edge = [ v for v in range(n) if count[v] != -1 ] g.add_edge(last_edge) return g def RandomTreePowerlaw(self, n, gamma=3, tries=100, seed=None): """ Returns a tree with a power law degree distribution. Returns False