本文整理汇总了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
示例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)
示例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)
示例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)
示例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
示例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))
示例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()))
示例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)
示例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)
示例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)
示例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
示例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)
示例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]
示例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))
示例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)]