test_classic.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. """
  2. ====================
  3. Generators - Classic
  4. ====================
  5. Unit tests for various classic graph generators in generators/classic.py
  6. """
  7. import itertools
  8. import typing
  9. import pytest
  10. import networkx as nx
  11. from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic
  12. from networkx.utils import edges_equal, nodes_equal
  13. is_isomorphic = graph_could_be_isomorphic
  14. class TestGeneratorClassic:
  15. def test_balanced_tree(self):
  16. # balanced_tree(r,h) is a tree with (r**(h+1)-1)/(r-1) edges
  17. for r, h in [(2, 2), (3, 3), (6, 2)]:
  18. t = nx.balanced_tree(r, h)
  19. order = t.order()
  20. assert order == (r ** (h + 1) - 1) / (r - 1)
  21. assert nx.is_connected(t)
  22. assert t.size() == order - 1
  23. dh = nx.degree_histogram(t)
  24. assert dh[0] == 0 # no nodes of 0
  25. assert dh[1] == r**h # nodes of degree 1 are leaves
  26. assert dh[r] == 1 # root is degree r
  27. assert dh[r + 1] == order - r**h - 1 # everyone else is degree r+1
  28. assert len(dh) == r + 2
  29. def test_balanced_tree_star(self):
  30. # balanced_tree(r,1) is the r-star
  31. t = nx.balanced_tree(r=2, h=1)
  32. assert is_isomorphic(t, nx.star_graph(2))
  33. t = nx.balanced_tree(r=5, h=1)
  34. assert is_isomorphic(t, nx.star_graph(5))
  35. t = nx.balanced_tree(r=10, h=1)
  36. assert is_isomorphic(t, nx.star_graph(10))
  37. def test_balanced_tree_path(self):
  38. """Tests that the balanced tree with branching factor one is the
  39. path graph.
  40. """
  41. # A tree of height four has five levels.
  42. T = nx.balanced_tree(1, 4)
  43. P = nx.path_graph(5)
  44. assert is_isomorphic(T, P)
  45. def test_full_rary_tree(self):
  46. r = 2
  47. n = 9
  48. t = nx.full_rary_tree(r, n)
  49. assert t.order() == n
  50. assert nx.is_connected(t)
  51. dh = nx.degree_histogram(t)
  52. assert dh[0] == 0 # no nodes of 0
  53. assert dh[1] == 5 # nodes of degree 1 are leaves
  54. assert dh[r] == 1 # root is degree r
  55. assert dh[r + 1] == 9 - 5 - 1 # everyone else is degree r+1
  56. assert len(dh) == r + 2
  57. def test_full_rary_tree_balanced(self):
  58. t = nx.full_rary_tree(2, 15)
  59. th = nx.balanced_tree(2, 3)
  60. assert is_isomorphic(t, th)
  61. def test_full_rary_tree_path(self):
  62. t = nx.full_rary_tree(1, 10)
  63. assert is_isomorphic(t, nx.path_graph(10))
  64. def test_full_rary_tree_empty(self):
  65. t = nx.full_rary_tree(0, 10)
  66. assert is_isomorphic(t, nx.empty_graph(10))
  67. t = nx.full_rary_tree(3, 0)
  68. assert is_isomorphic(t, nx.empty_graph(0))
  69. def test_full_rary_tree_3_20(self):
  70. t = nx.full_rary_tree(3, 20)
  71. assert t.order() == 20
  72. def test_barbell_graph(self):
  73. # number of nodes = 2*m1 + m2 (2 m1-complete graphs + m2-path + 2 edges)
  74. # number of edges = 2*(nx.number_of_edges(m1-complete graph) + m2 + 1
  75. m1 = 3
  76. m2 = 5
  77. b = nx.barbell_graph(m1, m2)
  78. assert nx.number_of_nodes(b) == 2 * m1 + m2
  79. assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
  80. m1 = 4
  81. m2 = 10
  82. b = nx.barbell_graph(m1, m2)
  83. assert nx.number_of_nodes(b) == 2 * m1 + m2
  84. assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
  85. m1 = 3
  86. m2 = 20
  87. b = nx.barbell_graph(m1, m2)
  88. assert nx.number_of_nodes(b) == 2 * m1 + m2
  89. assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
  90. # Raise NetworkXError if m1<2
  91. m1 = 1
  92. m2 = 20
  93. pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
  94. # Raise NetworkXError if m2<0
  95. m1 = 5
  96. m2 = -2
  97. pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
  98. # nx.barbell_graph(2,m) = nx.path_graph(m+4)
  99. m1 = 2
  100. m2 = 5
  101. b = nx.barbell_graph(m1, m2)
  102. assert is_isomorphic(b, nx.path_graph(m2 + 4))
  103. m1 = 2
  104. m2 = 10
  105. b = nx.barbell_graph(m1, m2)
  106. assert is_isomorphic(b, nx.path_graph(m2 + 4))
  107. m1 = 2
  108. m2 = 20
  109. b = nx.barbell_graph(m1, m2)
  110. assert is_isomorphic(b, nx.path_graph(m2 + 4))
  111. pytest.raises(
  112. nx.NetworkXError, nx.barbell_graph, m1, m2, create_using=nx.DiGraph()
  113. )
  114. mb = nx.barbell_graph(m1, m2, create_using=nx.MultiGraph())
  115. assert edges_equal(mb.edges(), b.edges())
  116. def test_binomial_tree(self):
  117. graphs = (None, nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph)
  118. for create_using in graphs:
  119. for n in range(0, 4):
  120. b = nx.binomial_tree(n, create_using)
  121. assert nx.number_of_nodes(b) == 2**n
  122. assert nx.number_of_edges(b) == (2**n - 1)
  123. def test_complete_graph(self):
  124. # complete_graph(m) is a connected graph with
  125. # m nodes and m*(m+1)/2 edges
  126. for m in [0, 1, 3, 5]:
  127. g = nx.complete_graph(m)
  128. assert nx.number_of_nodes(g) == m
  129. assert nx.number_of_edges(g) == m * (m - 1) // 2
  130. mg = nx.complete_graph(m, create_using=nx.MultiGraph)
  131. assert edges_equal(mg.edges(), g.edges())
  132. g = nx.complete_graph("abc")
  133. assert nodes_equal(g.nodes(), ["a", "b", "c"])
  134. assert g.size() == 3
  135. # creates a self-loop... should it? <backward compatible says yes>
  136. g = nx.complete_graph("abcb")
  137. assert nodes_equal(g.nodes(), ["a", "b", "c"])
  138. assert g.size() == 4
  139. g = nx.complete_graph("abcb", create_using=nx.MultiGraph)
  140. assert nodes_equal(g.nodes(), ["a", "b", "c"])
  141. assert g.size() == 6
  142. def test_complete_digraph(self):
  143. # complete_graph(m) is a connected graph with
  144. # m nodes and m*(m+1)/2 edges
  145. for m in [0, 1, 3, 5]:
  146. g = nx.complete_graph(m, create_using=nx.DiGraph)
  147. assert nx.number_of_nodes(g) == m
  148. assert nx.number_of_edges(g) == m * (m - 1)
  149. g = nx.complete_graph("abc", create_using=nx.DiGraph)
  150. assert len(g) == 3
  151. assert g.size() == 6
  152. assert g.is_directed()
  153. def test_circular_ladder_graph(self):
  154. G = nx.circular_ladder_graph(5)
  155. pytest.raises(
  156. nx.NetworkXError, nx.circular_ladder_graph, 5, create_using=nx.DiGraph
  157. )
  158. mG = nx.circular_ladder_graph(5, create_using=nx.MultiGraph)
  159. assert edges_equal(mG.edges(), G.edges())
  160. def test_circulant_graph(self):
  161. # Ci_n(1) is the cycle graph for all n
  162. Ci6_1 = nx.circulant_graph(6, [1])
  163. C6 = nx.cycle_graph(6)
  164. assert edges_equal(Ci6_1.edges(), C6.edges())
  165. # Ci_n(1, 2, ..., n div 2) is the complete graph for all n
  166. Ci7 = nx.circulant_graph(7, [1, 2, 3])
  167. K7 = nx.complete_graph(7)
  168. assert edges_equal(Ci7.edges(), K7.edges())
  169. # Ci_6(1, 3) is K_3,3 i.e. the utility graph
  170. Ci6_1_3 = nx.circulant_graph(6, [1, 3])
  171. K3_3 = nx.complete_bipartite_graph(3, 3)
  172. assert is_isomorphic(Ci6_1_3, K3_3)
  173. def test_cycle_graph(self):
  174. G = nx.cycle_graph(4)
  175. assert edges_equal(G.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
  176. mG = nx.cycle_graph(4, create_using=nx.MultiGraph)
  177. assert edges_equal(mG.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
  178. G = nx.cycle_graph(4, create_using=nx.DiGraph)
  179. assert not G.has_edge(2, 1)
  180. assert G.has_edge(1, 2)
  181. assert G.is_directed()
  182. G = nx.cycle_graph("abc")
  183. assert len(G) == 3
  184. assert G.size() == 3
  185. G = nx.cycle_graph("abcb")
  186. assert len(G) == 3
  187. assert G.size() == 2
  188. g = nx.cycle_graph("abc", nx.DiGraph)
  189. assert len(g) == 3
  190. assert g.size() == 3
  191. assert g.is_directed()
  192. g = nx.cycle_graph("abcb", nx.DiGraph)
  193. assert len(g) == 3
  194. assert g.size() == 4
  195. def test_dorogovtsev_goltsev_mendes_graph(self):
  196. G = nx.dorogovtsev_goltsev_mendes_graph(0)
  197. assert edges_equal(G.edges(), [(0, 1)])
  198. assert nodes_equal(list(G), [0, 1])
  199. G = nx.dorogovtsev_goltsev_mendes_graph(1)
  200. assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)])
  201. assert nx.average_clustering(G) == 1.0
  202. assert sorted(nx.triangles(G).values()) == [1, 1, 1]
  203. G = nx.dorogovtsev_goltsev_mendes_graph(10)
  204. assert nx.number_of_nodes(G) == 29526
  205. assert nx.number_of_edges(G) == 59049
  206. assert G.degree(0) == 1024
  207. assert G.degree(1) == 1024
  208. assert G.degree(2) == 1024
  209. pytest.raises(
  210. nx.NetworkXError,
  211. nx.dorogovtsev_goltsev_mendes_graph,
  212. 7,
  213. create_using=nx.DiGraph,
  214. )
  215. pytest.raises(
  216. nx.NetworkXError,
  217. nx.dorogovtsev_goltsev_mendes_graph,
  218. 7,
  219. create_using=nx.MultiGraph,
  220. )
  221. def test_create_using(self):
  222. G = nx.empty_graph()
  223. assert isinstance(G, nx.Graph)
  224. pytest.raises(TypeError, nx.empty_graph, create_using=0.0)
  225. pytest.raises(TypeError, nx.empty_graph, create_using="Graph")
  226. G = nx.empty_graph(create_using=nx.MultiGraph)
  227. assert isinstance(G, nx.MultiGraph)
  228. G = nx.empty_graph(create_using=nx.DiGraph)
  229. assert isinstance(G, nx.DiGraph)
  230. G = nx.empty_graph(create_using=nx.DiGraph, default=nx.MultiGraph)
  231. assert isinstance(G, nx.DiGraph)
  232. G = nx.empty_graph(create_using=None, default=nx.MultiGraph)
  233. assert isinstance(G, nx.MultiGraph)
  234. G = nx.empty_graph(default=nx.MultiGraph)
  235. assert isinstance(G, nx.MultiGraph)
  236. G = nx.path_graph(5)
  237. H = nx.empty_graph(create_using=G)
  238. assert not H.is_multigraph()
  239. assert not H.is_directed()
  240. assert len(H) == 0
  241. assert G is H
  242. H = nx.empty_graph(create_using=nx.MultiGraph())
  243. assert H.is_multigraph()
  244. assert not H.is_directed()
  245. assert G is not H
  246. # test for subclasses that also use typing.Protocol. See gh-6243
  247. class Mixin(typing.Protocol):
  248. pass
  249. class MyGraph(Mixin, nx.DiGraph):
  250. pass
  251. G = nx.empty_graph(create_using=MyGraph)
  252. def test_empty_graph(self):
  253. G = nx.empty_graph()
  254. assert nx.number_of_nodes(G) == 0
  255. G = nx.empty_graph(42)
  256. assert nx.number_of_nodes(G) == 42
  257. assert nx.number_of_edges(G) == 0
  258. G = nx.empty_graph("abc")
  259. assert len(G) == 3
  260. assert G.size() == 0
  261. # create empty digraph
  262. G = nx.empty_graph(42, create_using=nx.DiGraph(name="duh"))
  263. assert nx.number_of_nodes(G) == 42
  264. assert nx.number_of_edges(G) == 0
  265. assert isinstance(G, nx.DiGraph)
  266. # create empty multigraph
  267. G = nx.empty_graph(42, create_using=nx.MultiGraph(name="duh"))
  268. assert nx.number_of_nodes(G) == 42
  269. assert nx.number_of_edges(G) == 0
  270. assert isinstance(G, nx.MultiGraph)
  271. # create empty graph from another
  272. pete = nx.petersen_graph()
  273. G = nx.empty_graph(42, create_using=pete)
  274. assert nx.number_of_nodes(G) == 42
  275. assert nx.number_of_edges(G) == 0
  276. assert isinstance(G, nx.Graph)
  277. def test_ladder_graph(self):
  278. for i, G in [
  279. (0, nx.empty_graph(0)),
  280. (1, nx.path_graph(2)),
  281. (2, nx.hypercube_graph(2)),
  282. (10, nx.grid_graph([2, 10])),
  283. ]:
  284. assert is_isomorphic(nx.ladder_graph(i), G)
  285. pytest.raises(nx.NetworkXError, nx.ladder_graph, 2, create_using=nx.DiGraph)
  286. g = nx.ladder_graph(2)
  287. mg = nx.ladder_graph(2, create_using=nx.MultiGraph)
  288. assert edges_equal(mg.edges(), g.edges())
  289. def test_lollipop_graph_right_sizes(self):
  290. # number of nodes = m1 + m2
  291. # number of edges = nx.number_of_edges(nx.complete_graph(m1)) + m2
  292. for m1, m2 in [(3, 5), (4, 10), (3, 20)]:
  293. G = nx.lollipop_graph(m1, m2)
  294. assert nx.number_of_nodes(G) == m1 + m2
  295. assert nx.number_of_edges(G) == m1 * (m1 - 1) / 2 + m2
  296. for first, second in [("ab", ""), ("abc", "defg")]:
  297. m1, m2 = len(first), len(second)
  298. G = nx.lollipop_graph(first, second)
  299. assert nx.number_of_nodes(G) == m1 + m2
  300. assert nx.number_of_edges(G) == m1 * (m1 - 1) / 2 + m2
  301. def test_lollipop_graph_exceptions(self):
  302. # Raise NetworkXError if m<2
  303. pytest.raises(nx.NetworkXError, nx.lollipop_graph, -1, 2)
  304. pytest.raises(nx.NetworkXError, nx.lollipop_graph, 1, 20)
  305. pytest.raises(nx.NetworkXError, nx.lollipop_graph, "", 20)
  306. pytest.raises(nx.NetworkXError, nx.lollipop_graph, "a", 20)
  307. # Raise NetworkXError if n<0
  308. pytest.raises(nx.NetworkXError, nx.lollipop_graph, 5, -2)
  309. # raise NetworkXError if create_using is directed
  310. with pytest.raises(nx.NetworkXError):
  311. nx.lollipop_graph(2, 20, create_using=nx.DiGraph)
  312. with pytest.raises(nx.NetworkXError):
  313. nx.lollipop_graph(2, 20, create_using=nx.MultiDiGraph)
  314. def test_lollipop_graph_same_as_path_when_m1_is_2(self):
  315. # lollipop_graph(2,m) = path_graph(m+2)
  316. for m1, m2 in [(2, 0), (2, 5), (2, 10), ("ab", 20)]:
  317. G = nx.lollipop_graph(m1, m2)
  318. assert is_isomorphic(G, nx.path_graph(m2 + 2))
  319. def test_lollipop_graph_for_multigraph(self):
  320. G = nx.lollipop_graph(5, 20)
  321. MG = nx.lollipop_graph(5, 20, create_using=nx.MultiGraph)
  322. assert edges_equal(MG.edges(), G.edges())
  323. def test_lollipop_graph_mixing_input_types(self):
  324. cases = [(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])]
  325. for m1, m2 in cases:
  326. G = nx.lollipop_graph(m1, m2)
  327. assert len(G) == 7
  328. assert G.size() == 9
  329. def test_lollipop_graph_not_int_integer_inputs(self):
  330. # test non-int integers
  331. np = pytest.importorskip("numpy")
  332. G = nx.lollipop_graph(np.int32(4), np.int64(3))
  333. assert len(G) == 7
  334. assert G.size() == 9
  335. def test_null_graph(self):
  336. assert nx.number_of_nodes(nx.null_graph()) == 0
  337. def test_path_graph(self):
  338. p = nx.path_graph(0)
  339. assert is_isomorphic(p, nx.null_graph())
  340. p = nx.path_graph(1)
  341. assert is_isomorphic(p, nx.empty_graph(1))
  342. p = nx.path_graph(10)
  343. assert nx.is_connected(p)
  344. assert sorted(d for n, d in p.degree()) == [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]
  345. assert p.order() - 1 == p.size()
  346. dp = nx.path_graph(3, create_using=nx.DiGraph)
  347. assert dp.has_edge(0, 1)
  348. assert not dp.has_edge(1, 0)
  349. mp = nx.path_graph(10, create_using=nx.MultiGraph)
  350. assert edges_equal(mp.edges(), p.edges())
  351. G = nx.path_graph("abc")
  352. assert len(G) == 3
  353. assert G.size() == 2
  354. G = nx.path_graph("abcb")
  355. assert len(G) == 3
  356. assert G.size() == 2
  357. g = nx.path_graph("abc", nx.DiGraph)
  358. assert len(g) == 3
  359. assert g.size() == 2
  360. assert g.is_directed()
  361. g = nx.path_graph("abcb", nx.DiGraph)
  362. assert len(g) == 3
  363. assert g.size() == 3
  364. G = nx.path_graph((1, 2, 3, 2, 4))
  365. assert G.has_edge(2, 4)
  366. def test_star_graph(self):
  367. assert is_isomorphic(nx.star_graph(""), nx.empty_graph(0))
  368. assert is_isomorphic(nx.star_graph([]), nx.empty_graph(0))
  369. assert is_isomorphic(nx.star_graph(0), nx.empty_graph(1))
  370. assert is_isomorphic(nx.star_graph(1), nx.path_graph(2))
  371. assert is_isomorphic(nx.star_graph(2), nx.path_graph(3))
  372. assert is_isomorphic(nx.star_graph(5), nx.complete_bipartite_graph(1, 5))
  373. s = nx.star_graph(10)
  374. assert sorted(d for n, d in s.degree()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10]
  375. pytest.raises(nx.NetworkXError, nx.star_graph, 10, create_using=nx.DiGraph)
  376. ms = nx.star_graph(10, create_using=nx.MultiGraph)
  377. assert edges_equal(ms.edges(), s.edges())
  378. G = nx.star_graph("abc")
  379. assert len(G) == 3
  380. assert G.size() == 2
  381. G = nx.star_graph("abcb")
  382. assert len(G) == 3
  383. assert G.size() == 2
  384. G = nx.star_graph("abcb", create_using=nx.MultiGraph)
  385. assert len(G) == 3
  386. assert G.size() == 3
  387. G = nx.star_graph("abcdefg")
  388. assert len(G) == 7
  389. assert G.size() == 6
  390. def test_non_int_integers_for_star_graph(self):
  391. np = pytest.importorskip("numpy")
  392. G = nx.star_graph(np.int32(3))
  393. assert len(G) == 4
  394. assert G.size() == 3
  395. def test_trivial_graph(self):
  396. assert nx.number_of_nodes(nx.trivial_graph()) == 1
  397. def test_turan_graph(self):
  398. assert nx.number_of_edges(nx.turan_graph(13, 4)) == 63
  399. assert is_isomorphic(
  400. nx.turan_graph(13, 4), nx.complete_multipartite_graph(3, 4, 3, 3)
  401. )
  402. def test_wheel_graph(self):
  403. for n, G in [
  404. ("", nx.null_graph()),
  405. (0, nx.null_graph()),
  406. (1, nx.empty_graph(1)),
  407. (2, nx.path_graph(2)),
  408. (3, nx.complete_graph(3)),
  409. (4, nx.complete_graph(4)),
  410. ]:
  411. g = nx.wheel_graph(n)
  412. assert is_isomorphic(g, G)
  413. g = nx.wheel_graph(10)
  414. assert sorted(d for n, d in g.degree()) == [3, 3, 3, 3, 3, 3, 3, 3, 3, 9]
  415. pytest.raises(nx.NetworkXError, nx.wheel_graph, 10, create_using=nx.DiGraph)
  416. mg = nx.wheel_graph(10, create_using=nx.MultiGraph())
  417. assert edges_equal(mg.edges(), g.edges())
  418. G = nx.wheel_graph("abc")
  419. assert len(G) == 3
  420. assert G.size() == 3
  421. G = nx.wheel_graph("abcb")
  422. assert len(G) == 3
  423. assert G.size() == 4
  424. G = nx.wheel_graph("abcb", nx.MultiGraph)
  425. assert len(G) == 3
  426. assert G.size() == 6
  427. def test_non_int_integers_for_wheel_graph(self):
  428. np = pytest.importorskip("numpy")
  429. G = nx.wheel_graph(np.int32(3))
  430. assert len(G) == 3
  431. assert G.size() == 3
  432. def test_complete_0_partite_graph(self):
  433. """Tests that the complete 0-partite graph is the null graph."""
  434. G = nx.complete_multipartite_graph()
  435. H = nx.null_graph()
  436. assert nodes_equal(G, H)
  437. assert edges_equal(G.edges(), H.edges())
  438. def test_complete_1_partite_graph(self):
  439. """Tests that the complete 1-partite graph is the empty graph."""
  440. G = nx.complete_multipartite_graph(3)
  441. H = nx.empty_graph(3)
  442. assert nodes_equal(G, H)
  443. assert edges_equal(G.edges(), H.edges())
  444. def test_complete_2_partite_graph(self):
  445. """Tests that the complete 2-partite graph is the complete bipartite
  446. graph.
  447. """
  448. G = nx.complete_multipartite_graph(2, 3)
  449. H = nx.complete_bipartite_graph(2, 3)
  450. assert nodes_equal(G, H)
  451. assert edges_equal(G.edges(), H.edges())
  452. def test_complete_multipartite_graph(self):
  453. """Tests for generating the complete multipartite graph."""
  454. G = nx.complete_multipartite_graph(2, 3, 4)
  455. blocks = [(0, 1), (2, 3, 4), (5, 6, 7, 8)]
  456. # Within each block, no two vertices should be adjacent.
  457. for block in blocks:
  458. for u, v in itertools.combinations_with_replacement(block, 2):
  459. assert v not in G[u]
  460. assert G.nodes[u] == G.nodes[v]
  461. # Across blocks, all vertices should be adjacent.
  462. for block1, block2 in itertools.combinations(blocks, 2):
  463. for u, v in itertools.product(block1, block2):
  464. assert v in G[u]
  465. assert G.nodes[u] != G.nodes[v]