本文整理汇总了Python中networkx.dfs_postorder_nodes方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.dfs_postorder_nodes方法的具体用法?Python networkx.dfs_postorder_nodes怎么用?Python networkx.dfs_postorder_nodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.dfs_postorder_nodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _quasi_topological_sort
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def _quasi_topological_sort(self):
"""
Perform a quasi-topological sort on an already constructed CFG graph (a networkx DiGraph)
:return: None
"""
# Clear the existing sorting result
self._quasi_topological_order = {}
ctr = self._graph.number_of_nodes()
for ep in self._entry_points:
# FIXME: This is not always correct. We'd better store CFGNodes in self._entry_points
ep_node = self.get_any_node(ep)
if not ep_node:
continue
for n in networkx.dfs_postorder_nodes(self._graph, source=ep_node):
if n not in self._quasi_topological_order:
self._quasi_topological_order[n] = ctr
ctr -= 1
示例2: reverse_post_order_sort_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def reverse_post_order_sort_nodes(graph, nodes=None):
"""
Sort a given set of nodes in reverse post ordering.
:param networkx.DiGraph graph: A local transition graph of a function.
:param iterable nodes: A collection of nodes to sort.
:return: A list of sorted nodes.
:rtype: list
"""
post_order = networkx.dfs_postorder_nodes(graph)
if nodes is None:
return reversed(list(post_order))
addrs_to_index = {}
for i, n in enumerate(post_order):
addrs_to_index[n.addr] = i
return sorted(nodes, key=lambda n: addrs_to_index[n.addr], reverse=True)
示例3: _merge_single_entry_node
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def _merge_single_entry_node(self, graph):
r = False
while True:
for node in networkx.dfs_postorder_nodes(graph):
preds = graph.predecessors(node)
if len(preds) == 1:
# merge the two nodes
self._absorb_node(graph, preds[0], node)
r = True
break
else:
break
return r
示例4: compute_sfs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def compute_sfs(cls, leaf_states, demo):
liklist = cls(leaf_states, demo)
for event in nx.dfs_postorder_nodes(
demo._event_tree):
liklist._process_event(event)
assert len(liklist.likelihood_list) == 1
lik, = liklist.likelihood_list
return lik.sfs
示例5: get_dag
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def get_dag(dag):
sorted_dag = dag.copy()
# The children of a node are traversed in a random order. Therefore,
# copy the graph, sort the children and then traverse it
for adj in sorted_dag.adj:
sorted_dag.adj[adj] = OrderedDict(
sorted(sorted_dag.adj[adj].items(), key=lambda item: item[0]))
nodes = nx.dfs_postorder_nodes(sorted_dag,
source='__HPOlib_configuration_space_root__')
nodes = [node for node in nodes if node !=
'__HPOlib_configuration_space_root__']
return nodes
示例6: dfs_postorder_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def dfs_postorder_nodes(G,source=None):
"""Produce nodes in a depth-first-search post-ordering starting
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
-------
nodes: generator
A generator of nodes in a depth-first-search post-ordering.
Examples
--------
>>> G = nx.Graph()
>>> G.add_path([0,1,2])
>>> print(list(nx.dfs_postorder_nodes(G,0)))
[2, 1, 0]
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.
"""
post=(v for u,v,d in nx.dfs_labeled_edges(G,source=source)
if d['dir']=='reverse')
# potential modification: chain source to end of post-ordering
# return chain(post,[source])
return post
示例7: test_postorder_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def test_postorder_nodes(self):
assert_equal(list(nx.dfs_postorder_nodes(self.G,source=0)),
[3, 4, 2, 1, 0])
assert_equal(list(nx.dfs_postorder_nodes(self.D)),[1, 0, 3, 2])
示例8: dfs_postorder
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def dfs_postorder(self, root):
G = nx.Graph(self.E)
tree_graph = nx.dfs_tree(G,root)
clique_ordering = list(nx.dfs_postorder_nodes(tree_graph,root))
return clique_ordering
示例9: test_postorder_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def test_postorder_nodes(self):
assert_equal(list(nx.dfs_postorder_nodes(self.G, source=0)),
[3, 4, 2, 1, 0])
assert_equal(list(nx.dfs_postorder_nodes(self.D)), [1, 0, 3, 2])
示例10: dls_test_postorder_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def dls_test_postorder_nodes(self):
assert_equal(list(nx.dfs_postorder_nodes(self.G,
source=3, depth_limit=3)), [1, 7, 2, 5, 4, 3])
assert_equal(list(nx.dfs_postorder_nodes(self.D,
source=2, depth_limit=2)), ([3, 7, 2]))
示例11: dls_test_postorder_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def dls_test_postorder_nodes(self):
assert_equal(list(nx.dfs_postorder_nodes(self.G,
source=3, depth_limit=3)), [1, 7, 2, 5, 4, 3])
assert_equal(list(nx.dfs_postorder_nodes(self.D,
source=2, depth_limit=2)),([3, 7, 2]))
示例12: compute_dominance_frontier
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def compute_dominance_frontier(graph, domtree):
"""
Compute a dominance frontier based on the given post-dominator tree.
This implementation is based on figure 2 of paper An Efficient Method of Computing Static Single Assignment
Form by Ron Cytron, etc.
:param graph: The graph where we want to compute the dominance frontier.
:param domtree: The dominator tree
:returns: A dict of dominance frontier
"""
df = {}
# Perform a post-order search on the dominator tree
for x in networkx.dfs_postorder_nodes(domtree):
if x not in graph:
# Skip nodes that are not in the graph
continue
df[x] = set()
# local set
for y in graph.successors(x):
if x not in domtree.predecessors(y):
df[x].add(y)
# up set
if x is None:
continue
for z in domtree.successors(x):
if z is x:
continue
if z not in df:
continue
for y in df[z]:
if x not in list(domtree.predecessors(y)):
df[x].add(y)
return df
#
# Dominators and post-dominators
#
示例13: _make_cyclic_region
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def _make_cyclic_region(self, head, graph):
l.debug("Found cyclic region at %#08x", head.addr)
initial_loop_nodes = self._find_initial_loop_nodes(graph, head)
l.debug("Initial loop nodes %s", self._dbg_block_list(initial_loop_nodes))
# Make sure there is no other loop contained in the current loop
if {n for n in initial_loop_nodes if n.addr != head.addr}.intersection(self._loop_headers):
return None
normal_entries = {n for n in graph.predecessors(head) if n not in initial_loop_nodes}
abnormal_entries = set()
for n in initial_loop_nodes:
if n == head:
continue
preds = set(graph.predecessors(n))
abnormal_entries |= (preds - initial_loop_nodes)
l.debug("Normal entries %s", self._dbg_block_list(normal_entries))
l.debug("Abnormal entries %s", self._dbg_block_list(abnormal_entries))
initial_exit_nodes = set()
for n in initial_loop_nodes:
succs = set(graph.successors(n))
initial_exit_nodes |= (succs - initial_loop_nodes)
l.debug("Initial exit nodes %s", self._dbg_block_list(initial_exit_nodes))
refined_loop_nodes, refined_exit_nodes = self._refine_loop(graph, head, initial_loop_nodes,
initial_exit_nodes)
l.debug("Refined loop nodes %s", self._dbg_block_list(refined_loop_nodes))
l.debug("Refined exit nodes %s", self._dbg_block_list(refined_exit_nodes))
if len(refined_exit_nodes) > 1:
# self._get_start_node(graph)
node_post_order = list(networkx.dfs_postorder_nodes(graph, head))
sorted_exit_nodes = sorted(list(refined_exit_nodes), key=node_post_order.index)
normal_exit_node = sorted_exit_nodes[0]
abnormal_exit_nodes = set(sorted_exit_nodes[1:])
else:
normal_exit_node = next(iter(refined_exit_nodes)) if len(refined_exit_nodes) > 0 else None
abnormal_exit_nodes = set()
region = self._abstract_cyclic_region(graph, refined_loop_nodes, head, normal_entries, abnormal_entries,
normal_exit_node, abnormal_exit_nodes)
if len(region.successors) > 1:
# multi-successor region. refinement is required
self._refine_loop_successors(region, graph)
return region
示例14: kosaraju_strongly_connected_components
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def kosaraju_strongly_connected_components(G, source=None):
"""Generate nodes in strongly connected components of graph.
Parameters
----------
G : NetworkX Graph
An directed graph.
Returns
-------
comp : generator of sets
A genrator of sets of nodes, one for each strongly connected
component of G.
Raises
------
NetworkXNotImplemented:
If G is undirected.
Examples
--------
Generate a sorted list of strongly connected components, largest first.
>>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
>>> G.add_cycle([10, 11, 12])
>>> [len(c) for c in sorted(nx.kosaraju_strongly_connected_components(G),
... key=len, reverse=True)]
[4, 3]
If you only want the largest component, it's more efficient to
use max instead of sort.
>>> largest = max(nx.kosaraju_strongly_connected_components(G), key=len)
See Also
--------
connected_components
weakly_connected_components
Notes
-----
Uses Kosaraju's algorithm.
"""
with nx.utils.reversed(G):
post = list(nx.dfs_postorder_nodes(G, source=source))
seen = set()
while post:
r = post.pop()
if r in seen:
continue
c = nx.dfs_preorder_nodes(G, r)
new = {v for v in c if v not in seen}
yield new
seen.update(new)
示例15: dfs_postorder_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import dfs_postorder_nodes [as 别名]
def dfs_postorder_nodes(G, source=None, depth_limit=None):
"""Generate nodes in a depth-first-search post-ordering starting at 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
-------
nodes: generator
A generator of nodes in a depth-first-search post-ordering.
Examples
--------
>>> G = nx.path_graph(5)
>>> list(nx.dfs_postorder_nodes(G, source=0))
[4, 3, 2, 1, 0]
>>> list(nx.dfs_postorder_nodes(G, source=0, depth_limit=2))
[1, 0]
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
See Also
--------
dfs_edges
dfs_preorder_nodes
dfs_labeled_edges
"""
edges = nx.dfs_labeled_edges(G, source=source, depth_limit=depth_limit)
return (v for u, v, d in edges if d == 'reverse')