本文整理汇总了Python中networkx.strongly_connected_components方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.strongly_connected_components方法的具体用法?Python networkx.strongly_connected_components怎么用?Python networkx.strongly_connected_components使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.strongly_connected_components方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _high_degree_components
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def _high_degree_components(G, k):
"""Helper for filtering components that cant be k-edge-connected.
Removes and generates each node with degree less than k. Then generates
remaining components where all nodes have degree at least k.
"""
# Iteravely remove parts of the graph that are not k-edge-connected
H = G.copy()
singletons = set(_low_degree_nodes(H, k))
while singletons:
# Only search neighbors of removed nodes
nbunch = set(it.chain.from_iterable(map(H.neighbors, singletons)))
nbunch.difference_update(singletons)
H.remove_nodes_from(singletons)
for node in singletons:
yield {node}
singletons = set(_low_degree_nodes(H, k, nbunch))
# Note: remaining connected components may not be k-edge-connected
if G.is_directed():
for cc in nx.strongly_connected_components(H):
yield cc
else:
for cc in nx.connected_components(H):
yield cc
示例2: _parse_loops_from_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def _parse_loops_from_graph(self, graph):
"""
Return all Loop instances that can be extracted from a graph.
:param graph: The graph to analyze.
:return: A list of all the Loop instances that were found in the graph.
"""
outtop = []
outall = []
for subg in ( networkx.induced_subgraph(graph, nodes).copy() for nodes in networkx.strongly_connected_components(graph)):
if len(subg.nodes()) == 1:
if len(list(subg.successors(list(subg.nodes())[0]))) == 0:
continue
thisloop, allloops = self._parse_loop_graph(subg, graph)
if thisloop is not None:
outall += allloops
outtop.append(thisloop)
return outtop, outall
示例3: detect_deadlock
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def detect_deadlock(self):
"""
Detects whether the system is in a deadlocked state,
that is, is there a knot. Note that this code is taken
and adapted from the NetworkX Developer Zone Ticket
#663 knot.py (09/06/2015).
"""
knots = []
for c in nx.strongly_connected_components(self.statedigraph):
subgraph = self.statedigraph.subgraph(c)
nodes = set(subgraph.nodes())
if len(nodes) == 1:
n = nodes.pop()
nodes.add(n)
if set(self.statedigraph.successors(n)) == nodes:
knots.append(subgraph)
else:
for n in nodes:
successors = nx.descendants(self.statedigraph, n)
if successors <= nodes:
knots.append(subgraph)
break
if len(knots) > 0:
return True
return False
示例4: test_strong_connected_components
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def test_strong_connected_components(random_network):
from pathpy.classes.network import network_to_networkx
from networkx import strongly_connected_components
from pathpy.algorithms.components import connected_components
wrong_gcc = 0
for i in range(200):
hn = random_network(n=10, m=30, directed=True, seed=i)
hn_nx = network_to_networkx(hn)
size_largest_nx = len(max(strongly_connected_components(hn_nx), key=len))
components = connected_components(hn)
size_largest = max(len(c) for c in components)
if size_largest_nx != size_largest:
# print(f'seed {i} | nx: {size_largest_nx}, pp: {size_largest}')
wrong_gcc += 1
assert wrong_gcc < 1, 'wrong results {wrong_gcc/i:0.1%}'
示例5: test_condensation_as_quotient
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [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))
示例6: number_strongly_connected_components
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def number_strongly_connected_components(G):
"""Return number of strongly connected components in graph.
Parameters
----------
G : NetworkX graph
A directed graph.
Returns
-------
n : integer
Number of strongly connected components
See Also
--------
connected_components
Notes
-----
For directed graphs only.
"""
return len(list(strongly_connected_components(G)))
示例7: test_contract_scc1
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [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))
示例8: _high_degree_components
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def _high_degree_components(G, k):
"""Helper for filtering components that can't be k-edge-connected.
Removes and generates each node with degree less than k. Then generates
remaining components where all nodes have degree at least k.
"""
# Iteravely remove parts of the graph that are not k-edge-connected
H = G.copy()
singletons = set(_low_degree_nodes(H, k))
while singletons:
# Only search neighbors of removed nodes
nbunch = set(it.chain.from_iterable(map(H.neighbors, singletons)))
nbunch.difference_update(singletons)
H.remove_nodes_from(singletons)
for node in singletons:
yield {node}
singletons = set(_low_degree_nodes(H, k, nbunch))
# Note: remaining connected components may not be k-edge-connected
if G.is_directed():
for cc in nx.strongly_connected_components(H):
yield cc
else:
for cc in nx.connected_components(H):
yield cc
示例9: test_condensation_as_quotient
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [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))
示例10: find_widening_points
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def find_widening_points(function_addr, function_endpoints, graph): # pylint: disable=unused-argument
"""
Given a local transition graph of a function, find all widening points inside.
Correctly choosing widening points is very important in order to not lose too much information during static
analysis. We mainly consider merge points that has at least one loop back edges coming in as widening points.
:param int function_addr: Address of the function.
:param list function_endpoints: Endpoints of the function, typically coming from Function.endpoints.
:param networkx.DiGraph graph: A local transition graph of a function, normally Function.graph.
:return: A list of addresses of widening points.
:rtype: list
"""
sccs = networkx.strongly_connected_components(graph)
widening_addrs = set()
for scc in sccs:
if len(scc) == 1:
node = next(iter(scc))
if graph.has_edge(node, node):
# self loop
widening_addrs.add(node.addr)
else:
for n in scc:
predecessors = graph.predecessors(n)
if any([ p not in scc for p in predecessors]):
widening_addrs.add(n.addr)
break
return list(widening_addrs)
示例11: _calculate_safe_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def _calculate_safe_nodes(self):
if self._safe_nodes is not None:
return
connected_components = list(networkx.strongly_connected_components(self._digraph))
for component in connected_components:
if self._state.node.index in component:
assert self._safe_nodes is None
self._safe_nodes = component
assert self._safe_nodes is not None
示例12: test_strong_connected_tmp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def test_strong_connected_tmp(random_temp_network):
from pathpy.path_extraction.temporal_paths import paths_from_temporal_network_dag
from pathpy.algorithms.components import connected_components
from pathpy.classes.network import network_to_networkx
from networkx import strongly_connected_components
from pathpy.utils.log import Log, Severity
Log.set_min_severity(Severity.WARNING)
for delta in range(1, 900, 50):
print(delta)
tn = random_temp_network(n=10, m=100, min_t=0, max_t=800, seed=90) # type: pp.TemporalNetwork
obs_times = np.array([t[-1] for t in tn.tedges])
obs_times.sort()
p = paths_from_temporal_network_dag(tn, delta=delta)
hn = pp.HigherOrderNetwork(p, k=2)
# using NetworkX
nx_network = network_to_networkx(hn)
giant_size_nx = len(max(strongly_connected_components(nx_network), key=len))
# using pathpy
components = connected_components(hn)
if giant_size_nx > 3:
print(giant_size_nx)
giant_size_pp = max(len(c) for c in components)
assert giant_size_nx == giant_size_pp
示例13: flow_hierarchy
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def flow_hierarchy(G, weight=None):
"""Returns the flow hierarchy of a directed network.
Flow hierarchy is defined as the fraction of edges not participating
in cycles in a directed graph [1]_.
Parameters
----------
G : DiGraph or MultiDiGraph
A directed graph
weight : key,optional (default=None)
Attribute to use for node weights. If None the weight defaults to 1.
Returns
-------
h : float
Flow heirarchy value
Notes
-----
The algorithm described in [1]_ computes the flow hierarchy through
exponentiation of the adjacency matrix. This function implements an
alternative approach that finds strongly connected components.
An edge is in a cycle if and only if it is in a strongly connected
component, which can be found in `O(m)` time using Tarjan's algorithm.
References
----------
.. [1] Luo, J.; Magee, C.L. (2011),
Detecting evolving patterns of self-organizing networks by flow
hierarchy measurement, Complexity, Volume 16 Issue 6 53-61.
DOI: 10.1002/cplx.20368
http://web.mit.edu/~cmagee/www/documents/28-DetectingEvolvingPatterns_FlowHierarchy.pdf
"""
if not G.is_directed():
raise nx.NetworkXError("G must be a digraph in flow_heirarchy")
scc = nx.strongly_connected_components(G)
return 1.-sum(G.subgraph(c).size(weight) for c in scc)/float(G.size(weight))
示例14: is_strongly_connected
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def is_strongly_connected(G):
"""Test directed graph for strong connectivity.
Parameters
----------
G : NetworkX Graph
A directed graph.
Returns
-------
connected : bool
True if the graph is strongly connected, False otherwise.
See Also
--------
strongly_connected_components
Notes
-----
For directed graphs only.
"""
if len(G) == 0:
raise nx.NetworkXPointlessConcept(
"""Connectivity is undefined for the null graph.""")
return len(list(strongly_connected_components(G))[0]) == len(G)
示例15: test_tarjan
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_components [as 别名]
def test_tarjan(self):
scc = nx.strongly_connected_components
for G, C in self.gc:
assert_equal({frozenset(g) for g in scc(G)}, C)