本文整理匯總了Python中networkx.single_source_shortest_path_length方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.single_source_shortest_path_length方法的具體用法?Python networkx.single_source_shortest_path_length怎麽用?Python networkx.single_source_shortest_path_length使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.single_source_shortest_path_length方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_shortest_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_shortest_path(self):
deg=[3,2,2,1]
G=nx.generators.havel_hakimi_graph(deg)
cs1=nxt.creation_sequence(deg, with_labels=True)
for n, m in [(3, 0), (0, 3), (0, 2), (0, 1), (1, 3),
(3, 1), (1, 2), (2, 3)]:
assert_equal(nxt.shortest_path(cs1,n,m),
nx.shortest_path(G, n, m))
spl=nxt.shortest_path_length(cs1,3)
spl2=nxt.shortest_path_length([ t for v,t in cs1],2)
assert_equal(spl, spl2)
spld={}
for j,pl in enumerate(spl):
n=cs1[j][0]
spld[n]=pl
assert_equal(spld, nx.single_source_shortest_path_length(G, 3))
示例2: test_shortest_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_shortest_path(self):
deg = [3, 2, 2, 1]
G = nx.generators.havel_hakimi_graph(deg)
cs1 = nxt.creation_sequence(deg, with_labels=True)
for n, m in [(3, 0), (0, 3), (0, 2), (0, 1), (1, 3),
(3, 1), (1, 2), (2, 3)]:
assert_equal(nxt.shortest_path(cs1, n, m),
nx.shortest_path(G, n, m))
spl = nxt.shortest_path_length(cs1, 3)
spl2 = nxt.shortest_path_length([t for v, t in cs1], 2)
assert_equal(spl, spl2)
spld = {}
for j, pl in enumerate(spl):
n = cs1[j][0]
spld[n] = pl
assert_equal(spld, nx.single_source_shortest_path_length(G, 3))
assert_equal(nxt.shortest_path(['d', 'd', 'd', 'i', 'd', 'd'], 1, 2), [1, 2])
assert_equal(nxt.shortest_path([3, 1, 2], 1, 2), [1, 2])
assert_raises(TypeError, nxt.shortest_path, [3., 1., 2.], 1, 2)
assert_raises(ValueError, nxt.shortest_path, [3, 1, 2], 'a', 2)
assert_raises(ValueError, nxt.shortest_path, [3, 1, 2], 1, 'b')
assert_equal(nxt.shortest_path([3, 1, 2], 1, 1), [1])
示例3: _distances_from_function_start
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def _distances_from_function_start(function):
"""
:param function: A normalized Function object.
:returns: A dictionary of basic block addresses and their distance to the start of the function.
"""
return networkx.single_source_shortest_path_length(function.graph,
function.startpoint)
示例4: _distances_from_function_exit
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def _distances_from_function_exit(function):
"""
:param function: A normalized Function object.
:returns: A dictionary of basic block addresses and their distance to the exit of the function.
"""
reverse_graph = function.graph.reverse()
# we aren't guaranteed to have an exit from the function so explicitly add the node
reverse_graph.add_node("start")
found_exits = False
for n in function.graph.nodes():
if len(list(function.graph.successors(n))) == 0:
reverse_graph.add_edge("start", n)
found_exits = True
# if there were no exits (a function with a while 1) let's consider the block with the highest address to
# be the exit. This isn't the most scientific way, but since this case is pretty rare it should be okay
if not found_exits:
last = max(function.graph.nodes(), key=lambda x:x.addr)
reverse_graph.add_edge("start", last)
dists = networkx.single_source_shortest_path_length(reverse_graph, "start")
# remove temp node
del dists["start"]
# correct for the added node
for n in dists:
dists[n] -= 1
return dists
示例5: get_nodes_n_hops_away
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def get_nodes_n_hops_away(self, node_pub_key, n):
"""
Returns all nodes, which are n hops away from a given
node_pub_key node.
:param node_pub_key: string
:param n: int
:return: dict with nodes and distance as value
"""
return nx.single_source_shortest_path_length(
self.node.network.graph, node_pub_key, cutoff=n)
示例6: weiner_index
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def weiner_index(G, weight=None):
# compute sum of distances between all node pairs
# (with optional weights)
weiner=0.0
if weight is None:
for n in G:
path_length=nx.single_source_shortest_path_length(G,n)
weiner+=sum(path_length.values())
else:
for n in G:
path_length=nx.single_source_dijkstra_path_length(G,
n,weight=weight)
weiner+=sum(path_length.values())
return weiner
示例7: test_single_source_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_single_source_shortest_path_length(self):
l=nx.shortest_path_length(self.cycle,0)
assert_equal(l,{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
assert_equal(l,nx.single_source_shortest_path_length(self.cycle,0))
l=nx.shortest_path_length(self.grid,1)
assert_equal(l[16],6)
# now with weights
l=nx.shortest_path_length(self.cycle,0,weight='weight')
assert_equal(l,{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
assert_equal(l,nx.single_source_dijkstra_path_length(self.cycle,0))
l=nx.shortest_path_length(self.grid,1,weight='weight')
assert_equal(l[16],6)
示例8: test_single_source_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_single_source_shortest_path_length(self):
assert_equal(nx.single_source_shortest_path_length(self.cycle,0),
{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
示例9: all_pairs_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def all_pairs_shortest_path_length(G, cutoff=None):
"""Computes the shortest path lengths between all nodes in ``G``.
Parameters
----------
G : NetworkX graph
cutoff : integer, optional
Depth at which to stop the search. Only paths of length at most
``cutoff`` are returned.
Returns
-------
lengths : dictionary
Dictionary of shortest path lengths keyed by source and target.
Notes
-----
The dictionary returned only has keys for reachable node pairs.
Examples
--------
>>> G = nx.path_graph(5)
>>> length = nx.all_pairs_shortest_path_length(G)
>>> print(length[1][4])
3
>>> length[1]
{0: 1, 1: 0, 2: 1, 3: 2, 4: 3}
"""
length = single_source_shortest_path_length
# TODO This can be trivially parallelized.
return {n: length(G, n, cutoff=cutoff) for n in G}
示例10: single_source_shortest_path_length_range
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def single_source_shortest_path_length_range(graph, node_range, cutoff):
dists_dict = {}
for node in node_range:
dists_dict[node] = nx.single_source_shortest_path_length(graph, node, cutoff)
return dists_dict
示例11: test_single_source_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_single_source_shortest_path_length(self):
ans = dict(nx.shortest_path_length(self.cycle, 0))
assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans,
dict(nx.single_source_shortest_path_length(self.cycle,
0)))
ans = dict(nx.shortest_path_length(self.grid, 1))
assert_equal(ans[16], 6)
# now with weights
ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight'))
assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans, dict(nx.single_source_dijkstra_path_length(
self.cycle, 0)))
ans = dict(nx.shortest_path_length(self.grid, 1, weight='weight'))
assert_equal(ans[16], 6)
# weights and method specified
ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight',
method='dijkstra'))
assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans, dict(nx.single_source_dijkstra_path_length(
self.cycle, 0)))
ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight',
method='bellman-ford'))
assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans, dict(nx.single_source_bellman_ford_path_length(
self.cycle, 0)))
示例12: test_single_source_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_single_source_shortest_path_length(self):
pl = nx.single_source_shortest_path_length
lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}
assert_equal(dict(pl(self.cycle, 0)), lengths)
lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}
assert_equal(dict(pl(self.directed_cycle, 0)), lengths)
示例13: test_single_source_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_single_source_shortest_path_length(self):
l = dict(nx.shortest_path_length(self.cycle,0))
assert_equal(l,{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
assert_equal(l, dict(nx.single_source_shortest_path_length(self.cycle,0)))
l = dict(nx.shortest_path_length(self.grid,1))
assert_equal(l[16],6)
# now with weights
l = dict(nx.shortest_path_length(self.cycle, 0, weight='weight'))
assert_equal(l, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(l, dict(nx.single_source_dijkstra_path_length(
self.cycle, 0)))
l = dict(nx.shortest_path_length(self.grid, 1, weight='weight'))
assert_equal(l[16], 6)
示例14: test_single_source_shortest_path_length
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def test_single_source_shortest_path_length(self):
pl = nx.single_source_shortest_path_length
lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}
assert_equal(dict(pl(self.cycle,0)), lengths)
lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}
assert_equal(dict(pl(self.directed_cycle,0)), lengths)
示例15: query
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import single_source_shortest_path_length [as 別名]
def query(self, topic, max_depth=4, config=None, dont_follow=['enrichment', 'classification']):
"""
:param topic: a graph to return the context of. At least one node ID in topic \
must be in full graph g to return any context.
:param max_depth: The maximum distance from the topic to search
:param config: The titanDB configuration to use if not using the one configured with the plugin
:param dont_follow: A list of attribute types to not follow
:return: subgraph in networkx format
"""
distances = dict()
if config is None:
config = self.context_graph
# Conver topic from a graph into a set of nodes
topic_nodes = set()
for n, d in topic.nodes(data=True):
topic_nodes.add("class={0}&key={1}&value={2}".format(d['class'], d['key'], d['value']))
nodes = topic_nodes.copy()
for t in topic:
# get all nodes within max_depth distance from each topic and add them to the set
new_distances = nx.single_source_shortest_path_length(self.context_graph.to_undirected(), t, cutoff=max_depth)
nodes = nodes.union(set(new_distances.keys()))
# Update shortest distances from topic to node
for n in new_distances.keys():
if n in distances:
if new_distances[n] < distances[n]:
distances[n] = new_distances[n]
else:
distances[n] = new_distances[n]
# remove dont_follow nodes:
nodes_to_remove = set()
for n in nodes:
if self.context_graph.node[n]['key'] in dont_follow:
nodes_to_remove.add(n)
nodes = nodes.difference(nodes_to_remove)
# Get the subgraph represented by the nodes:
g = nx.MultiDiGraph(self.context_graph.subgraph(nodes))
# Prune out non-relevant components by removing those that contain no topic nodes.
# This gets ride of nodes that were found by following dont_follow nodes
for component in nx.connected_components(g.to_undirected()):
if len(topic_nodes.intersection(set(component))) <= 0: # if there's no overlap betweent the component and topic
g.remove_nodes_from(component) # remove the component
# add the topic distances to the subgraph
for n in g.nodes():
g.node[n]['topic_distance'] = distances[n]
return g