4
>>> import networkx as NX
5
>>> from networkx.operators import convert_node_labels_to_integers as cnlti
6
>>> G=cnlti(NX.grid_2d_graph(4,4),first_label=1,ordering="sorted")
10
>>> H=NX.cycle_graph(7)
11
>>> DH=NX.cycle_graph(7,create_using=NX.DiGraph())
13
Paths and path lengths
14
----------------------
16
>>> NX.shortest_path_length(G,1,16)
18
>>> sp=NX.single_source_shortest_path_length(G,1)
21
>>> NX.shortest_path(G,1,12)
24
>>> NX.bidirectional_shortest_path(G,1,12)
27
>>> sp=NX.single_source_shortest_path(G,1)
31
>>> NX.shortest_path(H,0,3)
33
>>> NX.shortest_path(H,0,4)
35
>>> NX.bidirectional_shortest_path(H,0,3)
37
>>> NX.bidirectional_shortest_path(H,0,4)
40
>>> NX.shortest_path(DH,0,3)
42
>>> NX.shortest_path(DH,0,4)
44
>>> NX.bidirectional_shortest_path(DH,0,3)
46
>>> NX.bidirectional_shortest_path(DH,0,4)
56
>>> XG.add_edges_from([('s','u',10) ,('s','x',5) ,('u','v',1) ,('u','x',2) ,('v','y',1) ,('x','u',3) ,('x','v',5) ,('x','y',2) ,('y','s',7) ,('y','v',6)])
57
>>> (D,P)= NX.single_source_dijkstra(XG,'s')
62
>>> NX.single_source_dijkstra_path(XG,'s')['v']
64
>>> NX.single_source_dijkstra_path_length(XG,'s')['v']
67
>>> NX.single_source_dijkstra(XG,'s')[1]['v']
70
>>> GG=XG.to_undirected()
71
>>> (D,P)= NX.single_source_dijkstra(GG,'s')
74
>>> D['v'] # uses lower weight of 2 on u<->x edge
77
>>> NX.dijkstra_path(GG,'s','v')
79
>>> NX.dijkstra_path_length(GG,'s','v')
83
>>> XG2.add_edges_from([[1,4,1],[4,5,1],[5,6,1],[6,3,1],[1,3,50],[1,2,100],[2,3,100]])
84
>>> NX.dijkstra_path(XG2,1,3)
88
>>> XG3.add_edges_from([ [0,1,2],[1,2,12],[2,3,1],[3,4,5],[4,5,1],[5,0,10] ])
89
>>> NX.dijkstra_path(XG3,0,3)
91
>>> NX.dijkstra_path_length(XG3,0,3)
94
>>> XG4.add_edges_from([ [0,1,2],[1,2,2],[2,3,1],[3,4,1],[4,5,1],[5,6,1],[6,7,1],[7,0,1] ])
95
>>> NX.dijkstra_path(XG4,0,2)
97
>>> NX.dijkstra_path_length(XG4,0,2)
100
>>> G=NX.DiGraph() # no weights
101
>>> G.add_edges_from([('s','u'), ('s','x'), ('u','v'), ('u','x'), ('v','y'), ('x','u'), ('x','v'), ('x','y'), ('y','s'), ('y','v')])
102
>>> NX.single_source_dijkstra(G,'s','v')[1]['v']
104
>>> NX.single_source_dijkstra(G,'s')[1]['v']
107
>>> NX.dijkstra_path(G,'s','v')
109
>>> NX.dijkstra_path_length(G,'s','v')
113
>>> NX.dijkstra_path(G,'s','moon')
114
Traceback (most recent call last):
116
NetworkXError: node s not reachable from moon
119
>>> NX.dijkstra_path_length(G,'s','moon')
120
Traceback (most recent call last):
122
NetworkXError: node s not reachable from moon
124
>>> NX.dijkstra_path(H,0,3)
127
>>> NX.dijkstra_path(H,0,4)
130
Bidirectional Dijkstra
131
----------------------
133
>>> NX.bidirectional_dijkstra(XG, 's', 'v')
134
(9, ['s', 'x', 'u', 'v'])
135
>>> NX.bidirectional_dijkstra(GG,'s','v')
137
>>> NX.bidirectional_dijkstra(G,'s','v')
139
>>> NX.bidirectional_dijkstra(H,0,3)
141
>>> NX.bidirectional_dijkstra(H,0,4)
143
>>> NX.bidirectional_dijkstra(XG3,0,3)
145
>>> NX.bidirectional_dijkstra(XG4,0,2)
148
# need more tests here
149
>>> NX.dijkstra_path(XG,'s','v')==NX.single_source_dijkstra_path(XG,'s')['v']
152
Floyd-Warshall all pair shortest path
153
-------------------------------------
155
>>> dist, path = NX.floyd_warshall(XG)
162
{'y': {'y': 0, 'x': 12, 's': 7, 'u': 15, 'v': 6}, 'x': {'y': 2, 'x': 0, 's': 9, 'u': 3, 'v': 4}, 's': {'y': 7, 'x': 5, 's': 0, 'u': 8, 'v': 9}, 'u': {'y': 2, 'x': 2, 's': 9, 'u': 0, 'v': 1}, 'v': {'y': 1, 'x': 13, 's': 8, 'u': 16, 'v': 0}}
164
>>> dist, path = NX.floyd_warshall(GG)
169
>>> dist, path = NX.floyd_warshall(G)
176
>>> dist, path = NX.floyd_warshall(H)
186
>>> XG3.add_edges_from([ [0,1,2],[1,2,12],[2,3,1],[3,4,5],[4,5,1],[5,0,10] ])
187
>>> dist, path = NX.floyd_warshall(XG3)
194
>>> XG4.add_edges_from([ [0,1,2],[1,2,2],[2,3,1],[3,4,1],[4,5,1],[5,6,1],[6,7,1],[7,0,1] ])
195
>>> dist, path = NX.floyd_warshall(XG4)
206
>>> G=NX.path_graph(4)
207
>>> NX.predecessor(G,0)
208
{0: [], 1: [0], 2: [1], 3: [2]}
209
>>> NX.predecessor(G,0,3)
211
>>> G=NX.grid_2d_graph(2,2)
212
>>> sorted(NX.predecessor(G,(0,0)).items())
213
[((0, 0), []), ((0, 1), [(0, 0)]), ((1, 0), [(0, 0)]), ((1, 1), [(0, 1), (1, 0)])]
218
>>> G=NX.path_graph(4)
219
>>> NX.dijkstra_predecessor_and_distance(G,0)
220
({0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3})
221
>>> G=NX.grid_2d_graph(2,2)
222
>>> pred,dist=NX.dijkstra_predecessor_and_distance(G,(0,0))
223
>>> sorted(pred.items())
224
[((0, 0), []), ((0, 1), [(0, 0)]), ((1, 0), [(0, 0)]), ((1, 1), [(0, 1), (1, 0)])]
225
>>> sorted(dist.items())
226
[((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)]
229
>>> XG.add_edges_from([('s','u',10) ,('s','x',5) ,('u','v',1) ,('u','x',2) ,('v','y',1) ,('x','u',3) ,('x','v',5) ,('x','y',2) ,('y','s',7) ,('y','v',6)])
230
>>> (P,D)= NX.dijkstra_predecessor_and_distance(XG,'s')