本文整理汇总了Python中networkx.strongly_connected_component_subgraphs方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.strongly_connected_component_subgraphs方法的具体用法?Python networkx.strongly_connected_component_subgraphs怎么用?Python networkx.strongly_connected_component_subgraphs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.strongly_connected_component_subgraphs方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def setUp(self):
self.undirected = [
nx.connected_component_subgraphs,
nx.biconnected_component_subgraphs,
]
self.directed = [
nx.weakly_connected_component_subgraphs,
nx.strongly_connected_component_subgraphs,
nx.attracting_component_subgraphs,
]
self.subgraph_funcs = self.undirected + self.directed
self.D = nx.DiGraph()
self.D.add_edge(1, 2, eattr='red')
self.D.add_edge(2, 1, eattr='red')
self.D.node[1]['nattr'] = 'blue'
self.D.graph['gattr'] = 'green'
self.G = nx.Graph()
self.G.add_edge(1, 2, eattr='red')
self.G.node[1]['nattr'] = 'blue'
self.G.graph['gattr'] = 'green'
示例2: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def setUp(self):
self.undirected = [
nx.connected_component_subgraphs,
nx.biconnected_component_subgraphs,
]
self.directed = [
nx.weakly_connected_component_subgraphs,
nx.strongly_connected_component_subgraphs,
nx.attracting_component_subgraphs,
]
self.subgraph_funcs = self.undirected + self.directed
self.D = nx.DiGraph()
self.D.add_edge(1, 2, eattr='red')
self.D.add_edge(2, 1, eattr='red')
self.D.nodes[1]['nattr'] = 'blue'
self.D.graph['gattr'] = 'green'
self.G = nx.Graph()
self.G.add_edge(1, 2, eattr='red')
self.G.nodes[1]['nattr'] = 'blue'
self.G.graph['gattr'] = 'green'
示例3: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def setUp(self):
self.undirected = [
nx.connected_component_subgraphs,
nx.biconnected_component_subgraphs,
]
self.directed = [
nx.weakly_connected_component_subgraphs,
nx.strongly_connected_component_subgraphs,
nx.attracting_component_subgraphs,
]
self.subgraph_funcs = self.undirected + self.directed
self.D = nx.DiGraph()
self.D.add_edge(1, 2, eattr='red')
self.D.add_edge(2, 1, eattr='red')
self.D.nodes[1]['nattr'] = 'blue'
self.D.graph['gattr'] = 'green'
self.G = nx.Graph()
self.G.add_edge(1, 2, eattr='red')
self.G.nodes[1]['nattr'] = 'blue'
self.G.graph['gattr'] = 'green'
示例4: __condensation
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def __condensation(self):
"""Produces condensation of cyclic graphs."""
subgraphs = nx.strongly_connected_component_subgraphs(self.digraph)
for subgraph in list(subgraphs):
if subgraph.number_of_nodes() == 1:
continue # not a cycle
pre_edges = []
suc_edges = []
for node in subgraph:
assert node not in self.node2cycle
assert node in self.digraph # no accidental copying
self.node2cycle[node] = subgraph
for pre_node in self.digraph.predecessors(node):
if not subgraph.has_node(pre_node):
pre_edges.append((pre_node, node))
self.digraph.add_edge(pre_node, subgraph)
for suc_node in self.digraph.successors(node):
if not subgraph.has_node(suc_node):
suc_edges.append((node, suc_node))
self.digraph.add_edge(subgraph, suc_node)
self.digraph.remove_node(node)
assert subgraph not in self.cycles
self.cycles[subgraph] = (pre_edges, suc_edges)
cycle_order = lambda x: min(str(u) for u in x)
for index, cycle in enumerate(sorted(self.cycles, key=cycle_order)):
self.cycle2index[cycle] = index
# pylint: disable=invalid-name
示例5: filter_big_scc
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def filter_big_scc(g,edges_to_be_removed):
#Given a graph g and edges to be removed
#Return a list of big scc subgraphs (# of nodes >= 2)
g.remove_edges_from(edges_to_be_removed)
sub_graphs = filter(lambda scc: scc.number_of_nodes() >= 2, nx.strongly_connected_component_subgraphs(g))
return sub_graphs
示例6: get_big_sccs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def get_big_sccs(g):
self_loop_edges = g.selfloop_edges()
g.remove_edges_from(g.selfloop_edges())
num_big_sccs = 0
edges_to_be_removed = []
big_sccs = []
for sub in nx.strongly_connected_component_subgraphs(g):
number_of_nodes = sub.number_of_nodes()
if number_of_nodes >= 2:
# strongly connected components
num_big_sccs += 1
big_sccs.append(sub)
#print(" # big sccs: %d" % (num_big_sccs))
return big_sccs
示例7: scc_nodes_edges
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def scc_nodes_edges(g):
scc_nodes = set()
scc_edges = set()
num_big_sccs = 0
num_nodes_biggest_scc = 0
biggest_scc = None
for sub in nx.strongly_connected_component_subgraphs(g):
number_nodes = sub.number_of_nodes()
if number_nodes >= 2:
scc_nodes.update(sub.nodes())
scc_edges.update(sub.edges())
num_big_sccs += 1
if num_nodes_biggest_scc < number_nodes:
num_nodes_biggest_scc = number_nodes
biggest_scc = sub
nonscc_nodes = set(g.nodes()) - scc_nodes
nonscc_edges = set(g.edges()) - scc_edges
print num_nodes_biggest_scc
print("num of big sccs: %d" % num_big_sccs)
if biggest_scc == None:
return scc_nodes,scc_nodes,nonscc_nodes,nonscc_edges
print("# nodes in biggest scc: %d, # edges in biggest scc: %d" % (biggest_scc.number_of_nodes(),biggest_scc.number_of_edges()))
print("# nodes,edges in scc: (%d,%d), # nodes, edges in non-scc: (%d,%d) " % (len(scc_nodes),len(scc_edges),len(nonscc_nodes),len(nonscc_edges)))
num_of_nodes = g.number_of_nodes()
num_of_edges = g.number_of_edges()
print("# nodes in graph: %d, # of edges in graph: %d, percentage nodes, edges in scc: (%0.4f,%0.4f), percentage nodes, edges in non-scc: (%0.4f,%0.4f)" % (num_of_nodes,num_of_edges,len(scc_nodes)*1.0/num_of_nodes,len(scc_edges)*1.0/num_of_edges,len(nonscc_nodes)*1.0/num_of_nodes,len(nonscc_edges)*1.0/num_of_edges))
return scc_nodes,scc_edges,nonscc_nodes,nonscc_edges
示例8: test_strongly_connected_component_subgraphs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def test_strongly_connected_component_subgraphs(self):
scc = nx.strongly_connected_component_subgraphs
for G, C in self.gc:
assert_equal({frozenset(g) for g in scc(G)}, C)
示例9: test_connected_raise
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [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: test_connected_raise
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [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
示例11: strongly_connected_component_subgraphs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def strongly_connected_component_subgraphs(G, copy=True):
"""Generate strongly connected components as subgraphs.
Parameters
----------
G : NetworkX Graph
A directed graph.
copy : boolean, optional
if copy is True, Graph, node, and edge attributes are copied to
the subgraphs.
Returns
-------
comp : generator of graphs
A generator of graphs, one for each strongly connected component of G.
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(Gc) for Gc in sorted(nx.strongly_connected_component_subgraphs(G),
... key=len, reverse=True)]
[4, 3]
If you only want the largest component, it's more efficient to
use max instead of sort.
>>> Gc = max(nx.strongly_connected_component_subgraphs(G), key=len)
See Also
--------
connected_component_subgraphs
weakly_connected_component_subgraphs
"""
for comp in strongly_connected_components(G):
if copy:
yield G.subgraph(comp).copy()
else:
yield G.subgraph(comp)
示例12: strongly_connected_component_subgraphs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import strongly_connected_component_subgraphs [as 别名]
def strongly_connected_component_subgraphs(G, copy=True):
"""Generate strongly connected components as subgraphs.
Parameters
----------
G : NetworkX Graph
A directed graph.
copy : boolean, optional
if copy is True, Graph, node, and edge attributes are copied to
the subgraphs.
Returns
-------
comp : generator of graphs
A generator of graphs, 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(Gc) for Gc in sorted(nx.strongly_connected_component_subgraphs(G),
... key=len, reverse=True)]
[4, 3]
If you only want the largest component, it's more efficient to
use max instead of sort.
>>> Gc = max(nx.strongly_connected_component_subgraphs(G), key=len)
See Also
--------
strongly_connected_components
connected_component_subgraphs
weakly_connected_component_subgraphs
"""
for comp in strongly_connected_components(G):
if copy:
yield G.subgraph(comp).copy()
else:
yield G.subgraph(comp)