本文整理匯總了Python中networkx.condensation方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.condensation方法的具體用法?Python networkx.condensation怎麽用?Python networkx.condensation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.condensation方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_condensation_as_quotient
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_condensation_as_quotient(self):
"""This tests that the condensation of a graph can be viewed as the
quotient graph under the "in the same connected component" equivalence
relation.
"""
# This example graph comes from the file `test_strongly_connected.py`.
G = nx.DiGraph()
G.add_edges_from([(1, 2), (2, 3), (2, 11), (2, 12), (3, 4), (4, 3),
(4, 5), (5, 6), (6, 5), (6, 7), (7, 8), (7, 9),
(7, 10), (8, 9), (9, 7), (10, 6), (11, 2), (11, 4),
(11, 6), (12, 6), (12, 11)])
scc = list(nx.strongly_connected_components(G))
C = nx.condensation(G, scc)
component_of = C.graph['mapping']
# Two nodes are equivalent if they are in the same connected component.
same_component = lambda u, v: component_of[u] == component_of[v]
Q = nx.quotient_graph(G, same_component)
assert_true(nx.is_isomorphic(C, Q))
示例2: test_contract_scc1
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_contract_scc1(self):
G = nx.DiGraph()
G.add_edges_from([
(1, 2), (2, 3), (2, 11), (2, 12), (3, 4), (4, 3), (4, 5), (5, 6),
(6, 5), (6, 7), (7, 8), (7, 9), (7, 10), (8, 9), (9, 7), (10, 6),
(11, 2), (11, 4), (11, 6), (12, 6), (12, 11),
])
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
# DAG
assert_true(nx.is_directed_acyclic_graph(cG))
# nodes
assert_equal(sorted(cG.nodes()), [0, 1, 2, 3])
# edges
mapping={}
for i, component in enumerate(scc):
for n in component:
mapping[n] = i
edge = (mapping[2], mapping[3])
assert_true(cG.has_edge(*edge))
edge = (mapping[2], mapping[5])
assert_true(cG.has_edge(*edge))
edge = (mapping[3], mapping[5])
assert_true(cG.has_edge(*edge))
示例3: test_condensation_as_quotient
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_condensation_as_quotient(self):
"""This tests that the condensation of a graph can be viewed as the
quotient graph under the "in the same connected component" equivalence
relation.
"""
# This example graph comes from the file `test_strongly_connected.py`.
G = nx.DiGraph()
G.add_edges_from([(1, 2), (2, 3), (2, 11), (2, 12), (3, 4), (4, 3),
(4, 5), (5, 6), (6, 5), (6, 7), (7, 8), (7, 9),
(7, 10), (8, 9), (9, 7), (10, 6), (11, 2), (11, 4),
(11, 6), (12, 6), (12, 11)])
scc = list(nx.strongly_connected_components(G))
C = nx.condensation(G, scc)
component_of = C.graph['mapping']
# Two nodes are equivalent if they are in the same connected component.
def same_component(u, v):
return component_of[u] == component_of[v]
Q = nx.quotient_graph(G, same_component)
assert_true(nx.is_isomorphic(C, Q))
示例4: test_contract_scc1
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_contract_scc1(self):
G = nx.DiGraph()
G.add_edges_from([
(1, 2), (2, 3), (2, 11), (2, 12), (3, 4), (4, 3), (4, 5), (5, 6),
(6, 5), (6, 7), (7, 8), (7, 9), (7, 10), (8, 9), (9, 7), (10, 6),
(11, 2), (11, 4), (11, 6), (12, 6), (12, 11),
])
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
# DAG
assert_true(nx.is_directed_acyclic_graph(cG))
# nodes
assert_equal(sorted(cG.nodes()), [0, 1, 2, 3])
# edges
mapping = {}
for i, component in enumerate(scc):
for n in component:
mapping[n] = i
edge = (mapping[2], mapping[3])
assert_true(cG.has_edge(*edge))
edge = (mapping[2], mapping[5])
assert_true(cG.has_edge(*edge))
edge = (mapping[3], mapping[5])
assert_true(cG.has_edge(*edge))
示例5: compute_condensation_in_topological_order
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def compute_condensation_in_topological_order(dependency_graph: nx.DiGraph, sort_by = lambda x: x):
if not dependency_graph.number_of_nodes():
return
condensed_graph = nx.condensation(dependency_graph)
assert isinstance(condensed_graph, nx.DiGraph)
for connected_component_index in nx.lexicographical_topological_sort(condensed_graph, key=sort_by):
yield list(sorted(condensed_graph.nodes[connected_component_index]['members'], key=sort_by))
示例6: recalculate_template_instantiation_can_trigger_static_asserts_info
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def recalculate_template_instantiation_can_trigger_static_asserts_info(header: ir.Header):
if not header.template_defns:
return header
template_defn_by_name = {template_defn.name: template_defn
for template_defn in header.template_defns}
template_defn_dependency_graph = compute_template_dependency_graph(header.template_defns, template_defn_by_name)
condensed_graph = nx.condensation(template_defn_dependency_graph)
assert isinstance(condensed_graph, nx.DiGraph)
template_defn_dependency_graph_transitive_closure = nx.transitive_closure(template_defn_dependency_graph)
assert isinstance(template_defn_dependency_graph_transitive_closure, nx.DiGraph)
# Determine which connected components can trigger static assert errors.
condensed_node_can_trigger_static_asserts = defaultdict(lambda: False)
for connected_component_index in reversed(list(nx.lexicographical_topological_sort(condensed_graph))):
condensed_node = condensed_graph.nodes[connected_component_index]
# If a template defn in this connected component can trigger a static assert, the whole component can.
for template_defn_name in condensed_node['members']:
if _template_defn_contains_static_assert_stmt(template_defn_by_name[template_defn_name]):
condensed_node_can_trigger_static_asserts[connected_component_index] = True
# If a template defn in this connected component references a template defn in a connected component that can
# trigger static asserts, this connected component can also trigger them.
for called_condensed_node_index in condensed_graph.successors(connected_component_index):
if condensed_node_can_trigger_static_asserts[called_condensed_node_index]:
condensed_node_can_trigger_static_asserts[connected_component_index] = True
template_defn_can_trigger_static_asserts = dict()
for connected_component_index in condensed_graph:
for template_defn_name in condensed_graph.nodes[connected_component_index]['members']:
template_defn_can_trigger_static_asserts[template_defn_name] = condensed_node_can_trigger_static_asserts[connected_component_index]
return _apply_template_instantiation_can_trigger_static_asserts_info(header, template_defn_can_trigger_static_asserts)
開發者ID:google,項目名稱:tmppy,代碼行數:38,代碼來源:_recalculate_template_instantiation_can_trigger_static_asserts_info.py
示例7: test_contract_scc_isolate
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_contract_scc_isolate(self):
# Bug found and fixed in [1687].
G = nx.DiGraph()
G.add_edge(1, 2)
G.add_edge(2, 1)
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
assert_equal(list(cG.nodes()), [0])
assert_equal(list(cG.edges()), [])
示例8: test_contract_scc_edge
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_contract_scc_edge(self):
G = nx.DiGraph()
G.add_edge(1, 2)
G.add_edge(2, 1)
G.add_edge(2, 3)
G.add_edge(3, 4)
G.add_edge(4, 3)
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
assert_equal(cG.nodes(), [0, 1])
if 1 in scc[0]:
edge = (0, 1)
else:
edge = (1, 0)
assert_equal(list(cG.edges()), [edge])
示例9: test_connected_raise
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_connected_raise(self):
G=nx.Graph()
assert_raises(NetworkXNotImplemented, nx.strongly_connected_components, G)
assert_raises(NetworkXNotImplemented, nx.kosaraju_strongly_connected_components, G)
assert_raises(NetworkXNotImplemented, nx.strongly_connected_components_recursive, G)
assert_raises(NetworkXNotImplemented, nx.strongly_connected_component_subgraphs, G)
assert_raises(NetworkXNotImplemented, nx.is_strongly_connected, G)
assert_raises(nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph())
assert_raises(NetworkXNotImplemented, nx.condensation, G)
示例10: attracting_components
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def attracting_components(G):
"""Generates a list of attracting components in `G`.
An attracting component in a directed graph `G` is a strongly connected
component with the property that a random walker on the graph will never
leave the component, once it enters the component.
The nodes in attracting components can also be thought of as recurrent
nodes. If a random walker enters the attractor containing the node, then
the node will be visited infinitely often.
Parameters
----------
G : DiGraph, MultiDiGraph
The graph to be analyzed.
Returns
-------
attractors : generator of sets
A generator of sets of nodes, one for each attracting component of G.
See Also
--------
number_attracting_components
is_attracting_component
attracting_component_subgraphs
"""
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
for n in cG:
if cG.out_degree(n) == 0:
yield scc[n]
示例11: test_contract_scc_edge
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_contract_scc_edge(self):
G = nx.DiGraph()
G.add_edge(1, 2)
G.add_edge(2, 1)
G.add_edge(2, 3)
G.add_edge(3, 4)
G.add_edge(4, 3)
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
assert_equal(sorted(cG.nodes()), [0, 1])
if 1 in scc[0]:
edge = (0, 1)
else:
edge = (1, 0)
assert_equal(list(cG.edges()), [edge])
示例12: test_null_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_null_graph(self):
G = nx.DiGraph()
assert_equal(list(nx.strongly_connected_components(G)), [])
assert_equal(list(nx.kosaraju_strongly_connected_components(G)), [])
assert_equal(list(nx.strongly_connected_components_recursive(G)), [])
assert_equal(len(nx.condensation(G)), 0)
assert_raises(nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph())
示例13: test_connected_raise
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def test_connected_raise(self):
G = nx.Graph()
assert_raises(NetworkXNotImplemented, nx.strongly_connected_components, G)
assert_raises(NetworkXNotImplemented, nx.kosaraju_strongly_connected_components, G)
assert_raises(NetworkXNotImplemented, nx.strongly_connected_components_recursive, G)
assert_raises(NetworkXNotImplemented, nx.is_strongly_connected, G)
assert_raises(nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph())
assert_raises(NetworkXNotImplemented, nx.condensation, G)
# deprecated
assert_raises(NetworkXNotImplemented, nx.strongly_connected_component_subgraphs, G)
# Commented out due to variability on Travis-CI hardware/operating systems
# def test_linear_time(self):
# # See Issue #2831
# count = 100 # base case
# dg = nx.DiGraph()
# dg.add_nodes_from([0, 1])
# for i in range(2, count):
# dg.add_node(i)
# dg.add_edge(i, 1)
# dg.add_edge(0, i)
# t = time.time()
# ret = tuple(nx.strongly_connected_components(dg))
# dt = time.time() - t
#
# count = 200
# dg = nx.DiGraph()
# dg.add_nodes_from([0, 1])
# for i in range(2, count):
# dg.add_node(i)
# dg.add_edge(i, 1)
# dg.add_edge(0, i)
# t = time.time()
# ret = tuple(nx.strongly_connected_components(dg))
# dt2 = time.time() - t
# assert_less(dt2, dt * 2.3) # should be 2 times longer for this graph
示例14: attracting_components
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def attracting_components(G):
"""Generates the attracting components in `G`.
An attracting component in a directed graph `G` is a strongly connected
component with the property that a random walker on the graph will never
leave the component, once it enters the component.
The nodes in attracting components can also be thought of as recurrent
nodes. If a random walker enters the attractor containing the node, then
the node will be visited infinitely often.
Parameters
----------
G : DiGraph, MultiDiGraph
The graph to be analyzed.
Returns
-------
attractors : generator of sets
A generator of sets of nodes, one for each attracting component of G.
Raises
------
NetworkXNotImplemented :
If the input graph is undirected.
See Also
--------
number_attracting_components
is_attracting_component
"""
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
for n in cG:
if cG.out_degree(n) == 0:
yield scc[n]
示例15: attracting_components
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import condensation [as 別名]
def attracting_components(G):
"""Generates a list of attracting components in `G`.
An attracting component in a directed graph `G` is a strongly connected
component with the property that a random walker on the graph will never
leave the component, once it enters the component.
The nodes in attracting components can also be thought of as recurrent
nodes. If a random walker enters the attractor containing the node, then
the node will be visited infinitely often.
Parameters
----------
G : DiGraph, MultiDiGraph
The graph to be analyzed.
Returns
-------
attractors : generator of sets
A generator of sets of nodes, one for each attracting component of G.
Raises
------
NetworkXNotImplemented :
If the input graph is undirected.
See Also
--------
number_attracting_components
is_attracting_component
attracting_component_subgraphs
"""
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
for n in cG:
if cG.out_degree(n) == 0:
yield scc[n]