Ticket #13578: trac_13578-rev.patch

File trac_13578-rev.patch, 6.8 KB (added by ncohen, 9 years ago)
  • sage/graphs/generators/families.py

    # 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  
    55This file gathers generators for some families of graphs.
    66- :meth:`RingedTree <GraphGenerators.RingedTree>`
    77
    8 
    98AUTHORS:
    109
    1110- David Coudert (2012) Ringed Trees
     
    1817#                         http://www.gnu.org/licenses/
    1918################################################################################
    2019
    21 # import from Python standard library
    22 from math import sin, cos, pi
    23 
    2420# import from Sage library
    2521from sage.graphs.graph import Graph
    2622
    27 
    28 def RingedTree(self, k):
     23def RingedTree(self, k, vertex_labels = True):
    2924    r"""
    3025    Return the ringed tree on k-levels.
    3126
    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
    3328    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 , k-1`, 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]_.
    4236
    4337    INPUT:
    4438
    4539    - ``k`` -- the number of levels of the ringed tree.
    4640
    47     EXAMPLE:
     41    - ``vertex_labels`` (boolean) -- whether to label vertices as binary words
     42      (default) or as integers.
     43
     44    EXAMPLE::
    4845
    4946        sage: G = graphs.RingedTree(5)
    5047        sage: P = G.plot(vertex_labels=False, vertex_size=10)
    5148        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']
    5254
    53     TEST:
     55    TEST::
    5456
    5557        sage: G = graphs.RingedTree(-1)
    5658        Traceback (most recent call last):
    5759        ...
    5860        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 Small-World and Tree-Like Random Graphs
     69      Wei Chen, Wenjie Fang, Guangda Hu, Michael W. Mahoney
     70      http://arxiv.org/abs/1201.1717
    5971    """
    6072    if k<1:
    6173        raise ValueError('The number of levels must be >= 1.')
    6274
    63     E = []
    64     W = {}
    65     # We first add the tree edges
    66     W[0] = ['']
    67     for l in xrange(k-1):
    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
    7477
    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][twol-1] ] = ( 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][twol-1]) )
    84         for i in xrange(twol-1):
    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,k-1)
     80    g.name('Ringed Tree on '+str(k)+' levels')
    8881
    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)
    9085
     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**(i-2)+.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(k-1):
     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
  • sage/graphs/graph_generators.py

    diff --git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
    a b  
    153153- :meth:`FuzzyBallGraph <GraphGenerators.FuzzyBallGraph>`
    154154- :meth:`GeneralizedPetersenGraph <GraphGenerators.GeneralizedPetersenGraph>`
    155155- :meth:`HanoiTowerGraph <GraphGenerators.HanoiTowerGraph>`
    156 
    157156- :meth:`HyperStarGraph <GraphGenerators.HyperStarGraph>`
    158157- :meth:`KneserGraph <GraphGenerators.KneserGraph>`
    159158- :meth:`LCFGraph <GraphGenerators.LCFGraph>`
     
    163162- :meth:`NStarGraph <GraphGenerators.NStarGraph>`
    164163- :meth:`OddGraph <GraphGenerators.OddGraph>`
    165164- :meth:`PaleyGraph <GraphGenerators.PaleyGraph>`
     165- :meth:`RingedTree <GraphGenerators.RingedTree>`
    166166- :meth:`line_graph_forbidden_subgraphs <GraphGenerators.line_graph_forbidden_subgraphs>`
    167167- :meth:`PermutationGraph <GraphGenerators.PermutationGraph>`
    168168- :meth:`trees <GraphGenerators.trees>`
    169169
     170Chessboard graphs :
     171
    170172- :meth:`BishopGraph <GraphGenerators.BishopGraph>`
    171173- :meth:`KingGraph <GraphGenerators.KingGraph>`
    172174- :meth:`KnightGraph <GraphGenerators.KnightGraph>`
    173175- :meth:`QueenGraph <GraphGenerators.QueenGraph>`
    174176- :meth:`RookGraph <GraphGenerators.RookGraph>`
    175177
    176 - :meth:`RingedTree <GraphGenerators.RingedTree>`
    177 
    178 
    179178Pseudofractal graphs
    180179--------------------
    181180
    182181- :meth:`DorogovtsevGoltsevMendesGraph <GraphGenerators.DorogovtsevGoltsevMendesGraph>`
    183182
    184 
    185183Random graphs
    186184-------------
    187185