Python networkx.dijkstra_path() Examples
The following are 20
code examples of networkx.dijkstra_path().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: test_astar.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 7 votes |
def test_random_graph(self): def dist(a, b): (x1, y1) = a (x2, y2) = b return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 G = nx.Graph() points = [(random(), random()) for _ in range(100)] # Build a path from points[0] to points[-1] to be sure it exists for p1, p2 in zip(points[:-1], points[1:]): G.add_edge(p1, p2, weight=dist(p1, p2)) # Add other random edges for _ in range(100): p1, p2 = choice(points), choice(points) G.add_edge(p1, p2, weight=dist(p1, p2)) path = nx.astar_path(G, points[0], points[-1], dist) assert path == nx.dijkstra_path(G, points[0], points[-1])
Example #2
Source File: computePathStats.py From Beeline with GNU General Public License v3.0 | 6 votes |
def getEdgeHistogram(inGraph, refGraph): falsePositives = set(inGraph.edges()).difference(refGraph.edges()) edgeHistogramCounts = {0:0} for fe in falsePositives: u,v = fe try: path = nx.dijkstra_path(refGraph,u,v) pathlength = len(path) -1 if pathlength in edgeHistogramCounts.keys(): edgeHistogramCounts[pathlength] +=1 else: edgeHistogramCounts[pathlength] = 0 except nx.exception.NetworkXNoPath: edgeHistogramCounts[0] +=1 return edgeHistogramCounts
Example #3
Source File: test_astar.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_random_graph(self): """Tests that the A* shortest path agrees with Dijkstra's shortest path for a random graph. """ G = nx.Graph() points = [(random(), random()) for _ in range(100)] # Build a path from points[0] to points[-1] to be sure it exists for p1, p2 in pairwise(points): G.add_edge(p1, p2, weight=dist(p1, p2)) # Add other random edges for _ in range(100): p1, p2 = choice(points), choice(points) G.add_edge(p1, p2, weight=dist(p1, p2)) path = nx.astar_path(G, points[0], points[-1], dist) assert_equal(path, nx.dijkstra_path(G, points[0], points[-1]))
Example #4
Source File: test_weighted.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_bidirectional_dijkstra(self): validate_length_path( self.XG, 's', 'v', 9, *nx.bidirectional_dijkstra(self.XG, 's', 'v')) validate_length_path( self.G, 's', 'v', 2, *nx.bidirectional_dijkstra(self.G, 's', 'v')) validate_length_path( self.cycle, 0, 3, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 3)) validate_length_path( self.cycle, 0, 4, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 4)) validate_length_path( self.XG3, 0, 3, 15, *nx.bidirectional_dijkstra(self.XG3, 0, 3)) validate_length_path( self.XG4, 0, 2, 4, *nx.bidirectional_dijkstra(self.XG4, 0, 2)) # need more tests here P = nx.single_source_dijkstra_path(self.XG, 's')['v'] validate_path(self.XG, 's', 'v', sum(self.XG[u][v]['weight'] for u, v in zip( P[:-1], P[1:])), nx.dijkstra_path(self.XG, 's', 'v'))
Example #5
Source File: test_weighted.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_bidirectional_dijkstra(self): validate_length_path( self.XG, 's', 'v', 9, *nx.bidirectional_dijkstra(self.XG, 's', 'v')) validate_length_path( self.G, 's', 'v', 2, *nx.bidirectional_dijkstra(self.G, 's', 'v')) validate_length_path( self.cycle, 0, 3, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 3)) validate_length_path( self.cycle, 0, 4, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 4)) validate_length_path( self.XG3, 0, 3, 15, *nx.bidirectional_dijkstra(self.XG3, 0, 3)) validate_length_path( self.XG4, 0, 2, 4, *nx.bidirectional_dijkstra(self.XG4, 0, 2)) # need more tests here P = nx.single_source_dijkstra_path(self.XG, 's')['v'] validate_path(self.XG, 's', 'v', sum(self.XG[u][v]['weight'] for u, v in zip( P[:-1], P[1:])), nx.dijkstra_path(self.XG, 's', 'v'))
Example #6
Source File: test_astar.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_astar_undirected3(self): XG4=nx.Graph() XG4.add_edges_from([ [0,1,{'weight':2}], [1,2,{'weight':2}], [2,3,{'weight':1}], [3,4,{'weight':1}], [4,5,{'weight':1}], [5,6,{'weight':1}], [6,7,{'weight':1}], [7,0,{'weight':1}] ]) assert nx.astar_path(XG4,0,2)==[0, 1, 2] assert nx.astar_path_length(XG4,0,2)==4 # >>> MXG4=NX.MultiGraph(XG4) # >>> MXG4.add_edge(0,1,3) # >>> NX.dijkstra_path(MXG4,0,2) # [0, 1, 2]
Example #7
Source File: test_astar.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_random_graph(self): """Tests that the A* shortest path agrees with Dijkstra's shortest path for a random graph. """ G = nx.Graph() points = [(random(), random()) for _ in range(100)] # Build a path from points[0] to points[-1] to be sure it exists for p1, p2 in pairwise(points): G.add_edge(p1, p2, weight=dist(p1, p2)) # Add other random edges for _ in range(100): p1, p2 = choice(points), choice(points) G.add_edge(p1, p2, weight=dist(p1, p2)) path = nx.astar_path(G, points[0], points[-1], dist) assert_equal(path, nx.dijkstra_path(G, points[0], points[-1]))
Example #8
Source File: test_weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_bidirectional_dijkstra(self): validate_length_path( self.XG, 's', 'v', 9, *nx.bidirectional_dijkstra(self.XG, 's', 'v')) validate_length_path( self.G, 's', 'v', 2, *nx.bidirectional_dijkstra(self.G, 's', 'v')) validate_length_path( self.cycle, 0, 3, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 3)) validate_length_path( self.cycle, 0, 4, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 4)) validate_length_path( self.XG3, 0, 3, 15, *nx.bidirectional_dijkstra(self.XG3, 0, 3)) validate_length_path( self.XG4, 0, 2, 4, *nx.bidirectional_dijkstra(self.XG4, 0, 2)) # need more tests here P = nx.single_source_dijkstra_path(self.XG, 's')['v'] validate_path(self.XG, 's', 'v', sum(self.XG[u][v]['weight'] for u, v in zip( P[:-1], P[1:])), nx.dijkstra_path(self.XG, 's', 'v')) # check absent source G = nx.path_graph(2) assert_raises(nx.NodeNotFound, nx.bidirectional_dijkstra, G, 3, 0)
Example #9
Source File: computePathStats.py From Beeline with GNU General Public License v3.0 | 5 votes |
def pathStats(inGraph, refGraph): """ Only returns TP, FP, numPredictions for each networks """ falsePositives = set(inGraph.edges()).difference(refGraph.edges()) truePositives = set(inGraph.edges()).intersection(refGraph.edges()) numPredictions = len(inGraph.edges()) nopath = 0 yespath = 0 edgeCounts = {0:0,2:0,3:0,4:0,5:0} for fe in falsePositives: u,v = fe try: path = nx.dijkstra_path(refGraph,u,v) pathlength = len(path) -1 yespath +=1 if pathlength in edgeCounts.keys(): edgeCounts[pathlength] +=1 except nx.exception.NetworkXNoPath: nopath +=1 edgeCounts['numPred'] = numPredictions edgeCounts['numTP'] = len(truePositives) edgeCounts['numFP_withPath'] = yespath edgeCounts['numFP_noPath'] = nopath return edgeCounts
Example #10
Source File: test_astar.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_astar_undirected3(self): XG4 = nx.Graph() edges = [(0, 1, 2), (1, 2, 2), (2, 3, 1), (3, 4, 1), (4, 5, 1), (5, 6, 1), (6, 7, 1), (7, 0, 1)] XG4.add_weighted_edges_from(edges) assert_equal(nx.astar_path(XG4, 0, 2), [0, 1, 2]) assert_equal(nx.astar_path_length(XG4, 0, 2), 4) # >>> MXG4=NX.MultiGraph(XG4) # >>> MXG4.add_edge(0,1,3) # >>> NX.dijkstra_path(MXG4,0,2) # [0, 1, 2]
Example #11
Source File: test_astar.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_cycle(self): C = nx.cycle_graph(7) assert_equal(nx.astar_path(C, 0, 3), [0, 1, 2, 3]) assert_equal(nx.dijkstra_path(C, 0, 4), [0, 6, 5, 4])
Example #12
Source File: test_weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_absent_source(self): # the check is in _dijkstra_multisource, but this will provide # regression testing against later changes to any of the "client" # Dijkstra or Bellman-Ford functions G = nx.path_graph(2) for fn in (nx.dijkstra_path, nx.dijkstra_path_length, nx.single_source_dijkstra_path, nx.single_source_dijkstra_path_length, nx.single_source_dijkstra, nx.dijkstra_predecessor_and_distance,): assert_raises(nx.NodeNotFound, fn, G, 3, 0)
Example #13
Source File: UserTrigger.py From dr_droid with Apache License 2.0 | 5 votes |
def user_CFG_API(fcgnx, source_node_list, j): flag=False for i in source_node_list: if nx.has_path(fcgnx, i, j): print (" {} -> {}".format(i,j)) #print (nx.dijkstra_path(fcgnx,i,j)) flag = True break return flag ### # user trigger feature ###
Example #14
Source File: test_propagation.py From oopt-gnpy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def propagation(input_power, con_in, con_out, dest): equipment = load_equipment(eqpt_library_name) network = load_network(network_file_name, equipment) build_network(network, equipment, 0, 20) # parametrize the network elements with the con losses and adapt gain # (assumes all spans are identical) for e in network.nodes(): if isinstance(e, Fiber): loss = e.params.loss_coef * e.params.length e.params.con_in = con_in e.params.con_out = con_out if isinstance(e, Edfa): e.operational.gain_target = loss + con_in + con_out transceivers = {n.uid: n for n in network.nodes() if isinstance(n, Transceiver)} p = input_power p = db2lin(p) * 1e-3 spacing = 50e9 # THz si = create_input_spectral_information(191.3e12, 191.3e12 + 79 * spacing, 0.15, 32e9, p, spacing) source = next(transceivers[uid] for uid in transceivers if uid == 'trx A') sink = next(transceivers[uid] for uid in transceivers if uid == dest) path = dijkstra_path(network, source, sink) for el in path: si = el(si) print(el) # remove this line when sweeping across several powers edfa_sample = next(el for el in path if isinstance(el, Edfa)) nf = mean(edfa_sample.nf) print(f'pw: {input_power} conn in: {con_in} con out: {con_out}', f'OSNR@0.1nm: {round(mean(sink.osnr_ase_01nm),2)}', f'SNR@bandwitdth: {round(mean(sink.snr),2)}') return sink, nf, path
Example #15
Source File: test_weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, 's') validate_path(self.XG, 's', 'v', 9, P['v']) assert_equal(D['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra_path(self.XG, 's')['v']) assert_equal(dict( nx.single_source_dijkstra_path_length(self.XG, 's'))['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra(self.XG, 's')[1]['v']) validate_path( self.MXG, 's', 'v', 9, nx.single_source_dijkstra_path(self.MXG, 's')['v']) GG = self.XG.to_undirected() # make sure we get lower weight # to_undirected might choose either edge with weight 2 or weight 3 GG['u']['x']['weight'] = 2 (D, P) = nx.single_source_dijkstra(GG, 's') validate_path(GG, 's', 'v', 8, P['v']) assert_equal(D['v'], 8) # uses lower weight of 2 on u<->x edge validate_path(GG, 's', 'v', 8, nx.dijkstra_path(GG, 's', 'v')) assert_equal(nx.dijkstra_path_length(GG, 's', 'v'), 8) validate_path(self.XG2, 1, 3, 4, nx.dijkstra_path(self.XG2, 1, 3)) validate_path(self.XG3, 0, 3, 15, nx.dijkstra_path(self.XG3, 0, 3)) assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15) validate_path(self.XG4, 0, 2, 4, nx.dijkstra_path(self.XG4, 0, 2)) assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4) validate_path(self.MXG4, 0, 2, 4, nx.dijkstra_path(self.MXG4, 0, 2)) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's', 'v')[1]) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's')[1]['v']) validate_path(self.G, 's', 'v', 2, nx.dijkstra_path(self.G, 's', 'v')) assert_equal(nx.dijkstra_path_length(self.G, 's', 'v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, 's', 'moon') assert_raises( nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, 's', 'moon') validate_path(self.cycle, 0, 3, 3, nx.dijkstra_path(self.cycle, 0, 3)) validate_path(self.cycle, 0, 4, 3, nx.dijkstra_path(self.cycle, 0, 4)) assert_equal(nx.single_source_dijkstra(self.cycle, 0, 0), (0, [0]))
Example #16
Source File: weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def bellman_ford_path(G, source, target, weight='weight'): """Returns the shortest path from source to target in a weighted graph G. Parameters ---------- G : NetworkX graph source : node Starting node target : node Ending node weight: string, optional (default='weight') Edge data key corresponding to the edge weight Returns ------- path : list List of nodes in a shortest path. Raises ------ NodeNotFound If `source` is not in `G`. NetworkXNoPath If no path exists between source and target. Examples -------- >>> G=nx.path_graph(5) >>> print(nx.bellman_ford_path(G, 0, 4)) [0, 1, 2, 3, 4] Notes ----- Edge weight attributes must be numerical. Distances are calculated as sums of weighted edges traversed. See Also -------- dijkstra_path(), bellman_ford_path_length() """ length, path = single_source_bellman_ford(G, source, target=target, weight=weight) return path
Example #17
Source File: test_weighted.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, 's') validate_path(self.XG, 's', 'v', 9, P['v']) assert_equal(D['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra_path(self.XG, 's')['v']) assert_equal( nx.single_source_dijkstra_path_length(self.XG, 's')['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra(self.XG, 's')[1]['v']) validate_path( self.MXG, 's', 'v', 9, nx.single_source_dijkstra_path(self.MXG, 's')['v']) GG = self.XG.to_undirected() # make sure we get lower weight # to_undirected might choose either edge with weight 2 or weight 3 GG['u']['x']['weight'] = 2 (D, P) = nx.single_source_dijkstra(GG, 's') validate_path(GG, 's', 'v', 8, P['v']) assert_equal(D['v'], 8) # uses lower weight of 2 on u<->x edge validate_path(GG, 's', 'v', 8, nx.dijkstra_path(GG, 's', 'v')) assert_equal(nx.dijkstra_path_length(GG, 's', 'v'), 8) validate_path(self.XG2, 1, 3, 4, nx.dijkstra_path(self.XG2, 1, 3)) validate_path(self.XG3, 0, 3, 15, nx.dijkstra_path(self.XG3, 0, 3)) assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15) validate_path(self.XG4, 0, 2, 4, nx.dijkstra_path(self.XG4, 0, 2)) assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4) validate_path(self.MXG4, 0, 2, 4, nx.dijkstra_path(self.MXG4, 0, 2)) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's', 'v')[1]['v']) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's')[1]['v']) validate_path(self.G, 's', 'v', 2, nx.dijkstra_path(self.G, 's', 'v')) assert_equal(nx.dijkstra_path_length(self.G, 's', 'v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, 's', 'moon') assert_raises( nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, 's', 'moon') validate_path(self.cycle, 0, 3, 3, nx.dijkstra_path(self.cycle, 0, 3)) validate_path(self.cycle, 0, 4, 3, nx.dijkstra_path(self.cycle, 0, 4)) assert_equal( nx.single_source_dijkstra(self.cycle, 0, 0), ({0: 0}, {0: [0]}))
Example #18
Source File: weighted.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def bellman_ford_path(G, source, target, weight='weight'): """Returns the shortest path from source to target in a weighted graph G. Parameters ---------- G : NetworkX graph source : node Starting node target : node Ending node weight: string, optional (default='weight') Edge data key corresponding to the edge weight Returns ------- path : list List of nodes in a shortest path. Raises ------ NetworkXNoPath If no path exists between source and target. Examples -------- >>> G=nx.path_graph(5) >>> print(nx.bellman_ford_path(G, 0, 4)) [0, 1, 2, 3, 4] Notes ----- Edge weight attributes must be numerical. Distances are calculated as sums of weighted edges traversed. See Also -------- dijkstra_path(), bellman_ford_path_length() """ length, path = single_source_bellman_ford(G, source, target=target, weight=weight) return path
Example #19
Source File: test_weighted.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, 's') validate_path(self.XG, 's', 'v', 9, P['v']) assert_equal(D['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra_path(self.XG, 's')['v']) assert_equal(dict( nx.single_source_dijkstra_path_length(self.XG, 's'))['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra(self.XG, 's')[1]['v']) validate_path( self.MXG, 's', 'v', 9, nx.single_source_dijkstra_path(self.MXG, 's')['v']) GG = self.XG.to_undirected() # make sure we get lower weight # to_undirected might choose either edge with weight 2 or weight 3 GG['u']['x']['weight'] = 2 (D, P) = nx.single_source_dijkstra(GG, 's') validate_path(GG, 's', 'v', 8, P['v']) assert_equal(D['v'], 8) # uses lower weight of 2 on u<->x edge validate_path(GG, 's', 'v', 8, nx.dijkstra_path(GG, 's', 'v')) assert_equal(nx.dijkstra_path_length(GG, 's', 'v'), 8) validate_path(self.XG2, 1, 3, 4, nx.dijkstra_path(self.XG2, 1, 3)) validate_path(self.XG3, 0, 3, 15, nx.dijkstra_path(self.XG3, 0, 3)) assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15) validate_path(self.XG4, 0, 2, 4, nx.dijkstra_path(self.XG4, 0, 2)) assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4) validate_path(self.MXG4, 0, 2, 4, nx.dijkstra_path(self.MXG4, 0, 2)) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's', 'v')[1]) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's')[1]['v']) validate_path(self.G, 's', 'v', 2, nx.dijkstra_path(self.G, 's', 'v')) assert_equal(nx.dijkstra_path_length(self.G, 's', 'v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, 's', 'moon') assert_raises( nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, 's', 'moon') validate_path(self.cycle, 0, 3, 3, nx.dijkstra_path(self.cycle, 0, 3)) validate_path(self.cycle, 0, 4, 3, nx.dijkstra_path(self.cycle, 0, 4)) assert_equal(nx.single_source_dijkstra(self.cycle, 0, 0), (0, [0]))
Example #20
Source File: weighted.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def dijkstra_path(G, source, target, weight='weight'): """Returns the shortest path from source to target in a weighted graph G. Parameters ---------- G : NetworkX graph source : node Starting node target : node Ending node weight: string, optional (default='weight') Edge data key corresponding to the edge weight Returns ------- path : list List of nodes in a shortest path. Raises ------ NetworkXNoPath If no path exists between source and target. Examples -------- >>> G=nx.path_graph(5) >>> print(nx.dijkstra_path(G,0,4)) [0, 1, 2, 3, 4] Notes ----- Edge weight attributes must be numerical. Distances are calculated as sums of weighted edges traversed. See Also -------- bidirectional_dijkstra() """ (length, path) = single_source_dijkstra(G, source, target=target, weight=weight) try: return path[target] except KeyError: raise nx.NetworkXNoPath( "node %s not reachable from %s" % (source, target))