Python networkx.dijkstra_predecessor_and_distance() Examples
The following are 16
code examples of networkx.dijkstra_predecessor_and_distance().
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_weighted.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_dijkstra_predecessor(self): G = nx.path_graph(4) assert_equal(nx.dijkstra_predecessor_and_distance(G, 0), ({0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3})) G = nx.grid_2d_graph(2, 2) pred, dist = nx.dijkstra_predecessor_and_distance(G, (0, 0)) assert_equal(sorted(pred.items()), [((0, 0), []), ((0, 1), [(0, 0)]), ((1, 0), [(0, 0)]), ((1, 1), [(0, 1), (1, 0)])]) assert_equal(sorted(dist.items()), [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)]) XG = nx.DiGraph() XG.add_weighted_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)]) (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's') assert_equal(P['v'], ['u']) assert_equal(D['v'], 9) (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's', cutoff=8) assert_false('v' in D)
Example #2
Source File: thermal_network.py From CityEnergyAnalyst with MIT License | 5 votes |
def calculate_pressure_loss_critical_path(dP_timestep, thermal_network): dP_all_edges = dP_timestep[0] plant_node = thermal_network.all_nodes_df[thermal_network.all_nodes_df['Type'] == 'PLANT'].index[0] if max(dP_all_edges) > 0.0: pressure_losses_in_critical_paths = np.zeros(len(dP_all_edges)) # initialize array G = nx.Graph() # initial networkx G.add_nodes_from(thermal_network.all_nodes_df.index) for ix, edge_name in enumerate(thermal_network.edge_df.index): start_node = thermal_network.edge_df.loc[edge_name, 'start node'] end_node = thermal_network.edge_df.loc[edge_name, 'end node'] dP_one_edge = dP_all_edges[ix] G.add_edge(start_node, end_node, weight=dP_one_edge, name=edge_name, ix=str(ix)) # find the path with the highest pressure drop _, distances_dict = nx.dijkstra_predecessor_and_distance(G, source=plant_node) critical_node = max(distances_dict, key=distances_dict.get) path_to_critical_node = nx.shortest_path(G, source=plant_node)[critical_node] # calculate pressure losses along the critical path for i in range(len(path_to_critical_node)): if i < len(path_to_critical_node) - 1: start_node = path_to_critical_node[i] end_node = path_to_critical_node[i+1] dP = G[start_node][end_node]['weight'] idx = int(G[start_node][end_node]['ix']) pressure_losses_in_critical_paths[idx] = dP # find substations substation_nodes_ix = [] node_df = thermal_network.all_nodes_df for node in path_to_critical_node: if node_df.ix[node]['Type'] != 'NONE': substation_nodes_ix.append(int(node.split('NODE')[1])) else: pressure_losses_in_critical_paths = np.zeros(len(dP_all_edges)) # zero array substation_nodes_ix = [] return pressure_losses_in_critical_paths, substation_nodes_ix
Example #3
Source File: test_weighted.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_dijkstra_pred_distance_multigraph(self): G = nx.MultiGraph() G.add_edge('a', 'b', key='short', foo=5, weight=100) G.add_edge('a', 'b', key='long', bar=1, weight=110) p, d = nx.dijkstra_predecessor_and_distance(G, 'a') assert_equal(p, {'a': [], 'b': ['a']}) assert_equal(d, {'a': 0, 'b': 100})
Example #4
Source File: test_weighted.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_negative_edge_cycle(self): G = nx.cycle_graph(5, create_using=nx.DiGraph()) assert_equal(nx.negative_edge_cycle(G), False) G.add_edge(8, 9, weight=-7) G.add_edge(9, 8, weight=3) graph_size = len(G) assert_equal(nx.negative_edge_cycle(G), True) assert_equal(graph_size, len(G)) assert_raises(ValueError, nx.single_source_dijkstra_path_length, G, 8) assert_raises(ValueError, nx.single_source_dijkstra, G, 8) assert_raises(ValueError, nx.dijkstra_predecessor_and_distance, G, 8) G.add_edge(9, 10) assert_raises(ValueError, nx.bidirectional_dijkstra, G, 8, 10)
Example #5
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 #6
Source File: test_weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_dijkstra_predecessor1(self): G = nx.path_graph(4) assert_equal(nx.dijkstra_predecessor_and_distance(G, 0), ({0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3}))
Example #7
Source File: test_weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_dijkstra_predecessor2(self): # 4-cycle G = nx.Graph([(0, 1), (1, 2), (2, 3), (3, 0)]) pred, dist = nx.dijkstra_predecessor_and_distance(G, (0)) assert_equal(pred[0], []) assert_equal(pred[1], [0]) assert_true(pred[2] in [[1, 3], [3, 1]]) assert_equal(pred[3], [0]) assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})
Example #8
Source File: test_weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_dijkstra_predecessor3(self): XG = nx.DiGraph() XG.add_weighted_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)]) (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's') assert_equal(P['v'], ['u']) assert_equal(D['v'], 9) (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's', cutoff=8) assert_false('v' in D)
Example #9
Source File: test_weighted.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_dijkstra_pred_distance_multigraph(self): G = nx.MultiGraph() G.add_edge('a', 'b', key='short', foo=5, weight=100) G.add_edge('a', 'b', key='long', bar=1, weight=110) p, d = nx.dijkstra_predecessor_and_distance(G, 'a') assert_equal(p, {'a': [], 'b': ['a']}) assert_equal(d, {'a': 0, 'b': 100})
Example #10
Source File: test_weighted.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_dijkstra_predecessor1(self): G = nx.path_graph(4) assert_equal(nx.dijkstra_predecessor_and_distance(G, 0), ({0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3}))
Example #11
Source File: test_weighted.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_dijkstra_predecessor2(self): # 4-cycle G = nx.Graph([(0,1),(1,2),(2,3),(3,0)]) pred, dist = nx.dijkstra_predecessor_and_distance(G, (0)) assert_equal(pred[0],[]) assert_equal(pred[1],[0]) assert_true(pred[2] in [[1,3],[3,1]]) assert_equal(pred[3],[0]) assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})
Example #12
Source File: test_weighted.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_dijkstra_predecessor3(self): XG = nx.DiGraph() XG.add_weighted_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)]) (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's') assert_equal(P['v'], ['u']) assert_equal(D['v'], 9) (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's', cutoff=8) assert_false('v' in D)
Example #13
Source File: test_weighted.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_dijkstra_pred_distance_multigraph(self): G = nx.MultiGraph() G.add_edge('a', 'b', key='short', foo=5, weight=100) G.add_edge('a', 'b', key='long', bar=1, weight=110) p, d = nx.dijkstra_predecessor_and_distance(G, 'a') assert_equal(p, {'a': [], 'b': ['a']}) assert_equal(d, {'a': 0, 'b': 100})
Example #14
Source File: test_weighted.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_negative_edge_cycle(self): G = nx.cycle_graph(5, create_using=nx.DiGraph()) assert_equal(nx.negative_edge_cycle(G), False) G.add_edge(8, 9, weight=-7) G.add_edge(9, 8, weight=3) graph_size = len(G) assert_equal(nx.negative_edge_cycle(G), True) assert_equal(graph_size, len(G)) assert_raises(ValueError, nx.single_source_dijkstra_path_length, G, 8) assert_raises(ValueError, nx.single_source_dijkstra, G, 8) assert_raises(ValueError, nx.dijkstra_predecessor_and_distance, G, 8) G.add_edge(9, 10) assert_raises(ValueError, nx.bidirectional_dijkstra, G, 8, 10)
Example #15
Source File: load.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def _node_betweenness(G,source,cutoff=False,normalized=True,weight=None): """Node betweenness helper: see betweenness_centrality for what you probably want. This actually computes "load" and not betweenness. See https://networkx.lanl.gov/ticket/103 This calculates the load of each node for paths from a single source. (The fraction of number of shortests paths from source that go through each node.) To get the load for a node you need to do all-pairs shortest paths. If weight is not None then use Dijkstra for finding shortest paths. In this case a cutoff is not implemented and so is ignored. """ # get the predecessor and path length data if weight is None: (pred,length)=nx.predecessor(G,source,cutoff=cutoff,return_seen=True) else: (pred,length)=nx.dijkstra_predecessor_and_distance(G,source,weight=weight) # order the nodes by path length onodes = [ (l,vert) for (vert,l) in length.items() ] onodes.sort() onodes[:] = [vert for (l,vert) in onodes if l>0] # intialize betweenness between={}.fromkeys(length,1.0) while onodes: v=onodes.pop() if v in pred: num_paths=len(pred[v]) # Discount betweenness if more than for x in pred[v]: # one shortest path. if x==source: # stop if hit source because all remaining v break # also have pred[v]==[source] between[x]+=between[v]/float(num_paths) # remove source for v in between: between[v]-=1 # rescale to be between 0 and 1 if normalized: l=len(between) if l > 2: scale=1.0/float((l-1)*(l-2)) # 1/the number of possible paths for v in between: between[v] *= scale return between
Example #16
Source File: load.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def _node_betweenness(G, source, cutoff=False, normalized=True, weight=None): """Node betweenness_centrality helper: See betweenness_centrality for what you probably want. This actually computes "load" and not betweenness. See https://networkx.lanl.gov/ticket/103 This calculates the load of each node for paths from a single source. (The fraction of number of shortests paths from source that go through each node.) To get the load for a node you need to do all-pairs shortest paths. If weight is not None then use Dijkstra for finding shortest paths. """ # get the predecessor and path length data if weight is None: (pred, length) = nx.predecessor(G, source, cutoff=cutoff, return_seen=True) else: (pred, length) = nx.dijkstra_predecessor_and_distance(G, source, cutoff, weight) # order the nodes by path length onodes = [(l, vert) for (vert, l) in length.items()] onodes.sort() onodes[:] = [vert for (l, vert) in onodes if l > 0] # initialize betweenness between = {}.fromkeys(length, 1.0) while onodes: v = onodes.pop() if v in pred: num_paths = len(pred[v]) # Discount betweenness if more than for x in pred[v]: # one shortest path. if x == source: # stop if hit source because all remaining v break # also have pred[v]==[source] between[x] += between[v] / float(num_paths) # remove source for v in between: between[v] -= 1 # rescale to be between 0 and 1 if normalized: l = len(between) if l > 2: # scale by 1/the number of possible paths scale = 1.0 / float((l - 1) * (l - 2)) for v in between: between[v] *= scale return between