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


Python networkx.gnp_random_graph方法代码示例

本文整理汇总了Python中networkx.gnp_random_graph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.gnp_random_graph方法的具体用法?Python networkx.gnp_random_graph怎么用?Python networkx.gnp_random_graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.gnp_random_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: n_community

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def n_community(c_sizes, p_inter=0.01):
    graphs = [nx.gnp_random_graph(c_sizes[i], 0.7, seed=i) for i in range(len(c_sizes))]
    G = nx.disjoint_union_all(graphs)
    communities = list(nx.connected_component_subgraphs(G))
    for i in range(len(communities)):
        subG1 = communities[i]
        nodes1 = list(subG1.nodes())
        for j in range(i+1, len(communities)):
            subG2 = communities[j]
            nodes2 = list(subG2.nodes())
            has_inter_edge = False
            for n1 in nodes1:
                for n2 in nodes2:
                    if np.random.rand() < p_inter:
                        G.add_edge(n1, n2)
                        has_inter_edge = True
            if not has_inter_edge:
                G.add_edge(nodes1[0], nodes2[0])
    #print('connected comp: ', len(list(nx.connected_component_subgraphs(G))))
    return G 
开发者ID:JiaxuanYou,项目名称:graph-generation,代码行数:22,代码来源:utils.py

示例2: test_vertex_cover_basic

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_vertex_cover_basic(self):
        """Runs the function on some small and simple graphs, just to make
        sure it works in basic functionality.
        """
        G = dnx.chimera_graph(1, 2, 2)
        cover = dnx.min_vertex_cover(G, ExactSolver())
        self.vertex_cover_check(G, cover)

        G = nx.path_graph(5)
        cover = dnx.min_vertex_cover(G, ExactSolver())
        self.vertex_cover_check(G, cover)

        for __ in range(10):
            G = nx.gnp_random_graph(5, .5)
            cover = dnx.min_vertex_cover(G, ExactSolver())
            self.vertex_cover_check(G, cover) 
开发者ID:dwavesystems,项目名称:dwave_networkx,代码行数:18,代码来源:test_cover.py

示例3: test_vertex_cover_weighted

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_vertex_cover_weighted(self):
        weight = 'weight'
        G = nx.path_graph(6)

        # favor even nodes
        nx.set_node_attributes(G, {node: node % 2 + 1 for node in G}, weight)
        cover = dnx.min_weighted_vertex_cover(G, weight, ExactSolver())
        self.assertEqual(set(cover), {0, 2, 4})

        # favor odd nodes
        nx.set_node_attributes(G, {node: (node + 1) % 2 + 1 for node in G}, weight)
        cover = dnx.min_weighted_vertex_cover(G, weight, ExactSolver())
        self.assertEqual(set(cover), {1, 3, 5})

        # make nodes 1 and 4 unlikely
        nx.set_node_attributes(G, {0: 1, 1: 3, 2: 1, 3: 1, 4: 3, 5: 1}, weight)
        cover = dnx.min_weighted_vertex_cover(G, weight, ExactSolver())
        self.assertEqual(set(cover), {0, 2, 3, 5})

        for __ in range(10):
            G = nx.gnp_random_graph(5, .5)
            nx.set_node_attributes(G, {node: random.random() for node in G}, weight)
            cover = dnx.min_weighted_vertex_cover(G, weight, ExactSolver())
            self.vertex_cover_check(G, cover) 
开发者ID:dwavesystems,项目名称:dwave_networkx,代码行数:26,代码来源:test_cover.py

示例4: test_qubo_circuit

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_qubo_circuit():

    # Random graph
    graph = nx.gnp_random_graph(4, 0.5)
    circ = qubo_circuit(graph, 4, [10, 11, 12, 13], [20, 21, 22, 23])
    # print(circ)

    # Circuit with edge weights
    graph = nx.Graph()
    graph.add_edge(0, 1, weight=0.1)
    graph.add_edge(1, 2, weight=0.4)

    circ = qubo_circuit(graph, 2, [1, 1], [2, 2])
    assert len(circ.elements) == 13
    # print(circ)

    # Add node weights
    graph.nodes[0]['weight'] = 4
    circ = qubo_circuit(graph, 2, [1, 1], [2, 2])
    assert len(circ.elements) == 15
    print(circ) 
开发者ID:rigetti,项目名称:quantumflow,代码行数:23,代码来源:test_qaoa.py

示例5: generate_random_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def generate_random_graph(variables_count, p_edge, allow_subgraph):
    if not allow_subgraph:
        graph = nx.gnp_random_graph(variables_count, p_edge)
        is_connected = nx.is_connected(graph)
        while not is_connected:
            graph = nx.gnp_random_graph(variables_count, p_edge)
            is_connected = nx.is_connected(graph)
    else:
        graph = nx.gnp_random_graph(variables_count, p_edge)
    return graph 
开发者ID:Orange-OpenSource,项目名称:pyDcop,代码行数:12,代码来源:graphcoloring.py

示例6: test_maximal_matching_typical

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_maximal_matching_typical(self):

        G = nx.complete_graph(5)
        matching = dnx.algorithms.matching.maximal_matching(G, ExactSolver())
        self.assertTrue(dnx.is_maximal_matching(G, matching))

        for __ in range(10):
            G = nx.gnp_random_graph(7, .5)
            matching = dnx.algorithms.matching.maximal_matching(G, ExactSolver())
            self.assertTrue(dnx.is_maximal_matching(G, matching)) 
