本文整理汇总了Python中networkx.bfs_edges方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.bfs_edges方法的具体用法?Python networkx.bfs_edges怎么用?Python networkx.bfs_edges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.bfs_edges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: strategy_connected_sequential
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def strategy_connected_sequential(G, colors, traversal='bfs'):
"""
Connected sequential ordering (CS). Yield nodes in such an order, that
each node, except the first one, has at least one neighbour in the
preceeding sequence. The sequence can be generated using both BFS and
DFS search (using the strategy_connected_sequential_bfs and
strategy_connected_sequential_dfs method). The default is bfs.
"""
for component_graph in nx.connected_component_subgraphs(G):
source = component_graph.nodes()[0]
yield source # Pick the first node as source
if traversal == 'bfs':
tree = nx.bfs_edges(component_graph, source)
elif traversal == 'dfs':
tree = nx.dfs_edges(component_graph, source)
else:
raise nx.NetworkXError(
'Please specify bfs or dfs for connected sequential ordering')
for (_, end) in tree:
# Then yield nodes in the order traversed by either BFS or DFS
yield end
示例2: _bfs_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def _bfs_nodes(cls, graph, source, size, **kwargs):
"""Traverse `graph` with BFS starting from `source`, up to `size` nodes.
Return an iterator of subgraph nodes (including source node).
"""
if size < 1:
return iter(())
return itertools.chain(
(source,),
itertools.islice((v for u, v in nx.bfs_edges(graph, source)), size-1)
)
示例3: next
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def next(self, state, **runopts):
CG = self.constraint_graph
size = self.size
constraints = self.constraints
bqm = state.problem
# get a random constraint to start with
n = random.choice(list(CG.nodes))
if len(constraints[n]) > size:
raise NotImplementedError
# starting from our constraint, do a breadth-first search adding constraints until our max
# size is reached
variables = set(constraints[n])
for _, ci in nx.bfs_edges(CG, n):
proposed = [v for v in constraints[ci] if v not in variables]
if len(proposed) + len(variables) <= size:
variables.union(proposed)
if len(variables) == size:
# can exit early
break
sample = state.samples.change_vartype(bqm.vartype).first.sample
subbqm = bqm_induced_by(bqm, variables, sample)
return state.updated(subproblem=subbqm)
示例4: verify_nx_algorithm_equivalence
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def verify_nx_algorithm_equivalence(self, tree, g):
for root in tree.roots:
self.assertTrue(nx.is_directed_acyclic_graph(g))
# test descendants
self.assertSetEqual(
{u for u in tree.nodes() if tree.is_descendant(u, root)},
set(nx.descendants(g, root)) | {root},
)
# test MRCA
if tree.num_nodes < 20:
for u, v in itertools.combinations(tree.nodes(), 2):
mrca = nx.lowest_common_ancestor(g, u, v)
if mrca is None:
mrca = -1
self.assertEqual(tree.mrca(u, v), mrca)
# test node traversal modes
self.assertEqual(
list(tree.nodes(root=root, order="breadthfirst")),
[root] + [v for u, v in nx.bfs_edges(g, root)],
)
self.assertEqual(
list(tree.nodes(root=root, order="preorder")),
list(nx.dfs_preorder_nodes(g, root)),
)
示例5: bfs_order
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def bfs_order(self, source="sourcebus"):
start_node = self.graph[source]
return set(nx.bfs_edges(self.graph, source))
示例6: bfs_tree
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def bfs_tree(G, source, reverse=False):
"""Return an oriented tree constructed from of a breadth-first-search
starting at source.
Parameters
----------
G : NetworkX graph
source : node
Specify starting node for breadth-first search and return edges in
the component reachable from source.
reverse : bool, optional
If True traverse a directed graph in the reverse direction
Returns
-------
T: NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.Graph()
>>> G.add_path([0,1,2])
>>> print(list(nx.bfs_edges(G,0)))
[(0, 1), (1, 2)]
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
by D. Eppstein, July 2004.
"""
T = nx.DiGraph()
T.add_node(source)
T.add_edges_from(bfs_edges(G,source,reverse=reverse))
return T
示例7: bfs_predecessors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def bfs_predecessors(G, source):
"""Return dictionary of predecessors 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
-------
pred: dict
A dictionary with nodes as keys and predecessor nodes as values.
Examples
--------
>>> G = nx.Graph()
>>> G.add_path([0,1,2])
>>> print(nx.bfs_predecessors(G,0))
{1: 0, 2: 1}
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
by D. Eppstein, July 2004.
"""
return dict((t,s) for s,t in bfs_edges(G,source))
示例8: bfs_successors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [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)
示例9: test_bfs_edges_reverse
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def test_bfs_edges_reverse(self):
D = nx.DiGraph()
D.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4)])
edges = nx.bfs_edges(D, source=4, reverse=True)
assert_equal(list(edges), [(4, 2), (4, 3), (2, 1), (1, 0)])
示例10: strategy_connected_sequential
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def strategy_connected_sequential(G, colors, traversal='bfs'):
"""Returns an iterable over nodes in ``G`` in the order given by a
breadth-first or depth-first traversal.
``traversal`` must be one of the strings ``'dfs'`` or ``'bfs'``,
representing depth-first traversal or breadth-first traversal,
respectively.
The generated sequence has the property that for each node except
the first, at least one neighbor appeared earlier in the sequence.
``G`` is a NetworkX graph. ``colors`` is ignored.
"""
if traversal == 'bfs':
traverse = nx.bfs_edges
elif traversal == 'dfs':
traverse = nx.dfs_edges
else:
raise nx.NetworkXError("Please specify one of the strings 'bfs' or"
" 'dfs' for connected sequential ordering")
for component in nx.connected_component_subgraphs(G):
source = arbitrary_element(component)
# Yield the source node, then all the nodes in the specified
# traversal order.
yield source
for (_, end) in traverse(component, source):
yield end
示例11: bfs_test_edges
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def bfs_test_edges(self):
edges = nx.bfs_edges(self.G, source=9, depth_limit=4)
assert_equal(list(edges), [(9, 8), (9, 10), (8, 7),
(7, 2), (2, 1), (2, 3)])
示例12: bfs_tree
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def bfs_tree(G, source, reverse=False):
"""Return an oriented tree constructed from of a breadth-first-search
starting at source.
Parameters
----------
G : NetworkX graph
source : node
Specify starting node for breadth-first search and return edges in
the component reachable from source.
reverse : bool, optional
If True traverse a directed graph in the reverse direction
Returns
-------
T: NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.path_graph(3)
>>> print(list(nx.bfs_tree(G,1).edges()))
[(1, 0), (1, 2)]
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
by D. Eppstein, July 2004.
"""
T = nx.DiGraph()
T.add_node(source)
T.add_edges_from(bfs_edges(G, source, reverse=reverse))
return T
示例13: bfs_predecessors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def bfs_predecessors(G, source):
"""Returns an iterator of predecessors 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
-------
pred: iterator
(node, predecessors) iterator where predecessors is the list of
predecessors of the node.
Examples
--------
>>> G = nx.path_graph(3)
>>> print(dict(nx.bfs_predecessors(G, 0)))
{1: 0, 2: 1}
>>> H = nx.Graph()
>>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
>>> dict(nx.bfs_predecessors(H, 0))
{1: 0, 2: 0, 3: 1, 4: 1, 5: 2, 6: 2}
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
by D. Eppstein, July 2004.
"""
for s, t in bfs_edges(G, source):
yield (t, s)
示例14: test_bfs_edges
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def test_bfs_edges(self):
edges = nx.bfs_edges(self.G, source=0)
assert_equal(list(edges), [(0, 1), (1, 2), (1, 3), (2, 4)])
示例15: test_bfs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import bfs_edges [as 别名]
def test_bfs(index_dtype, n=100):
def _bfs_nx(g_nx, src):
edges = nx.bfs_edges(g_nx, src)
layers_nx = [set([src])]
edges_nx = []
frontier = set()
edge_frontier = set()
for u, v in edges:
if u in layers_nx[-1]:
frontier.add(v)
edge_frontier.add(g.edge_id(u, v))
else:
layers_nx.append(frontier)
edges_nx.append(edge_frontier)
frontier = set([v])
edge_frontier = set([g.edge_id(u, v)])
# avoids empty successors
if len(frontier) > 0 and len(edge_frontier) > 0:
layers_nx.append(frontier)
edges_nx.append(edge_frontier)
return layers_nx, edges_nx
g = dgl.DGLGraph()
a = sp.random(n, n, 3 / n, data_rvs=lambda n: np.ones(n))
g.from_scipy_sparse_matrix(a)
if index_dtype == 'int32':
g = dgl.graph(g.edges()).int()
else:
g = dgl.graph(g.edges()).long()
g_nx = g.to_networkx()
src = random.choice(range(n))
layers_nx, _ = _bfs_nx(g_nx, src)
layers_dgl = dgl.bfs_nodes_generator(g, src)
assert len(layers_dgl) == len(layers_nx)
assert all(toset(x) == y for x, y in zip(layers_dgl, layers_nx))
g_nx = nx.random_tree(n, seed=42)
g = dgl.DGLGraph()
g.from_networkx(g_nx)
if index_dtype == 'int32':
g = dgl.graph(g.edges()).int()
else:
g = dgl.graph(g.edges()).long()
src = 0
_, edges_nx = _bfs_nx(g_nx, src)
edges_dgl = dgl.bfs_edges_generator(g, src)
assert len(edges_dgl) == len(edges_nx)
assert all(toset(x) == y for x, y in zip(edges_dgl, edges_nx))