本文整理匯總了Python中networkx.dfs_edges方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.dfs_edges方法的具體用法?Python networkx.dfs_edges怎麽用?Python networkx.dfs_edges使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.dfs_edges方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_max_steps
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def test_max_steps():
binary_path = os.path.join(test_location, "x86_64", "fauxware")
b = angr.Project(binary_path, load_options={'auto_load_libs': False})
cfg = b.analyses.CFGEmulated(max_steps=5, fail_fast=True)
dfs_edges = networkx.dfs_edges(cfg.graph)
depth_map = {}
for src, dst in dfs_edges:
if src not in depth_map:
depth_map[src] = 0
if dst not in depth_map:
depth_map[dst] = depth_map[src] + 1
depth_map[dst] = max(depth_map[src] + 1, depth_map[dst])
nose.tools.assert_less_equal(max(depth_map.values()), 5)
示例2: get_tree_schedule
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def get_tree_schedule(frcs, graph):
"""
Find the most constrained tree in the graph and returns which messages to compute
it. This is the minimum spanning tree of the perturb_radius edge attribute.
See forward_pass for parameters.
Returns
-------
tree_schedules : numpy.ndarray of numpy.int
Describes how to compute the max marginal for the most constrained tree.
Nx3 2D array of (source pool_idx, target pool_idx, perturb radius), where
each row represents a single outgoing factor message computation.
"""
min_tree = nx.minimum_spanning_tree(graph, 'perturb_radius')
return np.array([(target, source, graph.edge[source][target]['perturb_radius'])
for source, target in nx.dfs_edges(min_tree)])[::-1]
示例3: strategy_connected_sequential
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_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
示例4: _dfs_edges
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def _dfs_edges(graph, source, max_steps=None):
"""
Perform a depth-first search on the given DiGraph, with a limit on maximum steps.
:param networkx.DiGraph graph: The graph to traverse.
:param Any source: The source to begin traversal.
:param int max_steps: Maximum steps of the traversal, or None if not limiting steps.
:return: An iterator of edges.
"""
if max_steps is None:
yield networkx.dfs_edges(graph, source)
else:
steps_map = defaultdict(int)
traversed = { source }
stack = [ source ]
while stack:
src = stack.pop()
for dst in graph.successors(src):
if dst in traversed:
continue
traversed.add(dst)
dst_steps = max(steps_map[src] + 1, steps_map[dst])
if dst_steps > max_steps:
continue
yield src, dst
steps_map[dst] = dst_steps
stack.append(dst)
示例5: __transitive_reduction
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def __transitive_reduction(self):
"""Transitive reduction for acyclic graphs."""
assert nx.is_directed_acyclic_graph(self.digraph)
for u in self.digraph:
transitive_vertex = []
for v in self.digraph[u]:
transitive_vertex.extend(
x for _, x in nx.dfs_edges(self.digraph, v))
self.digraph.remove_edges_from((u, x) for x in transitive_vertex)
示例6: dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def dfs_tree(G, source):
"""Return oriented tree constructed from a depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search.
Returns
-------
T : NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.Graph()
>>> G.add_path([0,1,2])
>>> T = nx.dfs_tree(G,0)
>>> print(T.edges())
[(0, 1), (1, 2)]
"""
T = nx.DiGraph()
if source is None:
T.add_nodes_from(G)
else:
T.add_node(source)
T.add_edges_from(dfs_edges(G,source))
return T
示例7: dfs_predecessors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def dfs_predecessors(G, source=None):
"""Return dictionary of predecessors in depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-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.dfs_predecessors(G,0))
{1: 0, 2: 1}
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
by D. Eppstein, July 2004.
If a source is not specified then a source is chosen arbitrarily and
repeatedly until all components in the graph are searched.
"""
return dict((t,s) for s,t in dfs_edges(G,source=source))
示例8: dfs_successors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def dfs_successors(G, source=None):
"""Return dictionary of successors in depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search and return edges in
the component reachable from source.
Returns
-------
succ: dict
A dictionary with nodes as keys and list of successor nodes as values.
Examples
--------
>>> G = nx.Graph()
>>> G.add_path([0,1,2])
>>> print(nx.dfs_successors(G,0))
{0: [1], 1: [2]}
Notes
-----
Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
by D. Eppstein, July 2004.
If a source is not specified then a source is chosen arbitrarily and
repeatedly until all components in the graph are searched.
"""
d = defaultdict(list)
for s,t in dfs_edges(G,source=source):
d[s].append(t)
return dict(d)
示例9: strategy_connected_sequential
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_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
示例10: dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def dfs_tree(G, source=None, depth_limit=None):
"""Returns oriented tree constructed from a depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search.
depth_limit : int, optional (default=len(G))
Specify the maximum search depth.
Returns
-------
T : NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.path_graph(5)
>>> T = nx.dfs_tree(G, source=0, depth_limit=2)
>>> list(T.edges())
[(0, 1), (1, 2)]
>>> T = nx.dfs_tree(G, source=0)
>>> list(T.edges())
[(0, 1), (1, 2), (2, 3), (3, 4)]
"""
T = nx.DiGraph()
if source is None:
T.add_nodes_from(G)
else:
T.add_node(source)
T.add_edges_from(dfs_edges(G, source, depth_limit))
return T
示例11: dfs_predecessors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def dfs_predecessors(G, source=None, depth_limit=None):
"""Returns dictionary of predecessors in depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search and return edges in
the component reachable from source.
depth_limit : int, optional (default=len(G))
Specify the maximum search depth.
Returns
-------
pred: dict
A dictionary with nodes as keys and predecessor nodes as values.
Examples
--------
>>> G = nx.path_graph(4)
>>> nx.dfs_predecessors(G, source=0)
{1: 0, 2: 1, 3: 2}
>>> nx.dfs_predecessors(G, source=0, depth_limit=2)
{1: 0, 2: 1}
Notes
-----
If a source is not specified then a source is chosen arbitrarily and
repeatedly until all components in the graph are searched.
The implementation of this function is adapted from David Eppstein's
depth-first search function in `PADS`_, with modifications
to allow depth limits based on the Wikipedia article
"`Depth-limited search`_".
.. _PADS: http://www.ics.uci.edu/~eppstein/PADS
.. _Depth-limited search: https://en.wikipedia.org/wiki/Depth-limited_search
"""
return {t: s for s, t in dfs_edges(G, source, depth_limit)}
示例12: test_dfs_edges
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def test_dfs_edges(self):
edges = nx.dfs_edges(self.G, source=0)
assert_equal(list(edges), [(0, 1), (1, 2), (2, 4), (4, 3)])
edges = nx.dfs_edges(self.D)
assert_equal(list(edges), [(0, 1), (2, 3)])
示例13: test_dls_edges
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def test_dls_edges(self):
edges = nx.dfs_edges(self.G, source=9, depth_limit=4)
assert_equal(list(edges), [(9, 8), (8, 7),
(7, 2), (2, 1), (2, 3), (9, 10)])
示例14: dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def dfs_tree(G, source=None, depth_limit=None):
"""Return oriented tree constructed from a depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search.
depth_limit : int, optional (default=len(G))
Specify the maximum search depth.
Returns
-------
T : NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.path_graph(5)
>>> T = nx.dfs_tree(G, source=0, depth_limit=2)
>>> list(T.edges())
[(0, 1), (1, 2)]
>>> T = nx.dfs_tree(G, source=0)
>>> list(T.edges())
[(0, 1), (1, 2), (2, 3), (3, 4)]
"""
T = nx.DiGraph()
if source is None:
T.add_nodes_from(G)
else:
T.add_node(source)
T.add_edges_from(dfs_edges(G, source, depth_limit))
return T
示例15: dfs_predecessors
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_edges [as 別名]
def dfs_predecessors(G, source=None, depth_limit=None):
"""Return dictionary of predecessors in depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search and return edges in
the component reachable from source.
depth_limit : int, optional (default=len(G))
Specify the maximum search depth.
Returns
-------
pred: dict
A dictionary with nodes as keys and predecessor nodes as values.
Examples
--------
>>> G = nx.path_graph(4)
>>> nx.dfs_predecessors(G, source=0)
{1: 0, 2: 1, 3: 2}
>>> nx.dfs_predecessors(G, source=0, depth_limit=2)
{1: 0, 2: 1}
Notes
-----
If a source is not specified then a source is chosen arbitrarily and
repeatedly until all components in the graph are searched.
The implementation of this function is adapted from David Eppstein's
depth-first search function in `PADS`_, with modifications
to allow depth limits based on the Wikipedia article
"`Depth-limited search`_".
.. _PADS: http://www.ics.uci.edu/~eppstein/PADS
.. _Depth-limited search: https://en.wikipedia.org/wiki/Depth-limited_search
"""
return {t: s for s, t in dfs_edges(G, source, depth_limit)}