本文整理汇总了Python中networkx.dijkstra_predecessor_and_distance方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.dijkstra_predecessor_and_distance方法的具体用法?Python networkx.dijkstra_predecessor_and_distance怎么用?Python networkx.dijkstra_predecessor_and_distance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.dijkstra_predecessor_and_distance方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dijkstra_predecessor
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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)
示例2: calculate_pressure_loss_critical_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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
示例3: test_dijkstra_pred_distance_multigraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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})
示例4: test_negative_edge_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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)
示例5: test_absent_source
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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)
示例6: test_dijkstra_predecessor1
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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}))
示例7: test_dijkstra_predecessor2
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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})
示例8: test_dijkstra_predecessor3
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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)
示例9: test_dijkstra_predecessor2
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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})
示例10: _node_betweenness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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
示例11: _node_betweenness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_predecessor_and_distance [as 别名]
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