本文整理匯總了Python中networkx.NodeNotFound方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.NodeNotFound方法的具體用法?Python networkx.NodeNotFound怎麽用?Python networkx.NodeNotFound使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.NodeNotFound方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: compute_BEST_DES
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def compute_BEST_DES(self, node_src, alloc_DES, sim, DES_dst,message):
try:
bestLong = float('inf')
minPath = []
bestDES = []
#print len(DES_dst)
for dev in DES_dst:
#print "DES :",dev
node_dst = alloc_DES[dev]
path = list(nx.shortest_path(sim.topology.G, source=node_src, target=node_dst))
long = len(path)
if long < bestLong:
bestLong = long
minPath = path
bestDES = dev
#print bestDES,minPath
return minPath, bestDES
except (nx.NetworkXNoPath, nx.NodeNotFound) as e:
self.logger.warning("There is no path between two nodes: %s - %s " % (node_src, node_dst))
# print "Simulation ends?"
return [], None
示例2: compute_DSAR
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def compute_DSAR(self, node_src, alloc_DES, sim, DES_dst,message):
try:
bestSpeed = float('inf')
minPath = []
bestDES = []
# print "LEN %i" %len(DES_dst)
for dev in DES_dst:
#print "DES :",dev
node_dst = alloc_DES[dev]
path = list(nx.shortest_path(sim.topology.G, source=node_src, target=node_dst))
speed = 0
for i in range(len(path) - 1):
link = (path[i], path[i + 1])
#print " LINK : ",link
#print " BYTES :", message.bytes
speed += sim.topology.G.edges[link][Topology.LINK_PR] + (message.bytes/sim.topology.G.edges[link][Topology.LINK_BW])
#print " Spped :" , speed
#print sim.topology.G.edges[link][Topology.LINK_BW]
att_node = sim.topology.get_nodes_att()[path[-1]]
time_service = message.inst / float(att_node["IPT"])
# print "Tims serviice %s" %time_service
speed += time_service # HW - computation of last node
#print "SPEED: ",speed
if speed < bestSpeed:
bestSpeed = speed
minPath = path
bestDES = dev
# print "DES %s PATH %s" %(bestDES,minPath)
return minPath, bestDES
except (nx.NetworkXNoPath, nx.NodeNotFound) as e:
self.logger.warning("There is no path between two nodes: %s - %s " % (node_src, node_dst))
print "Simulation ends?"
return [], None
示例3: compute_DSAR
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def compute_DSAR(self, node_src, alloc_DES, sim, DES_dst,message):
try:
bestSpeed = float('inf')
minPath = []
bestDES = []
#print len(DES_dst)
for dev in DES_dst:
#print "DES :",dev
node_dst = alloc_DES[dev]
path = list(nx.shortest_path(sim.topology.G, source=node_src, target=node_dst))
speed = 0
for i in range(len(path) - 1):
link = (path[i], path[i + 1])
# print "LINK : ",link
# print " BYTES :", message.bytes
speed += sim.topology.G.edges[link][Topology.LINK_PR] + (message.bytes/sim.topology.G.edges[link][Topology.LINK_BW])
#print sim.topology.G.edges[link][Topology.LINK_BW]
att_node = sim.topology.get_nodes_att()[path[-1]]
time_service = message.inst / float(att_node["IPT"])
speed += time_service # HW - computation of last node
#print "SPEED: ",speed
if speed < bestSpeed:
bestSpeed = speed
minPath = path
bestDES = dev
#print bestDES,minPath
return minPath, bestDES
except (nx.NetworkXNoPath, nx.NodeNotFound) as e:
self.logger.warning("There is no path between two nodes: %s - %s " % (node_src, node_dst))
# print "Simulation ends?"
return [], None
示例4: compute_most_near
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def compute_most_near(self,node_src,alloc_DES,sim,DES_dst):
"""
This functions caches the minimun path among client-devices and fog-devices-Module Calculator and it chooses the best calculator process deployed in that node
"""
#By Placement policy we know that:
try:
minLenPath = float('inf')
minPath = []
bestDES = []
for dev in DES_dst:
node_dst = alloc_DES[dev]
path = list(nx.shortest_path(sim.topology.G, source=node_src, target=node_dst))
if len(path)<minLenPath:
minLenPath = len(path)
minPath = path
bestDES = dev
return minPath,bestDES
except nx.NetworkXNoPath as e:
self.logger.warning("There is no path between two nodes: %s - %s " % (node_src, node_dst))
print "Simulation ends?. Time:", sim.env.now
# sim.stop = True ## You can stop all DES process
return [], None
except nx.NodeNotFound as e:
self.logger.warning("Node not found: %s - %s "%(node_src,node_dst))
print "Simulation ends?. Time:",sim.env.now
# sim.stop = True ## You can stop all DES process
return [],None
示例5: astar_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def astar_path_length(G, source, target, heuristic=None, weight='weight'):
"""Returns the length of the shortest path between source and target using
the A* ("A-star") algorithm.
Parameters
----------
G : NetworkX graph
source : node
Starting node for path
target : node
Ending node for path
heuristic : function
A function to evaluate the estimate of the distance
from the a node to the target. The function takes
two nodes arguments and must return a number.
Raises
------
NetworkXNoPath
If no path exists between source and target.
See Also
--------
astar_path
"""
if source not in G or target not in G:
msg = 'Either source {} or target {} is not in G'
raise nx.NodeNotFound(msg.format(source, target))
path = astar_path(G, source, target, heuristic, weight)
return sum(G[u][v].get(weight, 1) for u, v in zip(path[:-1], path[1:]))
示例6: test_absent_source
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [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)
示例7: test_absent_source_bellman_ford
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_absent_source_bellman_ford(self):
# the check is in _bellman_ford; this provides regression testing
# against later changes to "client" Bellman-Ford functions
G = nx.path_graph(2)
for fn in (nx.bellman_ford_predecessor_and_distance,
nx.bellman_ford_path,
nx.bellman_ford_path_length,
nx.single_source_bellman_ford_path,
nx.single_source_bellman_ford_path_length,
nx.single_source_bellman_ford,):
assert_raises(nx.NodeNotFound, fn, G, 3, 0)
示例8: test_shortest_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_shortest_path(self):
assert_equal(nx.shortest_path(self.cycle, 0, 3), [0, 1, 2, 3])
assert_equal(nx.shortest_path(self.cycle, 0, 4), [0, 6, 5, 4])
validate_grid_path(4, 4, 1, 12, nx.shortest_path(self.grid, 1, 12))
assert_equal(nx.shortest_path(self.directed_cycle, 0, 3), [0, 1, 2, 3])
# now with weights
assert_equal(nx.shortest_path(self.cycle, 0, 3, weight='weight'),
[0, 1, 2, 3])
assert_equal(nx.shortest_path(self.cycle, 0, 4, weight='weight'),
[0, 6, 5, 4])
validate_grid_path(4, 4, 1, 12, nx.shortest_path(self.grid, 1, 12,
weight='weight'))
assert_equal(nx.shortest_path(self.directed_cycle, 0, 3,
weight='weight'),
[0, 1, 2, 3])
# weights and method specified
assert_equal(nx.shortest_path(self.directed_cycle, 0, 3,
weight='weight', method='dijkstra'),
[0, 1, 2, 3])
assert_equal(nx.shortest_path(self.directed_cycle, 0, 3,
weight='weight', method='bellman-ford'),
[0, 1, 2, 3])
# when Dijkstra's will probably (depending on precise implementation)
# incorrectly return [0, 1, 3] instead
assert_equal(nx.shortest_path(self.neg_weights, 0, 3, weight='weight',
method='bellman-ford'),
[0, 2, 3])
# confirm bad method rejection
assert_raises(ValueError, nx.shortest_path, self.cycle, method='SPAM')
# confirm absent source rejection
assert_raises(nx.NodeNotFound, nx.shortest_path, self.cycle, 8)
示例9: test_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_shortest_path_length(self):
assert_equal(nx.shortest_path_length(self.cycle, 0, 3), 3)
assert_equal(nx.shortest_path_length(self.grid, 1, 12), 5)
assert_equal(nx.shortest_path_length(self.directed_cycle, 0, 4), 4)
# now with weights
assert_equal(nx.shortest_path_length(self.cycle, 0, 3,
weight='weight'),
3)
assert_equal(nx.shortest_path_length(self.grid, 1, 12,
weight='weight'),
5)
assert_equal(nx.shortest_path_length(self.directed_cycle, 0, 4,
weight='weight'),
4)
# weights and method specified
assert_equal(nx.shortest_path_length(self.cycle, 0, 3, weight='weight',
method='dijkstra'),
3)
assert_equal(nx.shortest_path_length(self.cycle, 0, 3, weight='weight',
method='bellman-ford'),
3)
# confirm bad method rejection
assert_raises(ValueError,
nx.shortest_path_length,
self.cycle,
method='SPAM')
# confirm absent source rejection
assert_raises(nx.NodeNotFound, nx.shortest_path_length, self.cycle, 8)
示例10: test_tree_all_pairs_lowest_common_ancestor5
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_tree_all_pairs_lowest_common_ancestor5(self):
"""Handles invalid input correctly."""
empty_digraph = tree_all_pairs_lca(nx.DiGraph())
assert_raises(nx.NetworkXPointlessConcept, list, empty_digraph)
bad_pairs_digraph = tree_all_pairs_lca(self.DG, pairs=[(-1, -2)])
assert_raises(nx.NodeNotFound, list, bad_pairs_digraph)
示例11: test_tree_all_pairs_lowest_common_ancestor11
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_tree_all_pairs_lowest_common_ancestor11(self):
"""Test that None as a node in the graph raises an error."""
G = nx.DiGraph([(None, 3)])
assert_raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
assert_raises(nx.NodeNotFound, list,
tree_all_pairs_lca(self.DG, pairs=G.edges()))
示例12: test_all_pairs_lowest_common_ancestor5
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_all_pairs_lowest_common_ancestor5(self):
"""Test that pairs not in the graph raises error."""
assert_raises(nx.NodeNotFound, all_pairs_lca, self.DG, [(-1, -1)])
示例13: test_all_pairs_lowest_common_ancestor10
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_all_pairs_lowest_common_ancestor10(self):
"""Test that it bails on None as a node."""
G = nx.DiGraph([(None, 3)])
assert_raises(nx.NetworkXError, all_pairs_lca, G)
assert_raises(nx.NodeNotFound, all_pairs_lca,
self.DG, pairs=G.edges())
示例14: astar_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def astar_path_length(G, source, target, heuristic=None, weight='weight'):
"""Return the length of the shortest path between source and target using
the A* ("A-star") algorithm.
Parameters
----------
G : NetworkX graph
source : node
Starting node for path
target : node
Ending node for path
heuristic : function
A function to evaluate the estimate of the distance
from the a node to the target. The function takes
two nodes arguments and must return a number.
Raises
------
NetworkXNoPath
If no path exists between source and target.
See Also
--------
astar_path
"""
if source not in G or target not in G:
msg = 'Either source {} or target {} is not in G'
raise nx.NodeNotFound(msg.format(source, target))
path = astar_path(G, source, target, heuristic, weight)
return sum(G[u][v].get(weight, 1) for u, v in zip(path[:-1], path[1:]))
示例15: test_single_node_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import NodeNotFound [as 別名]
def test_single_node_graph(self):
G = nx.DiGraph()
G.add_node(0)
assert_equal(nx.single_source_bellman_ford_path(G, 0), {0: [0]})
assert_equal(nx.single_source_bellman_ford_path_length(G, 0), {0: 0})
assert_equal(nx.single_source_bellman_ford(G, 0), ({0: 0}, {0: [0]}))
assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0), ({0: [None]}, {0: 0}))
assert_equal(nx.goldberg_radzik(G, 0), ({0: None}, {0: 0}))
assert_raises(nx.NodeNotFound, nx.bellman_ford_predecessor_and_distance, G, 1)
assert_raises(nx.NodeNotFound, nx.goldberg_radzik, G, 1)