本文整理汇总了Python中networkx.condensation函数的典型用法代码示例。如果您正苦于以下问题:Python condensation函数的具体用法?Python condensation怎么用?Python condensation使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了condensation函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: strongly_connected_components
def strongly_connected_components():
conn = sqlite3.connect("zhihu.db")
#following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 50000) and user_url in (select user_url from User where agree_num > 50000)', conn)
following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 10000) and user_url in (select user_url from User where agree_num > 10000)', conn)
conn.close()
G = nx.DiGraph()
cnt = 0
for d in following_data.iterrows():
G.add_edge(d[1][0],d[1][1])
cnt += 1
print 'links number:', cnt
scompgraphs = nx.strongly_connected_component_subgraphs(G)
scomponents = sorted(nx.strongly_connected_components(G), key=len, reverse=True)
print 'components nodes distribution:', [len(c) for c in scomponents]
#plot graph of component, calculate saverage_shortest_path_length of components who has over 1 nodes
index = 0
print 'average_shortest_path_length of components who has over 1 nodes:'
for tempg in scompgraphs:
index += 1
if len(tempg.nodes()) != 1:
print nx.average_shortest_path_length(tempg)
print 'diameter', nx.diameter(tempg)
print 'radius', nx.radius(tempg)
pylab.figure(index)
nx.draw_networkx(tempg)
pylab.show()
# Components-as-nodes Graph
cG = nx.condensation(G)
pylab.figure('Components-as-nodes Graph')
nx.draw_networkx(cG)
pylab.show()
示例2: attracting_components
def attracting_components(G):
"""Returns 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 : list
The list of attracting components, sorted from largest attracting
component to smallest attracting component.
See Also
--------
number_attracting_components
is_attracting_component
attracting_component_subgraphs
"""
scc = nx.strongly_connected_components(G)
cG = nx.condensation(G, scc)
attractors = [scc[n] for n in cG if cG.out_degree(n) == 0]
attractors.sort(key=len,reverse=True)
return attractors
示例3: get_graph_compressed
def get_graph_compressed(graph_data):
"""
Getting the Compressed Graph. A Compressed Graph is a DAG, after removing
unreachable graph nodes, and getting bfs tree.
"""
# Creating the directed graphs, for graph1.
dgraph = nx.DiGraph(graph_data)
if not dgraph.has_node(0): # adding root node, on one node case.
dgraph.add_node(0)
# First, remove non reachable nodes, from the root.
# assuming node 0 is the function root node.
bfsy = nx.bfs_tree(dgraph, 0).nodes()
if 0 not in bfsy:
bfsy.append(0)
for i in dgraph.nodes():
if i not in bfsy:
dgraph.remove_node(i)
# Second, _collapse some vertices together...
dgraph = _collapse(dgraph)
# create DAG's (computing scc) from digraph before.
compressed_graph = nx.condensation(dgraph)
return compressed_graph.edges()
示例4: test_contract_scc2
def test_contract_scc2(self):
# Bug found and fixed in [1687].
G = nx.DiGraph()
G.add_edge(1,2)
G.add_edge(2,1)
cG = nx.condensation(G)
assert_true((1,2) in cG)
示例5: test_null_graph
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())
示例6: attracting_components
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]
示例7: _create_spatial_index
def _create_spatial_index(self):
G = nx.condensation(self.G) # convert to DAG
index = dict()
# Add 1 hop reachability for a DAG
for v in G.nodes(): # for each component
for w in G.node[v]['members']: # for each node in the component
if 'spatial' in self.G.node[w]: # is 'w' a spatial node?
index.upsert(v, {self.region(w)}) # if yes, add block # of 'w' to v's meta
# Add multi hop reachability
self._do_dfs(G, index)
# Update reachability information to each node in the component
for v in G.nodes(): # for each component
for w in G.node[v]['members']: # for each node in the component
try:
self.spatial_index[w] = index[v] # set component's index entry to each vertex in it
except KeyError:
pass # ignore if index entry is not found as they don't have any spatial connections
# free up space
G = None
index = None
gc.collect(0)
# Create region based index
c = count()
for v in self.G.nodes():
if 'spatial' in self.G.node[v]:
x = self.G.node[v]['spatial']['lng']
y = self.G.node[v]['spatial']['lat']
self.region_index.insert(next(c), (x, y, x, y), v)
示例8: condensation_plus
def condensation_plus(G):
C = nx.condensation(G)
maps = {}
maps = C.graph['mapping']
num_of_c = C.number_of_nodes();
nbunch = {}
for num in range(0,num_of_c):
nbunch[num] = []
#search through and divide G.nodes() into groups
for num in range(0,num_of_c):
for name in G.nodes():
if maps[name]==num:
nbunch[num].append(name)
G_sub = {};
for i in range(0, num_of_c):
G_sub[i] = G.subgraph(nbunch[i])
result = [];
result.append(C);
result.append(G_sub);
return result
示例9: test_contract_scc_isolate
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()), [])
示例10: test_condensation_mapping_and_members
def test_condensation_mapping_and_members(self):
G, C = self.gc[1]
cG = nx.condensation(G)
mapping = cG.graph['mapping']
assert_true(all(n in G for n in mapping))
assert_true(all(0 == cN for n, cN in mapping.items() if n in C[0]))
assert_true(all(1 == cN for n, cN in mapping.items() if n in C[1]))
for n, d in cG.nodes(data=True):
assert_equal(C[n], cG.node[n]['members'])
示例11: test_condensation_mapping_and_members
def test_condensation_mapping_and_members(self):
G, C = self.gc[1]
C = sorted(C, key=len, reverse=True)
cG = nx.condensation(G)
mapping = cG.graph["mapping"]
assert_true(all(n in G for n in mapping))
assert_true(all(0 == cN for n, cN in mapping.items() if n in C[0]))
assert_true(all(1 == cN for n, cN in mapping.items() if n in C[1]))
for n, d in cG.nodes(data=True):
assert_equal(set(C[n]), cG.node[n]["members"])
示例12: condensation
def condensation(self):
if DEBUG_FLAG:
print "self.dep_graph.number_of_nodes()", self.dep_graph.number_of_nodes()
c0 = nx.condensation(self.dep_graph)
for cnode in c0.nodes():
stratum_atoms = c0.node[cnode]['members']
rules = []
for atom in stratum_atoms:
for rule in self.head2rules[atom]:
rules.append(rule)
c0.node[cnode]['stratum'] = Stratum(stratum_atoms, rules)
return c0
示例13: condensation_nx
def condensation_nx( G, components) :
"""
G : DiGraph object. the nx.DiGraph attribute is extracted from this.
components : Given components C_1 .. C_k which partition the nodes of G, the
condensation graph cG has nodes <C_1> .. <C_k> and has edge
(<C_i>,<C_j>) iff there was an edge in G from a node in C_i to a
node in C_j.
"""
cG = DiGraph()
cG.graph = nx.condensation( G.graph, components )
return cG
示例14: is_semiconnected
def is_semiconnected(G):
"""Return True if the graph is semiconnected, False otherwise.
A graph is semiconnected if, and only if, for any pair of nodes, either one
is reachable from the other, or they are mutually reachable.
Parameters
----------
G : NetworkX graph
A directed graph.
Returns
-------
semiconnected : bool
True if the graph is semiconnected, False otherwise.
Raises
------
NetworkXNotImplemented :
If the input graph is undirected.
NetworkXPointlessConcept :
If the graph is empty.
Examples
--------
>>> G=nx.path_graph(4,create_using=nx.DiGraph())
>>> print(nx.is_semiconnected(G))
True
>>> G=nx.DiGraph([(1, 2), (3, 2)])
>>> print(nx.is_semiconnected(G))
False
See Also
--------
is_strongly_connected
is_weakly_connected
is_connected
is_biconnected
"""
if len(G) == 0:
raise nx.NetworkXPointlessConcept(
'Connectivity is undefined for the null graph.')
if not nx.is_weakly_connected(G):
return False
G = nx.condensation(G)
path = nx.topological_sort(G)
return all(G.has_edge(u, v) for u, v in pairwise(path))
示例15: test_contract_scc_edge
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])