本文整理匯總了Python中networkx.bfs_successors方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.bfs_successors方法的具體用法?Python networkx.bfs_successors怎麽用?Python networkx.bfs_successors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.bfs_successors方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: bfs_seq
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def bfs_seq(G, start_id):
'''
get a bfs node sequence
:param G:
:param start_id:
:return:
'''
dictionary = dict(nx.bfs_successors(G, start_id))
start = [start_id]
output = [start_id]
while len(start) > 0:
next = []
while len(start) > 0:
current = start.pop(0)
neighbor = dictionary.get(current)
if neighbor is not None:
#### a wrong example, should not permute here!
# shuffle(neighbor)
next = next + neighbor
output = output + next
start = next
return output
示例2: reroot_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def reroot_graph(G: nx.DiGraph, node: str) -> nx.DiGraph:
"""Return a copy of the graph rooted at the given node"""
G = G.copy()
for n, successors in list(nx.bfs_successors(G, source=node)):
for s in successors:
G.add_edge(s, n, **G.edges[n, s])
G.remove_edge(n, s)
return G
示例3: bfs_successors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def bfs_successors(self, node):
"""
Returns an iterator of tuples of (DAGNode, [DAGNodes]) where the DAGNode is the current node
and [DAGNode] is its successors in BFS order.
"""
return ((self._id_to_node[idx], [self._id_to_node[succ] for succ in succ_list])
for idx, succ_list in nx.bfs_successors(self._multi_graph, node._node_id))
示例4: bfs_successors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def bfs_successors(G, source):
"""Return dictionary of successors in breadth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node
Specify starting node for breadth-first search and return edges in
the component reachable from source.
Returns
-------
succ: dict
A dictionary with nodes as keys and list of succssors nodes as values.
Examples
--------
>>> G = nx.Graph()
>>> G.add_path([0,1,2])
>>> print(nx.bfs_successors(G,0))
{0: [1], 1: [2]}
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
by D. Eppstein, July 2004.
"""
d = defaultdict(list)
for s,t in bfs_edges(G,source):
d[s].append(t)
return dict(d)
示例5: test_successor
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def test_successor(self):
assert_equal(nx.bfs_successors(self.G, source=0),
{0: [1], 1: [2, 3], 2: [4]})
示例6: test_successor
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def test_successor(self):
assert_equal(dict(nx.bfs_successors(self.G, source=0)),
{0: [1], 1: [2, 3], 2: [4]})
示例7: bfs_test_successor
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def bfs_test_successor(self):
assert_equal(dict(nx.bfs_successors(self.G, source=1, depth_limit=3)),
{1: [0, 2], 2: [3, 7], 3: [4], 7: [8]})
result = {n: sorted(s) for n, s in nx.bfs_successors(self.D, source=7,
depth_limit=2)}
assert_equal(result, {8: [9], 2: [3], 7: [2, 8]})
示例8: _single_adsorption
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def _single_adsorption(
self,
adsorbate,
bond,
slab=None,
site_index=0,
auto_construct=True,
symmetric=True):
"""Bond and adsorbate by a single atom."""
if slab is None:
slab = self.slab.copy()
atoms = adsorbate.copy()
atoms.set_cell(slab.cell)
if symmetric:
ind = self.get_symmetric_sites()[site_index]
vector = self.get_adsorption_vectors()[site_index]
else:
ind = self.get_periodic_sites()[site_index]
vector = self.get_adsorption_vectors(unique=False)[site_index]
# Improved position estimate for site.
u = self.r1_topology[ind]
r = radii[slab[self.index[u]].numbers]
top_sites = self.coordinates[self.connectivity == 1]
numbers = atoms.numbers[bond]
R = radii[numbers]
base_position = utils.trilaterate(top_sites[u], r + R, vector)
branches = nx.bfs_successors(atoms.graph, bond)
atoms.translate(-atoms.positions[bond])
if auto_construct:
atoms = catkit.gen.molecules.get_3D_positions(atoms, bond)
# Align with the adsorption vector
atoms.rotate([0, 0, 1], vector)
atoms.translate(base_position)
n = len(slab)
slab += atoms
# Add graph connections
for metal_index in self.index[u]:
slab.graph.add_edge(metal_index, bond + n)
return slab
示例9: bfs_successors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def bfs_successors(G, source, depth_limit=None):
"""Returns an iterator of successors in breadth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node
Specify starting node for breadth-first search and return edges in
the component reachable from source.
depth_limit : int, optional(default=len(G))
Specify the maximum search depth
Returns
-------
succ: iterator
(node, successors) iterator where successors is the list of
successors of the node.
Examples
--------
>>> G = nx.path_graph(3)
>>> print(dict(nx.bfs_successors(G,0)))
{0: [1], 1: [2]}
>>> H = nx.Graph()
>>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
>>> print(dict(nx.bfs_successors(H, 0)))
{0: [1, 2], 1: [3, 4], 2: [5, 6]}
>>> G = nx.Graph()
>>> nx.add_path(G, [0, 1, 2, 3, 4, 5, 6])
>>> nx.add_path(G, [2, 7, 8, 9, 10])
>>> print(dict(nx.bfs_successors(G, source=1, depth_limit=3)))
{1: [0, 2], 2: [3, 7], 3: [4], 7: [8]}
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
by D. Eppstein, July 2004.The modifications
to allow depth limits based on the Wikipedia article
"`Depth-limited-search`_".
.. _Depth-limited-search: https://en.wikipedia.org/wiki/Depth-limited_search
"""
parent = source
children = []
for p, c in bfs_edges(G, source, depth_limit=depth_limit):
if p == parent:
children.append(c)
continue
yield (parent, children)
children = [c]
parent = p
yield (parent, children)
示例10: bfs_successors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import bfs_successors [as 別名]
def bfs_successors(G, source):
"""Returns an iterator of successors in breadth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node
Specify starting node for breadth-first search and return edges in
the component reachable from source.
Returns
-------
succ: iterator
(node, successors) iterator where successors is the list of
successors of the node.
Examples
--------
>>> G = nx.path_graph(3)
>>> print(dict(nx.bfs_successors(G,0)))
{0: [1], 1: [2]}
>>> H = nx.Graph()
>>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
>>> dict(nx.bfs_successors(H, 0))
{0: [1, 2], 1: [3, 4], 2: [5, 6]}
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
by D. Eppstein, July 2004.
"""
parent = source
children = []
for p, c in bfs_edges(G, source):
if p == parent:
children.append(c)
continue
yield (parent, children)
children = [c]
parent = p
yield (parent, children)