Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/graphs/generators/platonic_solids.py
8817 views
1
# -*- coding: utf-8 -*-
2
r"""
3
Platonic solids
4
5
The methods defined here appear in :mod:`sage.graphs.graph_generators`.
6
7
"""
8
###########################################################################
9
#
10
# Copyright (C) 2006 Robert L. Miller <[email protected]>
11
# and Emily A. Kirkman
12
# Copyright (C) 2009 Michael C. Yurko <[email protected]>
13
#
14
# Distributed under the terms of the GNU General Public License (GPL)
15
# http://www.gnu.org/licenses/
16
###########################################################################
17
18
# import from Sage library
19
from sage.graphs.graph import Graph
20
from math import sin, cos, pi
21
22
def TetrahedralGraph():
23
"""
24
Returns a tetrahedral graph (with 4 nodes).
25
26
A tetrahedron is a 4-sided triangular pyramid. The tetrahedral
27
graph corresponds to the connectivity of the vertices of the
28
tetrahedron. This graph is equivalent to a wheel graph with 4 nodes
29
and also a complete graph on four nodes. (See examples below).
30
31
PLOTTING: The tetrahedral graph should be viewed in 3 dimensions.
32
We chose to use the default spring-layout algorithm here, so that
33
multiple iterations might yield a different point of reference for
34
the user. We hope to add rotatable, 3-dimensional viewing in the
35
future. In such a case, a string argument will be added to select
36
the flat spring-layout over a future implementation.
37
38
EXAMPLES: Construct and show a Tetrahedral graph
39
40
::
41
42
sage: g = graphs.TetrahedralGraph()
43
sage: g.show() # long time
44
45
The following example requires networkx::
46
47
sage: import networkx as NX
48
49
Compare this Tetrahedral, Wheel(4), Complete(4), and the
50
Tetrahedral plotted with the spring-layout algorithm below in a
51
Sage graphics array::
52
53
sage: tetra_pos = graphs.TetrahedralGraph()
54
sage: tetra_spring = Graph(NX.tetrahedral_graph())
55
sage: wheel = graphs.WheelGraph(4)
56
sage: complete = graphs.CompleteGraph(4)
57
sage: g = [tetra_pos, tetra_spring, wheel, complete]
58
sage: j = []
59
sage: for i in range(2):
60
....: n = []
61
....: for m in range(2):
62
....: n.append(g[i + m].plot(vertex_size=50, vertex_labels=False))
63
....: j.append(n)
64
sage: G = sage.plot.graphics.GraphicsArray(j)
65
sage: G.show() # long time
66
"""
67
import networkx
68
G = networkx.tetrahedral_graph()
69
return Graph(G, name="Tetrahedron", pos =
70
{ 0 : (0, 0),
71
1 : (0, 1),
72
2 : (cos(3.5*pi/3), sin(3.5*pi/3)),
73
3 : (cos(5.5*pi/3), sin(5.5*pi/3))}
74
)
75
76
def HexahedralGraph():
77
"""
78
Returns a hexahedral graph (with 8 nodes).
79
80
A regular hexahedron is a 6-sided cube. The hexahedral graph
81
corresponds to the connectivity of the vertices of the hexahedron.
82
This graph is equivalent to a 3-cube.
83
84
PLOTTING: The hexahedral graph should be viewed in 3 dimensions. We
85
chose to use the default spring-layout algorithm here, so that
86
multiple iterations might yield a different point of reference for
87
the user. We hope to add rotatable, 3-dimensional viewing in the
88
future. In such a case, a string argument will be added to select
89
the flat spring-layout over a future implementation.
90
91
EXAMPLES: Construct and show a Hexahedral graph
92
93
::
94
95
sage: g = graphs.HexahedralGraph()
96
sage: g.show() # long time
97
98
Create several hexahedral graphs in a Sage graphics array. They
99
will be drawn differently due to the use of the spring-layout
100
algorithm.
101
102
::
103
104
sage: g = []
105
sage: j = []
106
sage: for i in range(9):
107
....: k = graphs.HexahedralGraph()
108
....: g.append(k)
109
sage: for i in range(3):
110
....: n = []
111
....: for m in range(3):
112
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
113
....: j.append(n)
114
sage: G = sage.plot.graphics.GraphicsArray(j)
115
sage: G.show() # long time
116
"""
117
return Graph({0:[1,3,4], 1:[2,5], 2:[3,6], 3:[7], 4:[5,7], 5:[6], 6:[7]},
118
name="Hexahedron",
119
pos = {
120
0 : (0,0),
121
1 : (1,0),
122
3 : (0,1),
123
2 : (1,1),
124
4 : (.5,.5),
125
5 : (1.5,.5),
126
7 : (.5,1.5),
127
6 : (1.5,1.5)
128
})
129
130
def OctahedralGraph():
131
"""
132
Returns an Octahedral graph (with 6 nodes).
133
134
The regular octahedron is an 8-sided polyhedron with triangular
135
faces. The octahedral graph corresponds to the connectivity of the
136
vertices of the octahedron. It is the line graph of the tetrahedral
137
graph. The octahedral is symmetric, so the spring-layout algorithm
138
will be very effective for display.
139
140
PLOTTING: The Octahedral graph should be viewed in 3 dimensions. We
141
chose to use the default spring-layout algorithm here, so that
142
multiple iterations might yield a different point of reference for
143
the user. We hope to add rotatable, 3-dimensional viewing in the
144
future. In such a case, a string argument will be added to select
145
the flat spring-layout over a future implementation.
146
147
EXAMPLES: Construct and show an Octahedral graph
148
149
::
150
151
sage: g = graphs.OctahedralGraph()
152
sage: g.show() # long time
153
154
Create several octahedral graphs in a Sage graphics array They will
155
be drawn differently due to the use of the spring-layout algorithm
156
157
::
158
159
sage: g = []
160
sage: j = []
161
sage: for i in range(9):
162
....: k = graphs.OctahedralGraph()
163
....: g.append(k)
164
sage: for i in range(3):
165
....: n = []
166
....: for m in range(3):
167
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
168
....: j.append(n)
169
sage: G = sage.plot.graphics.GraphicsArray(j)
170
sage: G.show() # long time
171
"""
172
import networkx
173
G = networkx.octahedral_graph()
174
175
pos = {}
176
r1 = 5
177
r2 = 1
178
for i,v in enumerate([0,1,2]):
179
i = i + 0.75
180
pos[v] = (r1*cos(i*2*pi/3),r1*sin(i*2*pi/3))
181
182
for i,v in enumerate([4,3,5]):
183
i = i + .25
184
pos[v] = (r2*cos(i*2*pi/3),r2*sin(i*2*pi/3))
185
186
return Graph(G, name="Octahedron", pos=pos)
187
188
def IcosahedralGraph():
189
"""
190
Returns an Icosahedral graph (with 12 nodes).
191
192
The regular icosahedron is a 20-sided triangular polyhedron. The
193
icosahedral graph corresponds to the connectivity of the vertices
194
of the icosahedron. It is dual to the dodecahedral graph. The
195
icosahedron is symmetric, so the spring-layout algorithm will be
196
very effective for display.
197
198
PLOTTING: The Icosahedral graph should be viewed in 3 dimensions.
199
We chose to use the default spring-layout algorithm here, so that
200
multiple iterations might yield a different point of reference for
201
the user. We hope to add rotatable, 3-dimensional viewing in the
202
future. In such a case, a string argument will be added to select
203
the flat spring-layout over a future implementation.
204
205
EXAMPLES: Construct and show an Octahedral graph
206
207
::
208
209
sage: g = graphs.IcosahedralGraph()
210
sage: g.show() # long time
211
212
Create several icosahedral graphs in a Sage graphics array. They
213
will be drawn differently due to the use of the spring-layout
214
algorithm.
215
216
::
217
218
sage: g = []
219
sage: j = []
220
sage: for i in range(9):
221
....: k = graphs.IcosahedralGraph()
222
....: g.append(k)
223
sage: for i in range(3):
224
....: n = []
225
....: for m in range(3):
226
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
227
....: j.append(n)
228
sage: G = sage.plot.graphics.GraphicsArray(j)
229
sage: G.show() # long time
230
"""
231
import networkx
232
G = networkx.icosahedral_graph()
233
234
pos = {}
235
r1 = 5
236
r2 = 2
237
for i,v in enumerate([2,8,7,11,4,6]):
238
i = i + .5
239
pos[v] = (r1*cos(i*pi/3),r1*sin(i*pi/3))
240
241
for i,v in enumerate([1,9,0,10,5,3]):
242
i = i + .5
243
pos[v] = (r2*cos(i*pi/3),r2*sin(i*pi/3))
244
245
return Graph(G, name="Icosahedron", pos = pos)
246
247
def DodecahedralGraph():
248
"""
249
Returns a Dodecahedral graph (with 20 nodes)
250
251
The dodecahedral graph is cubic symmetric, so the spring-layout
252
algorithm will be very effective for display. It is dual to the
253
icosahedral graph.
254
255
PLOTTING: The Dodecahedral graph should be viewed in 3 dimensions.
256
We chose to use the default spring-layout algorithm here, so that
257
multiple iterations might yield a different point of reference for
258
the user. We hope to add rotatable, 3-dimensional viewing in the
259
future. In such a case, a string argument will be added to select
260
the flat spring-layout over a future implementation.
261
262
EXAMPLES: Construct and show a Dodecahedral graph
263
264
::
265
266
sage: g = graphs.DodecahedralGraph()
267
sage: g.show() # long time
268
269
Create several dodecahedral graphs in a Sage graphics array They
270
will be drawn differently due to the use of the spring-layout
271
algorithm
272
273
::
274
275
sage: g = []
276
sage: j = []
277
sage: for i in range(9):
278
....: k = graphs.DodecahedralGraph()
279
....: g.append(k)
280
sage: for i in range(3):
281
....: n = []
282
....: for m in range(3):
283
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
284
....: j.append(n)
285
sage: G = sage.plot.graphics.GraphicsArray(j)
286
sage: G.show() # long time
287
"""
288
import networkx
289
G = networkx.dodecahedral_graph()
290
291
pos = {}
292
r1 = 7
293
r2 = 4.7
294
r3 = 3.8
295
r4 = 1.5
296
297
for i,v in enumerate([19,0,1,2,3]):
298
i = i + .25
299
pos[v] = (r1*cos(i*2*pi/5),r1*sin(i*2*pi/5))
300
301
for i,v in enumerate([18,10,8,6,4]):
302
i = i + .25
303
pos[v] = (r2*cos(i*2*pi/5),r2*sin(i*2*pi/5))
304
305
for i,v in enumerate([17,11,9,7,5]):
306
i = i - .25
307
pos[v] = (r3*cos(i*2*pi/5),r3*sin(i*2*pi/5))
308
309
for i,v in enumerate([12,13,14,15,16]):
310
i = i + .75
311
pos[v] = (r4*cos(i*2*pi/5),r4*sin(i*2*pi/5))
312
313
return Graph(G, name="Dodecahedron", pos=pos)
314
315
316