开发者ID:dwavesystems,项目名称:dwave_networkx,代码行数:12,代码来源:test_matching.py

示例7: test_min_maximal_matching_typical

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_min_maximal_matching_typical(self):

        G = nx.complete_graph(5)
        matching = dnx.min_maximal_matching(G, ExactSolver())
        self.assertTrue(dnx.is_maximal_matching(G, matching))

        for __ in range(10):
            G = nx.gnp_random_graph(7, .5)
            matching = dnx.min_maximal_matching(G, ExactSolver())
            self.assertTrue(dnx.is_maximal_matching(G, matching),
                            "nodes: {}\nedges:{}".format(G.nodes(), G.edges())) 
开发者ID:dwavesystems,项目名称:dwave_networkx,代码行数:13,代码来源:test_matching.py

示例8: testInitialPopulation

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def testInitialPopulation(self):
        '''Test that initial seeding of compartments works properly.'''
        self._er = networkx.gnp_random_graph(10000, 0.001)   # larger network to reduce variance
        m = SIR()
        e = StochasticDynamics(m)
        m.reset()
        m.setNetwork(self._er)
        m.build(self._params)
        m.setUp(self._params)
        self.assertAlmostEqual(len(m.compartment(SIR.INFECTED)) / self._er.order(), self._params[SIR.P_INFECTED], places=2)
        self.assertAlmostEqual(len(m.compartment(SIR.SUSCEPTIBLE)) / self._er.order(), (1.0 - self._params[SIR.P_INFECTED]), places=2) 
开发者ID:simoninireland,项目名称:epydemic,代码行数:13,代码来源:test_compartmentedmodel.py

示例9: testChangeInitialPopulation

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def testChangeInitialPopulation(self):
        '''Test that changing the initial seeding works.'''
        self._er = networkx.gnp_random_graph(10000, 0.001)   # larger network to reduce variance
        m = SIR()
        e = StochasticDynamics(m)
        m.reset()
        m.setNetwork(self._er)
        m.build(self._params)
        pInfected = 0.1       # new infection seed
        m.changeCompartmentInitialOccupancy(SIR.INFECTED, pInfected)
        m.changeCompartmentInitialOccupancy(SIR.SUSCEPTIBLE, 1.0 - pInfected)
        m.setUp(self._params)
        self.assertAlmostEqual(len(m.compartment(SIR.INFECTED)) / self._er.order(), pInfected, places=2)
        self.assertAlmostEqual(len(m.compartment(SIR.SUSCEPTIBLE)) / self._er.order(), (1.0 - pInfected), places=2) 
开发者ID:simoninireland,项目名称:epydemic,代码行数:16,代码来源:test_compartmentedmodel.py

示例10: test_random_gnp

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_random_gnp():
    G = nx.gnp_random_graph(100, 0.1)
    _check_separating_sets(G) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:5,代码来源:test_kcutsets.py

示例11: test_directed

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_directed():
    G = nx.gnp_random_graph(10, 0.2, directed=True)
    nx.k_components(G)

# Helper function 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:7,代码来源:test_kcomponents.py

示例12: test_random_gnp

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_random_gnp():
    G = nx.gnp_random_graph(50, 0.2)
    _check_connectivity(G) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:5,代码来源:test_kcomponents.py

示例13: setUp

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def setUp(self):
        self.path = nx.path_graph(7)
        self.directed_path = nx.path_graph(7, create_using=nx.DiGraph())
        self.cycle = nx.cycle_graph(7)
        self.directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph())
        self.gnp = nx.gnp_random_graph(30, 0.1)
        self.directed_gnp = nx.gnp_random_graph(30, 0.1, directed=True)
        self.K20 = nx.complete_graph(20)
        self.K10 = nx.complete_graph(10)
        self.K5 = nx.complete_graph(5)
        self.G_list = [self.path, self.directed_path, self.cycle,
            self.directed_cycle, self.gnp, self.directed_gnp, self.K10,
            self.K5, self.K20] 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:15,代码来源:test_connectivity.py

示例14: test_dominating_set

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def test_dominating_set():
    G = nx.gnp_random_graph(100, 0.1)
    D = nx.dominating_set(G)
    assert_true(nx.is_dominating_set(G, D))
    D = nx.dominating_set(G, start_with=0)
    assert_true(nx.is_dominating_set(G, D)) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:8,代码来源:test_dominating.py

示例15: setUp

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import gnp_random_graph [as 别名]
def setUp(self):
        self.Gnp = nx.gnp_random_graph(20,0.8)
        self.Anp = _AntiGraph(nx.complement(self.Gnp))
        self.Gd = nx.davis_southern_women_graph()
        self.Ad = _AntiGraph(nx.complement(self.Gd))
        self.Gk = nx.karate_club_graph()
        self.Ak = _AntiGraph(nx.complement(self.Gk))
        self.GA = [(self.Gnp, self.Anp),
                    (self.Gd,self.Ad),
                    (self.Gk, self.Ak)] 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:12,代码来源:test_kcomponents.py


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