small.py 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. """
  2. Various small and named graphs, together with some compact generators.
  3. """
  4. __all__ = [
  5. "LCF_graph",
  6. "bull_graph",
  7. "chvatal_graph",
  8. "cubical_graph",
  9. "desargues_graph",
  10. "diamond_graph",
  11. "dodecahedral_graph",
  12. "frucht_graph",
  13. "generalized_petersen_graph",
  14. "heawood_graph",
  15. "hoffman_singleton_graph",
  16. "house_graph",
  17. "house_x_graph",
  18. "icosahedral_graph",
  19. "krackhardt_kite_graph",
  20. "moebius_kantor_graph",
  21. "octahedral_graph",
  22. "pappus_graph",
  23. "petersen_graph",
  24. "sedgewick_maze_graph",
  25. "tetrahedral_graph",
  26. "truncated_cube_graph",
  27. "truncated_tetrahedron_graph",
  28. "tutte_graph",
  29. ]
  30. from functools import wraps
  31. import networkx as nx
  32. from networkx.exception import NetworkXError
  33. from networkx.generators.classic import (
  34. complete_graph,
  35. cycle_graph,
  36. empty_graph,
  37. path_graph,
  38. )
  39. def _raise_on_directed(func):
  40. """
  41. A decorator which inspects the `create_using` argument and raises a
  42. NetworkX exception when `create_using` is a DiGraph (class or instance) for
  43. graph generators that do not support directed outputs.
  44. `create_using` may be a keyword argument or the first positional argument.
  45. """
  46. @wraps(func)
  47. def wrapper(*args, **kwargs):
  48. create_using = args[0] if args else kwargs.get("create_using")
  49. if create_using is not None:
  50. G = nx.empty_graph(create_using=create_using)
  51. if G.is_directed():
  52. raise NetworkXError("Directed Graph not supported in create_using")
  53. return func(*args, **kwargs)
  54. return wrapper
  55. @nx._dispatchable(graphs=None, returns_graph=True)
  56. def LCF_graph(n, shift_list, repeats, create_using=None):
  57. """
  58. Return the cubic graph specified in LCF notation.
  59. LCF (Lederberg-Coxeter-Fruchte) notation[1]_ is a compressed
  60. notation used in the generation of various cubic Hamiltonian
  61. graphs of high symmetry. See, for example, `dodecahedral_graph`,
  62. `desargues_graph`, `heawood_graph` and `pappus_graph`.
  63. Nodes are drawn from ``range(n)``. Each node ``n_i`` is connected with
  64. node ``n_i + shift % n`` where ``shift`` is given by cycling through
  65. the input `shift_list` `repeat` s times.
  66. Parameters
  67. ----------
  68. n : int
  69. The starting graph is the `n`-cycle with nodes ``0, ..., n-1``.
  70. The null graph is returned if `n` < 1.
  71. shift_list : list
  72. A list of integer shifts mod `n`, ``[s1, s2, .., sk]``
  73. repeats : int
  74. Integer specifying the number of times that shifts in `shift_list`
  75. are successively applied to each current node in the n-cycle
  76. to generate an edge between ``n_current`` and ``n_current + shift mod n``.
  77. Returns
  78. -------
  79. G : Graph
  80. A graph instance created from the specified LCF notation.
  81. Examples
  82. --------
  83. The utility graph $K_{3,3}$
  84. >>> G = nx.LCF_graph(6, [3, -3], 3)
  85. >>> G.edges()
  86. EdgeView([(0, 1), (0, 5), (0, 3), (1, 2), (1, 4), (2, 3), (2, 5), (3, 4), (4, 5)])
  87. The Heawood graph:
  88. >>> G = nx.LCF_graph(14, [5, -5], 7)
  89. >>> nx.is_isomorphic(G, nx.heawood_graph())
  90. True
  91. References
  92. ----------
  93. .. [1] https://en.wikipedia.org/wiki/LCF_notation
  94. """
  95. if n <= 0:
  96. return empty_graph(0, create_using)
  97. # start with the n-cycle
  98. G = cycle_graph(n, create_using)
  99. if G.is_directed():
  100. raise NetworkXError("Directed Graph not supported")
  101. G.name = "LCF_graph"
  102. nodes = sorted(G)
  103. n_extra_edges = repeats * len(shift_list)
  104. # edges are added n_extra_edges times
  105. # (not all of these need be new)
  106. if n_extra_edges < 1:
  107. return G
  108. for i in range(n_extra_edges):
  109. shift = shift_list[i % len(shift_list)] # cycle through shift_list
  110. v1 = nodes[i % n] # cycle repeatedly through nodes
  111. v2 = nodes[(i + shift) % n]
  112. G.add_edge(v1, v2)
  113. return G
  114. # -------------------------------------------------------------------------------
  115. # Various small and named graphs
  116. # -------------------------------------------------------------------------------
  117. @_raise_on_directed
  118. @nx._dispatchable(graphs=None, returns_graph=True)
  119. def bull_graph(create_using=None):
  120. """
  121. Returns the Bull Graph
  122. The Bull Graph has 5 nodes and 5 edges. It is a planar undirected
  123. graph in the form of a triangle with two disjoint pendant edges [1]_
  124. The name comes from the triangle and pendant edges representing
  125. respectively the body and legs of a bull.
  126. Parameters
  127. ----------
  128. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  129. Graph type to create. If graph instance, then cleared before populated.
  130. Returns
  131. -------
  132. G : networkx Graph
  133. A bull graph with 5 nodes
  134. References
  135. ----------
  136. .. [1] https://en.wikipedia.org/wiki/Bull_graph.
  137. """
  138. G = nx.from_dict_of_lists(
  139. {0: [1, 2], 1: [0, 2, 3], 2: [0, 1, 4], 3: [1], 4: [2]},
  140. create_using=create_using,
  141. )
  142. G.name = "Bull Graph"
  143. return G
  144. @_raise_on_directed
  145. @nx._dispatchable(graphs=None, returns_graph=True)
  146. def chvatal_graph(create_using=None):
  147. """
  148. Returns the Chvátal Graph
  149. The Chvátal Graph is an undirected graph with 12 nodes and 24 edges [1]_.
  150. It has 370 distinct (directed) Hamiltonian cycles, giving a unique generalized
  151. LCF notation of order 4, two of order 6 , and 43 of order 1 [2]_.
  152. Parameters
  153. ----------
  154. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  155. Graph type to create. If graph instance, then cleared before populated.
  156. Returns
  157. -------
  158. G : networkx Graph
  159. The Chvátal graph with 12 nodes and 24 edges
  160. References
  161. ----------
  162. .. [1] https://en.wikipedia.org/wiki/Chv%C3%A1tal_graph
  163. .. [2] https://mathworld.wolfram.com/ChvatalGraph.html
  164. """
  165. G = nx.from_dict_of_lists(
  166. {
  167. 0: [1, 4, 6, 9],
  168. 1: [2, 5, 7],
  169. 2: [3, 6, 8],
  170. 3: [4, 7, 9],
  171. 4: [5, 8],
  172. 5: [10, 11],
  173. 6: [10, 11],
  174. 7: [8, 11],
  175. 8: [10],
  176. 9: [10, 11],
  177. },
  178. create_using=create_using,
  179. )
  180. G.name = "Chvatal Graph"
  181. return G
  182. @_raise_on_directed
  183. @nx._dispatchable(graphs=None, returns_graph=True)
  184. def cubical_graph(create_using=None):
  185. """
  186. Returns the 3-regular Platonic Cubical Graph
  187. The skeleton of the cube (the nodes and edges) form a graph, with 8
  188. nodes, and 12 edges. It is a special case of the hypercube graph.
  189. It is one of 5 Platonic graphs, each a skeleton of its
  190. Platonic solid [1]_.
  191. Such graphs arise in parallel processing in computers.
  192. Parameters
  193. ----------
  194. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  195. Graph type to create. If graph instance, then cleared before populated.
  196. Returns
  197. -------
  198. G : networkx Graph
  199. A cubical graph with 8 nodes and 12 edges
  200. See Also
  201. --------
  202. tetrahedral_graph, octahedral_graph, dodecahedral_graph, icosahedral_graph
  203. References
  204. ----------
  205. .. [1] https://en.wikipedia.org/wiki/Cube#Cubical_graph
  206. """
  207. G = nx.from_dict_of_lists(
  208. {
  209. 0: [1, 3, 4],
  210. 1: [0, 2, 7],
  211. 2: [1, 3, 6],
  212. 3: [0, 2, 5],
  213. 4: [0, 5, 7],
  214. 5: [3, 4, 6],
  215. 6: [2, 5, 7],
  216. 7: [1, 4, 6],
  217. },
  218. create_using=create_using,
  219. )
  220. G.name = "Platonic Cubical Graph"
  221. return G
  222. @nx._dispatchable(graphs=None, returns_graph=True)
  223. def desargues_graph(create_using=None):
  224. """
  225. Returns the Desargues Graph
  226. The Desargues Graph is a non-planar, distance-transitive cubic graph
  227. with 20 nodes and 30 edges [1]_. It is isomorphic to the Generalized
  228. Petersen Graph GP(10, 3). It is a symmetric graph. It can be represented
  229. in LCF notation as [5,-5,9,-9]^5 [2]_.
  230. Parameters
  231. ----------
  232. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  233. Graph type to create. If graph instance, then cleared before populated.
  234. Returns
  235. -------
  236. G : networkx Graph
  237. Desargues Graph with 20 nodes and 30 edges
  238. References
  239. ----------
  240. .. [1] https://en.wikipedia.org/wiki/Desargues_graph
  241. .. [2] https://mathworld.wolfram.com/DesarguesGraph.html
  242. """
  243. G = LCF_graph(20, [5, -5, 9, -9], 5, create_using)
  244. G.name = "Desargues Graph"
  245. return G
  246. @_raise_on_directed
  247. @nx._dispatchable(graphs=None, returns_graph=True)
  248. def diamond_graph(create_using=None):
  249. """
  250. Returns the Diamond graph
  251. The Diamond Graph is planar undirected graph with 4 nodes and 5 edges.
  252. It is also sometimes known as the double triangle graph or kite graph [1]_.
  253. Parameters
  254. ----------
  255. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  256. Graph type to create. If graph instance, then cleared before populated.
  257. Returns
  258. -------
  259. G : networkx Graph
  260. Diamond Graph with 4 nodes and 5 edges
  261. References
  262. ----------
  263. .. [1] https://mathworld.wolfram.com/DiamondGraph.html
  264. """
  265. G = nx.from_dict_of_lists(
  266. {0: [1, 2], 1: [0, 2, 3], 2: [0, 1, 3], 3: [1, 2]}, create_using=create_using
  267. )
  268. G.name = "Diamond Graph"
  269. return G
  270. @nx._dispatchable(graphs=None, returns_graph=True)
  271. def dodecahedral_graph(create_using=None):
  272. """
  273. Returns the Platonic Dodecahedral graph.
  274. The dodecahedral graph has 20 nodes and 30 edges. The skeleton of the
  275. dodecahedron forms a graph. It is one of 5 Platonic graphs [1]_.
  276. It can be described in LCF notation as:
  277. ``[10, 7, 4, -4, -7, 10, -4, 7, -7, 4]^2`` [2]_.
  278. Parameters
  279. ----------
  280. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  281. Graph type to create. If graph instance, then cleared before populated.
  282. Returns
  283. -------
  284. G : networkx Graph
  285. Dodecahedral Graph with 20 nodes and 30 edges
  286. See Also
  287. --------
  288. tetrahedral_graph, cubical_graph, octahedral_graph, icosahedral_graph
  289. References
  290. ----------
  291. .. [1] https://en.wikipedia.org/wiki/Regular_dodecahedron#Dodecahedral_graph
  292. .. [2] https://mathworld.wolfram.com/DodecahedralGraph.html
  293. """
  294. G = LCF_graph(20, [10, 7, 4, -4, -7, 10, -4, 7, -7, 4], 2, create_using)
  295. G.name = "Dodecahedral Graph"
  296. return G
  297. @nx._dispatchable(graphs=None, returns_graph=True)
  298. def frucht_graph(create_using=None):
  299. """
  300. Returns the Frucht Graph.
  301. The Frucht Graph is the smallest cubical graph whose
  302. automorphism group consists only of the identity element [1]_.
  303. It has 12 nodes and 18 edges and no nontrivial symmetries.
  304. It is planar and Hamiltonian [2]_.
  305. Parameters
  306. ----------
  307. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  308. Graph type to create. If graph instance, then cleared before populated.
  309. Returns
  310. -------
  311. G : networkx Graph
  312. Frucht Graph with 12 nodes and 18 edges
  313. References
  314. ----------
  315. .. [1] https://en.wikipedia.org/wiki/Frucht_graph
  316. .. [2] https://mathworld.wolfram.com/FruchtGraph.html
  317. """
  318. G = cycle_graph(7, create_using)
  319. G.add_edges_from(
  320. [
  321. [0, 7],
  322. [1, 7],
  323. [2, 8],
  324. [3, 9],
  325. [4, 9],
  326. [5, 10],
  327. [6, 10],
  328. [7, 11],
  329. [8, 11],
  330. [8, 9],
  331. [10, 11],
  332. ]
  333. )
  334. G.name = "Frucht Graph"
  335. return G
  336. @nx._dispatchable(graphs=None, returns_graph=True)
  337. def heawood_graph(create_using=None):
  338. """
  339. Returns the Heawood Graph, a (3,6) cage.
  340. The Heawood Graph is an undirected graph with 14 nodes and 21 edges,
  341. named after Percy John Heawood [1]_.
  342. It is cubic symmetric, nonplanar, Hamiltonian, and can be represented
  343. in LCF notation as ``[5,-5]^7`` [2]_.
  344. It is the unique (3,6)-cage: the regular cubic graph of girth 6 with
  345. minimal number of vertices [3]_.
  346. Parameters
  347. ----------
  348. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  349. Graph type to create. If graph instance, then cleared before populated.
  350. Returns
  351. -------
  352. G : networkx Graph
  353. Heawood Graph with 14 nodes and 21 edges
  354. References
  355. ----------
  356. .. [1] https://en.wikipedia.org/wiki/Heawood_graph
  357. .. [2] https://mathworld.wolfram.com/HeawoodGraph.html
  358. .. [3] https://www.win.tue.nl/~aeb/graphs/Heawood.html
  359. """
  360. G = LCF_graph(14, [5, -5], 7, create_using)
  361. G.name = "Heawood Graph"
  362. return G
  363. @nx._dispatchable(graphs=None, returns_graph=True)
  364. def hoffman_singleton_graph():
  365. """
  366. Returns the Hoffman-Singleton Graph.
  367. The Hoffman–Singleton graph is a symmetrical undirected graph
  368. with 50 nodes and 175 edges.
  369. All indices lie in ``Z % 5``: that is, the integers mod 5 [1]_.
  370. It is the only regular graph of vertex degree 7, diameter 2, and girth 5.
  371. It is the unique (7,5)-cage graph and Moore graph, and contains many
  372. copies of the Petersen Graph [2]_.
  373. Returns
  374. -------
  375. G : networkx Graph
  376. Hoffman–Singleton Graph with 50 nodes and 175 edges
  377. Notes
  378. -----
  379. Constructed from pentagon and pentagram as follows: Take five pentagons $P_h$
  380. and five pentagrams $Q_i$ . Join vertex $j$ of $P_h$ to vertex $h·i+j$ of $Q_i$ [3]_.
  381. References
  382. ----------
  383. .. [1] https://blogs.ams.org/visualinsight/2016/02/01/hoffman-singleton-graph/
  384. .. [2] https://mathworld.wolfram.com/Hoffman-SingletonGraph.html
  385. .. [3] https://en.wikipedia.org/wiki/Hoffman%E2%80%93Singleton_graph
  386. """
  387. G = nx.Graph()
  388. for i in range(5):
  389. for j in range(5):
  390. G.add_edge(("pentagon", i, j), ("pentagon", i, (j - 1) % 5))
  391. G.add_edge(("pentagon", i, j), ("pentagon", i, (j + 1) % 5))
  392. G.add_edge(("pentagram", i, j), ("pentagram", i, (j - 2) % 5))
  393. G.add_edge(("pentagram", i, j), ("pentagram", i, (j + 2) % 5))
  394. for k in range(5):
  395. G.add_edge(("pentagon", i, j), ("pentagram", k, (i * k + j) % 5))
  396. G = nx.convert_node_labels_to_integers(G)
  397. G.name = "Hoffman-Singleton Graph"
  398. return G
  399. @_raise_on_directed
  400. @nx._dispatchable(graphs=None, returns_graph=True)
  401. def house_graph(create_using=None):
  402. """
  403. Returns the House graph (square with triangle on top)
  404. The house graph is a simple undirected graph with
  405. 5 nodes and 6 edges [1]_.
  406. Parameters
  407. ----------
  408. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  409. Graph type to create. If graph instance, then cleared before populated.
  410. Returns
  411. -------
  412. G : networkx Graph
  413. House graph in the form of a square with a triangle on top
  414. References
  415. ----------
  416. .. [1] https://mathworld.wolfram.com/HouseGraph.html
  417. """
  418. G = nx.from_dict_of_lists(
  419. {0: [1, 2], 1: [0, 3], 2: [0, 3, 4], 3: [1, 2, 4], 4: [2, 3]},
  420. create_using=create_using,
  421. )
  422. G.name = "House Graph"
  423. return G
  424. @_raise_on_directed
  425. @nx._dispatchable(graphs=None, returns_graph=True)
  426. def house_x_graph(create_using=None):
  427. """
  428. Returns the House graph with a cross inside the house square.
  429. The House X-graph is the House graph plus the two edges connecting diagonally
  430. opposite vertices of the square base. It is also one of the two graphs
  431. obtained by removing two edges from the pentatope graph [1]_.
  432. Parameters
  433. ----------
  434. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  435. Graph type to create. If graph instance, then cleared before populated.
  436. Returns
  437. -------
  438. G : networkx Graph
  439. House graph with diagonal vertices connected
  440. References
  441. ----------
  442. .. [1] https://mathworld.wolfram.com/HouseGraph.html
  443. """
  444. G = house_graph(create_using)
  445. G.add_edges_from([(0, 3), (1, 2)])
  446. G.name = "House-with-X-inside Graph"
  447. return G
  448. @_raise_on_directed
  449. @nx._dispatchable(graphs=None, returns_graph=True)
  450. def icosahedral_graph(create_using=None):
  451. """
  452. Returns the Platonic Icosahedral graph.
  453. The icosahedral graph has 12 nodes and 30 edges. It is a Platonic graph
  454. whose nodes have the connectivity of the icosahedron. It is undirected,
  455. regular and Hamiltonian [1]_.
  456. Parameters
  457. ----------
  458. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  459. Graph type to create. If graph instance, then cleared before populated.
  460. Returns
  461. -------
  462. G : networkx Graph
  463. Icosahedral graph with 12 nodes and 30 edges.
  464. See Also
  465. --------
  466. tetrahedral_graph, cubical_graph, octahedral_graph, dodecahedral_graph
  467. References
  468. ----------
  469. .. [1] https://mathworld.wolfram.com/IcosahedralGraph.html
  470. """
  471. G = nx.from_dict_of_lists(
  472. {
  473. 0: [1, 5, 7, 8, 11],
  474. 1: [2, 5, 6, 8],
  475. 2: [3, 6, 8, 9],
  476. 3: [4, 6, 9, 10],
  477. 4: [5, 6, 10, 11],
  478. 5: [6, 11],
  479. 7: [8, 9, 10, 11],
  480. 8: [9],
  481. 9: [10],
  482. 10: [11],
  483. },
  484. create_using=create_using,
  485. )
  486. G.name = "Platonic Icosahedral Graph"
  487. return G
  488. @_raise_on_directed
  489. @nx._dispatchable(graphs=None, returns_graph=True)
  490. def krackhardt_kite_graph(create_using=None):
  491. """
  492. Returns the Krackhardt Kite Social Network.
  493. A 10 actor social network introduced by David Krackhardt
  494. to illustrate different centrality measures [1]_.
  495. Parameters
  496. ----------
  497. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  498. Graph type to create. If graph instance, then cleared before populated.
  499. Returns
  500. -------
  501. G : networkx Graph
  502. Krackhardt Kite graph with 10 nodes and 18 edges
  503. Notes
  504. -----
  505. The traditional labeling is:
  506. Andre=1, Beverley=2, Carol=3, Diane=4,
  507. Ed=5, Fernando=6, Garth=7, Heather=8, Ike=9, Jane=10.
  508. References
  509. ----------
  510. .. [1] Krackhardt, David. "Assessing the Political Landscape: Structure,
  511. Cognition, and Power in Organizations". Administrative Science Quarterly.
  512. 35 (2): 342–369. doi:10.2307/2393394. JSTOR 2393394. June 1990.
  513. """
  514. G = nx.from_dict_of_lists(
  515. {
  516. 0: [1, 2, 3, 5],
  517. 1: [0, 3, 4, 6],
  518. 2: [0, 3, 5],
  519. 3: [0, 1, 2, 4, 5, 6],
  520. 4: [1, 3, 6],
  521. 5: [0, 2, 3, 6, 7],
  522. 6: [1, 3, 4, 5, 7],
  523. 7: [5, 6, 8],
  524. 8: [7, 9],
  525. 9: [8],
  526. },
  527. create_using=create_using,
  528. )
  529. G.name = "Krackhardt Kite Social Network"
  530. return G
  531. @nx._dispatchable(graphs=None, returns_graph=True)
  532. def moebius_kantor_graph(create_using=None):
  533. """
  534. Returns the Moebius-Kantor graph.
  535. The Möbius-Kantor graph is the cubic symmetric graph on 16 nodes.
  536. Its LCF notation is [5,-5]^8, and it is isomorphic to the generalized
  537. Petersen Graph GP(8, 3) [1]_.
  538. Parameters
  539. ----------
  540. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  541. Graph type to create. If graph instance, then cleared before populated.
  542. Returns
  543. -------
  544. G : networkx Graph
  545. Moebius-Kantor graph
  546. References
  547. ----------
  548. .. [1] https://en.wikipedia.org/wiki/M%C3%B6bius%E2%80%93Kantor_graph
  549. """
  550. G = LCF_graph(16, [5, -5], 8, create_using)
  551. G.name = "Moebius-Kantor Graph"
  552. return G
  553. @_raise_on_directed
  554. @nx._dispatchable(graphs=None, returns_graph=True)
  555. def octahedral_graph(create_using=None):
  556. """
  557. Returns the Platonic Octahedral graph.
  558. The octahedral graph is the 6-node 12-edge Platonic graph having the
  559. connectivity of the octahedron [1]_. If 6 couples go to a party,
  560. and each person shakes hands with every person except his or her partner,
  561. then this graph describes the set of handshakes that take place;
  562. for this reason it is also called the cocktail party graph [2]_.
  563. Parameters
  564. ----------
  565. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  566. Graph type to create. If graph instance, then cleared before populated.
  567. Returns
  568. -------
  569. G : networkx Graph
  570. Octahedral graph
  571. See Also
  572. --------
  573. tetrahedral_graph, cubical_graph, dodecahedral_graph, icosahedral_graph
  574. References
  575. ----------
  576. .. [1] https://mathworld.wolfram.com/OctahedralGraph.html
  577. .. [2] https://en.wikipedia.org/wiki/Tur%C3%A1n_graph#Special_cases
  578. """
  579. G = nx.from_dict_of_lists(
  580. {0: [1, 2, 3, 4], 1: [2, 3, 5], 2: [4, 5], 3: [4, 5], 4: [5]},
  581. create_using=create_using,
  582. )
  583. G.name = "Platonic Octahedral Graph"
  584. return G
  585. @nx._dispatchable(graphs=None, returns_graph=True)
  586. def pappus_graph():
  587. """
  588. Returns the Pappus graph.
  589. The Pappus graph is a cubic symmetric distance-regular graph with 18 nodes
  590. and 27 edges. It is Hamiltonian and can be represented in LCF notation as
  591. [5,7,-7,7,-7,-5]^3 [1]_.
  592. Returns
  593. -------
  594. G : networkx Graph
  595. Pappus graph
  596. References
  597. ----------
  598. .. [1] https://en.wikipedia.org/wiki/Pappus_graph
  599. """
  600. G = LCF_graph(18, [5, 7, -7, 7, -7, -5], 3)
  601. G.name = "Pappus Graph"
  602. return G
  603. @_raise_on_directed
  604. @nx._dispatchable(graphs=None, returns_graph=True)
  605. def petersen_graph(create_using=None):
  606. """
  607. Returns the Petersen Graph.
  608. The Peterson Graph is a cubic, undirected graph with 10 nodes and 15 edges [1]_.
  609. Julius Petersen constructed the graph as the smallest counterexample
  610. against the claim that a connected bridgeless cubic graph
  611. has an edge colouring with three colours [2]_.
  612. Parameters
  613. ----------
  614. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  615. Graph type to create. If graph instance, then cleared before populated.
  616. Returns
  617. -------
  618. G : networkx Graph
  619. Petersen Graph
  620. References
  621. ----------
  622. .. [1] https://en.wikipedia.org/wiki/Petersen_graph
  623. .. [2] https://www.win.tue.nl/~aeb/drg/graphs/Petersen.html
  624. """
  625. G = nx.from_dict_of_lists(
  626. {
  627. 0: [1, 4, 5],
  628. 1: [0, 2, 6],
  629. 2: [1, 3, 7],
  630. 3: [2, 4, 8],
  631. 4: [3, 0, 9],
  632. 5: [0, 7, 8],
  633. 6: [1, 8, 9],
  634. 7: [2, 5, 9],
  635. 8: [3, 5, 6],
  636. 9: [4, 6, 7],
  637. },
  638. create_using=create_using,
  639. )
  640. G.name = "Petersen Graph"
  641. return G
  642. @nx._dispatchable(graphs=None, returns_graph=True)
  643. def generalized_petersen_graph(n, k, *, create_using=None):
  644. """
  645. Returns the Generalized Petersen Graph GP(n,k).
  646. The Generalized Peterson Graph consists of an outer cycle of n nodes
  647. connected to an inner circulant graph of n nodes, where nodes in the
  648. inner circulant are connected to their kth nearest neighbor [1]_ [2]_.
  649. A Generalized Petersen Graph is cubic with 2n nodes and 3n edges.
  650. Some well known graphs are examples of Generalized Petersen Graphs such
  651. as the Petersen Graph GP(5, 2), the Desargues graph GP(10, 3), the
  652. Moebius-Kantor graph GP(8, 3), and the dodecahedron graph GP(10, 2).
  653. Parameters
  654. ----------
  655. n : int
  656. Number of nodes in the outer cycle and inner circulant. ``n >= 3`` is required.
  657. k : int
  658. Neighbor to connect in the inner circulant. ``1 <= k <= n/2``.
  659. Note that some people require ``k < n/2`` but we and others allow equality.
  660. Also, ``k < n/2`` is equivalent to ``k <= floor((n-1)/2)``
  661. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  662. Graph type to create. If graph instance, then cleared before populated.
  663. Returns
  664. -------
  665. G : networkx Graph
  666. Generalized Petersen Graph n k
  667. References
  668. ----------
  669. .. [1] https://mathworld.wolfram.com/GeneralizedPetersenGraph.html
  670. .. [2] https://en.wikipedia.org/wiki/Generalized_Petersen_graph
  671. """
  672. if n <= 2:
  673. raise NetworkXError(f"n >= 3 required. Got {n=}")
  674. if k < 1 or k > n / 2:
  675. raise NetworkXError(f" Got {n=} {k=}. Need 1 <= k <= n/2")
  676. G = nx.cycle_graph(range(n), create_using=create_using) # u-nodes
  677. if G.is_directed():
  678. raise NetworkXError("Directed Graph not supported in create_using")
  679. for i in range(n):
  680. G.add_edge(i, n + i) # add v-nodes and u to v edges
  681. G.add_edge(n + i, n + (i + k) % n) # edge from v_i to v_(i+k)%n
  682. G.name = f"Generalized Petersen Graph GP({n}, {k})"
  683. return G
  684. @nx._dispatchable(graphs=None, returns_graph=True)
  685. def sedgewick_maze_graph(create_using=None):
  686. """
  687. Return a small maze with a cycle.
  688. This is the maze used in Sedgewick, 3rd Edition, Part 5, Graph
  689. Algorithms, Chapter 18, e.g. Figure 18.2 and following [1]_.
  690. Nodes are numbered 0,..,7
  691. Parameters
  692. ----------
  693. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  694. Graph type to create. If graph instance, then cleared before populated.
  695. Returns
  696. -------
  697. G : networkx Graph
  698. Small maze with a cycle
  699. References
  700. ----------
  701. .. [1] Figure 18.2, Chapter 18, Graph Algorithms (3rd Ed), Sedgewick
  702. """
  703. G = empty_graph(0, create_using)
  704. G.add_nodes_from(range(8))
  705. G.add_edges_from([[0, 2], [0, 7], [0, 5]])
  706. G.add_edges_from([[1, 7], [2, 6]])
  707. G.add_edges_from([[3, 4], [3, 5]])
  708. G.add_edges_from([[4, 5], [4, 7], [4, 6]])
  709. G.name = "Sedgewick Maze"
  710. return G
  711. @nx._dispatchable(graphs=None, returns_graph=True)
  712. def tetrahedral_graph(create_using=None):
  713. """
  714. Returns the 3-regular Platonic Tetrahedral graph.
  715. Tetrahedral graph has 4 nodes and 6 edges. It is a
  716. special case of the complete graph, K4, and wheel graph, W4.
  717. It is one of the 5 platonic graphs [1]_.
  718. Parameters
  719. ----------
  720. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  721. Graph type to create. If graph instance, then cleared before populated.
  722. Returns
  723. -------
  724. G : networkx Graph
  725. Tetrahedral Graph
  726. See Also
  727. --------
  728. cubical_graph, octahedral_graph, dodecahedral_graph, icosahedral_graph
  729. References
  730. ----------
  731. .. [1] https://en.wikipedia.org/wiki/Tetrahedron#Tetrahedral_graph
  732. """
  733. G = complete_graph(4, create_using)
  734. G.name = "Platonic Tetrahedral Graph"
  735. return G
  736. @_raise_on_directed
  737. @nx._dispatchable(graphs=None, returns_graph=True)
  738. def truncated_cube_graph(create_using=None):
  739. """
  740. Returns the skeleton of the truncated cube.
  741. The truncated cube is an Archimedean solid with 14 regular
  742. faces (6 octagonal and 8 triangular), 36 edges and 24 nodes [1]_.
  743. The truncated cube is created by truncating (cutting off) the tips
  744. of the cube one third of the way into each edge [2]_.
  745. Parameters
  746. ----------
  747. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  748. Graph type to create. If graph instance, then cleared before populated.
  749. Returns
  750. -------
  751. G : networkx Graph
  752. Skeleton of the truncated cube
  753. References
  754. ----------
  755. .. [1] https://en.wikipedia.org/wiki/Truncated_cube
  756. .. [2] https://www.coolmath.com/reference/polyhedra-truncated-cube
  757. """
  758. G = nx.from_dict_of_lists(
  759. {
  760. 0: [1, 2, 4],
  761. 1: [11, 14],
  762. 2: [3, 4],
  763. 3: [6, 8],
  764. 4: [5],
  765. 5: [16, 18],
  766. 6: [7, 8],
  767. 7: [10, 12],
  768. 8: [9],
  769. 9: [17, 20],
  770. 10: [11, 12],
  771. 11: [14],
  772. 12: [13],
  773. 13: [21, 22],
  774. 14: [15],
  775. 15: [19, 23],
  776. 16: [17, 18],
  777. 17: [20],
  778. 18: [19],
  779. 19: [23],
  780. 20: [21],
  781. 21: [22],
  782. 22: [23],
  783. },
  784. create_using=create_using,
  785. )
  786. G.name = "Truncated Cube Graph"
  787. return G
  788. @nx._dispatchable(graphs=None, returns_graph=True)
  789. def truncated_tetrahedron_graph(create_using=None):
  790. """
  791. Returns the skeleton of the truncated Platonic tetrahedron.
  792. The truncated tetrahedron is an Archimedean solid with 4 regular hexagonal faces,
  793. 4 equilateral triangle faces, 12 nodes and 18 edges. It can be constructed by truncating
  794. all 4 vertices of a regular tetrahedron at one third of the original edge length [1]_.
  795. Parameters
  796. ----------
  797. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  798. Graph type to create. If graph instance, then cleared before populated.
  799. Returns
  800. -------
  801. G : networkx Graph
  802. Skeleton of the truncated tetrahedron
  803. References
  804. ----------
  805. .. [1] https://en.wikipedia.org/wiki/Truncated_tetrahedron
  806. """
  807. G = path_graph(12, create_using)
  808. G.add_edges_from([(0, 2), (0, 9), (1, 6), (3, 11), (4, 11), (5, 7), (8, 10)])
  809. G.name = "Truncated Tetrahedron Graph"
  810. return G
  811. @_raise_on_directed
  812. @nx._dispatchable(graphs=None, returns_graph=True)
  813. def tutte_graph(create_using=None):
  814. """
  815. Returns the Tutte graph.
  816. The Tutte graph is a cubic polyhedral, non-Hamiltonian graph. It has
  817. 46 nodes and 69 edges.
  818. It is a counterexample to Tait's conjecture that every 3-regular polyhedron
  819. has a Hamiltonian cycle.
  820. It can be realized geometrically from a tetrahedron by multiply truncating
  821. three of its vertices [1]_.
  822. Parameters
  823. ----------
  824. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  825. Graph type to create. If graph instance, then cleared before populated.
  826. Returns
  827. -------
  828. G : networkx Graph
  829. Tutte graph
  830. References
  831. ----------
  832. .. [1] https://en.wikipedia.org/wiki/Tutte_graph
  833. """
  834. G = nx.from_dict_of_lists(
  835. {
  836. 0: [1, 2, 3],
  837. 1: [4, 26],
  838. 2: [10, 11],
  839. 3: [18, 19],
  840. 4: [5, 33],
  841. 5: [6, 29],
  842. 6: [7, 27],
  843. 7: [8, 14],
  844. 8: [9, 38],
  845. 9: [10, 37],
  846. 10: [39],
  847. 11: [12, 39],
  848. 12: [13, 35],
  849. 13: [14, 15],
  850. 14: [34],
  851. 15: [16, 22],
  852. 16: [17, 44],
  853. 17: [18, 43],
  854. 18: [45],
  855. 19: [20, 45],
  856. 20: [21, 41],
  857. 21: [22, 23],
  858. 22: [40],
  859. 23: [24, 27],
  860. 24: [25, 32],
  861. 25: [26, 31],
  862. 26: [33],
  863. 27: [28],
  864. 28: [29, 32],
  865. 29: [30],
  866. 30: [31, 33],
  867. 31: [32],
  868. 34: [35, 38],
  869. 35: [36],
  870. 36: [37, 39],
  871. 37: [38],
  872. 40: [41, 44],
  873. 41: [42],
  874. 42: [43, 45],
  875. 43: [44],
  876. },
  877. create_using=create_using,
  878. )
  879. G.name = "Tutte's Graph"
  880. return G