test_simple_paths.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. import random
  2. import pytest
  3. import networkx as nx
  4. from networkx import convert_node_labels_to_integers as cnlti
  5. from networkx.algorithms.simple_paths import (
  6. _bidirectional_dijkstra,
  7. _bidirectional_shortest_path,
  8. )
  9. from networkx.utils import arbitrary_element, pairwise
  10. class TestIsSimplePath:
  11. """Unit tests for the
  12. :func:`networkx.algorithms.simple_paths.is_simple_path` function.
  13. """
  14. def test_empty_list(self):
  15. """Tests that the empty list is not a valid path, since there
  16. should be a one-to-one correspondence between paths as lists of
  17. nodes and paths as lists of edges.
  18. """
  19. G = nx.trivial_graph()
  20. assert not nx.is_simple_path(G, [])
  21. def test_trivial_path(self):
  22. """Tests that the trivial path, a path of length one, is
  23. considered a simple path in a graph.
  24. """
  25. G = nx.trivial_graph()
  26. assert nx.is_simple_path(G, [0])
  27. def test_trivial_nonpath(self):
  28. """Tests that a list whose sole element is an object not in the
  29. graph is not considered a simple path.
  30. """
  31. G = nx.trivial_graph()
  32. assert not nx.is_simple_path(G, ["not a node"])
  33. def test_simple_path(self):
  34. G = nx.path_graph(2)
  35. assert nx.is_simple_path(G, [0, 1])
  36. def test_non_simple_path(self):
  37. G = nx.path_graph(2)
  38. assert not nx.is_simple_path(G, [0, 1, 0])
  39. def test_cycle(self):
  40. G = nx.cycle_graph(3)
  41. assert not nx.is_simple_path(G, [0, 1, 2, 0])
  42. def test_missing_node(self):
  43. G = nx.path_graph(2)
  44. assert not nx.is_simple_path(G, [0, 2])
  45. def test_missing_starting_node(self):
  46. G = nx.path_graph(2)
  47. assert not nx.is_simple_path(G, [2, 0])
  48. def test_directed_path(self):
  49. G = nx.DiGraph([(0, 1), (1, 2)])
  50. assert nx.is_simple_path(G, [0, 1, 2])
  51. def test_directed_non_path(self):
  52. G = nx.DiGraph([(0, 1), (1, 2)])
  53. assert not nx.is_simple_path(G, [2, 1, 0])
  54. def test_directed_cycle(self):
  55. G = nx.DiGraph([(0, 1), (1, 2), (2, 0)])
  56. assert not nx.is_simple_path(G, [0, 1, 2, 0])
  57. def test_multigraph(self):
  58. G = nx.MultiGraph([(0, 1), (0, 1)])
  59. assert nx.is_simple_path(G, [0, 1])
  60. def test_multidigraph(self):
  61. G = nx.MultiDiGraph([(0, 1), (0, 1), (1, 0), (1, 0)])
  62. assert nx.is_simple_path(G, [0, 1])
  63. # Tests for all_simple_paths
  64. def test_all_simple_paths():
  65. G = nx.path_graph(4)
  66. paths = nx.all_simple_paths(G, 0, 3)
  67. assert {tuple(p) for p in paths} == {(0, 1, 2, 3)}
  68. def test_all_simple_paths_with_two_targets_emits_two_paths():
  69. G = nx.path_graph(4)
  70. G.add_edge(2, 4)
  71. paths = nx.all_simple_paths(G, 0, [3, 4])
  72. assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
  73. def test_digraph_all_simple_paths_with_two_targets_emits_two_paths():
  74. G = nx.path_graph(4, create_using=nx.DiGraph())
  75. G.add_edge(2, 4)
  76. paths = nx.all_simple_paths(G, 0, [3, 4])
  77. assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
  78. def test_all_simple_paths_with_two_targets_cutoff():
  79. G = nx.path_graph(4)
  80. G.add_edge(2, 4)
  81. paths = nx.all_simple_paths(G, 0, [3, 4], cutoff=3)
  82. assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
  83. def test_digraph_all_simple_paths_with_two_targets_cutoff():
  84. G = nx.path_graph(4, create_using=nx.DiGraph())
  85. G.add_edge(2, 4)
  86. paths = nx.all_simple_paths(G, 0, [3, 4], cutoff=3)
  87. assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
  88. def test_all_simple_paths_with_two_targets_in_line_emits_two_paths():
  89. G = nx.path_graph(4)
  90. paths = nx.all_simple_paths(G, 0, [2, 3])
  91. assert {tuple(p) for p in paths} == {(0, 1, 2), (0, 1, 2, 3)}
  92. def test_all_simple_paths_ignores_cycle():
  93. G = nx.cycle_graph(3, create_using=nx.DiGraph())
  94. G.add_edge(1, 3)
  95. paths = nx.all_simple_paths(G, 0, 3)
  96. assert {tuple(p) for p in paths} == {(0, 1, 3)}
  97. def test_all_simple_paths_with_two_targets_inside_cycle_emits_two_paths():
  98. G = nx.cycle_graph(3, create_using=nx.DiGraph())
  99. G.add_edge(1, 3)
  100. paths = nx.all_simple_paths(G, 0, [2, 3])
  101. assert {tuple(p) for p in paths} == {(0, 1, 2), (0, 1, 3)}
  102. def test_all_simple_paths_source_target():
  103. G = nx.path_graph(4)
  104. paths = nx.all_simple_paths(G, 1, 1)
  105. assert list(paths) == []
  106. def test_all_simple_paths_cutoff():
  107. G = nx.complete_graph(4)
  108. paths = nx.all_simple_paths(G, 0, 1, cutoff=1)
  109. assert {tuple(p) for p in paths} == {(0, 1)}
  110. paths = nx.all_simple_paths(G, 0, 1, cutoff=2)
  111. assert {tuple(p) for p in paths} == {(0, 1), (0, 2, 1), (0, 3, 1)}
  112. def test_all_simple_paths_on_non_trivial_graph():
  113. """you may need to draw this graph to make sure it is reasonable"""
  114. G = nx.path_graph(5, create_using=nx.DiGraph())
  115. G.add_edges_from([(0, 5), (1, 5), (1, 3), (5, 4), (4, 2), (4, 3)])
  116. paths = nx.all_simple_paths(G, 1, [2, 3])
  117. assert {tuple(p) for p in paths} == {
  118. (1, 2),
  119. (1, 3, 4, 2),
  120. (1, 5, 4, 2),
  121. (1, 3),
  122. (1, 2, 3),
  123. (1, 5, 4, 3),
  124. (1, 5, 4, 2, 3),
  125. }
  126. paths = nx.all_simple_paths(G, 1, [2, 3], cutoff=3)
  127. assert {tuple(p) for p in paths} == {
  128. (1, 2),
  129. (1, 3, 4, 2),
  130. (1, 5, 4, 2),
  131. (1, 3),
  132. (1, 2, 3),
  133. (1, 5, 4, 3),
  134. }
  135. paths = nx.all_simple_paths(G, 1, [2, 3], cutoff=2)
  136. assert {tuple(p) for p in paths} == {(1, 2), (1, 3), (1, 2, 3)}
  137. def test_all_simple_paths_multigraph():
  138. G = nx.MultiGraph([(1, 2), (1, 2)])
  139. paths = nx.all_simple_paths(G, 1, 1)
  140. assert list(paths) == []
  141. nx.add_path(G, [3, 1, 10, 2])
  142. paths = list(nx.all_simple_paths(G, 1, 2))
  143. assert len(paths) == 3
  144. assert {tuple(p) for p in paths} == {(1, 2), (1, 2), (1, 10, 2)}
  145. def test_all_simple_paths_multigraph_with_cutoff():
  146. G = nx.MultiGraph([(1, 2), (1, 2), (1, 10), (10, 2)])
  147. paths = list(nx.all_simple_paths(G, 1, 2, cutoff=1))
  148. assert len(paths) == 2
  149. assert {tuple(p) for p in paths} == {(1, 2), (1, 2)}
  150. def test_all_simple_paths_directed():
  151. G = nx.DiGraph()
  152. nx.add_path(G, [1, 2, 3])
  153. nx.add_path(G, [3, 2, 1])
  154. paths = nx.all_simple_paths(G, 1, 3)
  155. assert {tuple(p) for p in paths} == {(1, 2, 3)}
  156. def test_all_simple_paths_empty():
  157. G = nx.path_graph(4)
  158. paths = nx.all_simple_paths(G, 0, 3, cutoff=2)
  159. assert list(paths) == []
  160. def test_all_simple_paths_corner_cases():
  161. assert list(nx.all_simple_paths(nx.empty_graph(2), 0, 0)) == []
  162. assert list(nx.all_simple_paths(nx.empty_graph(2), 0, 1)) == []
  163. assert list(nx.all_simple_paths(nx.path_graph(9), 0, 8, 0)) == []
  164. def hamiltonian_path(G, source):
  165. source = arbitrary_element(G)
  166. neighbors = set(G[source]) - {source}
  167. n = len(G)
  168. for target in neighbors:
  169. for path in nx.all_simple_paths(G, source, target):
  170. if len(path) == n:
  171. yield path
  172. def test_hamiltonian_path():
  173. from itertools import permutations
  174. G = nx.complete_graph(4)
  175. paths = [list(p) for p in hamiltonian_path(G, 0)]
  176. exact = [[0] + list(p) for p in permutations([1, 2, 3], 3)]
  177. assert sorted(paths) == sorted(exact)
  178. def test_cutoff_zero():
  179. G = nx.complete_graph(4)
  180. paths = nx.all_simple_paths(G, 0, 3, cutoff=0)
  181. assert [list(p) for p in paths] == []
  182. paths = nx.all_simple_paths(nx.MultiGraph(G), 0, 3, cutoff=0)
  183. assert [list(p) for p in paths] == []
  184. def test_source_missing():
  185. with pytest.raises(nx.NodeNotFound):
  186. G = nx.Graph()
  187. nx.add_path(G, [1, 2, 3])
  188. list(nx.all_simple_paths(nx.MultiGraph(G), 0, 3))
  189. def test_target_missing():
  190. with pytest.raises(nx.NodeNotFound):
  191. G = nx.Graph()
  192. nx.add_path(G, [1, 2, 3])
  193. list(nx.all_simple_paths(nx.MultiGraph(G), 1, 4))
  194. # Tests for all_simple_edge_paths
  195. def test_all_simple_edge_paths():
  196. G = nx.path_graph(4)
  197. paths = nx.all_simple_edge_paths(G, 0, 3)
  198. assert {tuple(p) for p in paths} == {((0, 1), (1, 2), (2, 3))}
  199. def test_all_simple_edge_paths_with_two_targets_emits_two_paths():
  200. G = nx.path_graph(4)
  201. G.add_edge(2, 4)
  202. paths = nx.all_simple_edge_paths(G, 0, [3, 4])
  203. assert {tuple(p) for p in paths} == {
  204. ((0, 1), (1, 2), (2, 3)),
  205. ((0, 1), (1, 2), (2, 4)),
  206. }
  207. def test_digraph_all_simple_edge_paths_with_two_targets_emits_two_paths():
  208. G = nx.path_graph(4, create_using=nx.DiGraph())
  209. G.add_edge(2, 4)
  210. paths = nx.all_simple_edge_paths(G, 0, [3, 4])
  211. assert {tuple(p) for p in paths} == {
  212. ((0, 1), (1, 2), (2, 3)),
  213. ((0, 1), (1, 2), (2, 4)),
  214. }
  215. def test_all_simple_edge_paths_with_two_targets_cutoff():
  216. G = nx.path_graph(4)
  217. G.add_edge(2, 4)
  218. paths = nx.all_simple_edge_paths(G, 0, [3, 4], cutoff=3)
  219. assert {tuple(p) for p in paths} == {
  220. ((0, 1), (1, 2), (2, 3)),
  221. ((0, 1), (1, 2), (2, 4)),
  222. }
  223. def test_digraph_all_simple_edge_paths_with_two_targets_cutoff():
  224. G = nx.path_graph(4, create_using=nx.DiGraph())
  225. G.add_edge(2, 4)
  226. paths = nx.all_simple_edge_paths(G, 0, [3, 4], cutoff=3)
  227. assert {tuple(p) for p in paths} == {
  228. ((0, 1), (1, 2), (2, 3)),
  229. ((0, 1), (1, 2), (2, 4)),
  230. }
  231. def test_all_simple_edge_paths_with_two_targets_in_line_emits_two_paths():
  232. G = nx.path_graph(4)
  233. paths = nx.all_simple_edge_paths(G, 0, [2, 3])
  234. assert {tuple(p) for p in paths} == {((0, 1), (1, 2)), ((0, 1), (1, 2), (2, 3))}
  235. def test_all_simple_edge_paths_ignores_cycle():
  236. G = nx.cycle_graph(3, create_using=nx.DiGraph())
  237. G.add_edge(1, 3)
  238. paths = nx.all_simple_edge_paths(G, 0, 3)
  239. assert {tuple(p) for p in paths} == {((0, 1), (1, 3))}
  240. def test_all_simple_edge_paths_with_two_targets_inside_cycle_emits_two_paths():
  241. G = nx.cycle_graph(3, create_using=nx.DiGraph())
  242. G.add_edge(1, 3)
  243. paths = nx.all_simple_edge_paths(G, 0, [2, 3])
  244. assert {tuple(p) for p in paths} == {((0, 1), (1, 2)), ((0, 1), (1, 3))}
  245. def test_all_simple_edge_paths_source_target():
  246. G = nx.path_graph(4)
  247. paths = nx.all_simple_edge_paths(G, 1, 1)
  248. assert list(paths) == []
  249. def test_all_simple_edge_paths_cutoff():
  250. G = nx.complete_graph(4)
  251. paths = nx.all_simple_edge_paths(G, 0, 1, cutoff=1)
  252. assert {tuple(p) for p in paths} == {((0, 1),)}
  253. paths = nx.all_simple_edge_paths(G, 0, 1, cutoff=2)
  254. assert {tuple(p) for p in paths} == {((0, 1),), ((0, 2), (2, 1)), ((0, 3), (3, 1))}
  255. def test_all_simple_edge_paths_on_non_trivial_graph():
  256. """you may need to draw this graph to make sure it is reasonable"""
  257. G = nx.path_graph(5, create_using=nx.DiGraph())
  258. G.add_edges_from([(0, 5), (1, 5), (1, 3), (5, 4), (4, 2), (4, 3)])
  259. paths = nx.all_simple_edge_paths(G, 1, [2, 3])
  260. assert {tuple(p) for p in paths} == {
  261. ((1, 2),),
  262. ((1, 3), (3, 4), (4, 2)),
  263. ((1, 5), (5, 4), (4, 2)),
  264. ((1, 3),),
  265. ((1, 2), (2, 3)),
  266. ((1, 5), (5, 4), (4, 3)),
  267. ((1, 5), (5, 4), (4, 2), (2, 3)),
  268. }
  269. paths = nx.all_simple_edge_paths(G, 1, [2, 3], cutoff=3)
  270. assert {tuple(p) for p in paths} == {
  271. ((1, 2),),
  272. ((1, 3), (3, 4), (4, 2)),
  273. ((1, 5), (5, 4), (4, 2)),
  274. ((1, 3),),
  275. ((1, 2), (2, 3)),
  276. ((1, 5), (5, 4), (4, 3)),
  277. }
  278. paths = nx.all_simple_edge_paths(G, 1, [2, 3], cutoff=2)
  279. assert {tuple(p) for p in paths} == {((1, 2),), ((1, 3),), ((1, 2), (2, 3))}
  280. def test_all_simple_edge_paths_multigraph():
  281. G = nx.MultiGraph([(1, 2), (1, 2)])
  282. paths = nx.all_simple_edge_paths(G, 1, 1)
  283. assert list(paths) == []
  284. nx.add_path(G, [3, 1, 10, 2])
  285. paths = list(nx.all_simple_edge_paths(G, 1, 2))
  286. assert len(paths) == 3
  287. assert {tuple(p) for p in paths} == {
  288. ((1, 2, 0),),
  289. ((1, 2, 1),),
  290. ((1, 10, 0), (10, 2, 0)),
  291. }
  292. def test_all_simple_edge_paths_multigraph_with_cutoff():
  293. G = nx.MultiGraph([(1, 2), (1, 2), (1, 10), (10, 2)])
  294. paths = list(nx.all_simple_edge_paths(G, 1, 2, cutoff=1))
  295. assert len(paths) == 2
  296. assert {tuple(p) for p in paths} == {((1, 2, 0),), ((1, 2, 1),)}
  297. def test_all_simple_edge_paths_directed():
  298. G = nx.DiGraph()
  299. nx.add_path(G, [1, 2, 3])
  300. nx.add_path(G, [3, 2, 1])
  301. paths = nx.all_simple_edge_paths(G, 1, 3)
  302. assert {tuple(p) for p in paths} == {((1, 2), (2, 3))}
  303. def test_all_simple_edge_paths_empty():
  304. G = nx.path_graph(4)
  305. paths = nx.all_simple_edge_paths(G, 0, 3, cutoff=2)
  306. assert list(paths) == []
  307. def test_all_simple_edge_paths_corner_cases():
  308. assert list(nx.all_simple_edge_paths(nx.empty_graph(2), 0, 0)) == []
  309. assert list(nx.all_simple_edge_paths(nx.empty_graph(2), 0, 1)) == []
  310. assert list(nx.all_simple_edge_paths(nx.path_graph(9), 0, 8, 0)) == []
  311. def hamiltonian_edge_path(G, source):
  312. source = arbitrary_element(G)
  313. neighbors = set(G[source]) - {source}
  314. n = len(G)
  315. for target in neighbors:
  316. for path in nx.all_simple_edge_paths(G, source, target):
  317. if len(path) == n - 1:
  318. yield path
  319. def test_hamiltonian__edge_path():
  320. from itertools import permutations
  321. G = nx.complete_graph(4)
  322. paths = hamiltonian_edge_path(G, 0)
  323. exact = [list(pairwise([0] + list(p))) for p in permutations([1, 2, 3], 3)]
  324. assert sorted(exact) == sorted(paths)
  325. def test_edge_cutoff_zero():
  326. G = nx.complete_graph(4)
  327. paths = nx.all_simple_edge_paths(G, 0, 3, cutoff=0)
  328. assert [list(p) for p in paths] == []
  329. paths = nx.all_simple_edge_paths(nx.MultiGraph(G), 0, 3, cutoff=0)
  330. assert [list(p) for p in paths] == []
  331. def test_edge_source_missing():
  332. with pytest.raises(nx.NodeNotFound):
  333. G = nx.Graph()
  334. nx.add_path(G, [1, 2, 3])
  335. list(nx.all_simple_edge_paths(nx.MultiGraph(G), 0, 3))
  336. def test_edge_target_missing():
  337. with pytest.raises(nx.NodeNotFound):
  338. G = nx.Graph()
  339. nx.add_path(G, [1, 2, 3])
  340. list(nx.all_simple_edge_paths(nx.MultiGraph(G), 1, 4))
  341. # Tests for shortest_simple_paths
  342. def test_shortest_simple_paths():
  343. G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
  344. paths = nx.shortest_simple_paths(G, 1, 12)
  345. assert next(paths) == [1, 2, 3, 4, 8, 12]
  346. assert next(paths) == [1, 5, 6, 7, 8, 12]
  347. assert [len(path) for path in nx.shortest_simple_paths(G, 1, 12)] == sorted(
  348. len(path) for path in nx.all_simple_paths(G, 1, 12)
  349. )
  350. def test_shortest_simple_paths_directed():
  351. G = nx.cycle_graph(7, create_using=nx.DiGraph())
  352. paths = nx.shortest_simple_paths(G, 0, 3)
  353. assert list(paths) == [[0, 1, 2, 3]]
  354. def test_shortest_simple_paths_directed_with_weight_function():
  355. def cost(u, v, x):
  356. return 1
  357. G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
  358. paths = nx.shortest_simple_paths(G, 1, 12)
  359. assert next(paths) == [1, 2, 3, 4, 8, 12]
  360. assert next(paths) == [1, 5, 6, 7, 8, 12]
  361. assert [
  362. len(path) for path in nx.shortest_simple_paths(G, 1, 12, weight=cost)
  363. ] == sorted(len(path) for path in nx.all_simple_paths(G, 1, 12))
  364. def test_shortest_simple_paths_with_weight_function():
  365. def cost(u, v, x):
  366. return 1
  367. G = nx.cycle_graph(7, create_using=nx.DiGraph())
  368. paths = nx.shortest_simple_paths(G, 0, 3, weight=cost)
  369. assert list(paths) == [[0, 1, 2, 3]]
  370. def test_Greg_Bernstein():
  371. g1 = nx.Graph()
  372. g1.add_nodes_from(["N0", "N1", "N2", "N3", "N4"])
  373. g1.add_edge("N4", "N1", weight=10.0, capacity=50, name="L5")
  374. g1.add_edge("N4", "N0", weight=7.0, capacity=40, name="L4")
  375. g1.add_edge("N0", "N1", weight=10.0, capacity=45, name="L1")
  376. g1.add_edge("N3", "N0", weight=10.0, capacity=50, name="L0")
  377. g1.add_edge("N2", "N3", weight=12.0, capacity=30, name="L2")
  378. g1.add_edge("N1", "N2", weight=15.0, capacity=42, name="L3")
  379. solution = [["N1", "N0", "N3"], ["N1", "N2", "N3"], ["N1", "N4", "N0", "N3"]]
  380. result = list(nx.shortest_simple_paths(g1, "N1", "N3", weight="weight"))
  381. assert result == solution
  382. def test_weighted_shortest_simple_path():
  383. def cost_func(path):
  384. return sum(G.adj[u][v]["weight"] for (u, v) in zip(path, path[1:]))
  385. G = nx.complete_graph(5)
  386. weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
  387. nx.set_edge_attributes(G, weight, "weight")
  388. cost = 0
  389. for path in nx.shortest_simple_paths(G, 0, 3, weight="weight"):
  390. this_cost = cost_func(path)
  391. assert cost <= this_cost
  392. cost = this_cost
  393. def test_directed_weighted_shortest_simple_path():
  394. def cost_func(path):
  395. return sum(G.adj[u][v]["weight"] for (u, v) in zip(path, path[1:]))
  396. G = nx.complete_graph(5)
  397. G = G.to_directed()
  398. weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
  399. nx.set_edge_attributes(G, weight, "weight")
  400. cost = 0
  401. for path in nx.shortest_simple_paths(G, 0, 3, weight="weight"):
  402. this_cost = cost_func(path)
  403. assert cost <= this_cost
  404. cost = this_cost
  405. def test_weighted_shortest_simple_path_issue2427():
  406. G = nx.Graph()
  407. G.add_edge("IN", "OUT", weight=2)
  408. G.add_edge("IN", "A", weight=1)
  409. G.add_edge("IN", "B", weight=2)
  410. G.add_edge("B", "OUT", weight=2)
  411. assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
  412. ["IN", "OUT"],
  413. ["IN", "B", "OUT"],
  414. ]
  415. G = nx.Graph()
  416. G.add_edge("IN", "OUT", weight=10)
  417. G.add_edge("IN", "A", weight=1)
  418. G.add_edge("IN", "B", weight=1)
  419. G.add_edge("B", "OUT", weight=1)
  420. assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
  421. ["IN", "B", "OUT"],
  422. ["IN", "OUT"],
  423. ]
  424. def test_directed_weighted_shortest_simple_path_issue2427():
  425. G = nx.DiGraph()
  426. G.add_edge("IN", "OUT", weight=2)
  427. G.add_edge("IN", "A", weight=1)
  428. G.add_edge("IN", "B", weight=2)
  429. G.add_edge("B", "OUT", weight=2)
  430. assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
  431. ["IN", "OUT"],
  432. ["IN", "B", "OUT"],
  433. ]
  434. G = nx.DiGraph()
  435. G.add_edge("IN", "OUT", weight=10)
  436. G.add_edge("IN", "A", weight=1)
  437. G.add_edge("IN", "B", weight=1)
  438. G.add_edge("B", "OUT", weight=1)
  439. assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
  440. ["IN", "B", "OUT"],
  441. ["IN", "OUT"],
  442. ]
  443. def test_weight_name():
  444. G = nx.cycle_graph(7)
  445. nx.set_edge_attributes(G, 1, "weight")
  446. nx.set_edge_attributes(G, 1, "foo")
  447. G.adj[1][2]["foo"] = 7
  448. paths = list(nx.shortest_simple_paths(G, 0, 3, weight="foo"))
  449. solution = [[0, 6, 5, 4, 3], [0, 1, 2, 3]]
  450. assert paths == solution
  451. def test_ssp_source_missing():
  452. with pytest.raises(nx.NodeNotFound):
  453. G = nx.Graph()
  454. nx.add_path(G, [1, 2, 3])
  455. list(nx.shortest_simple_paths(G, 0, 3))
  456. def test_ssp_target_missing():
  457. with pytest.raises(nx.NodeNotFound):
  458. G = nx.Graph()
  459. nx.add_path(G, [1, 2, 3])
  460. list(nx.shortest_simple_paths(G, 1, 4))
  461. def test_ssp_multigraph():
  462. with pytest.raises(nx.NetworkXNotImplemented):
  463. G = nx.MultiGraph()
  464. nx.add_path(G, [1, 2, 3])
  465. list(nx.shortest_simple_paths(G, 1, 4))
  466. def test_ssp_source_missing2():
  467. with pytest.raises(nx.NetworkXNoPath):
  468. G = nx.Graph()
  469. nx.add_path(G, [0, 1, 2])
  470. nx.add_path(G, [3, 4, 5])
  471. list(nx.shortest_simple_paths(G, 0, 3))
  472. def test_bidirectional_shortest_path_restricted_cycle():
  473. cycle = nx.cycle_graph(7)
  474. length, path = _bidirectional_shortest_path(cycle, 0, 3)
  475. assert path == [0, 1, 2, 3]
  476. length, path = _bidirectional_shortest_path(cycle, 0, 3, ignore_nodes=[1])
  477. assert path == [0, 6, 5, 4, 3]
  478. def test_bidirectional_shortest_path_restricted_wheel():
  479. wheel = nx.wheel_graph(6)
  480. length, path = _bidirectional_shortest_path(wheel, 1, 3)
  481. assert path in [[1, 0, 3], [1, 2, 3]]
  482. length, path = _bidirectional_shortest_path(wheel, 1, 3, ignore_nodes=[0])
  483. assert path == [1, 2, 3]
  484. length, path = _bidirectional_shortest_path(wheel, 1, 3, ignore_nodes=[0, 2])
  485. assert path == [1, 5, 4, 3]
  486. length, path = _bidirectional_shortest_path(
  487. wheel, 1, 3, ignore_edges=[(1, 0), (5, 0), (2, 3)]
  488. )
  489. assert path in [[1, 2, 0, 3], [1, 5, 4, 3]]
  490. def test_bidirectional_shortest_path_restricted_directed_cycle():
  491. directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph())
  492. length, path = _bidirectional_shortest_path(directed_cycle, 0, 3)
  493. assert path == [0, 1, 2, 3]
  494. pytest.raises(
  495. nx.NetworkXNoPath,
  496. _bidirectional_shortest_path,
  497. directed_cycle,
  498. 0,
  499. 3,
  500. ignore_nodes=[1],
  501. )
  502. length, path = _bidirectional_shortest_path(
  503. directed_cycle, 0, 3, ignore_edges=[(2, 1)]
  504. )
  505. assert path == [0, 1, 2, 3]
  506. pytest.raises(
  507. nx.NetworkXNoPath,
  508. _bidirectional_shortest_path,
  509. directed_cycle,
  510. 0,
  511. 3,
  512. ignore_edges=[(1, 2)],
  513. )
  514. def test_bidirectional_shortest_path_ignore():
  515. G = nx.Graph()
  516. nx.add_path(G, [1, 2])
  517. nx.add_path(G, [1, 3])
  518. nx.add_path(G, [1, 4])
  519. pytest.raises(
  520. nx.NetworkXNoPath, _bidirectional_shortest_path, G, 1, 2, ignore_nodes=[1]
  521. )
  522. pytest.raises(
  523. nx.NetworkXNoPath, _bidirectional_shortest_path, G, 1, 2, ignore_nodes=[2]
  524. )
  525. G = nx.Graph()
  526. nx.add_path(G, [1, 3])
  527. nx.add_path(G, [1, 4])
  528. nx.add_path(G, [3, 2])
  529. pytest.raises(
  530. nx.NetworkXNoPath, _bidirectional_shortest_path, G, 1, 2, ignore_nodes=[1, 2]
  531. )
  532. def validate_path(G, s, t, soln_len, path):
  533. assert path[0] == s
  534. assert path[-1] == t
  535. assert soln_len == sum(
  536. G[u][v].get("weight", 1) for u, v in zip(path[:-1], path[1:])
  537. )
  538. def validate_length_path(G, s, t, soln_len, length, path):
  539. assert soln_len == length
  540. validate_path(G, s, t, length, path)
  541. def test_bidirectional_dijksta_restricted():
  542. XG = nx.DiGraph()
  543. XG.add_weighted_edges_from(
  544. [
  545. ("s", "u", 10),
  546. ("s", "x", 5),
  547. ("u", "v", 1),
  548. ("u", "x", 2),
  549. ("v", "y", 1),
  550. ("x", "u", 3),
  551. ("x", "v", 5),
  552. ("x", "y", 2),
  553. ("y", "s", 7),
  554. ("y", "v", 6),
  555. ]
  556. )
  557. XG3 = nx.Graph()
  558. XG3.add_weighted_edges_from(
  559. [[0, 1, 2], [1, 2, 12], [2, 3, 1], [3, 4, 5], [4, 5, 1], [5, 0, 10]]
  560. )
  561. validate_length_path(XG, "s", "v", 9, *_bidirectional_dijkstra(XG, "s", "v"))
  562. validate_length_path(
  563. XG, "s", "v", 10, *_bidirectional_dijkstra(XG, "s", "v", ignore_nodes=["u"])
  564. )
  565. validate_length_path(
  566. XG,
  567. "s",
  568. "v",
  569. 11,
  570. *_bidirectional_dijkstra(XG, "s", "v", ignore_edges=[("s", "x")]),
  571. )
  572. pytest.raises(
  573. nx.NetworkXNoPath,
  574. _bidirectional_dijkstra,
  575. XG,
  576. "s",
  577. "v",
  578. ignore_nodes=["u"],
  579. ignore_edges=[("s", "x")],
  580. )
  581. validate_length_path(XG3, 0, 3, 15, *_bidirectional_dijkstra(XG3, 0, 3))
  582. validate_length_path(
  583. XG3, 0, 3, 16, *_bidirectional_dijkstra(XG3, 0, 3, ignore_nodes=[1])
  584. )
  585. validate_length_path(
  586. XG3, 0, 3, 16, *_bidirectional_dijkstra(XG3, 0, 3, ignore_edges=[(2, 3)])
  587. )
  588. pytest.raises(
  589. nx.NetworkXNoPath,
  590. _bidirectional_dijkstra,
  591. XG3,
  592. 0,
  593. 3,
  594. ignore_nodes=[1],
  595. ignore_edges=[(5, 4)],
  596. )
  597. def test_bidirectional_dijkstra_no_path():
  598. with pytest.raises(nx.NetworkXNoPath):
  599. G = nx.Graph()
  600. nx.add_path(G, [1, 2, 3])
  601. nx.add_path(G, [4, 5, 6])
  602. _bidirectional_dijkstra(G, 1, 6)
  603. def test_bidirectional_dijkstra_ignore():
  604. G = nx.Graph()
  605. nx.add_path(G, [1, 2, 10])
  606. nx.add_path(G, [1, 3, 10])
  607. pytest.raises(nx.NetworkXNoPath, _bidirectional_dijkstra, G, 1, 2, ignore_nodes=[1])
  608. pytest.raises(nx.NetworkXNoPath, _bidirectional_dijkstra, G, 1, 2, ignore_nodes=[2])
  609. pytest.raises(
  610. nx.NetworkXNoPath, _bidirectional_dijkstra, G, 1, 2, ignore_nodes=[1, 2]
  611. )