Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

# Source code for networkx.algorithms.community.kclique

#-*- coding: utf-8 -*-
#    Aric Hagberg <hagberg@lanl.gov>
from collections import defaultdict
import networkx as nx
'Aric Hagberg <aric.hagberg@gmail.com>'])
__all__ = ['k_clique_communities']

[docs]def k_clique_communities(G, k, cliques=None):
"""Find k-clique communities in graph using the percolation method.

A k-clique community is the union of all cliques of size k that
can be reached through adjacent (sharing k-1 nodes) k-cliques.

Parameters
----------
G : NetworkX graph

k : int
Size of smallest clique

cliques: list or generator
Precomputed cliques (use networkx.find_cliques(G))

Returns
-------
Yields sets of nodes, one for each k-clique community.

Examples
--------
>>> G = nx.complete_graph(5)
>>> K5 = nx.convert_node_labels_to_integers(G,first_label=2)
>>> c = list(nx.k_clique_communities(G, 4))
>>> list(c[0])
[0, 1, 2, 3, 4, 5, 6]
>>> list(nx.k_clique_communities(G, 6))
[]

References
----------
.. [1] Gergely Palla, Imre Derényi, Illés Farkas1, and Tamás Vicsek,
Uncovering the overlapping community structure of complex networks
in nature and society Nature 435, 814-818, 2005,
doi:10.1038/nature03607
"""
if k < 2:
raise nx.NetworkXError("k=%d, k must be greater than 1."%k)
if cliques is None:
cliques = nx.find_cliques(G)
cliques = [frozenset(c) for c in cliques if len(c) >= k]

# First index which nodes are in which cliques
membership_dict = defaultdict(list)
for clique in cliques:
for node in clique:
membership_dict[node].append(clique)

# For each clique, see which adjacent cliques percolate
perc_graph = nx.Graph()
for clique in cliques:
if len(clique.intersection(adj_clique)) >= (k - 1):

# Connected components of clique graph with perc edges
# are the percolated cliques
for component in nx.connected_components(perc_graph):
yield(frozenset.union(*component))