Path: blob/master/src/sage/graphs/generators/platonic_solids.py
8817 views
# -*- coding: utf-8 -*-1r"""2Platonic solids34The methods defined here appear in :mod:`sage.graphs.graph_generators`.56"""7###########################################################################8#9# Copyright (C) 2006 Robert L. Miller <[email protected]>10# and Emily A. Kirkman11# Copyright (C) 2009 Michael C. Yurko <[email protected]>12#13# Distributed under the terms of the GNU General Public License (GPL)14# http://www.gnu.org/licenses/15###########################################################################1617# import from Sage library18from sage.graphs.graph import Graph19from math import sin, cos, pi2021def TetrahedralGraph():22"""23Returns a tetrahedral graph (with 4 nodes).2425A tetrahedron is a 4-sided triangular pyramid. The tetrahedral26graph corresponds to the connectivity of the vertices of the27tetrahedron. This graph is equivalent to a wheel graph with 4 nodes28and also a complete graph on four nodes. (See examples below).2930PLOTTING: The tetrahedral graph should be viewed in 3 dimensions.31We chose to use the default spring-layout algorithm here, so that32multiple iterations might yield a different point of reference for33the user. We hope to add rotatable, 3-dimensional viewing in the34future. In such a case, a string argument will be added to select35the flat spring-layout over a future implementation.3637EXAMPLES: Construct and show a Tetrahedral graph3839::4041sage: g = graphs.TetrahedralGraph()42sage: g.show() # long time4344The following example requires networkx::4546sage: import networkx as NX4748Compare this Tetrahedral, Wheel(4), Complete(4), and the49Tetrahedral plotted with the spring-layout algorithm below in a50Sage graphics array::5152sage: tetra_pos = graphs.TetrahedralGraph()53sage: tetra_spring = Graph(NX.tetrahedral_graph())54sage: wheel = graphs.WheelGraph(4)55sage: complete = graphs.CompleteGraph(4)56sage: g = [tetra_pos, tetra_spring, wheel, complete]57sage: j = []58sage: for i in range(2):59....: n = []60....: for m in range(2):61....: n.append(g[i + m].plot(vertex_size=50, vertex_labels=False))62....: j.append(n)63sage: G = sage.plot.graphics.GraphicsArray(j)64sage: G.show() # long time65"""66import networkx67G = networkx.tetrahedral_graph()68return Graph(G, name="Tetrahedron", pos =69{ 0 : (0, 0),701 : (0, 1),712 : (cos(3.5*pi/3), sin(3.5*pi/3)),723 : (cos(5.5*pi/3), sin(5.5*pi/3))}73)7475def HexahedralGraph():76"""77Returns a hexahedral graph (with 8 nodes).7879A regular hexahedron is a 6-sided cube. The hexahedral graph80corresponds to the connectivity of the vertices of the hexahedron.81This graph is equivalent to a 3-cube.8283PLOTTING: The hexahedral graph should be viewed in 3 dimensions. We84chose to use the default spring-layout algorithm here, so that85multiple iterations might yield a different point of reference for86the user. We hope to add rotatable, 3-dimensional viewing in the87future. In such a case, a string argument will be added to select88the flat spring-layout over a future implementation.8990EXAMPLES: Construct and show a Hexahedral graph9192::9394sage: g = graphs.HexahedralGraph()95sage: g.show() # long time9697Create several hexahedral graphs in a Sage graphics array. They98will be drawn differently due to the use of the spring-layout99algorithm.100101::102103sage: g = []104sage: j = []105sage: for i in range(9):106....: k = graphs.HexahedralGraph()107....: g.append(k)108sage: for i in range(3):109....: n = []110....: for m in range(3):111....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))112....: j.append(n)113sage: G = sage.plot.graphics.GraphicsArray(j)114sage: G.show() # long time115"""116return Graph({0:[1,3,4], 1:[2,5], 2:[3,6], 3:[7], 4:[5,7], 5:[6], 6:[7]},117name="Hexahedron",118pos = {1190 : (0,0),1201 : (1,0),1213 : (0,1),1222 : (1,1),1234 : (.5,.5),1245 : (1.5,.5),1257 : (.5,1.5),1266 : (1.5,1.5)127})128129def OctahedralGraph():130"""131Returns an Octahedral graph (with 6 nodes).132133The regular octahedron is an 8-sided polyhedron with triangular134faces. The octahedral graph corresponds to the connectivity of the135vertices of the octahedron. It is the line graph of the tetrahedral136graph. The octahedral is symmetric, so the spring-layout algorithm137will be very effective for display.138139PLOTTING: The Octahedral graph should be viewed in 3 dimensions. We140chose to use the default spring-layout algorithm here, so that141multiple iterations might yield a different point of reference for142the user. We hope to add rotatable, 3-dimensional viewing in the143future. In such a case, a string argument will be added to select144the flat spring-layout over a future implementation.145146EXAMPLES: Construct and show an Octahedral graph147148::149150sage: g = graphs.OctahedralGraph()151sage: g.show() # long time152153Create several octahedral graphs in a Sage graphics array They will154be drawn differently due to the use of the spring-layout algorithm155156::157158sage: g = []159sage: j = []160sage: for i in range(9):161....: k = graphs.OctahedralGraph()162....: g.append(k)163sage: for i in range(3):164....: n = []165....: for m in range(3):166....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))167....: j.append(n)168sage: G = sage.plot.graphics.GraphicsArray(j)169sage: G.show() # long time170"""171import networkx172G = networkx.octahedral_graph()173174pos = {}175r1 = 5176r2 = 1177for i,v in enumerate([0,1,2]):178i = i + 0.75179pos[v] = (r1*cos(i*2*pi/3),r1*sin(i*2*pi/3))180181for i,v in enumerate([4,3,5]):182i = i + .25183pos[v] = (r2*cos(i*2*pi/3),r2*sin(i*2*pi/3))184185return Graph(G, name="Octahedron", pos=pos)186187def IcosahedralGraph():188"""189Returns an Icosahedral graph (with 12 nodes).190191The regular icosahedron is a 20-sided triangular polyhedron. The192icosahedral graph corresponds to the connectivity of the vertices193of the icosahedron. It is dual to the dodecahedral graph. The194icosahedron is symmetric, so the spring-layout algorithm will be195very effective for display.196197PLOTTING: The Icosahedral graph should be viewed in 3 dimensions.198We chose to use the default spring-layout algorithm here, so that199multiple iterations might yield a different point of reference for200the user. We hope to add rotatable, 3-dimensional viewing in the201future. In such a case, a string argument will be added to select202the flat spring-layout over a future implementation.203204EXAMPLES: Construct and show an Octahedral graph205206::207208sage: g = graphs.IcosahedralGraph()209sage: g.show() # long time210211Create several icosahedral graphs in a Sage graphics array. They212will be drawn differently due to the use of the spring-layout213algorithm.214215::216217sage: g = []218sage: j = []219sage: for i in range(9):220....: k = graphs.IcosahedralGraph()221....: g.append(k)222sage: for i in range(3):223....: n = []224....: for m in range(3):225....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))226....: j.append(n)227sage: G = sage.plot.graphics.GraphicsArray(j)228sage: G.show() # long time229"""230import networkx231G = networkx.icosahedral_graph()232233pos = {}234r1 = 5235r2 = 2236for i,v in enumerate([2,8,7,11,4,6]):237i = i + .5238pos[v] = (r1*cos(i*pi/3),r1*sin(i*pi/3))239240for i,v in enumerate([1,9,0,10,5,3]):241i = i + .5242pos[v] = (r2*cos(i*pi/3),r2*sin(i*pi/3))243244return Graph(G, name="Icosahedron", pos = pos)245246def DodecahedralGraph():247"""248Returns a Dodecahedral graph (with 20 nodes)249250The dodecahedral graph is cubic symmetric, so the spring-layout251algorithm will be very effective for display. It is dual to the252icosahedral graph.253254PLOTTING: The Dodecahedral graph should be viewed in 3 dimensions.255We chose to use the default spring-layout algorithm here, so that256multiple iterations might yield a different point of reference for257the user. We hope to add rotatable, 3-dimensional viewing in the258future. In such a case, a string argument will be added to select259the flat spring-layout over a future implementation.260261EXAMPLES: Construct and show a Dodecahedral graph262263::264265sage: g = graphs.DodecahedralGraph()266sage: g.show() # long time267268Create several dodecahedral graphs in a Sage graphics array They269will be drawn differently due to the use of the spring-layout270algorithm271272::273274sage: g = []275sage: j = []276sage: for i in range(9):277....: k = graphs.DodecahedralGraph()278....: g.append(k)279sage: for i in range(3):280....: n = []281....: for m in range(3):282....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))283....: j.append(n)284sage: G = sage.plot.graphics.GraphicsArray(j)285sage: G.show() # long time286"""287import networkx288G = networkx.dodecahedral_graph()289290pos = {}291r1 = 7292r2 = 4.7293r3 = 3.8294r4 = 1.5295296for i,v in enumerate([19,0,1,2,3]):297i = i + .25298pos[v] = (r1*cos(i*2*pi/5),r1*sin(i*2*pi/5))299300for i,v in enumerate([18,10,8,6,4]):301i = i + .25302pos[v] = (r2*cos(i*2*pi/5),r2*sin(i*2*pi/5))303304for i,v in enumerate([17,11,9,7,5]):305i = i - .25306pos[v] = (r3*cos(i*2*pi/5),r3*sin(i*2*pi/5))307308for i,v in enumerate([12,13,14,15,16]):309i = i + .75310pos[v] = (r4*cos(i*2*pi/5),r4*sin(i*2*pi/5))311312return Graph(G, name="Dodecahedron", pos=pos)313314315316