本文整理汇总了Python中networkx.single_source_shortest_path方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.single_source_shortest_path方法的具体用法?Python networkx.single_source_shortest_path怎么用?Python networkx.single_source_shortest_path使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.single_source_shortest_path方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: collect_concepts_and_relations
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def collect_concepts_and_relations(self):
g = self.graph
nodes, depths, is_connected = self.bfs()
concepts = [self.name2concept[n] for n in nodes]
relations = dict()
for i, src in enumerate(nodes):
relations[i] = dict()
paths = nx.single_source_shortest_path(g, src)
for j, tgt in enumerate(nodes):
relations[i][j] = list()
assert tgt in paths
path = paths[tgt]
info = dict()
#info['node'] = path[1:-1]
info['edge'] = [g[path[i]][path[i+1]]['label'] for i in range(len(path)-1)]
info['length'] = len(info['edge'])
relations[i][j].append(info)
## TODO, we just use the sequential order
depths = nodes
return concepts, depths, relations, is_connected
示例2: test_single_source_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def test_single_source_shortest_path(self):
p = nx.shortest_path(self.cycle, 0)
assert_equal(p[3], [0, 1, 2, 3])
assert_equal(p, nx.single_source_shortest_path(self.cycle, 0))
p = nx.shortest_path(self.grid, 1)
validate_grid_path(4, 4, 1, 12, p[12])
# now with weights
p = nx.shortest_path(self.cycle, 0, weight='weight')
assert_equal(p[3], [0, 1, 2, 3])
assert_equal(p, nx.single_source_dijkstra_path(self.cycle, 0))
p = nx.shortest_path(self.grid, 1, weight='weight')
validate_grid_path(4, 4, 1, 12, p[12])
# weights and method specified
p = nx.shortest_path(self.cycle, 0, method='dijkstra', weight='weight')
assert_equal(p[3], [0, 1, 2, 3])
assert_equal(p, nx.single_source_shortest_path(self.cycle, 0))
p = nx.shortest_path(self.cycle, 0, method='bellman-ford',
weight='weight')
assert_equal(p[3], [0, 1, 2, 3])
assert_equal(p, nx.single_source_shortest_path(self.cycle, 0))
示例3: remove_unused_toplevel_elems
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def remove_unused_toplevel_elems(header: ir.Header, linking_final_header: bool):
toplevel_elem_names = {elem.name
for elem in itertools.chain(header.toplevel_content, header.template_defns)
if not isinstance(elem, (ir.StaticAssert, ir.NoOpStmt))}
public_names = header.public_names
if not linking_final_header:
public_names = public_names.union(split_name
for _, split_name in header.split_template_name_by_old_name_and_result_element_name)
elem_dependency_graph = nx.DiGraph()
for elem in itertools.chain(header.template_defns, header.toplevel_content):
if isinstance(elem, (ir.TemplateDefn, ir.ConstantDef, ir.Typedef)):
elem_name = elem.name
else:
# We'll use a dummy name for non-template toplevel elems.
elem_name = ''
elem_dependency_graph.add_node(elem_name)
if elem_name in public_names or (isinstance(elem, (ir.ConstantDef, ir.Typedef)) and any(isinstance(expr, ir.TemplateInstantiation) and expr.instantiation_might_trigger_static_asserts
for expr in elem.transitive_subexpressions)):
# We also add an edge from the node '' to all toplevel defns that must remain, so that we can use '' as a source below.
elem_dependency_graph.add_edge('', elem_name)
for identifier in elem.referenced_identifiers:
if identifier in toplevel_elem_names:
elem_dependency_graph.add_edge(elem_name, identifier)
elem_dependency_graph.add_node('')
used_elem_names = nx.single_source_shortest_path(elem_dependency_graph, source='').keys()
return ir.Header(template_defns=tuple(template_defn for template_defn in header.template_defns if
template_defn.name in used_elem_names),
toplevel_content=tuple(elem for elem in header.toplevel_content if
isinstance(elem, (ir.StaticAssert, ir.NoOpStmt)) or elem.name in used_elem_names),
public_names=header.public_names,
split_template_name_by_old_name_and_result_element_name=header.split_template_name_by_old_name_and_result_element_name,
check_if_error_specializations=header.check_if_error_specializations)
示例4: test_single_source_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def test_single_source_shortest_path(self):
p=nx.shortest_path(self.cycle,0)
assert_equal(p[3],[0,1,2,3])
assert_equal(p,nx.single_source_shortest_path(self.cycle,0))
p=nx.shortest_path(self.grid,1)
validate_grid_path(4, 4, 1, 12, p[12])
# now with weights
p=nx.shortest_path(self.cycle,0,weight='weight')
assert_equal(p[3],[0,1,2,3])
assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0))
p=nx.shortest_path(self.grid,1,weight='weight')
validate_grid_path(4, 4, 1, 12, p[12])
示例5: test_single_source_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def test_single_source_shortest_path(self):
p=nx.single_source_shortest_path(self.cycle,0)
assert_equal(p[3],[0,1,2,3])
p=nx.single_source_shortest_path(self.cycle,0, cutoff=0)
assert_equal(p,{0 : [0]})
示例6: all_pairs_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def all_pairs_shortest_path(G, cutoff=None):
"""Compute shortest paths between all nodes.
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, keyed by source and target, of shortest paths.
Examples
--------
>>> G = nx.path_graph(5)
>>> path = nx.all_pairs_shortest_path(G)
>>> print(path[0][4])
[0, 1, 2, 3, 4]
See Also
--------
floyd_warshall()
"""
# TODO This can be trivially parallelized.
return {n: single_source_shortest_path(G, n, cutoff=cutoff) for n in G}
示例7: test_single_source_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def test_single_source_shortest_path(self):
p = nx.single_source_shortest_path(self.directed_cycle, 3)
assert_equal(p[0], [3, 4, 5, 6, 0])
p = nx.single_source_shortest_path(self.cycle, 0)
assert_equal(p[3], [0, 1, 2, 3])
p = nx.single_source_shortest_path(self.cycle, 0, cutoff=0)
assert_equal(p, {0: [0]})
示例8: all_pairs_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def all_pairs_shortest_path(G, cutoff=None):
"""Compute shortest paths between all nodes.
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, keyed by source and target, of shortest paths.
Examples
--------
>>> G = nx.path_graph(5)
>>> path = dict(nx.all_pairs_shortest_path(G))
>>> print(path[0][4])
[0, 1, 2, 3, 4]
See Also
--------
floyd_warshall()
"""
# TODO This can be trivially parallelized.
for n in G:
yield (n, single_source_shortest_path(G, n, cutoff=cutoff))
示例9: test_single_source_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def test_single_source_shortest_path(self):
p = nx.single_source_shortest_path(self.directed_cycle, 3)
assert_equal(p[0], [3, 4, 5, 6, 0])
p = nx.single_source_shortest_path(self.cycle, 0)
assert_equal(p[3], [0, 1, 2, 3])
p = nx.single_source_shortest_path(self.cycle, 0, cutoff=0)
assert_equal(p,{0 : [0]})
示例10: single_source_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def single_source_shortest_path(G,source,cutoff=None):
"""Compute shortest path between source
and all other nodes reachable from source.
Parameters
----------
G : NetworkX graph
source : node label
Starting node for path
cutoff : integer, optional
Depth to stop the search. Only paths of length <= cutoff are returned.
Returns
-------
lengths : dictionary
Dictionary, keyed by target, of shortest paths.
Examples
--------
>>> G=nx.path_graph(5)
>>> path=nx.single_source_shortest_path(G,0)
>>> path[4]
[0, 1, 2, 3, 4]
Notes
-----
The shortest path is not necessarily unique. So there can be multiple
paths between the source and each target node, all of which have the
same 'shortest' length. For each target node, this function returns
only one of those paths.
See Also
--------
shortest_path
"""
level=0 # the current level
nextlevel={source:1} # list of nodes to check at next level
paths={source:[source]} # paths dictionary (paths to key from source)
if cutoff==0:
return paths
while nextlevel:
thislevel=nextlevel
nextlevel={}
for v in thislevel:
for w in G[v]:
if w not in paths:
paths[w]=paths[v]+[w]
nextlevel[w]=1
level=level+1
if (cutoff is not None and cutoff <= level): break
return paths
示例11: single_source_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def single_source_shortest_path(G, source, cutoff=None):
"""Compute shortest path between source
and all other nodes reachable from source.
Parameters
----------
G : NetworkX graph
source : node label
Starting node for path
cutoff : integer, optional
Depth to stop the search. Only paths of length <= cutoff are returned.
Returns
-------
lengths : dictionary
Dictionary, keyed by target, of shortest paths.
Examples
--------
>>> G = nx.path_graph(5)
>>> path = nx.single_source_shortest_path(G, 0)
>>> path[4]
[0, 1, 2, 3, 4]
Notes
-----
The shortest path is not necessarily unique. So there can be multiple
paths between the source and each target node, all of which have the
same 'shortest' length. For each target node, this function returns
only one of those paths.
See Also
--------
shortest_path
"""
if source not in G:
raise nx.NodeNotFound("Source {} not in G".format(source))
def join(p1, p2):
return p1 + p2
if cutoff is None:
cutoff = float('inf')
nextlevel = {source: 1} # list of nodes to check at next level
paths = {source: [source]} # paths dictionary (paths to key from source)
return dict(_single_shortest_path(G.adj, nextlevel, paths, cutoff, join))
示例12: single_target_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def single_target_shortest_path(G, target, cutoff=None):
"""Compute shortest path to target from all nodes that reach target.
Parameters
----------
G : NetworkX graph
target : node label
Target node for path
cutoff : integer, optional
Depth to stop the search. Only paths of length <= cutoff are returned.
Returns
-------
lengths : dictionary
Dictionary, keyed by target, of shortest paths.
Examples
--------
>>> G = nx.path_graph(5, create_using=nx.DiGraph())
>>> path = nx.single_target_shortest_path(G, 4)
>>> path[0]
[0, 1, 2, 3, 4]
Notes
-----
The shortest path is not necessarily unique. So there can be multiple
paths between the source and each target node, all of which have the
same 'shortest' length. For each target node, this function returns
only one of those paths.
See Also
--------
shortest_path, single_source_shortest_path
"""
if target not in G:
raise nx.NodeNotFound("Target {} not in G".format(target))
def join(p1, p2):
return p2 + p1
# handle undirected graphs
adj = G.pred if G.is_directed() else G.adj
if cutoff is None:
cutoff = float('inf')
nextlevel = {target: 1} # list of nodes to check at next level
paths = {target: [target]} # paths dictionary (paths to key from source)
return dict(_single_shortest_path(adj, nextlevel, paths, cutoff, join))
示例13: single_target_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path [as 别名]
def single_target_shortest_path(G, target, cutoff=None):
"""Compute shortest path to target from all nodes that reach target.
Parameters
----------
G : NetworkX graph
target : node label
Target node for path
cutoff : integer, optional
Depth to stop the search. Only paths of length <= cutoff are returned.
Returns
-------
lengths : dictionary
Dictionary, keyed by target, of shortest paths.
Examples
--------
>>> G = nx.path_graph(5, create_using=nx.DiGraph())
>>> path = nx.single_target_shortest_path(G, 4)
>>> path[0]
[0, 1, 2, 3, 4]
Notes
-----
The shortest path is not necessarily unique. So there can be multiple
paths between the source and each target node, all of which have the
same 'shortest' length. For each target node, this function returns
only one of those paths.
See Also
--------
shortest_path, single_source_shortest_path
"""
if target not in G:
raise nx.NodeNotFound("Target {} not in G".format(source))
def join(p1, p2):
return p2 + p1
# handle undirected graphs
adj = G.pred if G.is_directed() else G.adj
if cutoff is None:
cutoff = float('inf')
nextlevel = {target: 1} # list of nodes to check at next level
paths = {target: [target]} # paths dictionary (paths to key from source)
return dict(_single_shortest_path(adj, nextlevel, paths, cutoff, join))