Python networkx.dfs_postorder_nodes() Examples
The following are 21
code examples of networkx.dfs_postorder_nodes().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: cfg_emulated.py From angr with BSD 2-Clause "Simplified" License | 6 votes |
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
Example #2
Source File: cfg_utils.py From angr with BSD 2-Clause "Simplified" License | 6 votes |
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)
Example #3
Source File: region_identifier.py From angr with BSD 2-Clause "Simplified" License | 6 votes |
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
Example #4
Source File: cliquetree.py From pyBN with MIT License | 5 votes |
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
Example #5
Source File: test_dfs.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
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]))
Example #6
Source File: test_dfs.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
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])
Example #7
Source File: test_dfs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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]))
Example #8
Source File: test_dfs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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])
Example #9
Source File: test_dfs.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
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])
Example #10
Source File: depth_first_search.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
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
Example #11
Source File: configuration_space.py From HPOlib with GNU General Public License v3.0 | 5 votes |
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
Example #12
Source File: compute_sfs.py From momi2 with GNU General Public License v3.0 | 5 votes |
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
Example #13
Source File: strongly_connected.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
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)
Example #14
Source File: graph.py From angr with BSD 2-Clause "Simplified" License | 4 votes |
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 #
Example #15
Source File: depth_first_search.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
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')
Example #16
Source File: depth_first_search.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def dfs_preorder_nodes(G, source=None, depth_limit=None): """Generate nodes in a depth-first-search pre-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 pre-ordering. Examples -------- >>> G = nx.path_graph(5) >>> list(nx.dfs_preorder_nodes(G, source=0)) [0, 1, 2, 3, 4] >>> list(nx.dfs_preorder_nodes(G, source=0, depth_limit=2)) [0, 1, 2] 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_postorder_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 == 'forward')
Example #17
Source File: strongly_connected.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def kosaraju_strongly_connected_components(G, source=None): """Generate nodes in strongly connected components of graph. Parameters ---------- G : NetworkX Graph A 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()) >>> nx.add_cycle(G, [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 -------- strongly_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)
Example #18
Source File: depth_first_search.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
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')
Example #19
Source File: depth_first_search.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def dfs_preorder_nodes(G, source=None, depth_limit=None): """Generate nodes in a depth-first-search pre-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 pre-ordering. Examples -------- >>> G = nx.path_graph(5) >>> list(nx.dfs_preorder_nodes(G, source=0)) [0, 1, 2, 3, 4] >>> list(nx.dfs_preorder_nodes(G, source=0, depth_limit=2)) [0, 1, 2] 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_postorder_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 == 'forward')
Example #20
Source File: region_identifier.py From angr with BSD 2-Clause "Simplified" License | 4 votes |
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
Example #21
Source File: strongly_connected.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def kosaraju_strongly_connected_components(G, source=None): """Generate nodes in strongly connected components of graph. Parameters ---------- G : NetworkX Graph A 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()) >>> nx.add_cycle(G, [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 -------- strongly_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)