# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1349628950 7200
# Node ID e7b3b1cc4441ca2deff7d62b524d7bd26294b7a0
# Parent 477fc9acc814949a648436ac568f9e337f32658d
graphs.RingedTree  reviewer's patch
diff git a/sage/graphs/generators/families.py b/sage/graphs/generators/families.py
a

b


5  5  This file gathers generators for some families of graphs. 
6  6   :meth:`RingedTree <GraphGenerators.RingedTree>` 
7  7  
8   
9  8  AUTHORS: 
10  9  
11  10   David Coudert (2012) Ringed Trees 
… 
… 

18  17  # http://www.gnu.org/licenses/ 
19  18  ################################################################################ 
20  19  
21   # import from Python standard library 
22   from math import sin, cos, pi 
23   
24  20  # import from Sage library 
25  21  from sage.graphs.graph import Graph 
26  22  
27   
28   def RingedTree(self, k): 
 23  def RingedTree(self, k, vertex_labels = True): 
29  24  r""" 
30  25  Return the ringed tree on klevels. 
31  26  
32   A ringed tree of level `k` is a fully binary tree with `k` levels (counting 
 27  A ringed tree of level `k` is a binary tree with `k` levels (counting 
33  28  the root as a level), in which all vertices at the same level are connected 
34   by a ring. More precisely, we can use a binary string to represent each 
35   vertex in the tree, such that the root (at level 0) is represented by an 
36   empty string, and the left child and the right child of a vertex with string 
37   `\sigma` are represented as `\sigma0` and \sigma1`, respectively. Then, at 
38   each level `i = 1, 2, \cdots , k1`, we connect two vertices `u` and `v` 
39   represented by binary strings `\sigma_u` and `\sigma_v` if `(\sigma_u + 1) 
40   \pmod{2^i}\equiv \sigma_v`, where the addition treats the binary strings as 
41   the integers they represent. 
 29  by a ring. 
 30  
 31  More precisely, in each layer of the binary tree (i.e. a layer is the set of 
 32  vertices `[2^i...2^{i+1}1]`) two vertices `u,v` are adjacent if `u=v+1` or 
 33  if `u=2^i` and `v=`2^{i+1}1`. 
 34  
 35  Ringed trees are defined in [CFHM12]_. 
42  36  
43  37  INPUT: 
44  38  
45  39   ``k``  the number of levels of the ringed tree. 
46  40  
47   EXAMPLE: 
 41   ``vertex_labels`` (boolean)  whether to label vertices as binary words 
 42  (default) or as integers. 
 43  
 44  EXAMPLE:: 
48  45  
49  46  sage: G = graphs.RingedTree(5) 
50  47  sage: P = G.plot(vertex_labels=False, vertex_size=10) 
51  48  sage: P.show() # long time 
 49  sage: G.vertices() 
 50  ['', '0', '00', '000', '0000', '0001', '001', '0010', '0011', '01', 
 51  '010', '0100', '0101', '011', '0110', '0111', '1', '10', '100', 
 52  '1000', '1001', '101', '1010', '1011', '11', '110', '1100', '1101', 
 53  '111', '1110', '1111'] 
52  54  
53   TEST: 
 55  TEST:: 
54  56  
55  57  sage: G = graphs.RingedTree(1) 
56  58  Traceback (most recent call last): 
57  59  ... 
58  60  ValueError: The number of levels must be >= 1. 
 61  sage: G = graphs.RingedTree(5, vertex_labels = False) 
 62  sage: G.vertices() 
 63  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
 64  18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] 
 65  
 66  REFERENCES: 
 67  
 68  .. [CFHM12] On the Hyperbolicity of SmallWorld and TreeLike Random Graphs 
 69  Wei Chen, Wenjie Fang, Guangda Hu, Michael W. Mahoney 
 70  http://arxiv.org/abs/1201.1717 
59  71  """ 
60  72  if k<1: 
61  73  raise ValueError('The number of levels must be >= 1.') 
62  74  
63   E = [] 
64   W = {} 
65   # We first add the tree edges 
66   W[0] = [''] 
67   for l in xrange(k1): 
68   W[l+1] = [] 
69   for r in W[l]: 
70   E.append( (r,r+'0') ) 
71   E.append( (r,r+'1') ) 
72   W[l+1].append(r+'0') 
73   W[l+1].append(r+'1') 
 75  from sage.graphs.graph_generators import _circle_embedding 
 76  from sage.graphs.graph_generators import GraphGenerators 
