当前位置: 首页>>代码示例>>Python>>正文


Python networkx.number_connected_components方法代码示例

本文整理汇总了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) 
开发者ID:jsexauer,项目名称:networkx_viewer,代码行数:18,代码来源:tests.py

示例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)) 
开发者ID:Dru-Mara,项目名称:EvalNE,代码行数:26,代码来源:split_train_test.py

示例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 
开发者ID:Hanjun-Dai,项目名称:graph_adversarial_attack,代码行数:21,代码来源:genetic_algorithm.py

示例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 
开发者ID:holzschu,项目名称:Carnets,代码行数:23,代码来源:centrality.py

示例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 
开发者ID:mims-harvard,项目名称:ohmnet,代码行数:19,代码来源:utility.py

示例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) 
开发者ID:jsexauer,项目名称:networkx_viewer,代码行数:15,代码来源:tests.py

示例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) 
开发者ID:jsexauer,项目名称:networkx_viewer,代码行数:15,代码来源:tests.py

示例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) 
开发者ID:jsexauer,项目名称:networkx_viewer,代码行数:15,代码来源:tests.py

示例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) 
开发者ID:jsexauer,项目名称:networkx_viewer,代码行数:15,代码来源:tests.py

示例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) 
开发者ID:AdrienGuille,项目名称:GrowingNeuralGas,代码行数:4,代码来源:gng.py

示例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') 
开发者ID:AdrienGuille,项目名称:GrowingNeuralGas,代码行数:14,代码来源:gng.py

示例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())) 
开发者ID:AdrienGuille,项目名称:GrowingNeuralGas,代码行数:16,代码来源:evaluate.py

示例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) 
开发者ID:Hanjun-Dai,项目名称:graph_adversarial_attack,代码行数:41,代码来源:rl_common.py

示例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) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:5,代码来源:test_connected.py

示例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) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:5,代码来源:test_connected.py


注:本文中的networkx.number_connected_components方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。