maximal_cliques#
- graph_tool.structure.maximal_cliques(g, min_size=None, max_size=None, vertices=None)[git][source]#
Return an iterator over the maximal cliques of the graph.
- Parameters:
- g
Graph Graph to be used.
- min_size
int(optional, default:None) If given, only the maximal cliques with at least this many vertices are returned.
- max_size
int(optional, default:None) If given, only the maximal cliques with at most this many vertices are returned.
- verticesiterable of
int(optional, default:None) If given, only the maximal cliques that contain all these vertices are returned. They must form a clique.
- g
- Returns:
- maximal_cliquesiterator over
numpy.ndarrayinstances Iterator over lists of vertices corresponding to the maximal cliques.
- maximal_cliquesiterator over
See also
all_cliquesAll cliques, maximal or not
largest_cliquesCliques of maximum size
clique_numberSize of a largest clique
chordal_cliquesMaximal cliques of a chordal graph
graph_tool.clustering.motifsCounts of connected subgraphs
Notes
This implements the Bron-Kerbosch algorithm [bron_algorithm_1973] [bron-kerbosh-wiki] with pivoting [tomita_worst-case_2006] [cazals_note_2008].
The edge directions and self-loops are ignored, and the maximal cliques of size one (i.e. vertices without neighbors) are not included.
The worst case complexity of this algorithm is \(O(3^{V/3})\) for a graph of \(V\) vertices, but for sparse graphs it is typically much faster. The branches of the search that cannot lead to a clique within the size bounds are pruned, and with
verticesthe search starts from these vertices and their common neighbors. Isolated vertices are not returned as cliques of size one, unless they are given invertices.References
[bron_algorithm_1973]Coen Bron and Joep Kerbosch, “Algorithm 457: finding all cliques of an undirected graph”, Commun. ACM 16, 9, 575-577 (1973), DOI: 10.1145/362342.362367 [sci-hub, @tor]
[tomita_worst-case_2006]Etsuji Tomita, Akira Tanaka, and Haruhisa Takahashi. “The worst-case time complexity for generating all maximal cliques and computational experiments.” Theoretical Computer Science 363.1 28-42 (2006), DOI: 10.1016/j.tcs.2006.06.015 [sci-hub, @tor]
[cazals_note_2008]Frédéric Cazals, and Chinmay Karande, “A note on the problem of reporting maximal cliques.” Theoretical Computer Science 407.1-3 564-568 (2008), DOI: 10.1016/j.tcs.2008.05.010 [sci-hub, @tor]
Examples
>>> g = gt.collection.data["polblogs"] >>> len(list(gt.maximal_cliques(g))) 49618 >>> len(list(gt.maximal_cliques(g, min_size=10))) 25165 >>> len(list(gt.maximal_cliques(g, vertices=[0]))) 37