Python networkx.caveman_graph() Examples
The following are 15
code examples of networkx.caveman_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: molecule.py From rl_graph_generation with BSD 3-Clause "New" or "Revised" License | 6 votes |
def caveman_special(c=2,k=20,p_path=0.1,p_edge=0.3): p = p_path path_count = max(int(np.ceil(p * k)),1) G = nx.caveman_graph(c, k) # remove 50% edges p = 1-p_edge for (u, v) in list(G.edges()): if np.random.rand() < p and ((u < k and v < k) or (u >= k and v >= k)): G.remove_edge(u, v) # add path_count links for i in range(path_count): u = np.random.randint(0, k) v = np.random.randint(k, k * 2) G.add_edge(u, v) G = max(nx.connected_component_subgraphs(G), key=len) return G
Example #2
Source File: utils.py From graph-generation with MIT License | 6 votes |
def caveman_special(c=2,k=20,p_path=0.1,p_edge=0.3): p = p_path path_count = max(int(np.ceil(p * k)),1) G = nx.caveman_graph(c, k) # remove 50% edges p = 1-p_edge for (u, v) in list(G.edges()): if np.random.rand() < p and ((u < k and v < k) or (u >= k and v >= k)): G.remove_edge(u, v) # add path_count links for i in range(path_count): u = np.random.randint(0, k) v = np.random.randint(k, k * 2) G.add_edge(u, v) G = max(nx.connected_component_subgraphs(G), key=len) return G
Example #3
Source File: utils.py From GraphRNN with MIT License | 6 votes |
def caveman_special(c=2,k=20,p_path=0.1,p_edge=0.3): p = p_path path_count = max(int(np.ceil(p * k)),1) G = nx.caveman_graph(c, k) # remove 50% edges p = 1-p_edge for (u, v) in list(G.edges()): if np.random.rand() < p and ((u < k and v < k) or (u >= k and v >= k)): G.remove_edge(u, v) # add path_count links for i in range(path_count): u = np.random.randint(0, k) v = np.random.randint(k, k * 2) G.add_edge(u, v) G = max(nx.connected_component_subgraphs(G), key=len) return G
Example #4
Source File: test_community.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_caveman_graph(): G = nx.caveman_graph(4,3) assert_equal(len(G),12) assert_equal(len(G.nodes()),12) G = nx.caveman_graph(1,5) K5 = nx.complete_graph(5) assert_true(nx.is_isomorphic(G,K5))
Example #5
Source File: community.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def connected_caveman_graph(l, k): """Returns a connected caveman graph of `l` cliques of size `k`. The connected caveman graph is formed by creating `n` cliques of size `k`, then a single edge in each clique is rewired to a node in an adjacent clique. Parameters ---------- l : int number of cliques k : int size of cliques Returns ------- G : NetworkX Graph connected caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.connected_caveman_graph(3, 3) References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. """ G = nx.caveman_graph(l, k) for start in range(0, l * k, k): G.remove_edge(start, start + 1) G.add_edge(start, (start - 1) % (l * k)) return G
Example #6
Source File: test_community.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_caveman_graph(): G = nx.caveman_graph(4, 3) assert_equal(len(G), 12) G = nx.caveman_graph(1, 5) K5 = nx.complete_graph(5) assert_true(nx.is_isomorphic(G, K5))
Example #7
Source File: community.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def connected_caveman_graph(l, k): """Returns a connected caveman graph of `l` cliques of size `k`. The connected caveman graph is formed by creating `n` cliques of size `k`, then a single edge in each clique is rewired to a node in an adjacent clique. Parameters ---------- l : int number of cliques k : int size of cliques Returns ------- G : NetworkX Graph connected caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.connected_caveman_graph(3, 3) References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. """ G = nx.caveman_graph(l, k) for start in range(0, l * k, k): G.remove_edge(start, start + 1) G.add_edge(start, (start - 1) % (l * k)) return G
Example #8
Source File: test_community.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_caveman_graph(): G = nx.caveman_graph(4, 3) assert_equal(len(G), 12) G = nx.caveman_graph(1, 5) K5 = nx.complete_graph(5) assert_true(nx.is_isomorphic(G, K5))
Example #9
Source File: community.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def caveman_graph(l, k): """Returns a caveman graph of ``l`` cliques of size ``k``. Parameters ---------- l : int Number of cliques k : int Size of cliques Returns ------- G : NetworkX Graph caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.caveman_graph(3, 3) See also -------- connected_caveman_graph References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. """ # l disjoint cliques of size k G = nx.empty_graph(l*k) G.name = "caveman_graph(%s,%s)" % (l*k, k) if k > 1: for start in range(0, l*k, k): edges = itertools.combinations(range(start, start+k), 2) G.add_edges_from(edges) return G
Example #10
Source File: community.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def connected_caveman_graph(l, k): """Returns a connected caveman graph of ``l`` cliques of size ``k``. The connected caveman graph is formed by creating ``n`` cliques of size ``k``, then a single edge in each clique is rewired to a node in an adjacent clique. Parameters ---------- l : int number of cliques k : int size of cliques Returns ------- G : NetworkX Graph connected caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.connected_caveman_graph(3, 3) References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. """ G = nx.caveman_graph(l, k) G.name = "connected_caveman_graph(%s,%s)" % (l, k) for start in range(0, l*k, k): G.remove_edge(start, start+1) G.add_edge(start, (start-1) % (l*k)) return G
Example #11
Source File: community.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def relaxed_caveman_graph(l, k, p, seed=None): """Return a relaxed caveman graph. A relaxed caveman graph starts with ``l`` cliques of size ``k``. Edges are then randomly rewired with probability ``p`` to link different cliques. Parameters ---------- l : int Number of groups k : int Size of cliques p : float Probabilty of rewiring each edge. seed : int,optional Seed for random number generator(default=None) Returns ------- G : NetworkX Graph Relaxed Caveman Graph Raises ------ NetworkXError: If p is not in [0,1] Examples -------- >>> G = nx.relaxed_caveman_graph(2, 3, 0.1, seed=42) References ---------- .. [1] Santo Fortunato, Community Detection in Graphs, Physics Reports Volume 486, Issues 3-5, February 2010, Pages 75-174. http://arxiv.org/abs/0906.0612 """ if not seed is None: random.seed(seed) G = nx.caveman_graph(l, k) nodes = G.nodes() G.name = "relaxed_caveman_graph (%s,%s,%s)" % (l, k, p) for (u, v) in G.edges(): if random.random() < p: # rewire the edge x = random.choice(nodes) if G.has_edge(u, x): continue G.remove_edge(u, v) G.add_edge(u, x) return G
Example #12
Source File: community.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def caveman_graph(l, k): """Returns a caveman graph of `l` cliques of size `k`. Parameters ---------- l : int Number of cliques k : int Size of cliques Returns ------- G : NetworkX Graph caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.caveman_graph(3, 3) See also -------- connected_caveman_graph References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. """ # l disjoint cliques of size k G = nx.empty_graph(l * k) if k > 1: for start in range(0, l * k, k): edges = itertools.combinations(range(start, start + k), 2) G.add_edges_from(edges) return G
Example #13
Source File: community.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def relaxed_caveman_graph(l, k, p, seed=None): """Returns a relaxed caveman graph. A relaxed caveman graph starts with `l` cliques of size `k`. Edges are then randomly rewired with probability `p` to link different cliques. Parameters ---------- l : int Number of groups k : int Size of cliques p : float Probabilty of rewiring each edge. seed : integer, random_state, or None (default) Indicator of random number generation state. See :ref:`Randomness<randomness>`. Returns ------- G : NetworkX Graph Relaxed Caveman Graph Raises ------ NetworkXError: If p is not in [0,1] Examples -------- >>> G = nx.relaxed_caveman_graph(2, 3, 0.1, seed=42) References ---------- .. [1] Santo Fortunato, Community Detection in Graphs, Physics Reports Volume 486, Issues 3-5, February 2010, Pages 75-174. https://arxiv.org/abs/0906.0612 """ G = nx.caveman_graph(l, k) nodes = list(G) for (u, v) in G.edges(): if seed.random() < p: # rewire the edge x = seed.choice(nodes) if G.has_edge(u, x): continue G.remove_edge(u, v) G.add_edge(u, x) return G
Example #14
Source File: community.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def caveman_graph(l, k): """Returns a caveman graph of `l` cliques of size `k`. Parameters ---------- l : int Number of cliques k : int Size of cliques Returns ------- G : NetworkX Graph caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.caveman_graph(3, 3) See also -------- connected_caveman_graph References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. """ # l disjoint cliques of size k G = nx.empty_graph(l * k) if k > 1: for start in range(0, l * k, k): edges = itertools.combinations(range(start, start + k), 2) G.add_edges_from(edges) return G
Example #15
Source File: community.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def relaxed_caveman_graph(l, k, p, seed=None): """Return a relaxed caveman graph. A relaxed caveman graph starts with `l` cliques of size `k`. Edges are then randomly rewired with probability `p` to link different cliques. Parameters ---------- l : int Number of groups k : int Size of cliques p : float Probabilty of rewiring each edge. seed : int,optional Seed for random number generator(default=None) Returns ------- G : NetworkX Graph Relaxed Caveman Graph Raises ------ NetworkXError: If p is not in [0,1] Examples -------- >>> G = nx.relaxed_caveman_graph(2, 3, 0.1, seed=42) References ---------- .. [1] Santo Fortunato, Community Detection in Graphs, Physics Reports Volume 486, Issues 3-5, February 2010, Pages 75-174. https://arxiv.org/abs/0906.0612 """ if seed is not None: random.seed(seed) G = nx.caveman_graph(l, k) nodes = list(G) for (u, v) in G.edges(): if random.random() < p: # rewire the edge x = random.choice(nodes) if G.has_edge(u, x): continue G.remove_edge(u, v) G.add_edge(u, x) return G