Ticket #9375: trac_9375-graph-doctests.patch

File trac_9375-graph-doctests.patch, 26.7 KB (added by mvngu, 3 years ago)
  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Minh Van Nguyen <nguyenminh2@gmail.com>
    # Date 1276617601 25200
    # Node ID 2a72cc454c889bd724863c7e2e0d14bc8a3ce601
    # Parent  1077395365dabd8864bf0f9f1972aaeeca80bcfd
    #9375: more documentation & doctests for BalancedTree, BarbellGraph, BubbleSortGraph, BullGraph, ChvatalGraph
    
    diff --git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
    a b  
    403403        3 20 
    404404        4 90 
    405405        5 544 
    406      
    407     Generate all graphs with a specified degree sequence: (see 
    408     http://www.research.att.com/~njas/sequences/A002851) 
    409      
    410     :: 
    411      
     406 
     407    Generate all graphs with a specified degree sequence (see 
     408    http://www.research.att.com/~njas/sequences/A002851):: 
     409 
    412410        sage: for i in [4,6,8]: 
    413         ...    print i, len([g for g in graphs(i,deg_seq=[3]*i) if g.is_connected()]) 
     411        ...       print i, len([g for g in graphs(i, deg_seq=[3]*i) if g.is_connected()]) 
    414412        4 1 
    415413        6 2 
    416414        8 5 
    417415        sage: for i in [4,6,8]:                                                                          # long time 
    418         ...    print i, len([g for g in graphs(i,augment='vertices',deg_seq=[3]*i) if g.is_connected()]) # long time 
     416        ...       print i, len([g for g in graphs(i,augment='vertices',deg_seq=[3]*i) if g.is_connected()]) # long time 
    419417        4 1 
    420418        6 2 
    421419        8 5 
    422      
    423     :: 
    424      
     420 
     421    :: 
     422 
    425423        sage: print 10, len([g for g in graphs(10,deg_seq=[3]*10) if g.is_connected()]) # not tested 
    426424        10 19 
    427      
     425 
    428426    REFERENCE: 
    429427 
    430     - Brendan D. McKay, Isomorph-Free Exhaustive generation.  Journal 
    431       of Algorithms Volume 26, Issue 2, February 1998, pages 306-324. 
    432     """ 
    433      
    434 ################################################################################ 
    435 #   Basic Structures 
    436 ################################################################################ 
    437          
     428    - Brendan D. McKay, Isomorph-Free Exhaustive generation.  *Journal 
     429      of Algorithms*, Volume 26, Issue 2, February 1998, pages 306-324. 
     430    """ 
     431 
     432    ####################################################################### 
     433    #   Basic Structures 
     434    ####################################################################### 
     435 
    438436    def BarbellGraph(self, n1, n2): 
    439         """ 
    440         Returns a barbell graph with 2\*n1 + n2 nodes. n1 must be greater 
    441         than or equal to 2. 
    442          
     437        r""" 
     438        Returns a barbell graph with ``2*n1 + n2`` nodes. The argument ``n1`` 
     439        must be greater than or equal to 2. 
     440 
    443441        A barbell graph is a basic structure that consists of a path graph 
    444         of order n2 connecting two complete graphs of order n1 each. 
    445          
    446         This constructor depends on NetworkX numeric labels. In this case, 
    447         the (n1)th node connects to the path graph from one complete graph 
    448         and the (n1+n2+1)th node connects to the path graph from the other 
    449         complete graph. 
    450          
    451         PLOTTING: Upon construction, the position dictionary is filled to 
     442        of order ``n2`` connecting two complete graphs of order ``n1`` each. 
     443 
     444        This constructor depends on `NetworkX <http://networkx.lanl.gov>`_ 
     445        numeric labels. In this case, the ``n1``-th node connects to the 
     446        path graph from one complete graph and the ``n1 + n2 + 1``-th node 
     447        connects to the path graph from the other complete graph. 
     448 
     449        INPUT: 
     450 
     451        - ``n1`` -- integer `\geq 2`. The order of each of the two 
     452          complete graphs. 
     453 
     454        - ``n2`` -- nonnegative integer. The order of the path graph 
     455          connecting the two complete graphs. 
     456 
     457        OUTPUT: 
     458 
     459        A barbell graph of order ``2*n1 + n2``. A ``ValueError`` is 
     460        returned if ``n1 < 2`` or ``n2 < 0``. 
     461 
     462        ALGORITHM: 
     463 
     464        Uses `NetworkX <http://networkx.lanl.gov>`_. 
     465 
     466        PLOTTING: 
     467 
     468        Upon construction, the position dictionary is filled to 
    452469        override the spring-layout algorithm. By convention, each barbell 
    453470        graph will be displayed with the two complete graphs in the 
    454471        lower-left and upper-right corners, with the path graph connecting 
    455         diagonally between the two. Thus the (n1)th node will be drawn at a 
     472        diagonally between the two. Thus the ``n1``-th node will be drawn at a 
    456473        45 degree angle from the horizontal right center of the first 
    457         complete graph, and the (n1+n2+1)th node will be drawn 45 degrees 
    458         below the left horizontal center of the second complete graph. 
    459          
    460         EXAMPLES: Construct and show a barbell graph Bar = 4, Bells = 9 
    461          
    462         :: 
    463          
    464             sage: g = graphs.BarbellGraph(9,4) 
    465             sage: g.show() # long time 
    466          
    467         Create several barbell graphs in a Sage graphics array 
    468          
    469         :: 
    470          
     474        complete graph, and the ``n1 + n2 + 1``-th node will be drawn 45 
     475        degrees below the left horizontal center of the second complete graph. 
     476 
     477        EXAMPLES: 
     478 
     479        Construct and show a barbell graph ``Bar = 4``, ``Bells = 9``:: 
     480 
     481            sage: g = graphs.BarbellGraph(9, 4); g 
     482            Barbell graph: Graph on 22 vertices 
     483            sage: g.show() # long time 
     484 
     485        An ``n1 >= 2``, ``n2 >= 0`` barbell graph has order ``2*n1 + n2``. It 
     486        has the complete graph on ``n1`` vertices as a subgraph. It also has 
     487        the path graph on ``n2`` vertices as a subgraph. :: 
     488 
     489            sage: n1 = randint(2, 2*10^2) 
     490            sage: n2 = randint(0, 2*10^2) 
     491            sage: g = graphs.BarbellGraph(n1, n2) 
     492            sage: v = 2*n1 + n2 
     493            sage: g.order() == v 
     494            True 
     495            sage: K_n1 = graphs.CompleteGraph(n1) 
     496            sage: P_n2 = graphs.PathGraph(n2) 
     497            sage: s_K = g.subgraph_search(K_n1, induced=True) 
     498            sage: s_P = g.subgraph_search(P_n2, induced=True) 
     499            sage: K_n1.is_isomorphic(s_K) 
     500            True 
     501            sage: P_n2.is_isomorphic(s_P) 
     502            True 
     503 
     504        Create several barbell graphs in a Sage graphics array:: 
     505 
    471506            sage: g = [] 
    472507            sage: j = [] 
    473508            sage: for i in range(6): 
    474             ...    k = graphs.BarbellGraph(i+2,4) 
    475             ...    g.append(k) 
     509            ...       k = graphs.BarbellGraph(i + 2, 4) 
     510            ...       g.append(k) 
    476511            ... 
    477512            sage: for i in range(2): 
    478             ...    n = [] 
    479             ...    for m in range(3): 
    480             ...        n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False)) 
    481             ...    j.append(n) 
    482             ... 
    483             sage: G = sage.plot.plot.GraphicsArray(j) 
    484             sage: G.show() # long time 
    485         """ 
    486         pos_dict = {} 
    487          
     513            ...       n = [] 
     514            ...       for m in range(3): 
     515            ...           n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False)) 
     516            ...       j.append(n) 
     517            ... 
     518            sage: G = sage.plot.plot.GraphicsArray(j) 
     519            sage: G.show() # long time 
     520 
     521        TESTS: 
     522 
     523        The input ``n1`` must be `\geq 2`:: 
     524 
     525            sage: graphs.BarbellGraph(1, randint(0, 10^6)) 
     526            Traceback (most recent call last): 
     527            ... 
     528            ValueError: Invalid graph description, n1 should be >= 2 
     529            sage: graphs.BarbellGraph(randint(-10^6, 1), randint(0, 10^6)) 
     530            Traceback (most recent call last): 
     531            ... 
     532            ValueError: Invalid graph description, n1 should be >= 2 
     533 
     534        The input ``n2`` must be `\geq 0`:: 
     535 
     536            sage: graphs.BarbellGraph(randint(2, 10^6), -1) 
     537            Traceback (most recent call last): 
     538            ... 
     539            ValueError: Invalid graph description, n2 should be >= 0 
     540            sage: graphs.BarbellGraph(randint(2, 10^6), randint(-10^6, -1)) 
     541            Traceback (most recent call last): 
     542            ... 
     543            ValueError: Invalid graph description, n2 should be >= 0 
     544            sage: graphs.BarbellGraph(randint(-10^6, 1), randint(-10^6, -1)) 
     545            Traceback (most recent call last): 
     546            ... 
     547            ValueError: Invalid graph description, n1 should be >= 2 
     548        """ 
     549        # sanity checks 
     550        if n1 < 2: 
     551            raise ValueError("Invalid graph description, n1 should be >= 2") 
     552        if n2 < 0: 
     553            raise ValueError("Invalid graph description, n2 should be >= 0") 
     554 
     555        pos_dict = {} 
     556 
    488557        for i in range(n1): 
    489             x = float(cos((pi/4) - ((2*pi)/n1)*i) - n2/2 - 1) 
    490             y = float(sin((pi/4) - ((2*pi)/n1)*i) - n2/2 - 1) 
    491             j = n1-1-i 
    492             pos_dict[j] = (x,y) 
    493         for i in range(n1,n1+n2): 
    494             x = float(i - n1 - n2/2 + 1) 
    495             y = float(i - n1 - n2/2 + 1) 
    496             pos_dict[i] = (x,y) 
    497         for i in range(n1+n2,2*n1+n2): 
    498             x = float(cos((5*pi/4) + ((2*pi)/n1)*(i-n1-n2)) + n2/2 + 2) 
    499             y = float(sin((5*pi/4) + ((2*pi)/n1)*(i-n1-n2)) + n2/2 + 2) 
    500             pos_dict[i] = (x,y) 
    501          
    502         import networkx 
    503         G = networkx.barbell_graph(n1,n2) 
     558            x = float(cos((pi / 4) - ((2 * pi) / n1) * i) - (n2 / 2) - 1) 
     559            y = float(sin((pi / 4) - ((2 * pi) / n1) * i) - (n2 / 2) - 1) 
     560            j = n1 - 1 - i 
     561            pos_dict[j] = (x, y) 
     562        for i in range(n1, n1 + n2): 
     563            x = float(i - n1 - (n2 / 2) + 1) 
     564            y = float(i - n1 - (n2 / 2) + 1) 
     565            pos_dict[i] = (x, y) 
     566        for i in range(n1 + n2, (2 * n1) + n2): 
     567            x = float( 
     568                cos((5 * (pi / 4)) + ((2 * pi) / n1) * (i - n1 - n2)) 
     569                + (n2 / 2) + 2) 
     570            y = float( 
     571                sin((5 * (pi / 4)) + ((2 * pi) / n1) * (i - n1 - n2)) 
     572                + (n2 / 2) + 2) 
     573            pos_dict[i] = (x, y) 
     574 
     575        import networkx 
     576        G = networkx.barbell_graph(n1, n2) 
    504577        return graph.Graph(G, pos=pos_dict, name="Barbell graph") 
    505          
     578 
    506579    def BullGraph(self): 
    507         """ 
     580        r""" 
    508581        Returns a bull graph with 5 nodes. 
    509          
     582 
    510583        A bull graph is named for its shape. It's a triangle with horns. 
    511          
    512         This constructor depends on NetworkX numeric labeling. 
    513          
    514         PLOTTING: Upon construction, the position dictionary is filled to 
     584        This constructor depends on `NetworkX <http://networkx.lanl.gov>`_ 
     585        numeric labeling. For more information, see this 
     586        `Wikipedia article on the bull graph <http://en.wikipedia.org/wiki/Bull_graph>`_. 
     587 
     588        PLOTTING: 
     589 
     590        Upon construction, the position dictionary is filled to 
    515591        override the spring-layout algorithm. By convention, the bull graph 
    516592        is drawn as a triangle with the first node (0) on the bottom. The 
    517593        second and third nodes (1 and 2) complete the triangle. Node 3 is 
    518594        the horn connected to 1 and node 4 is the horn connected to node 
    519595        2. 
    520          
    521         EXAMPLES: Construct and show a bull graph 
    522          
    523         :: 
    524          
    525             sage: g = graphs.BullGraph() 
    526             sage: g.show() # long time 
    527         """ 
    528         pos_dict = {0:(0,0),1:(-1,1),2:(1,1),3:(-2,2),4:(2,2)} 
     596 
     597        ALGORITHM: 
     598 
     599        Uses `NetworkX <http://networkx.lanl.gov>`_. 
     600 
     601        EXAMPLES: 
     602 
     603        Construct and show a bull graph:: 
     604 
     605            sage: g = graphs.BullGraph(); g 
     606            Bull graph: Graph on 5 vertices 
     607            sage: g.show() # long time 
     608 
     609        The bull graph has 5 vertices and 5 edges. Its radius is 2, its 
     610        diameter 3, and its girth 3. The bull graph is planar with chromatic 
     611        number 3 and chromatic index also 3. :: 
     612 
     613            sage: g.order(); g.size() 
     614            5 
     615            5 
     616            sage: g.radius(); g.diameter(); g.girth() 
     617            2 
     618            3 
     619            3 
     620            sage: g.chromatic_number() 
     621            3 
     622 
     623        The bull graph has chromatic polynomial `x(x - 2)(x - 1)^3` and 
     624        Tutte polynomial `x^4 + x^3 + x^2 y`. Its characteristic polynomial 
     625        is `x(x^2 - x - 3)(x^2 + x - 1)`, which follows from the definition of 
     626        characteristic polynomials for graphs, i.e. `\det(xI - A)`, where 
     627        `x` is a variable, `A` the adjacency matrix of the graph, and `I` 
     628        the identity matrix of the same dimensions as `A`. :: 
     629 
     630            sage: chrompoly = g.chromatic_polynomial() 
     631            sage: bool(expand(x * (x - 2) * (x - 1)^3) == chrompoly) 
     632            True 
     633            sage: charpoly = g.characteristic_polynomial() 
     634            sage: M = g.adjacency_matrix(); M 
     635            [0 1 1 0 0] 
     636            [1 0 1 1 0] 
     637            [1 1 0 0 1] 
     638            [0 1 0 0 0] 
     639            [0 0 1 0 0] 
     640            sage: Id = identity_matrix(ZZ, M.nrows()) 
     641            sage: D = x*Id - M 
     642            sage: bool(D.determinant() == charpoly) 
     643            True 
     644            sage: bool(expand(x * (x^2 - x - 3) * (x^2 + x - 1)) == charpoly) 
     645            True 
     646        """ 
     647        pos_dict = {0:(0,0), 1:(-1,1), 2:(1,1), 3:(-2,2), 4:(2,2)} 
    529648        import networkx 
    530649        G = networkx.bull_graph() 
    531         return graph.Graph(G, pos=pos_dict, name="Bull Graph") 
    532          
     650        return graph.Graph(G, pos=pos_dict, name="Bull graph") 
    533651 
    534652    def CircularLadderGraph(self, n): 
    535653        """ 
    536654        Returns a circular ladder graph with 2\*n nodes. 
    537          
     655 
    538656        A Circular ladder graph is a ladder graph that is connected at the 
    539657        ends, i.e.: a ladder bent around so that top meets bottom. Thus it 
    540658        can be described as two parallel cycle graphs connected at each 
    541659        corresponding node pair. 
    542          
    543         This constructor depends on NetworkX numeric labels. 
    544          
     660 
     661        This constructor depends on NetworkX numeric labels. 
     662 
    545663        PLOTTING: Upon construction, the position dictionary is filled to 
    546664        override the spring-layout algorithm. By convention, the circular 
    547665        ladder graph is displayed as an inner and outer cycle pair, with 
     
    762880        (`n_1` rows and `n_2` columns). 
    763881 
    764882        The toroidal 2-dimensional grid with parameters `n_1,n_2` is  
    765         the 2-dimensional grid graph with identital parameters 
     883        the 2-dimensional grid graph with identical parameters 
    766884        to which are added the edges `((i,0),(i,n_2-1))` and  
    767885        `((0,i),(n_1-1,i))`. 
    768886 
     
    9171035        but slow down creation of the graph.  Likewise computing layout 
    9181036        information also incurs a significant speed penalty. For maximum 
    9191037        speed, turn off labels and layout and decode the 
    920         vertices explicily as needed.  The 
     1038        vertices explicitly as needed.  The 
    9211039        :meth:`sage.rings.integer.Integer.digits` 
    9221040        with the ``padsto`` option is a quick way to do this, though you 
    9231041        may want to reverse the list that is output. 
     
    18201938        G = networkx.dodecahedral_graph() 
    18211939        return graph.Graph(G, name="Dodecahedron") 
    18221940 
    1823 ################################################################################ 
    1824 #   Named Graphs 
    1825 ################################################################################ 
    1826      
     1941    ####################################################################### 
     1942    #   Named Graphs 
     1943    ####################################################################### 
     1944 
    18271945    def ChvatalGraph(self): 
    1828         """ 
     1946        r""" 
    18291947        Returns the Chvatal graph. 
    1830          
    1831         The Chvatal graph has 12 vertices. It is a 4-regular, 4-chromatic 
    1832         graph. It is one of the few known graphs to satisfy Grunbaum's 
    1833         conjecture that for every m 1, n 2, there is an m-regular, 
    1834         m-chromatic graph of girth at least n. 
    1835          
    1836         EXAMPLE:: 
    1837          
    1838             sage: G = graphs.ChvatalGraph() 
     1948 
     1949        Chvatal graph is one of the few known graphs to satisfy Grunbaum's 
     1950        conjecture that for every m, n, there is an m-regular, 
     1951        m-chromatic graph of girth at least n. For more information, see this 
     1952        `Wikipedia article on the Chvatal graph <http://en.wikipedia.org/wiki/Chv%C3%A1tal_graph>`_. 
     1953 
     1954        EXAMPLES: 
     1955 
     1956        The Chvatal graph has 12 vertices and 24 edges. It is a 4-regular, 
     1957        4-chromatic graph with radius 2, diameter 2, and girth 4. :: 
     1958 
     1959            sage: G = graphs.ChvatalGraph(); G 
     1960            Chvatal graph: Graph on 12 vertices 
     1961            sage: G.order(); G.size() 
     1962            12 
     1963            24 
    18391964            sage: G.degree() 
    18401965            [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4] 
    1841         """ 
    1842         import networkx 
    1843         pos_dict = {} 
    1844         for i in range(5,10): 
    1845             x = float(cos((pi/2) + ((2*pi)/5)*i)) 
    1846             y = float(sin((pi/2) + ((2*pi)/5)*i)) 
    1847             pos_dict[i] = (x,y) 
     1966            sage: G.chromatic_number() 
     1967            4 
     1968            sage: G.radius(); G.diameter(); G.girth() 
     1969            2 
     1970            2 
     1971            4 
     1972        """ 
     1973        import networkx 
     1974        pos_dict = {} 
     1975        for i in range(5, 10): 
     1976            x = float(cos((pi / 2) + ((2 * pi) / 5) * i)) 
     1977            y = float(sin((pi / 2) + ((2 * pi) / 5) * i)) 
     1978            pos_dict[i] = (x, y) 
    18481979        for i in range(5): 
    1849             x = float(2*(cos((pi/2) + ((2*pi)/5)*(i-5)))) 
    1850             y = float(2*(sin((pi/2) + ((2*pi)/5)*(i-5)))) 
    1851             pos_dict[i] = (x,y) 
    1852         pos_dict[10] = (.5,0) 
    1853         pos_dict[11] = (-.5,0) 
    1854          
    1855         return graph.Graph(networkx.chvatal_graph(), pos=pos_dict, name="Chvatal Graph") 
    1856      
     1980            x = float(2 * (cos((pi / 2) + ((2 * pi) / 5) * (i - 5)))) 
     1981            y = float(2 * (sin((pi / 2) + ((2 * pi) / 5) * (i - 5)))) 
     1982            pos_dict[i] = (x, y) 
     1983        pos_dict[10] = (0.5, 0) 
     1984        pos_dict[11] = (-0.5, 0) 
     1985 
     1986        return graph.Graph(networkx.chvatal_graph(), pos=pos_dict, name="Chvatal graph") 
     1987 
    18571988    def DesarguesGraph(self): 
    18581989        """ 
    18591990        Returns the Desargues graph. 
    1860          
     1991 
    18611992        PLOTTING: The layout chosen is the same as on the cover of [1]. 
    1862          
    1863         EXAMPLE:: 
    1864          
     1993 
     1994        EXAMPLE:: 
     1995 
    18651996            sage: D = graphs.DesarguesGraph() 
    18661997            sage: L = graphs.LCFGraph(20,[5,-5,9,-9],5) 
    18671998            sage: D.is_isomorphic(L) 
    18681999            True 
    18692000            sage: D.show()  # long time 
    1870          
     2001 
    18712002        REFERENCE: 
    18722003 
    18732004        - [1] Harary, F. Graph Theory. Reading, MA: Addison-Wesley, 
     
    28302961        p = {'':(float(0),float(0))} 
    28312962        pn={} 
    28322963 
    2833         # construt recursively the adjacency dict and the positions 
     2964        # construct recursively the adjacency dict and the positions 
    28342965        for i in xrange(n): 
    28352966            ci = float(cos(i*theta)) 
    28362967            si = float(sin(i*theta)) 
     
    30643195        - Michael Yurko (2009-09-01) 
    30653196        """ 
    30663197        from sage.combinat.combination import Combinations 
    3067         # dictionnary associating the positions of the 1s to the corresponding 
     3198        # dictionary associating the positions of the 1s to the corresponding 
    30683199        # string: e.g. if n=6 and k=3, comb_to_str([0,1,4])=='110010' 
    30693200        comb_to_str={} 
    30703201        for c in Combinations(n,k): 
     
    31953326                    v[0], v[i] = v[i], v[0] 
    31963327            d["".join(v)] = tmp_dict 
    31973328        return graph.Graph(d, name = "%d-star"%n) 
    3198          
    3199     def BubbleSortGraph(self,n): 
    3200         r''' 
    3201         Returns the bubble sort graph, B(n). 
    3202          
     3329 
     3330    def BubbleSortGraph(self, n): 
     3331        r""" 
     3332        Returns the bubble sort graph `B(n)`. 
     3333 
    32033334        The vertices of the bubble sort graph are the set of permutations on 
    3204         n symbols. Two vertices are adjacent if one can be obtained from the 
    3205         other by swapping the labels in the ith and (i+1)th position for 
    3206         `1 \leq i \leq n-1`. 
    3207          
    3208         INPUT: 
    3209          
    3210         -  ``n``  
    3211           
    3212         EXAMPLES:: 
    3213          
    3214             sage: g = graphs.BubbleSortGraph(4) 
     3335        `n` symbols. Two vertices are adjacent if one can be obtained from the 
     3336        other by swapping the labels in the `i`-th and `(i+1)`-th positions for 
     3337        `1 \leq i \leq n-1`. In total, `B(n)` has order `n!`. Thus, the order 
     3338        of `B(n)` increases according to `f(n) = n!`. 
     3339 
     3340        INPUT: 
     3341 
     3342        - ``n`` -- positive integer. The number of symbols to permute. 
     3343 
     3344        OUTPUT: 
     3345 
     3346        The bubble sort graph `B(n)` on `n` symbols. If `n < 1`, a 
     3347        ``ValueError`` is returned. 
     3348 
     3349        EXAMPLES:: 
     3350 
     3351            sage: g = graphs.BubbleSortGraph(4); g 
     3352            Bubble sort: Graph on 24 vertices 
    32153353            sage: g.plot() # long time 
    3216              
    3217         AUTHORS: 
    3218          
     3354 
     3355        The bubble sort graph on `n = 1` symbol is the trivial graph `K_1`:: 
     3356 
     3357            sage: graphs.BubbleSortGraph(1) 
     3358            Bubble sort: Graph on 1 vertex 
     3359 
     3360        If `n \geq 1`, then the order of `B(n)` is `n!`:: 
     3361 
     3362            sage: n = randint(1, 8) 
     3363            sage: g = graphs.BubbleSortGraph(n) 
     3364            sage: g.order() == factorial(n) 
     3365            True 
     3366 
     3367        TESTS: 
     3368 
     3369        Input ``n`` must be positive:: 
     3370 
     3371            sage: graphs.BubbleSortGraph(0) 
     3372            Traceback (most recent call last): 
     3373            ... 
     3374            ValueError: Invalid number of symbols to permute, n should be >= 1 
     3375            sage: graphs.BubbleSortGraph(randint(-10^6, 0)) 
     3376            Traceback (most recent call last): 
     3377            ... 
     3378            ValueError: Invalid number of symbols to permute, n should be >= 1 
     3379 
     3380        AUTHORS: 
     3381 
    32193382        - Michael Yurko (2009-09-01) 
    3220  
    3221         ''' 
     3383        """ 
     3384        # sanity checks 
     3385        if n < 1: 
     3386            raise ValueError( 
     3387                "Invalid number of symbols to permute, n should be >= 1") 
     3388        if n == 1: 
     3389            return graph.Graph(self.CompleteGraph(n), name="Bubble sort") 
    32223390        from sage.combinat.permutation import Permutations 
    32233391        #create set from which to permute 
    3224         label_set = [str(i) for i in xrange(1,n+1)] 
     3392        label_set = [str(i) for i in xrange(1, n + 1)] 
    32253393        d = {} 
    32263394        #iterate through all vertices 
    32273395        for v in Permutations(label_set): 
    32283396            tmp_dict = {} 
    32293397            #add all adjacencies 
    3230             for i in xrange(n-1): 
     3398            for i in xrange(n - 1): 
    32313399                #swap entries 
    3232                 v[i], v[i+1] = v[i+1], v[i] 
     3400                v[i], v[i + 1] = v[i + 1], v[i] 
    32333401                #add new vertex 
    32343402                new_vert = ''.join(v) 
    32353403                tmp_dict[new_vert] = None 
    32363404                #swap back 
    3237                 v[i], v[i+1] = v[i+1], v[i] 
     3405                v[i], v[i + 1] = v[i + 1], v[i] 
    32383406            #add adjacency dict 
    32393407            d[''.join(v)] = tmp_dict 
    3240         return graph.Graph(d,name = "B(%d)"%n) 
    3241          
     3408        return graph.Graph(d, name="Bubble sort") 
     3409 
    32423410    def BalancedTree(self, r, h): 
    32433411        r""" 
    32443412        Returns the perfectly balanced tree of height `h \geq 1`, 
    32453413        whose root has degree `r \geq 2`. 
    3246          
     3414 
    32473415        The number of vertices of this graph is 
    32483416        `1 + r + r^2 + \cdots + r^h`, that is, 
    32493417        `\frac{r^{h+1} - 1}{r - 1}`. The number of edges is one 
    32503418        less than the number of vertices. 
    3251          
    3252         EXAMPLE: Plot a balanced tree of height 4 with r = 3 
    3253          
    3254         :: 
    3255          
     3419 
     3420        INPUT: 
     3421 
     3422        - ``r`` -- positive integer `\geq 2`. The degree of the root node. 
     3423 
     3424        - ``h`` -- positive integer `\geq 1`. The height of the balanced tree. 
     3425 
     3426        OUTPUT: 
     3427 
     3428        The perfectly balanced tree of height `h \geq 1` and whose root has 
     3429        degree `r \geq 2`. A ``NetworkXError`` is returned if `r < 2` or 
     3430        `h < 1`. 
     3431 
     3432        ALGORITHM: 
     3433 
     3434        Uses `NetworkX <http://networkx.lanl.gov>`_. 
     3435 
     3436        EXAMPLES: 
     3437 
     3438        A balanced tree whose root node has degree `r = 2`, and of height 
     3439        `h = 1`, has order 3 and size 2:: 
     3440 
     3441            sage: G = graphs.BalancedTree(2, 1); G 
     3442            Balanced tree: Graph on 3 vertices 
     3443            sage: G.order(); G.size() 
     3444            3 
     3445            2 
     3446            sage: r = 2; h = 1 
     3447            sage: v = 1 + r 
     3448            sage: v; v - 1 
     3449            3 
     3450            2 
     3451 
     3452        Plot a balanced tree of height 5, whose root node has degree `r = 3`:: 
     3453 
    32563454            sage: G = graphs.BalancedTree(3, 5) 
    32573455            sage: G.show()   # long time 
    3258         """ 
    3259         import networkx 
    3260         return graph.Graph(networkx.balanced_tree(r, h), name="Balanced Tree") 
     3456 
     3457        A tree is bipartite. If its vertex set is finite, then it is planar. :: 
     3458 
     3459            sage: r = randint(2, 5); h = randint(1, 7) 
     3460            sage: T = graphs.BalancedTree(r, h) 
     3461            sage: T.is_bipartite() 
     3462            True 
     3463            sage: T.is_planar() 
     3464            True 
     3465            sage: v = (r^(h + 1) - 1) / (r - 1) 
     3466            sage: T.order() == v 
     3467            True 
     3468            sage: T.size() == v - 1 
     3469            True 
     3470 
     3471        TESTS: 
     3472 
     3473        We only consider balanced trees whose root node has degree `r \geq 2`:: 
     3474 
     3475            sage: graphs.BalancedTree(1, randint(1, 10^6)) 
     3476            Traceback (most recent call last): 
     3477            ... 
     3478            NetworkXError: Invalid graph description, r should be >=2 
     3479            sage: graphs.BalancedTree(randint(-10^6, 1), randint(1, 10^6)) 
     3480            Traceback (most recent call last): 
     3481            ... 
     3482            NetworkXError: Invalid graph description, r should be >=2 
     3483 
     3484        The tree must have height `h \geq 1`:: 
     3485 
     3486            sage: graphs.BalancedTree(randint(2, 10^6), 0) 
     3487            Traceback (most recent call last): 
     3488            ... 
     3489            NetworkXError: Invalid graph description, h should be >=1 
     3490            sage: graphs.BalancedTree(randint(2, 10^6), randint(-10^6, 0)) 
     3491            Traceback (most recent call last): 
     3492            ... 
     3493            NetworkXError: Invalid graph description, h should be >=1 
     3494            sage: graphs.BalancedTree(randint(-10^6, 1), randint(-10^6, 0)) 
     3495            Traceback (most recent call last): 
     3496            ... 
     3497            NetworkXError: Invalid graph description, r should be >=2 
     3498        """ 
     3499        import networkx 
     3500        return graph.Graph(networkx.balanced_tree(r, h), name="Balanced tree") 
    32613501 
    32623502    def LCFGraph(self, n, shift_list, repeats): 
    32633503        """ 
    32643504        Returns the cubic graph specified in LCF notation. 
    3265          
     3505 
    32663506        LCF (Lederberg-Coxeter-Fruchte) notation is a concise way of 
    32673507        describing cubic Hamiltonian graphs. The way a graph is constructed 
    32683508        is as follows. Since there is a Hamiltonian cycle, we first create 
     
    44664706        - ``s_1`` -- list of integers corresponding to the degree 
    44674707          sequence of the first set. 
    44684708        - ``s_2`` -- list of integers corresponding to the degree 
    4469           sequence of the seccond set. 
     4709          sequence of the second set. 
    44704710 
    44714711        ALGORITHM: 
    44724712 
     
    44844724            sage: g.is_isomorphic(graphs.CompleteBipartiteGraph(5,2)) 
    44854725            True 
    44864726 
    4487         Some sequences being uncompatible if, for example, their sums 
     4727        Some sequences being incompatible if, for example, their sums 
    44884728        are different, the functions raises a ``ValueError`` when no 
    44894729        graph corresponding to the degree sequences exists. :: 
    44904730