Ticket #7536: trac_7536.patch

File trac_7536.patch, 3.5 KB (added by ncohen, 11 years ago)
  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1259507882 -3600
    # Node ID 785fec521c35cccbff80cf8c389f8ad677c8ac93
    # Parent  fee71ad457d0b6881f989cbe69b9bfe2eee0cd55
    Toroidal Grid in graph_generators
    
    diff -r fee71ad457d0 -r 785fec521c35 sage/graphs/graph_generators.py
    a b  
    5959        - LollipopGraph
    6060        - PathGraph
    6161        - StarGraph
     62        - ToroidalGrid2dGraph
    6263        - WheelGraph
    6364    Platonic Solids:
    6465        - TetrahedralGraph
     
    192193                    - LollipopGraph
    193194                    - PathGraph
    194195                    - StarGraph
     196                    - ToroidalGrid2dGraph
    195197                    - WheelGraph
    196198                Platonic Solids:
    197199                    - TetrahedralGraph
     
    764766        """
    765767        return graph.Graph(sparse=True)
    766768
     769    def ToroidalGrid2dGraph(self,n1,n2):
     770        r"""
     771        Returns a toroidal 2-dimensional grid graph with `n_1n_2` nodes
     772        (`n_1` rows and `n_2` columns).
     773
     774        The toroidal 2-dimensional grid with parameters `n_1,n_2` is
     775        the 2-dimensional grid graph with identital parameters
     776        to which are added the edges `((i,0),(i,n_2-1))` and
     777        `((0,i),(n_1-1,i))`.
     778
     779        EXAMPLE:
     780
     781        The toroidal 2-dimensional grid is a regular graph, while
     782        the usual 2-dimensional grid is not ::
     783
     784            sage: tgrid = graphs.ToroidalGrid2dGraph(8,9)
     785            sage: print tgrid
     786            Toroidal 2D Grid Graph with parameters 8,9
     787            sage: grid = graphs.Grid2dGraph(8,9)
     788            sage: grid.is_regular()
     789            False
     790            sage: tgrid.is_regular()
     791            True
     792        """
     793
     794        g = self.Grid2dGraph(n1,n2)
     795
     796        g.add_edges([((i,0),(i,n2-1)) for i in range(n1)] + [((0,i),(n1-1,i)) for i in range(n2)])
     797
     798        g.name("Toroidal 2D Grid Graph with parameters "+str(n1)+","+str(n2))
     799
     800        return g
     801       
    767802    def Grid2dGraph(self, n1, n2):
    768         """
    769         Returns a 2-dimensional grid graph with n1\*n2 nodes (n1 rows and
    770         n2 columns).
    771        
    772         A 2d grid graph resembles a 2 dimensional grid. All inner nodes are
    773         connected to their 4 neighbors. Outer (non-corner) nodes are
    774         connected to their 3 neighbors. Corner nodes are connected to their
     803        r"""
     804        Returns a `2`-dimensional grid graph with `n_1n_2` nodes (`n_1` rows and
     805        `n_2` columns).
     806       
     807        A 2d grid graph resembles a `2` dimensional grid. All inner nodes are
     808        connected to their `4` neighbors. Outer (non-corner) nodes are
     809        connected to their `3` neighbors. Corner nodes are connected to their
    775810        2 neighbors.
    776811       
    777812        This constructor depends on NetworkX numeric labels.
    778813       
    779814        PLOTTING: Upon construction, the position dictionary is filled to
    780815        override the spring-layout algorithm. By convention, nodes are
    781         labelled in (row, column) pairs with (0, 0) in the top left corner.
     816        labelled in (row, column) pairs with `(0, 0)` in the top left corner.
    782817        Edges will always be horizontal and vertical - another advantage of
    783818        filling the position dictionary.
    784819       
    785         EXAMPLES: Construct and show a grid 2d graph Rows = 5, Columns = 7
     820        EXAMPLES: Construct and show a grid 2d graph Rows = `5`, Columns = `7`
    786821       
    787822        ::
    788823