classic.py 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. """Generators for some classic graphs.
  2. The typical graph builder function is called as follows:
  3. >>> G = nx.complete_graph(100)
  4. returning the complete graph on n nodes labeled 0, .., 99
  5. as a simple graph. Except for `empty_graph`, all the functions
  6. in this module return a Graph class (i.e. a simple, undirected graph).
  7. """
  8. import itertools
  9. import numbers
  10. import networkx as nx
  11. from networkx.classes import Graph
  12. from networkx.exception import NetworkXError
  13. from networkx.utils import nodes_or_number, pairwise
  14. __all__ = [
  15. "balanced_tree",
  16. "barbell_graph",
  17. "binomial_tree",
  18. "complete_graph",
  19. "complete_multipartite_graph",
  20. "circular_ladder_graph",
  21. "circulant_graph",
  22. "cycle_graph",
  23. "dorogovtsev_goltsev_mendes_graph",
  24. "empty_graph",
  25. "full_rary_tree",
  26. "kneser_graph",
  27. "ladder_graph",
  28. "lollipop_graph",
  29. "null_graph",
  30. "path_graph",
  31. "star_graph",
  32. "tadpole_graph",
  33. "trivial_graph",
  34. "turan_graph",
  35. "wheel_graph",
  36. ]
  37. # -------------------------------------------------------------------
  38. # Some Classic Graphs
  39. # -------------------------------------------------------------------
  40. def _tree_edges(n, r):
  41. if n == 0:
  42. return
  43. # helper function for trees
  44. # yields edges in rooted tree at 0 with n nodes and branching ratio r
  45. nodes = iter(range(n))
  46. parents = [next(nodes)] # stack of max length r
  47. while parents:
  48. source = parents.pop(0)
  49. for i in range(r):
  50. try:
  51. target = next(nodes)
  52. parents.append(target)
  53. yield source, target
  54. except StopIteration:
  55. break
  56. @nx._dispatchable(graphs=None, returns_graph=True)
  57. def full_rary_tree(r, n, create_using=None):
  58. """Creates a full r-ary tree of `n` nodes.
  59. Sometimes called a k-ary, n-ary, or m-ary tree.
  60. "... all non-leaf nodes have exactly r children and all levels
  61. are full except for some rightmost position of the bottom level
  62. (if a leaf at the bottom level is missing, then so are all of the
  63. leaves to its right." [1]_
  64. .. plot::
  65. >>> nx.draw(nx.full_rary_tree(2, 10))
  66. Parameters
  67. ----------
  68. r : int
  69. branching factor of the tree
  70. n : int
  71. Number of nodes in the tree
  72. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  73. Graph type to create. If graph instance, then cleared before populated.
  74. Returns
  75. -------
  76. G : networkx Graph
  77. An r-ary tree with n nodes
  78. References
  79. ----------
  80. .. [1] An introduction to data structures and algorithms,
  81. James Andrew Storer, Birkhauser Boston 2001, (page 225).
  82. """
  83. G = empty_graph(n, create_using)
  84. G.add_edges_from(_tree_edges(n, r))
  85. return G
  86. @nx._dispatchable(graphs=None, returns_graph=True)
  87. def kneser_graph(n, k):
  88. """Returns the Kneser Graph with parameters `n` and `k`.
  89. The Kneser Graph has nodes that are k-tuples (subsets) of the integers
  90. between 0 and ``n-1``. Nodes are adjacent if their corresponding sets are disjoint.
  91. Parameters
  92. ----------
  93. n: int
  94. Number of integers from which to make node subsets.
  95. Subsets are drawn from ``set(range(n))``.
  96. k: int
  97. Size of the subsets.
  98. Returns
  99. -------
  100. G : NetworkX Graph
  101. Examples
  102. --------
  103. >>> G = nx.kneser_graph(5, 2)
  104. >>> G.number_of_nodes()
  105. 10
  106. >>> G.number_of_edges()
  107. 15
  108. >>> nx.is_isomorphic(G, nx.petersen_graph())
  109. True
  110. """
  111. if n <= 0:
  112. raise NetworkXError("n should be greater than zero")
  113. if k <= 0 or k > n:
  114. raise NetworkXError("k should be greater than zero and smaller than n")
  115. G = nx.Graph()
  116. # Create all k-subsets of [0, 1, ..., n-1]
  117. subsets = list(itertools.combinations(range(n), k))
  118. if 2 * k > n:
  119. G.add_nodes_from(subsets)
  120. universe = set(range(n))
  121. comb = itertools.combinations # only to make it all fit on one line
  122. G.add_edges_from((s, t) for s in subsets for t in comb(universe - set(s), k))
  123. return G
  124. @nx._dispatchable(graphs=None, returns_graph=True)
  125. def balanced_tree(r, h, create_using=None):
  126. """Returns the perfectly balanced `r`-ary tree of height `h`.
  127. .. plot::
  128. >>> nx.draw(nx.balanced_tree(2, 3))
  129. Parameters
  130. ----------
  131. r : int
  132. Branching factor of the tree; each node will have `r`
  133. children.
  134. h : int
  135. Height of the tree.
  136. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  137. Graph type to create. If graph instance, then cleared before populated.
  138. Returns
  139. -------
  140. G : NetworkX graph
  141. A balanced `r`-ary tree of height `h`.
  142. Notes
  143. -----
  144. This is the rooted tree where all leaves are at distance `h` from
  145. the root. The root has degree `r` and all other internal nodes
  146. have degree `r + 1`.
  147. Node labels are integers, starting from zero.
  148. A balanced tree is also known as a *complete r-ary tree*.
  149. """
  150. # The number of nodes in the balanced tree is `1 + r + ... + r^h`,
  151. # which is computed by using the closed-form formula for a geometric
  152. # sum with ratio `r`. In the special case that `r` is 1, the number
  153. # of nodes is simply `h + 1` (since the tree is actually a path
  154. # graph).
  155. if r == 1:
  156. n = h + 1
  157. else:
  158. # This must be an integer if both `r` and `h` are integers. If
  159. # they are not, we force integer division anyway.
  160. n = (1 - r ** (h + 1)) // (1 - r)
  161. return full_rary_tree(r, n, create_using=create_using)
  162. @nx._dispatchable(graphs=None, returns_graph=True)
  163. def barbell_graph(m1, m2, create_using=None):
  164. """Returns the Barbell Graph: two complete graphs connected by a path.
  165. .. plot::
  166. >>> nx.draw(nx.barbell_graph(4, 2))
  167. Parameters
  168. ----------
  169. m1 : int
  170. Size of the left and right barbells, must be greater than 2.
  171. m2 : int
  172. Length of the path connecting the barbells.
  173. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  174. Graph type to create. If graph instance, then cleared before populated.
  175. Only undirected Graphs are supported.
  176. Returns
  177. -------
  178. G : NetworkX graph
  179. A barbell graph.
  180. Notes
  181. -----
  182. Two identical complete graphs $K_{m1}$ form the left and right bells,
  183. and are connected by a path $P_{m2}$.
  184. The `2*m1+m2` nodes are numbered
  185. `0, ..., m1-1` for the left barbell,
  186. `m1, ..., m1+m2-1` for the path,
  187. and `m1+m2, ..., 2*m1+m2-1` for the right barbell.
  188. The 3 subgraphs are joined via the edges `(m1-1, m1)` and
  189. `(m1+m2-1, m1+m2)`. If `m2=0`, this is merely two complete
  190. graphs joined together.
  191. This graph is an extremal example in David Aldous
  192. and Jim Fill's e-text on Random Walks on Graphs.
  193. """
  194. if m1 < 2:
  195. raise NetworkXError("Invalid graph description, m1 should be >=2")
  196. if m2 < 0:
  197. raise NetworkXError("Invalid graph description, m2 should be >=0")
  198. # left barbell
  199. G = complete_graph(m1, create_using)
  200. if G.is_directed():
  201. raise NetworkXError("Directed Graph not supported")
  202. # connecting path
  203. G.add_nodes_from(range(m1, m1 + m2 - 1))
  204. if m2 > 1:
  205. G.add_edges_from(pairwise(range(m1, m1 + m2)))
  206. # right barbell
  207. G.add_edges_from(
  208. (u, v) for u in range(m1 + m2, 2 * m1 + m2) for v in range(u + 1, 2 * m1 + m2)
  209. )
  210. # connect it up
  211. G.add_edge(m1 - 1, m1)
  212. if m2 > 0:
  213. G.add_edge(m1 + m2 - 1, m1 + m2)
  214. return G
  215. @nx._dispatchable(graphs=None, returns_graph=True)
  216. def binomial_tree(n, create_using=None):
  217. """Returns the Binomial Tree of order n.
  218. The binomial tree of order 0 consists of a single node. A binomial tree of order k
  219. is defined recursively by linking two binomial trees of order k-1: the root of one is
  220. the leftmost child of the root of the other.
  221. .. plot::
  222. >>> nx.draw(nx.binomial_tree(3))
  223. Parameters
  224. ----------
  225. n : int
  226. Order of the binomial tree.
  227. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  228. Graph type to create. If graph instance, then cleared before populated.
  229. Returns
  230. -------
  231. G : NetworkX graph
  232. A binomial tree of $2^n$ nodes and $2^n - 1$ edges.
  233. """
  234. G = nx.empty_graph(1, create_using)
  235. N = 1
  236. for i in range(n):
  237. # Use G.edges() to ensure 2-tuples. G.edges is 3-tuple for MultiGraph
  238. edges = [(u + N, v + N) for (u, v) in G.edges()]
  239. G.add_edges_from(edges)
  240. G.add_edge(0, N)
  241. N *= 2
  242. return G
  243. @nx._dispatchable(graphs=None, returns_graph=True)
  244. @nodes_or_number(0)
  245. def complete_graph(n, create_using=None):
  246. """Return the complete graph `K_n` with n nodes.
  247. A complete graph on `n` nodes means that all pairs
  248. of distinct nodes have an edge connecting them.
  249. .. plot::
  250. >>> nx.draw(nx.complete_graph(5))
  251. Parameters
  252. ----------
  253. n : int or iterable container of nodes
  254. If n is an integer, nodes are from range(n).
  255. If n is a container of nodes, those nodes appear in the graph.
  256. Warning: n is not checked for duplicates and if present the
  257. resulting graph may not be as desired. Make sure you have no duplicates.
  258. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  259. Graph type to create. If graph instance, then cleared before populated.
  260. Examples
  261. --------
  262. >>> G = nx.complete_graph(9)
  263. >>> len(G)
  264. 9
  265. >>> G.size()
  266. 36
  267. >>> G = nx.complete_graph(range(11, 14))
  268. >>> list(G.nodes())
  269. [11, 12, 13]
  270. >>> G = nx.complete_graph(4, nx.DiGraph())
  271. >>> G.is_directed()
  272. True
  273. """
  274. _, nodes = n
  275. G = empty_graph(nodes, create_using)
  276. if len(nodes) > 1:
  277. if G.is_directed():
  278. edges = itertools.permutations(nodes, 2)
  279. else:
  280. edges = itertools.combinations(nodes, 2)
  281. G.add_edges_from(edges)
  282. return G
  283. @nx._dispatchable(graphs=None, returns_graph=True)
  284. def circular_ladder_graph(n, create_using=None):
  285. """Returns the circular ladder graph $CL_n$ of length n.
  286. $CL_n$ consists of two concentric n-cycles in which
  287. each of the n pairs of concentric nodes are joined by an edge.
  288. Node labels are the integers 0 to n-1
  289. .. plot::
  290. >>> nx.draw(nx.circular_ladder_graph(5))
  291. """
  292. G = ladder_graph(n, create_using)
  293. G.add_edge(0, n - 1)
  294. G.add_edge(n, 2 * n - 1)
  295. return G
  296. @nx._dispatchable(graphs=None, returns_graph=True)
  297. def circulant_graph(n, offsets, create_using=None):
  298. r"""Returns the circulant graph $Ci_n(x_1, x_2, ..., x_m)$ with $n$ nodes.
  299. The circulant graph $Ci_n(x_1, ..., x_m)$ consists of $n$ nodes $0, ..., n-1$
  300. such that node $i$ is connected to nodes $(i + x) \mod n$ and $(i - x) \mod n$
  301. for all $x$ in $x_1, ..., x_m$. Thus $Ci_n(1)$ is a cycle graph.
  302. .. plot::
  303. >>> nx.draw(nx.circulant_graph(10, [1]))
  304. Parameters
  305. ----------
  306. n : integer
  307. The number of nodes in the graph.
  308. offsets : list of integers
  309. A list of node offsets, $x_1$ up to $x_m$, as described above.
  310. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  311. Graph type to create. If graph instance, then cleared before populated.
  312. Returns
  313. -------
  314. NetworkX Graph of type create_using
  315. Examples
  316. --------
  317. Many well-known graph families are subfamilies of the circulant graphs;
  318. for example, to create the cycle graph on n points, we connect every
  319. node to nodes on either side (with offset plus or minus one). For n = 10,
  320. >>> G = nx.circulant_graph(10, [1])
  321. >>> edges = [
  322. ... (0, 9),
  323. ... (0, 1),
  324. ... (1, 2),
  325. ... (2, 3),
  326. ... (3, 4),
  327. ... (4, 5),
  328. ... (5, 6),
  329. ... (6, 7),
  330. ... (7, 8),
  331. ... (8, 9),
  332. ... ]
  333. >>> sorted(edges) == sorted(G.edges())
  334. True
  335. Similarly, we can create the complete graph
  336. on 5 points with the set of offsets [1, 2]:
  337. >>> G = nx.circulant_graph(5, [1, 2])
  338. >>> edges = [
  339. ... (0, 1),
  340. ... (0, 2),
  341. ... (0, 3),
  342. ... (0, 4),
  343. ... (1, 2),
  344. ... (1, 3),
  345. ... (1, 4),
  346. ... (2, 3),
  347. ... (2, 4),
  348. ... (3, 4),
  349. ... ]
  350. >>> sorted(edges) == sorted(G.edges())
  351. True
  352. """
  353. G = empty_graph(n, create_using)
  354. G.add_edges_from((i, (i - j) % n) for i in range(n) for j in offsets)
  355. G.add_edges_from((i, (i + j) % n) for i in range(n) for j in offsets)
  356. return G
  357. @nx._dispatchable(graphs=None, returns_graph=True)
  358. @nodes_or_number(0)
  359. def cycle_graph(n, create_using=None):
  360. """Returns the cycle graph $C_n$ of cyclically connected nodes.
  361. $C_n$ is a path with its two end-nodes connected.
  362. .. plot::
  363. >>> nx.draw(nx.cycle_graph(5))
  364. Parameters
  365. ----------
  366. n : int or iterable container of nodes
  367. If n is an integer, nodes are from `range(n)`.
  368. If n is a container of nodes, those nodes appear in the graph.
  369. Warning: n is not checked for duplicates and if present the
  370. resulting graph may not be as desired. Make sure you have no duplicates.
  371. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  372. Graph type to create. If graph instance, then cleared before populated.
  373. Notes
  374. -----
  375. If create_using is directed, the direction is in increasing order.
  376. """
  377. _, nodes = n
  378. G = empty_graph(nodes, create_using)
  379. G.add_edges_from(pairwise(nodes, cyclic=True))
  380. return G
  381. @nx._dispatchable(graphs=None, returns_graph=True)
  382. def dorogovtsev_goltsev_mendes_graph(n, create_using=None):
  383. """Returns the hierarchically constructed Dorogovtsev--Goltsev--Mendes graph.
  384. The Dorogovtsev--Goltsev--Mendes [1]_ procedure deterministically produces a
  385. scale-free graph with ``3/2 * (3**(n-1) + 1)`` nodes
  386. and ``3**n`` edges for a given `n`.
  387. Note that `n` denotes the number of times the state transition is applied,
  388. starting from the base graph with ``n = 0`` (no transitions), as in [2]_.
  389. This is different from the parameter ``t = n - 1`` in [1]_.
  390. .. plot::
  391. >>> nx.draw(nx.dorogovtsev_goltsev_mendes_graph(3))
  392. Parameters
  393. ----------
  394. n : integer
  395. The generation number.
  396. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  397. Graph type to create. Directed graphs and multigraphs are not supported.
  398. Returns
  399. -------
  400. G : NetworkX `Graph`
  401. Raises
  402. ------
  403. NetworkXError
  404. If `n` is less than zero.
  405. If `create_using` is a directed graph or multigraph.
  406. Examples
  407. --------
  408. >>> G = nx.dorogovtsev_goltsev_mendes_graph(3)
  409. >>> G.number_of_nodes()
  410. 15
  411. >>> G.number_of_edges()
  412. 27
  413. >>> nx.is_planar(G)
  414. True
  415. References
  416. ----------
  417. .. [1] S. N. Dorogovtsev, A. V. Goltsev and J. F. F. Mendes,
  418. "Pseudofractal scale-free web", Physical Review E 65, 066122, 2002.
  419. https://arxiv.org/pdf/cond-mat/0112143.pdf
  420. .. [2] Weisstein, Eric W. "Dorogovtsev--Goltsev--Mendes Graph".
  421. From MathWorld--A Wolfram Web Resource.
  422. https://mathworld.wolfram.com/Dorogovtsev-Goltsev-MendesGraph.html
  423. """
  424. if n < 0:
  425. raise NetworkXError("n must be greater than or equal to 0")
  426. G = empty_graph(0, create_using)
  427. if G.is_directed():
  428. raise NetworkXError("directed graph not supported")
  429. if G.is_multigraph():
  430. raise NetworkXError("multigraph not supported")
  431. G.add_edge(0, 1)
  432. new_node = 2 # next node to be added
  433. for _ in range(n): # iterate over number of generations.
  434. new_edges = []
  435. for u, v in G.edges():
  436. new_edges.append((u, new_node))
  437. new_edges.append((v, new_node))
  438. new_node += 1
  439. G.add_edges_from(new_edges)
  440. return G
  441. @nx._dispatchable(graphs=None, returns_graph=True)
  442. @nodes_or_number(0)
  443. def empty_graph(n=0, create_using=None, default=Graph):
  444. """Returns the empty graph with n nodes and zero edges.
  445. .. plot::
  446. >>> nx.draw(nx.empty_graph(5))
  447. Parameters
  448. ----------
  449. n : int or iterable container of nodes (default = 0)
  450. If n is an integer, nodes are from `range(n)`.
  451. If n is a container of nodes, those nodes appear in the graph.
  452. create_using : Graph Instance, Constructor or None
  453. Indicator of type of graph to return.
  454. If a Graph-type instance, then clear and use it.
  455. If None, use the `default` constructor.
  456. If a constructor, call it to create an empty graph.
  457. default : Graph constructor (optional, default = nx.Graph)
  458. The constructor to use if create_using is None.
  459. If None, then nx.Graph is used.
  460. This is used when passing an unknown `create_using` value
  461. through your home-grown function to `empty_graph` and
  462. you want a default constructor other than nx.Graph.
  463. Examples
  464. --------
  465. >>> G = nx.empty_graph(10)
  466. >>> G.number_of_nodes()
  467. 10
  468. >>> G.number_of_edges()
  469. 0
  470. >>> G = nx.empty_graph("ABC")
  471. >>> G.number_of_nodes()
  472. 3
  473. >>> sorted(G)
  474. ['A', 'B', 'C']
  475. Notes
  476. -----
  477. The variable create_using should be a Graph Constructor or a
  478. "graph"-like object. Constructors, e.g. `nx.Graph` or `nx.MultiGraph`
  479. will be used to create the returned graph. "graph"-like objects
  480. will be cleared (nodes and edges will be removed) and refitted as
  481. an empty "graph" with nodes specified in n. This capability
  482. is useful for specifying the class-nature of the resulting empty
  483. "graph" (i.e. Graph, DiGraph, MyWeirdGraphClass, etc.).
  484. The variable create_using has three main uses:
  485. Firstly, the variable create_using can be used to create an
  486. empty digraph, multigraph, etc. For example,
  487. >>> n = 10
  488. >>> G = nx.empty_graph(n, create_using=nx.DiGraph)
  489. will create an empty digraph on n nodes.
  490. Secondly, one can pass an existing graph (digraph, multigraph,
  491. etc.) via create_using. For example, if G is an existing graph
  492. (resp. digraph, multigraph, etc.), then empty_graph(n, create_using=G)
  493. will empty G (i.e. delete all nodes and edges using G.clear())
  494. and then add n nodes and zero edges, and return the modified graph.
  495. Thirdly, when constructing your home-grown graph creation function
  496. you can use empty_graph to construct the graph by passing a user
  497. defined create_using to empty_graph. In this case, if you want the
  498. default constructor to be other than nx.Graph, specify `default`.
  499. >>> def mygraph(n, create_using=None):
  500. ... G = nx.empty_graph(n, create_using, nx.MultiGraph)
  501. ... G.add_edges_from([(0, 1), (0, 1)])
  502. ... return G
  503. >>> G = mygraph(3)
  504. >>> G.is_multigraph()
  505. True
  506. >>> G = mygraph(3, nx.Graph)
  507. >>> G.is_multigraph()
  508. False
  509. See also create_empty_copy(G).
  510. """
  511. if create_using is None:
  512. G = default()
  513. elif isinstance(create_using, type):
  514. G = create_using()
  515. elif not hasattr(create_using, "adj"):
  516. raise TypeError("create_using is not a valid NetworkX graph type or instance")
  517. else:
  518. # create_using is a NetworkX style Graph
  519. create_using.clear()
  520. G = create_using
  521. _, nodes = n
  522. G.add_nodes_from(nodes)
  523. return G
  524. @nx._dispatchable(graphs=None, returns_graph=True)
  525. def ladder_graph(n, create_using=None):
  526. """Returns the Ladder graph of length n.
  527. This is two paths of n nodes, with
  528. each pair connected by a single edge.
  529. Node labels are the integers 0 to 2*n - 1.
  530. .. plot::
  531. >>> nx.draw(nx.ladder_graph(5))
  532. """
  533. G = empty_graph(2 * n, create_using)
  534. if G.is_directed():
  535. raise NetworkXError("Directed Graph not supported")
  536. G.add_edges_from(pairwise(range(n)))
  537. G.add_edges_from(pairwise(range(n, 2 * n)))
  538. G.add_edges_from((v, v + n) for v in range(n))
  539. return G
  540. @nx._dispatchable(graphs=None, returns_graph=True)
  541. @nodes_or_number([0, 1])
  542. def lollipop_graph(m, n, create_using=None):
  543. """Returns the Lollipop Graph; ``K_m`` connected to ``P_n``.
  544. This is the Barbell Graph without the right barbell.
  545. .. plot::
  546. >>> nx.draw(nx.lollipop_graph(3, 4))
  547. Parameters
  548. ----------
  549. m, n : int or iterable container of nodes
  550. If an integer, nodes are from ``range(m)`` and ``range(m, m+n)``.
  551. If a container of nodes, those nodes appear in the graph.
  552. Warning: `m` and `n` are not checked for duplicates and if present the
  553. resulting graph may not be as desired. Make sure you have no duplicates.
  554. The nodes for `m` appear in the complete graph $K_m$ and the nodes
  555. for `n` appear in the path $P_n$
  556. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  557. Graph type to create. If graph instance, then cleared before populated.
  558. Returns
  559. -------
  560. Networkx graph
  561. A complete graph with `m` nodes connected to a path of length `n`.
  562. Notes
  563. -----
  564. The 2 subgraphs are joined via an edge ``(m-1, m)``.
  565. If ``n=0``, this is merely a complete graph.
  566. (This graph is an extremal example in David Aldous and Jim
  567. Fill's etext on Random Walks on Graphs.)
  568. """
  569. m, m_nodes = m
  570. M = len(m_nodes)
  571. if M < 2:
  572. raise NetworkXError("Invalid description: m should indicate at least 2 nodes")
  573. n, n_nodes = n
  574. if isinstance(m, numbers.Integral) and isinstance(n, numbers.Integral):
  575. n_nodes = list(range(M, M + n))
  576. N = len(n_nodes)
  577. # the ball
  578. G = complete_graph(m_nodes, create_using)
  579. if G.is_directed():
  580. raise NetworkXError("Directed Graph not supported")
  581. # the stick
  582. G.add_nodes_from(n_nodes)
  583. if N > 1:
  584. G.add_edges_from(pairwise(n_nodes))
  585. if len(G) != M + N:
  586. raise NetworkXError("Nodes must be distinct in containers m and n")
  587. # connect ball to stick
  588. if M > 0 and N > 0:
  589. G.add_edge(m_nodes[-1], n_nodes[0])
  590. return G
  591. @nx._dispatchable(graphs=None, returns_graph=True)
  592. def null_graph(create_using=None):
  593. """Returns the Null graph with no nodes or edges.
  594. See empty_graph for the use of create_using.
  595. """
  596. G = empty_graph(0, create_using)
  597. return G
  598. @nx._dispatchable(graphs=None, returns_graph=True)
  599. @nodes_or_number(0)
  600. def path_graph(n, create_using=None):
  601. """Returns the Path graph `P_n` of linearly connected nodes.
  602. .. plot::
  603. >>> nx.draw(nx.path_graph(5))
  604. Parameters
  605. ----------
  606. n : int or iterable
  607. If an integer, nodes are 0 to n - 1.
  608. If an iterable of nodes, in the order they appear in the path.
  609. Warning: n is not checked for duplicates and if present the
  610. resulting graph may not be as desired. Make sure you have no duplicates.
  611. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  612. Graph type to create. If graph instance, then cleared before populated.
  613. """
  614. _, nodes = n
  615. G = empty_graph(nodes, create_using)
  616. G.add_edges_from(pairwise(nodes))
  617. return G
  618. @nx._dispatchable(graphs=None, returns_graph=True)
  619. @nodes_or_number(0)
  620. def star_graph(n, create_using=None):
  621. """Return a star graph.
  622. The star graph consists of one center node connected to `n` outer nodes.
  623. .. plot::
  624. >>> nx.draw(nx.star_graph(6))
  625. Parameters
  626. ----------
  627. n : int or iterable
  628. If an integer, node labels are ``0`` to `n`, with center ``0``.
  629. If an iterable of nodes, the center is the first.
  630. Warning: `n` is not checked for duplicates and if present, the
  631. resulting graph may not be as desired. Make sure you have no duplicates.
  632. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  633. Graph type to create. If graph instance, then cleared before populated.
  634. Examples
  635. --------
  636. A star graph with 3 spokes can be generated with
  637. >>> G = nx.star_graph(3)
  638. >>> sorted(G.edges)
  639. [(0, 1), (0, 2), (0, 3)]
  640. For directed graphs, the convention is to have edges pointing from the hub
  641. to the spokes:
  642. >>> DG1 = nx.star_graph(3, create_using=nx.DiGraph)
  643. >>> sorted(DG1.edges)
  644. [(0, 1), (0, 2), (0, 3)]
  645. Other possible definitions have edges pointing from the spokes to the hub:
  646. >>> DG2 = nx.star_graph(3, create_using=nx.DiGraph).reverse()
  647. >>> sorted(DG2.edges)
  648. [(1, 0), (2, 0), (3, 0)]
  649. or have bidirectional edges:
  650. >>> DG3 = nx.star_graph(3).to_directed()
  651. >>> sorted(DG3.edges)
  652. [(0, 1), (0, 2), (0, 3), (1, 0), (2, 0), (3, 0)]
  653. Notes
  654. -----
  655. The graph has ``n + 1`` nodes for integer `n`.
  656. So ``star_graph(3)`` is the same as ``star_graph(range(4))``.
  657. """
  658. n, nodes = n
  659. if isinstance(n, numbers.Integral):
  660. nodes.append(int(n)) # There should be n + 1 nodes.
  661. G = empty_graph(nodes, create_using)
  662. if len(nodes) > 1:
  663. hub, *spokes = nodes
  664. G.add_edges_from((hub, node) for node in spokes)
  665. return G
  666. @nx._dispatchable(graphs=None, returns_graph=True)
  667. @nodes_or_number([0, 1])
  668. def tadpole_graph(m, n, create_using=None):
  669. """Returns the (m,n)-tadpole graph; ``C_m`` connected to ``P_n``.
  670. This graph on m+n nodes connects a cycle of size `m` to a path of length `n`.
  671. It looks like a tadpole. It is also called a kite graph or a dragon graph.
  672. .. plot::
  673. >>> nx.draw(nx.tadpole_graph(3, 5))
  674. Parameters
  675. ----------
  676. m, n : int or iterable container of nodes
  677. If an integer, nodes are from ``range(m)`` and ``range(m,m+n)``.
  678. If a container of nodes, those nodes appear in the graph.
  679. Warning: `m` and `n` are not checked for duplicates and if present the
  680. resulting graph may not be as desired.
  681. The nodes for `m` appear in the cycle graph $C_m$ and the nodes
  682. for `n` appear in the path $P_n$.
  683. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  684. Graph type to create. If graph instance, then cleared before populated.
  685. Returns
  686. -------
  687. Networkx graph
  688. A cycle of size `m` connected to a path of length `n`.
  689. Raises
  690. ------
  691. NetworkXError
  692. If ``m < 2``. The tadpole graph is undefined for ``m<2``.
  693. Notes
  694. -----
  695. The 2 subgraphs are joined via an edge ``(m-1, m)``.
  696. If ``n=0``, this is a cycle graph.
  697. `m` and/or `n` can be a container of nodes instead of an integer.
  698. """
  699. m, m_nodes = m
  700. M = len(m_nodes)
  701. if M < 2:
  702. raise NetworkXError("Invalid description: m should indicate at least 2 nodes")
  703. n, n_nodes = n
  704. if isinstance(m, numbers.Integral) and isinstance(n, numbers.Integral):
  705. n_nodes = list(range(M, M + n))
  706. # the circle
  707. G = cycle_graph(m_nodes, create_using)
  708. if G.is_directed():
  709. raise NetworkXError("Directed Graph not supported")
  710. # the stick
  711. nx.add_path(G, [m_nodes[-1]] + list(n_nodes))
  712. return G
  713. @nx._dispatchable(graphs=None, returns_graph=True)
  714. def trivial_graph(create_using=None):
  715. """Return the Trivial graph with one node (with label 0) and no edges.
  716. .. plot::
  717. >>> nx.draw(nx.trivial_graph(), with_labels=True)
  718. """
  719. G = empty_graph(1, create_using)
  720. return G
  721. @nx._dispatchable(graphs=None, returns_graph=True)
  722. def turan_graph(n, r):
  723. r"""Return the Turan Graph
  724. The Turan Graph is a complete multipartite graph on $n$ nodes
  725. with $r$ disjoint subsets. That is, edges connect each node to
  726. every node not in its subset.
  727. Given $n$ and $r$, we create a complete multipartite graph with
  728. $r-(n \mod r)$ partitions of size $n/r$, rounded down, and
  729. $n \mod r$ partitions of size $n/r+1$, rounded down.
  730. .. plot::
  731. >>> nx.draw(nx.turan_graph(6, 2))
  732. Parameters
  733. ----------
  734. n : int
  735. The number of nodes.
  736. r : int
  737. The number of partitions.
  738. Must be less than or equal to n.
  739. Notes
  740. -----
  741. Must satisfy $1 <= r <= n$.
  742. The graph has $(r-1)(n^2)/(2r)$ edges, rounded down.
  743. """
  744. if not 1 <= r <= n:
  745. raise NetworkXError("Must satisfy 1 <= r <= n")
  746. partitions = [n // r] * (r - (n % r)) + [n // r + 1] * (n % r)
  747. G = complete_multipartite_graph(*partitions)
  748. return G
  749. @nx._dispatchable(graphs=None, returns_graph=True)
  750. @nodes_or_number(0)
  751. def wheel_graph(n, create_using=None):
  752. """Return the wheel graph
  753. The wheel graph consists of a hub node connected to a cycle of (n-1) nodes.
  754. .. plot::
  755. >>> nx.draw(nx.wheel_graph(5))
  756. Parameters
  757. ----------
  758. n : int or iterable
  759. If an integer, node labels are 0 to n with center 0.
  760. If an iterable of nodes, the center is the first.
  761. Warning: n is not checked for duplicates and if present the
  762. resulting graph may not be as desired. Make sure you have no duplicates.
  763. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  764. Graph type to create. If graph instance, then cleared before populated.
  765. Node labels are the integers 0 to n - 1.
  766. """
  767. _, nodes = n
  768. G = empty_graph(nodes, create_using)
  769. if G.is_directed():
  770. raise NetworkXError("Directed Graph not supported")
  771. if len(nodes) > 1:
  772. hub, *rim = nodes
  773. G.add_edges_from((hub, node) for node in rim)
  774. if len(rim) > 1:
  775. G.add_edges_from(pairwise(rim, cyclic=True))
  776. return G
  777. @nx._dispatchable(graphs=None, returns_graph=True)
  778. def complete_multipartite_graph(*subset_sizes):
  779. """Returns the complete multipartite graph with the specified subset sizes.
  780. .. plot::
  781. >>> nx.draw(nx.complete_multipartite_graph(1, 2, 3))
  782. Parameters
  783. ----------
  784. subset_sizes : tuple of integers or tuple of node iterables
  785. The arguments can either all be integer number of nodes or they
  786. can all be iterables of nodes. If integers, they represent the
  787. number of nodes in each subset of the multipartite graph.
  788. If iterables, each is used to create the nodes for that subset.
  789. The length of subset_sizes is the number of subsets.
  790. Returns
  791. -------
  792. G : NetworkX Graph
  793. Returns the complete multipartite graph with the specified subsets.
  794. For each node, the node attribute 'subset' is an integer
  795. indicating which subset contains the node.
  796. Examples
  797. --------
  798. Creating a complete tripartite graph, with subsets of one, two, and three
  799. nodes, respectively.
  800. >>> G = nx.complete_multipartite_graph(1, 2, 3)
  801. >>> [G.nodes[u]["subset"] for u in G]
  802. [0, 1, 1, 2, 2, 2]
  803. >>> list(G.edges(0))
  804. [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)]
  805. >>> list(G.edges(2))
  806. [(2, 0), (2, 3), (2, 4), (2, 5)]
  807. >>> list(G.edges(4))
  808. [(4, 0), (4, 1), (4, 2)]
  809. >>> G = nx.complete_multipartite_graph("a", "bc", "def")
  810. >>> [G.nodes[u]["subset"] for u in sorted(G)]
  811. [0, 1, 1, 2, 2, 2]
  812. Notes
  813. -----
  814. This function generalizes several other graph builder functions.
  815. - If no subset sizes are given, this returns the null graph.
  816. - If a single subset size `n` is given, this returns the empty graph on
  817. `n` nodes.
  818. - If two subset sizes `m` and `n` are given, this returns the complete
  819. bipartite graph on `m + n` nodes.
  820. - If subset sizes `1` and `n` are given, this returns the star graph on
  821. `n + 1` nodes.
  822. See also
  823. --------
  824. complete_bipartite_graph
  825. """
  826. # The complete multipartite graph is an undirected simple graph.
  827. G = Graph()
  828. if len(subset_sizes) == 0:
  829. return G
  830. # set up subsets of nodes
  831. try:
  832. extents = pairwise(itertools.accumulate((0,) + subset_sizes))
  833. subsets = [range(start, end) for start, end in extents]
  834. except TypeError:
  835. subsets = subset_sizes
  836. else:
  837. if any(size < 0 for size in subset_sizes):
  838. raise NetworkXError(f"Negative number of nodes not valid: {subset_sizes}")
  839. # add nodes with subset attribute
  840. # while checking that ints are not mixed with iterables
  841. try:
  842. for i, subset in enumerate(subsets):
  843. G.add_nodes_from(subset, subset=i)
  844. except TypeError as err:
  845. raise NetworkXError("Arguments must be all ints or all iterables") from err
  846. # Across subsets, all nodes should be adjacent.
  847. # We can use itertools.combinations() because undirected.
  848. for subset1, subset2 in itertools.combinations(subsets, 2):
  849. G.add_edges_from(itertools.product(subset1, subset2))
  850. return G