本文整理匯總了Python中networkx.number_connected_components方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.number_connected_components方法的具體用法?Python networkx.number_connected_components怎麽用?Python networkx.number_connected_components使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.number_connected_components方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_plot_three_connected
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def test_plot_three_connected(self):
self.a.plot(['a','c','qqqq'])
self.check_subgraph()
# No islands
ni = nx.number_connected_components(self.a.dispG)
self.assertEqual(ni, 1)
# Make sure the path that conects the a,c group to qqqq
# is displayed (ie, node 11 and TTTT are in the graph
node_11 = self.a._find_disp_node(11)
TTTTT = self.a._find_disp_node('TTTTT')
self.assertIsNot(node_11, None)
self.assertIsNot(TTTTT, None)
self.check_num_nodes_edges(9, 11)
示例2: _sanity_check
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def _sanity_check(G):
r"""
Helper function that checks if the input graphs contains a single connected component. Raises an error if not.
Parameters
----------
G : graph
A NetworkX graph
Raises
------
ValueError
If the graph has more than one (weakly) connected component.
"""
# Compute the number of connected components
if G.is_directed():
num_ccs = nx.number_weakly_connected_components(G)
else:
num_ccs = nx.number_connected_components(G)
# Rise an error if more than one CC exists
if num_ccs != 1:
raise ValueError("Input graph should contain one (weakly) connected component. "
"This graph contains: " + str(num_ccs))
示例3: get_fitness
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def get_fitness(self):
g_list = []
g = self.s2v_g.to_networkx()
for edges in self.population:
g2 = g.copy()
g2.add_edge(edges[0][0], edges[0][1])
# g2.add_edges_from(edges)
assert nx.number_connected_components(g2) == self.s2v_g.label
g_list.append(S2VGraph(g2, self.s2v_g.label))
log_ll, _, acc = self.classifier(g_list)
acc = acc.cpu().double().numpy()
if self.solution is None:
for i in range(len(self.population)):
if acc[i] < 1.0:
self.solution = self.population[i]
break
nll = -log_ll[:, self.classifier.label_map[self.s2v_g.label]]
return nll
示例4: _without_most_central_edges
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def _without_most_central_edges(G, most_valuable_edge):
"""Returns the connected components of the graph that results from
repeatedly removing the most "valuable" edge in the graph.
`G` must be a non-empty graph. This function modifies the graph `G`
in-place; that is, it removes edges on the graph `G`.
`most_valuable_edge` is a function that takes the graph `G` as input
(or a subgraph with one or more edges of `G` removed) and returns an
edge. That edge will be removed and this process will be repeated
until the number of connected components in the graph increases.
"""
original_num_components = nx.number_connected_components(G)
num_new_components = original_num_components
while num_new_components <= original_num_components:
edge = most_valuable_edge(G)
G.remove_edge(*edge)
new_components = tuple(nx.connected_components(G))
num_new_components = len(new_components)
return new_components
示例5: read_net
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def read_net(fname, weighted, directed, log):
if weighted:
G = nx.read_edgelist(inodetype=int, data=(('weight', float),),
create_using=nx.DiGraph())
else:
G = nx.read_edgelist(fname, nodetype=int, create_using=nx.DiGraph())
for edge in G.edges():
G[edge[0]][edge[1]]['weight'] = 1
if not directed:
G = G.to_undirected()
log.info('N: %d E: %d' % (G.number_of_nodes(), G.number_of_edges()))
log.info('CC: %d' % nx.number_connected_components(G))
giant = max(nx.connected_component_subgraphs(G), key=len)
log.info('N: %d E: %d' % (giant.number_of_nodes(), giant.number_of_edges()))
return giant
示例6: test_plot_three_no_connection
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def test_plot_three_no_connection(self):
self.a.plot(['a','qqqq','alone'])
# There should be two islands (a-11-TTTTT-qqqq and alone)
ni = nx.number_connected_components(self.a.dispG)
self.assertEqual(ni, 2)
# a and qqqq should be connected via 11 and TTTTT
self.check_num_nodes_edges(9, 9)
node_11 = self.a._find_disp_node(11)
TTTTT = self.a._find_disp_node('TTTTT')
self.assertIsNot(node_11, None)
self.assertIsNot(TTTTT, None)
示例7: test_add_to_plot_with_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def test_add_to_plot_with_path(self):
# Test adding nodes around qqqq to a display showing nodes around a
self.display_a()
with patch(ASKYESNO_FUNC) as prompt:
prompt.return_value = True # Yes, we want to include path
self.a.plot_additional(set(['qqqq']), levels=1)
self.assertTrue(prompt.called)
self.check_subgraph()
self.check_num_nodes_edges(9, 11)
# All connected together
self.assertEqual(nx.number_connected_components(self.a.dispG), 1)
示例8: test_add_to_plot_without_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def test_add_to_plot_without_path(self):
# Test adding nodes around qqqq to a display but as an island
self.display_a()
with patch(ASKYESNO_FUNC) as prompt:
prompt.return_value = False # No, we don't want to include path
self.a.plot_additional(set(['qqqq']), levels=1)
self.assertTrue(prompt.called)
self.check_subgraph()
self.check_num_nodes_edges(8, 9)
# There are two islands
self.assertEqual(nx.number_connected_components(self.a.dispG), 2)
示例9: test_add_to_plot_without_path2
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def test_add_to_plot_without_path2(self):
# Test adding nodes around qqqq but because our levels is so big, we
# should just connect to the existing graph (no prompt)
self.display_a()
with patch(ASKYESNO_FUNC) as prompt:
self.a.plot_additional(set(['qqqq']), levels=2)
self.assertFalse(prompt.called) # We should not prompt
self.check_subgraph()
self.check_num_nodes_edges(9, 11)
# All connected together
self.assertEqual(nx.number_connected_components(self.a.dispG), 1)
示例10: number_of_clusters
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def number_of_clusters(self):
return nx.number_connected_components(self.network)
示例11: plot_clusters
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def plot_clusters(self, clustered_data):
number_of_clusters = nx.number_connected_components(self.network)
plt.clf()
plt.title('Cluster affectation')
color = ['r', 'b', 'g', 'k', 'm', 'r', 'b', 'g', 'k', 'm']
for i in range(number_of_clusters):
observations = [observation for observation, s in clustered_data if s == i]
if len(observations) > 0:
observations = np.array(observations)
plt.scatter(observations[:, 0], observations[:, 1], color=color[i], label='cluster #'+str(i))
plt.legend()
plt.savefig('visualization/clusters.png')
示例12: evaluate_on_digits
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def evaluate_on_digits():
digits = datasets.load_digits()
data = digits.data
target = digits.target
gng = GrowingNeuralGas(data)
gng.fit_network(e_b=0.05, e_n=0.006, a_max=8, l=100, a=0.5, d=0.995, passes=5, plot_evolution=False)
clustered_data = gng.cluster_data()
print('Found %d clusters.' % nx.number_connected_components(gng.network))
target_infered = []
for observation, cluster in clustered_data:
target_infered.append(cluster)
homogeneity = metrics.homogeneity_score(target, target_infered)
print(homogeneity)
gng.plot_clusters(gng.reduce_dimension(gng.cluster_data()))
示例13: attackable
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def attackable(classifier, s2v_g, x = None, y = None):
g = s2v_g.to_networkx()
comps = [c for c in nx.connected_component_subgraphs(g)]
set_id = {}
for i in range(len(comps)):
for j in comps[i].nodes():
set_id[j] = i
if x is not None:
r_i = [x]
else:
r_i = range(len(g) - 1)
g_list = []
for i in r_i:
if y is not None:
assert x is not None
r_j = [y]
else:
if x is not None:
r_j = range(len(g) - 1)
else:
r_j = range(i + 1, len(g))
for j in r_j:
if set_id[i] != set_id[j]:
continue
g2 = g.copy()
g2.add_edge(i, j)
assert nx.number_connected_components(g2) == s2v_g.label
g_list.append(S2VGraph(g2, s2v_g.label))
if len(g_list) == 0:
print(x, y)
print(g.edges(), s2v_g.label)
print(set_id)
assert len(g_list)
_, _, acc = classifier(g_list)
return np.sum(acc.view(-1).numpy()) < len(g_list)
示例14: test_number_connected_components
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def test_number_connected_components(self):
ncc = nx.number_connected_components
assert_equal(ncc(self.G), 3)
示例15: test_number_connected_components2
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import number_connected_components [as 別名]
def test_number_connected_components2(self):
ncc = nx.number_connected_components
assert_equal(ncc(self.grid), 1)