Skip to content

Commit

Permalink
Merge pull request #1815 from Overriders/ring-of-cliques
Browse files Browse the repository at this point in the history
Add generator for a ring of cliques graph
  • Loading branch information
dschult committed Nov 12, 2015
2 parents 44a0c8d + 90b354c commit c7af34a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/reference/generators.rst
Expand Up @@ -218,6 +218,7 @@ Community
random_partition_graph
planted_partition_graph
gaussian_random_partition_graph
ring_of_cliques


Non Isomorphic Trees
Expand Down
64 changes: 61 additions & 3 deletions networkx/generators/community.py
Expand Up @@ -3,16 +3,20 @@
import math
import random
import networkx as nx
# Copyright(C) 2011 by
# Copyright(C) 2011, 2015 by
# Ben Edwards <bedwards@cs.unm.edu>
# Aric Hagberg <hagberg@lanl.gov>
# Konstantinos Karakatsanis <dinoskarakas@gmail.com>
# All rights reserved.
# BSD license.
__author__ = """\n""".join(['Ben Edwards (bedwards@cs.unm.edu)',
'Aric Hagberg (hagberg@lanl.gov)'])
'Aric Hagberg (hagberg@lanl.gov)',
'Konstantinos Karakatsanis '
'<dinoskarakas@gmail.com>'])
__all__ = ['caveman_graph', 'connected_caveman_graph',
'relaxed_caveman_graph', 'random_partition_graph',
'planted_partition_graph', 'gaussian_random_partition_graph']
'planted_partition_graph', 'gaussian_random_partition_graph',
'ring_of_cliques']


def caveman_graph(l, k):
Expand Down Expand Up @@ -406,3 +410,57 @@ def gaussian_random_partition_graph(n, s, v, p_in, p_out, directed=False,
assigned += size
sizes.append(size)
return random_partition_graph(sizes, p_in, p_out, directed, seed)


def ring_of_cliques(num_cliques, clique_size):
"""Defines a "ring of cliques" graph.
A ring of cliques graph is consisting of cliques, connected through single
links. Each clique is a complete graph.
Parameters
----------
num_cliques : int
Number of cliques
clique_size : int
Size of cliques
Returns
-------
G : NetworkX Graph
ring of cliques graph
Raises
------
NetworkXError
If the number of cliques is lower than 2 or
if the size of cliques is smaller than 2.
Examples
--------
>>> G = nx.ring_of_cliques(8, 4)
See Also
--------
connected_caveman_graph
Notes
-----
The `connected_caveman_graph` graph removes a link from each clique to
connect it with the next clique. Instead, the `ring_of_cliques` graph
simply adds the link without removing any link from the cliques.
"""
if num_cliques < 2:
raise nx.NetworkXError('A ring of cliques must have at least '
'two cliques')
if clique_size < 2:
raise nx.NetworkXError('The cliques must have at least two nodes')

G = nx.Graph()
for i in range(num_cliques):
edges = itertools.combinations(range(i*clique_size, i*clique_size +
clique_size), 2)
G.add_edges_from(edges)
G.add_edge(i*clique_size+1, (i+1)*clique_size %
(num_cliques*clique_size))
return G
13 changes: 13 additions & 0 deletions networkx/generators/tests/test_community.py
Expand Up @@ -110,3 +110,16 @@ def test_gaussian_random_partition_graph():
assert_raises(nx.NetworkXError,
nx.gaussian_random_partition_graph, 100, 101, 10, 1, 0)

def test_ring_of_cliques():
for i in range(2, 20):
for j in range(2, 20):
G = nx.ring_of_cliques(i, j)
assert_equal(G.number_of_nodes(), i*j)
if i != 2 or j != 1:
expected_num_edges = i * (((j * (j - 1)) // 2) + 1)
else:
# the edge that already exists cannot be duplicated
expected_num_edges = i * (((j * (j - 1)) // 2) + 1) - 1
assert_equal(G.number_of_edges(), expected_num_edges)
assert_raises(nx.NetworkXError, nx.ring_of_cliques, 1, 5)
assert_raises(nx.NetworkXError, nx.ring_of_cliques, 3, 0)

0 comments on commit c7af34a

Please sign in to comment.