small.py 26 KB

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