# 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
|
|
59 | 59 | - LollipopGraph |
60 | 60 | - PathGraph |
61 | 61 | - StarGraph |
| 62 | - ToroidalGrid2dGraph |
62 | 63 | - WheelGraph |
63 | 64 | Platonic Solids: |
64 | 65 | - TetrahedralGraph |
… |
… |
|
192 | 193 | - LollipopGraph |
193 | 194 | - PathGraph |
194 | 195 | - StarGraph |
| 196 | - ToroidalGrid2dGraph |
195 | 197 | - WheelGraph |
196 | 198 | Platonic Solids: |
197 | 199 | - TetrahedralGraph |
… |
… |
|
764 | 766 | """ |
765 | 767 | return graph.Graph(sparse=True) |
766 | 768 | |
| 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 | |
767 | 802 | 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 |
775 | 810 | 2 neighbors. |
776 | 811 | |
777 | 812 | This constructor depends on NetworkX numeric labels. |
778 | 813 | |
779 | 814 | PLOTTING: Upon construction, the position dictionary is filled to |
780 | 815 | 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. |
782 | 817 | Edges will always be horizontal and vertical - another advantage of |
783 | 818 | filling the position dictionary. |
784 | 819 | |
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` |
786 | 821 | |
787 | 822 | :: |
788 | 823 | |