Python networkx.immediate_dominators() Examples

The following are 30 code examples of networkx.immediate_dominators(). 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: region_identifier.py    From angr with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _refine_loop(self, graph, head, initial_loop_nodes, initial_exit_nodes):
        refined_loop_nodes = initial_loop_nodes.copy()
        refined_exit_nodes = initial_exit_nodes.copy()

        idom = networkx.immediate_dominators(graph, self._start_node)

        new_exit_nodes = refined_exit_nodes
        while len(refined_exit_nodes) > 1 and new_exit_nodes:
            new_exit_nodes = set()
            for n in list(refined_exit_nodes):
                if all(pred in refined_loop_nodes for pred in graph.predecessors(n)) and dominates(idom, head, n):
                    refined_loop_nodes.add(n)
                    refined_exit_nodes.remove(n)
                    for u in (set(graph.successors(n)) - refined_loop_nodes):
                        new_exit_nodes.add(u)
            refined_exit_nodes |= new_exit_nodes

        refined_loop_nodes = refined_loop_nodes - refined_exit_nodes

        return refined_loop_nodes, refined_exit_nodes 
Example #2
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_boost_example(self):
        # Graph taken from Figure 1 of
        # http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm
        edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6),
                 (5, 7), (6, 4)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 0),
                     {0: 0, 1: 0, 2: 1, 3: 1, 4: 3, 5: 4, 6: 4, 7: 1})
        # Test postdominance.
        with nx.utils.reversed(G):
            assert_equal(nx.immediate_dominators(G, 7),
                         {0: 1, 1: 7, 2: 7, 3: 4, 4: 5, 5: 7, 6: 4, 7: 7}) 
Example #3
Source File: veritesting.py    From angr with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _post_dominate(reversed_graph, n1, n2):
        """
        Checks whether `n1` post-dominates `n2` in the *original* (not reversed) graph.

        :param networkx.DiGraph reversed_graph:  The reversed networkx.DiGraph instance.
        :param networkx.Node n1:                 Node 1.
        :param networkx.Node n2:                 Node 2.
        :returns bool:                           True/False.
        """

        ds = networkx.immediate_dominators(reversed_graph, n1)
        return n2 in ds 
Example #4
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_domrel_png(self):
        # Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png
        edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 1),
                     {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 2})
        # Test postdominance.
        with nx.utils.reversed(G):
            assert_equal(nx.immediate_dominators(G, 6),
                         {1: 2, 2: 6, 3: 5, 4: 5, 5: 2, 6: 6}) 
Example #5
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_irreducible2(self):
        # Graph taken from Figure 4 of
        # K. D. Cooper, T. J. Harvey, and K. Kennedy.
        # A simple, fast dominance algorithm.
        # Software Practice & Experience, 4:110, 2001.
        edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1),
                 (6, 4), (6, 5)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 6),
                     {i: 6 for i in range(1, 7)}) 
Example #6
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_irreducible1(self):
        # Graph taken from Figure 2 of
        # K. D. Cooper, T. J. Harvey, and K. Kennedy.
        # A simple, fast dominance algorithm.
        # Software Practice & Experience, 4:110, 2001.
        edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 5),
                     {i: 5 for i in range(1, 6)}) 
Example #7
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_cycle(self):
        n = 5
        G = nx.cycle_graph(n, create_using=nx.DiGraph())
        assert_equal(nx.immediate_dominators(G, 0),
                     {i: max(i - 1, 0) for i in range(n)}) 
Example #8
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_path(self):
        n = 5
        G = nx.path_graph(n, create_using=nx.DiGraph())
        assert_equal(nx.immediate_dominators(G, 0),
                     {i: max(i - 1, 0) for i in range(n)}) 
Example #9
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_singleton(self):
        G = nx.DiGraph()
        G.add_node(0)
        assert_equal(nx.immediate_dominators(G, 0), {0: 0})
        G.add_edge(0, 0)
        assert_equal(nx.immediate_dominators(G, 0), {0: 0}) 
Example #10
Source File: test_dominance.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_exceptions(self):
        G = nx.Graph()
        G.add_node(0)
        assert_raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
        G = nx.MultiGraph(G)
        assert_raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
        G = nx.DiGraph([[0, 0]])
        assert_raises(nx.NetworkXError, nx.immediate_dominators, G, 1) 