74  77  
75   # We then add the ring edges and fix positions of vertices for nice drawing 
76   pos_dict = {'':(0,0.2)} 
77   for l in xrange(1,k): 
78   twol = 1<<l 
79   r = 1 if l==1 else 1.5**l 
80   theta0 = pi/twol 
81   pos_dict[ W[l][twol1] ] = ( r*sin(theta0), r*cos(theta0) ) 
82   if l>1: # to avoid adding twice edge ('0','1') 
83   E.append( (W[l][0],W[l][twol1]) ) 
84   for i in xrange(twol1): 
85   theta = (2*i+1)*theta0 
86   pos_dict[ W[l][i] ] = ( r*sin(theta), r*cos(theta) ) 
87   E.append( (W[l][i],W[l][i+1]) ) 
 78  # Creating the Balanced tree, which contains most edges already 
 79  g = GraphGenerators().BalancedTree(2,k1) 
 80  g.name('Ringed Tree on '+str(k)+' levels') 
88  81  
89   return Graph(E, pos=pos_dict, name='Ringed Tree on '+str(k)+' levels' ) 
 82  # We consider edges layer by layer 
 83  for i in range(1,k): 
 84  vertices = range(2**(i)1,2**(i+1)1) 
90  85  
 86  # Add the missing edges 
 87  g.add_cycle(vertices) 
 88  
 89  # And set the vertices' positions 
 90  radius = i if i <= 1 else 1.5**i 
 91  shift = 2**(i2)+.5 if i > 1 else 0 
 92  _circle_embedding(g, vertices, radius = radius, shift = shift) 
 93  
 94  # Specific position for the central vertex 
 95  g.get_pos()[0] = (0,0.2) 
 96  
 97  # Relabel vertices as binary words 
 98  if not vertex_labels: 
 99  return g 
 100  
 101  vertices = [''] 
 102  for i in range(k1): 
 103  for j in range(2**(i)1,2**(i+1)1): 
 104  v = vertices[j] 
 105  vertices.append(v+'0') 
 106  vertices.append(v+'1') 
 107  
 108  g.relabel(vertices) 
 109  
 110  return g 
diff git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
a

b


153  153   :meth:`FuzzyBallGraph <GraphGenerators.FuzzyBallGraph>` 
154  154   :meth:`GeneralizedPetersenGraph <GraphGenerators.GeneralizedPetersenGraph>` 
155  155   :meth:`HanoiTowerGraph <GraphGenerators.HanoiTowerGraph>` 
156   
157  156   :meth:`HyperStarGraph <GraphGenerators.HyperStarGraph>` 
158  157   :meth:`KneserGraph <GraphGenerators.KneserGraph>` 
159  158   :meth:`LCFGraph <GraphGenerators.LCFGraph>` 
… 
… 

163  162   :meth:`NStarGraph <GraphGenerators.NStarGraph>` 
164  163   :meth:`OddGraph <GraphGenerators.OddGraph>` 
165  164   :meth:`PaleyGraph <GraphGenerators.PaleyGraph>` 
 165   :meth:`RingedTree <GraphGenerators.RingedTree>` 
166  166   :meth:`line_graph_forbidden_subgraphs <GraphGenerators.line_graph_forbidden_subgraphs>` 
167  167   :meth:`PermutationGraph <GraphGenerators.PermutationGraph>` 
168  168   :meth:`trees <GraphGenerators.trees>` 
169  169  
 170  Chessboard graphs : 
 171  
170  172   :meth:`BishopGraph <GraphGenerators.BishopGraph>` 
171  173   :meth:`KingGraph <GraphGenerators.KingGraph>` 
172  174   :meth:`KnightGraph <GraphGenerators.KnightGraph>` 
173  175   :meth:`QueenGraph <GraphGenerators.QueenGraph>` 
174  176   :meth:`RookGraph <GraphGenerators.RookGraph>` 
175  177  
176    :meth:`RingedTree <GraphGenerators.RingedTree>` 
177   
178   
179  178  Pseudofractal graphs 
180  179   
181  180  
182  181   :meth:`DorogovtsevGoltsevMendesGraph <GraphGenerators.DorogovtsevGoltsevMendesGraph>` 
183  182  
184   
185  183  Random graphs 
186  184   
187  185  