Python networkx.gnp_random_graph() Examples
The following are 30
code examples of networkx.gnp_random_graph().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: utils.py From graph-generation with MIT License | 6 votes |
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
Example #2
Source File: test_edge_kcomponents.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_random_gnp(): # seeds = [1550709854, 1309423156, 4208992358, 2785630813, 1915069929] seeds = [12, 13] for seed in seeds: G = nx.gnp_random_graph(20, 0.2, seed=seed) _check_edge_connectivity(G)
Example #3
Source File: test_qaoa.py From quantumflow with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_cover.py From dwave_networkx with Apache License 2.0 | 6 votes |
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)
Example #5
Source File: test_cover.py From dwave_networkx with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: utils.py From GraphRNN with MIT License | 6 votes |
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
Example #7
Source File: test_edge_kcomponents.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_random_gnp(): # seeds = [1550709854, 1309423156, 4208992358, 2785630813, 1915069929] seeds = [2785630813, 1915069929] for seed in seeds: G = nx.gnp_random_graph(20, 0.2, seed=seed) _check_edge_connectivity(G)
Example #8
Source File: test_connectivity.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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, seed=42) self.directed_gnp = nx.gnp_random_graph(30, 0.1, directed=True, seed=42) 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]
Example #9
Source File: test_sparsifiers.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_spanner_unweighted_gnp_graph(): """Test spanner construction on an unweighted gnp graph.""" G = nx.gnp_random_graph(20, 0.4, seed=_seed) spanner = nx.spanner(G, 4, seed=_seed) _test_spanner(G, spanner, 4) spanner = nx.spanner(G, 10, seed=_seed) _test_spanner(G, spanner, 10)
Example #10
Source File: test_sparsifiers.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_spanner_weighted_gnp_graph(): """Test spanner construction on an weighted gnp graph.""" G = nx.gnp_random_graph(20, 0.4, seed=_seed) _assign_random_weights(G, seed=_seed) spanner = nx.spanner(G, 4, weight='weight', seed=_seed) _test_spanner(G, spanner, 4, weight='weight') spanner = nx.spanner(G, 10, weight='weight', seed=_seed) _test_spanner(G, spanner, 10, weight='weight')
Example #11
Source File: test_dominating.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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))
Example #12
Source File: test_kcomponents.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_directed(): G = nx.gnp_random_graph(10, 0.4, directed=True) kc = k_components(G)
Example #13
Source File: test_connectivity.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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]
Example #14
Source File: test_katz_centrality.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_eigenvector_v_katz_random(self): G = nx.gnp_random_graph(10, 0.5, seed=1234) l = float(max(eigvals(nx.adjacency_matrix(G).todense()))) e = nx.eigenvector_centrality_numpy(G) k = nx.katz_centrality_numpy(G, 1.0 / l) for n in G: assert_almost_equal(e[n], k[n])
Example #15
Source File: test_kcutsets.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_random_gnp(): G = nx.gnp_random_graph(100, 0.1) _check_separating_sets(G)
Example #16
Source File: test_kcomponents.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_directed(): G = nx.gnp_random_graph(10, 0.2, directed=True, seed=42) nx.k_components(G) # Helper function
Example #17
Source File: test_edge_kcomponents.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_random_gnp_directed(): # seeds = [3894723670, 500186844, 267231174, 2181982262, 1116750056] seeds = [2181982262] for seed in seeds: G = nx.gnp_random_graph(20, 0.2, directed=True, seed=seed) _check_edge_connectivity(G)
Example #18
Source File: test_kcomponents.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_directed(): G = nx.gnp_random_graph(10, 0.2, directed=True) nx.k_components(G) # Helper function
Example #19
Source File: test_kcomponents.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_random_gnp(): G = nx.gnp_random_graph(50, 0.2) _check_connectivity(G)
Example #20
Source File: test_dominating.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
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))
Example #21
Source File: test_kcomponents.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_directed(): G = nx.gnp_random_graph(10, 0.4, directed=True) kc = k_components(G)
Example #22
Source File: test_kcomponents.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
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)]
Example #23
Source File: test_connectivity.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
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]
Example #24
Source File: test_katz_centrality.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_eigenvector_v_katz_random(self): G = nx.gnp_random_graph(10, 0.5, seed=1234) l = float(max(eigvals(nx.adjacency_matrix(G).todense()))) e = nx.eigenvector_centrality_numpy(G) k = nx.katz_centrality_numpy(G, 1.0/l) for n in G: assert_almost_equal(e[n], k[n])
Example #25
Source File: test_dominating.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
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))
Example #26
Source File: test_matching.py From dwave_networkx with Apache License 2.0 | 5 votes |
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))
Example #27
Source File: test_matching.py From dwave_networkx with Apache License 2.0 | 5 votes |
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()))
Example #28
Source File: test_compartmentedmodel.py From epydemic with GNU General Public License v3.0 | 5 votes |
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)
Example #29
Source File: test_compartmentedmodel.py From epydemic with GNU General Public License v3.0 | 5 votes |
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)
Example #30
Source File: test_kcutsets.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_random_gnp(): G = nx.gnp_random_graph(100, 0.1) _check_separating_sets(G)