- find_cliques(G, nodes=None)#
Returns all maximal cliques in an undirected graph.
For each node n, a maximal clique for n is a largest complete subgraph containing n. The largest maximal clique is sometimes called the maximum clique.
This function returns an iterator over cliques, each of which is a list of nodes. It is an iterative implementation, so should not suffer from recursion depth issues.
This function accepts a list of
nodesand only the maximal cliques containing all of these
nodesare returned. It can considerably speed up the running time if some specific cliques are desired.
- GNetworkX graph
An undirected graph.
- nodeslist, optional (default=None)
If provided, only yield maximal cliques containing all nodes in
nodesisn’t a clique itself, a ValueError is raised.
An iterator over maximal cliques, each of which is a list of nodes in
nodesis provided, only the maximal cliques containing all the nodes in
nodesare returned. The order of cliques is arbitrary.
nodesis not a clique.
A recursive version of the same algorithm.
To obtain a list of all maximal cliques, use
list(find_cliques(G)). However, be aware that in the worst-case, the length of this list can be exponential in the number of nodes in the graph. This function avoids storing all cliques in memory by only keeping current candidate node lists in memory during its search.
This implementation is based on the algorithm published by Bron and Kerbosch (1973) , as adapted by Tomita, Tanaka and Takahashi (2006)  and discussed in Cazals and Karande (2008) . It essentially unrolls the recursion used in the references to avoid issues of recursion stack depth (for a recursive implementation, see
This algorithm ignores self-loops and parallel edges, since cliques are not conventionally defined with such edges.
Bron, C. and Kerbosch, J. “Algorithm 457: finding all cliques of an undirected graph”. Communications of the ACM 16, 9 (Sep. 1973), 575–577. <http://portal.acm.org/citation.cfm?doid=362342.362367>
Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi, “The worst-case time complexity for generating all maximal cliques and computational experiments”, Theoretical Computer Science, Volume 363, Issue 1, Computing and Combinatorics, 10th Annual International Conference on Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28–42 <https://doi.org/10.1016/j.tcs.2006.06.015>