Example #11
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_boost_example(self):
        # Graph taken from Figure 1 of
        # http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm
        edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6),
                 (5, 7), (6, 4)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 0),
                     {0: 0, 1: 0, 2: 1, 3: 1, 4: 3, 5: 4, 6: 4, 7: 1})
        # Test postdominance.
        with nx.utils.reversed(G):
            assert_equal(nx.immediate_dominators(G, 7),
                         {0: 1, 1: 7, 2: 7, 3: 4, 4: 5, 5: 7, 6: 4, 7: 7}) 
Example #12
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_domrel_png(self):
        # Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png
        edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 1),
                     {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 2})
        # Test postdominance.
        with nx.utils.reversed(G):
            assert_equal(nx.immediate_dominators(G, 6),
                         {1: 2, 2: 6, 3: 5, 4: 5, 5: 2, 6: 6}) 
Example #13
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_irreducible2(self):
        # Graph taken from Figure 4 of
        # K. D. Cooper, T. J. Harvey, and K. Kennedy.
        # A simple, fast dominance algorithm.
        # Software Practice & Experience, 4:110, 2001.
        edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1),
                 (6, 4), (6, 5)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 6),
                     {i: 6 for i in range(1, 7)}) 
Example #14
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_irreducible1(self):
        # Graph taken from Figure 2 of
        # K. D. Cooper, T. J. Harvey, and K. Kennedy.
        # A simple, fast dominance algorithm.
        # Software Practice & Experience, 4:110, 2001.
        edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 5),
                     {i: 5 for i in range(1, 6)}) 
Example #15
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cycle(self):
        n = 5
        G = nx.cycle_graph(n, create_using=nx.DiGraph())
        assert_equal(nx.immediate_dominators(G, 0),
                     {i: max(i - 1, 0) for i in range(n)}) 
Example #16
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_path(self):
        n = 5
        G = nx.path_graph(n, create_using=nx.DiGraph())
        assert_equal(nx.immediate_dominators(G, 0),
                     {i: max(i - 1, 0) for i in range(n)}) 
Example #17
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_singleton(self):
        G = nx.DiGraph()
        G.add_node(0)
        assert_equal(nx.immediate_dominators(G, 0), {0: 0})
        G.add_edge(0, 0)
        assert_equal(nx.immediate_dominators(G, 0), {0: 0}) 
Example #18
Source File: test_dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_exceptions(self):
        G = nx.Graph()
        G.add_node(0)
        assert_raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
        G = nx.MultiGraph(G)
        assert_raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
        G = nx.DiGraph([[0, 0]])
        assert_raises(nx.NetworkXError, nx.immediate_dominators, G, 1) 
Example #19
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_boost_example(self):
        # Graph taken from Figure 1 of
        # http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm
        edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6),
                 (5, 7), (6, 4)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 0),
                     {0: 0, 1: 0, 2: 1, 3: 1, 4: 3, 5: 4, 6: 4, 7: 1})
        # Test postdominance.
        with nx.utils.reversed(G):
            assert_equal(nx.immediate_dominators(G, 7),
                         {0: 1, 1: 7, 2: 7, 3: 4, 4: 5, 5: 7, 6: 4, 7: 7}) 
Example #20
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_domrel_png(self):
        # Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png
        edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 1),
                     {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 2})
        # Test postdominance.
        with nx.utils.reversed(G):
            assert_equal(nx.immediate_dominators(G, 6),
                         {1: 2, 2: 6, 3: 5, 4: 5, 5: 2, 6: 6}) 
Example #21
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_irreducible2(self):
        # Graph taken from Figure 4 of
        # K. D. Cooper, T. J. Harvey, and K. Kennedy.
        # A simple, fast dominance algorithm.
        # Software Practice & Experience, 4:110, 2001.
        edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1),
                 (6, 4), (6, 5)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 6),
                     {i: 6 for i in range(1, 7)}) 
Example #22
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_irreducible1(self):
        # Graph taken from Figure 2 of
        # K. D. Cooper, T. J. Harvey, and K. Kennedy.
        # A simple, fast dominance algorithm.
        # Software Practice & Experience, 4:110, 2001.
        edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)]
        G = nx.DiGraph(edges)
        assert_equal(nx.immediate_dominators(G, 5),
                     {i: 5 for i in range(1, 6)}) 
Example #23
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_cycle(self):
        n = 5
        G = nx.cycle_graph(n, create_using=nx.DiGraph())
        assert_equal(nx.immediate_dominators(G, 0),
                     {i: max(i - 1, 0) for i in range(n)}) 
