本文整理匯總了Python中networkx.immediate_dominators方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.immediate_dominators方法的具體用法?Python networkx.immediate_dominators怎麽用?Python networkx.immediate_dominators使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.immediate_dominators方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _refine_loop
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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
示例2: _post_dominate
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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
示例3: _compute_dominators
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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()}
示例4: dominator_forest
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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
示例5: test_exceptions
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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)
示例6: test_singleton
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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})
示例7: test_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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)})
示例8: test_cycle
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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)})
示例9: test_irreducible1
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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)})
示例10: test_irreducible2
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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)})
示例11: test_domrel_png
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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})
示例12: test_boost_example
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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})
示例13: dominance_frontiers
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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: [] for u in idom}
for u in idom:
if len(G.pred[u]) - int(u in G.pred[u]) >= 2:
p = set()
for v in G.pred[u]:
while v != idom[u] and v not in p:
p.add(v)
v = idom[v]
p.discard(u)
for v in p:
df[v].append(u)
return df
示例14: dominance_frontiers
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import immediate_dominators [as 別名]
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