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