Example #24
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_path(self):
        n = 5
        G = nx.path_graph(n, create_using=nx.DiGraph())
        assert_equal(nx.immediate_dominators(G, 0),
                     {i: max(i - 1, 0) for i in range(n)}) 
Example #25
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_singleton(self):
        G = nx.DiGraph()
        G.add_node(0)
        assert_equal(nx.immediate_dominators(G, 0), {0: 0})
        G.add_edge(0, 0)
        assert_equal(nx.immediate_dominators(G, 0), {0: 0}) 
Example #26
Source File: test_dominance.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_exceptions(self):
        G = nx.Graph()
        G.add_node(0)
        assert_raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
        G = nx.MultiGraph(G)
        assert_raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
        G = nx.DiGraph([[0, 0]])
        assert_raises(nx.NetworkXError, nx.immediate_dominators, G, 1) 
Example #27
Source File: cfg.py    From polyfile with Apache License 2.0 5 votes vote down vote up
def dominator_forest(self):
        if self._dominator_forest is not None:
            return self._dominator_forest
        self._dominator_forest = DAG()
        for root in self.roots:
            for node, dominated_by in nx.immediate_dominators(self, root).items():
                if node != dominated_by:
                    self._dominator_forest.add_edge(dominated_by, node)
        return self._dominator_forest 
Example #28
Source File: cfg.py    From teether with Apache License 2.0 5 votes vote down vote up
def _compute_dominators(self):
        import networkx
        g = networkx.DiGraph()
        for bb in self.bbs:
            for succ in bb.succ:
                g.add_edge(bb.start, succ.start)
        self._dominators = {self._bb_at[k]: self._bb_at[v] for k, v in networkx.immediate_dominators(g, 0).items()} 
Example #29
Source File: dominance.py    From Carnets with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def dominance_frontiers(G, start):
    """Returns the dominance frontiers of all nodes of a directed graph.

    Parameters
    ----------
    G : a DiGraph or MultiDiGraph
        The graph where dominance is to be computed.

    start : node
        The start node of dominance computation.

    Returns
    -------
    df : dict keyed by nodes
        A dict containing the dominance frontiers of each node reachable from
        `start` as lists.

    Raises
    ------
    NetworkXNotImplemented
        If `G` is undirected.

    NetworkXError
        If `start` is not in `G`.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 5), (3, 4), (4, 5)])
    >>> sorted((u, sorted(df)) for u, df in nx.dominance_frontiers(G, 1).items())
    [(1, []), (2, [5]), (3, [5]), (4, [5]), (5, [])]

    References
    ----------
    .. [1] K. D. Cooper, T. J. Harvey, and K. Kennedy.
           A simple, fast dominance algorithm.
           Software Practice & Experience, 4:110, 2001.
    """
    idom = nx.immediate_dominators(G, start)

    df = {u: set() for u in idom}
    for u in idom:
        if len(G.pred[u]) >= 2:
            for v in G.pred[u]:
                if v in idom:
                    while v != idom[u]:
                        df[v].add(u)
                        v = idom[v]
    return df 
Example #30
Source File: dominance.py    From aws-kube-codesuite with Apache License 2.0 4 votes vote down vote up
def dominance_frontiers(G, start):
    """Returns the dominance frontiers of all nodes of a directed graph.

    Parameters
    ----------
    G : a DiGraph or MultiDiGraph
        The graph where dominance is to be computed.

    start : node
        The start node of dominance computation.

    Returns
    -------
    df : dict keyed by nodes
        A dict containing the dominance frontiers of each node reachable from
        `start` as lists.

    Raises
    ------
    NetworkXNotImplemented
        If `G` is undirected.

    NetworkXError
        If `start` is not in `G`.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 5), (3, 4), (4, 5)])
    >>> sorted((u, sorted(df)) for u, df in nx.dominance_frontiers(G, 1).items())
    [(1, []), (2, [5]), (3, [5]), (4, [5]), (5, [])]

    References
    ----------
    .. [1] K. D. Cooper, T. J. Harvey, and K. Kennedy.
           A simple, fast dominance algorithm.
           Software Practice & Experience, 4:110, 2001.
    """
    idom = nx.immediate_dominators(G, start)

    df = {u: set() for u in idom}
    for u in idom:
        if len(G.pred[u]) >= 2:
            for v in G.pred[u]:
                if v in idom:
                    while v != idom[u]:
                        df[v].add(u)
                        v = idom[v]
    return df