本文整理汇总了Python中networkx.dijkstra_path_length方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.dijkstra_path_length方法的具体用法?Python networkx.dijkstra_path_length怎么用?Python networkx.dijkstra_path_length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.dijkstra_path_length方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_weight_function
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
def test_weight_function(self):
"""Tests for computing the length of the shortest path using
Dijkstra's algorithm with a user-defined weight function.
"""
# Create a triangle in which the edge from node 0 to node 2 has
# a large weight and the other two edges have a small weight.
G = nx.complete_graph(3)
G.adj[0][2]['weight'] = 10
G.adj[0][1]['weight'] = 1
G.adj[1][2]['weight'] = 1
# The weight function will take the multiplicative inverse of
# the weights on the edges. This way, weights that were large
# before now become small and vice versa.
def weight(u, v, d): return 1 / d['weight']
# The shortest path from 0 to 2 using the actual weights on the
# edges should be [0, 1, 2]. However, with the above weight
# function, the shortest path should be [0, 2], since that has a
# very small weight.
length = nx.dijkstra_path_length(G, 0, 2, weight=weight)
assert_equal(length, 1 / 10)
示例2: test_weight_function
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
def test_weight_function(self):
"""Tests for computing the length of the shortest path using
Dijkstra's algorithm with a user-defined weight function.
"""
# Create a triangle in which the edge from node 0 to node 2 has
# a large weight and the other two edges have a small weight.
G = nx.complete_graph(3)
G.adj[0][2]['weight'] = 10
G.adj[0][1]['weight'] = 1
G.adj[1][2]['weight'] = 1
# The weight function will take the multiplicative inverse of
# the weights on the edges. This way, weights that were large
# before now become small and vice versa.
weight = lambda u, v, d: 1 / d['weight']
# The shortest path from 0 to 2 using the actual weights on the
# edges should be [0, 1, 2]. However, with the above weight
# function, the shortest path should be [0, 2], since that has a
# very small weight.
length = nx.dijkstra_path_length(G, 0, 2, weight=weight)
assert_equal(length, 1 / 10)
示例3: get_shortest_paths_distances
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
def get_shortest_paths_distances(graph, pairs, edge_weight_name='distance'):
"""
Calculate shortest distance between each pair of nodes in a graph
Args:
graph (networkx graph)
pairs (list[2tuple]): List of length 2 tuples containing node pairs to calculate shortest path between
edge_weight_name (str): edge attribute used for distance calculation
Returns:
dict: mapping each pair in `pairs` to the shortest path using `edge_weight_name` between them.
"""
distances = {}
for pair in pairs:
distances[pair] = nx.dijkstra_path_length(graph, pair[0], pair[1], weight=edge_weight_name)
return distances
示例4: add_augmenting_path_to_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
def add_augmenting_path_to_graph(graph, min_weight_pairs, edge_weight_name='weight'):
"""
Add the min weight matching edges to the original graph
Note the resulting graph could (and likely will) have edges that didn't exist on the original graph. To get the
true circuit, we must breakdown these augmented edges into the shortest path through the edges that do exist. This
is done with `create_eulerian_circuit`.
Args:
graph (networkx graph):
min_weight_pairs (list[2tuples): output of `dedupe_matching` specifying the odd degree nodes to link together
edge_weight_name (str): edge attribute used for distance calculation
Returns:
networkx graph: `graph` augmented with edges between the odd nodes specified in `min_weight_pairs`
"""
graph_aug = graph.copy() # so we don't mess with the original graph
for pair in min_weight_pairs:
graph_aug.add_edge(pair[0],
pair[1],
**{'distance': nx.dijkstra_path_length(graph, pair[0], pair[1], weight=edge_weight_name),
'augmented': True}
)
return graph_aug
示例5: test_absent_source
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [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: dijkstra_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
def dijkstra_path_length(G, source, target, weight='weight'):
"""Returns the shortest path length from source to target
in a weighted graph.
Parameters
----------
G : NetworkX graph
source : node label
starting node for path
target : node label
ending node for path
weight: string, optional (default='weight')
Edge data key corresponding to the edge weight
Returns
-------
length : number
Shortest path length.
Raises
------
NetworkXNoPath
If no path exists between source and target.
Examples
--------
>>> G=nx.path_graph(5)
>>> print(nx.dijkstra_path_length(G,0,4))
4
Notes
-----
Edge weight attributes must be numerical.
Distances are calculated as sums of weighted edges traversed.
See Also
--------
bidirectional_dijkstra()
"""
length = single_source_dijkstra_path_length(G, source, weight=weight)
try:
return length[target]
except KeyError:
raise nx.NetworkXNoPath(
"node %s not reachable from %s" % (source, target))
示例7: test_dijkstra
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
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]}))
示例8: bellman_ford_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
def bellman_ford_path_length(G, source, target, weight='weight'):
"""Returns the shortest path length from source to target
in a weighted graph.
Parameters
----------
G : NetworkX graph
source : node label
starting node for path
target : node label
ending node for path
weight: string, optional (default='weight')
Edge data key corresponding to the edge weight
Returns
-------
length : number
Shortest path length.
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_length(G,0,4))
4
Notes
-----
Edge weight attributes must be numerical.
Distances are calculated as sums of weighted edges traversed.
See Also
--------
dijkstra_path_length(), bellman_ford_path()
"""
if source == target:
return 0
weight = _weight_function(G, weight)
length = _bellman_ford(G, [source], weight, target=target)
try:
return length[target]
except KeyError:
raise nx.NetworkXNoPath(
"node %s not reachable from %s" % (source, target))
示例9: test_dijkstra
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
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]))
示例10: bellman_ford_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dijkstra_path_length [as 别名]
def bellman_ford_path_length(G, source, target, weight='weight'):
"""Returns the shortest path length from source to target
in a weighted graph.
Parameters
----------
G : NetworkX graph
source : node label
starting node for path
target : node label
ending node for path
weight: string, optional (default='weight')
Edge data key corresponding to the edge weight
Returns
-------
length : number
Shortest path length.
Raises
------
NetworkXNoPath
If no path exists between source and target.
Examples
--------
>>> G=nx.path_graph(5)
>>> print(nx.bellman_ford_path_length(G,0,4))
4
Notes
-----
Edge weight attributes must be numerical.
Distances are calculated as sums of weighted edges traversed.
See Also
--------
dijkstra_path_length(), bellman_ford_path()
"""
if source == target:
return 0
weight = _weight_function(G, weight)
length = _bellman_ford(G, [source], weight, target=target)
try:
return length[target]
except KeyError:
raise nx.NetworkXNoPath(
"node %s not reachable from %s" % (source, target))