list of sets of nodes each representing a maximal clique
1 auto g = Graph!int([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); 2 g.add(g.edge(0, 1)); 3 g.add(g.edge(0, 2)); 4 g.add(g.edge(1, 2)); 5 g.add(g.edge(1, 7)); 6 g.add(g.edge(1, 8)); 7 g.add(g.edge(2, 3)); 8 g.add(g.edge(3, 4)); 9 g.add(g.edge(3, 5)); 10 g.add(g.edge(3, 6)); 11 g.add(g.edge(4, 5)); 12 g.add(g.edge(4, 6)); 13 g.add(g.edge(5, 6)); 14 g.add(g.edge(6, 7)); 15 g.add(g.edge(7, 8)); 16 17 auto cliques = array(findAllCliques(g.adjacencyList())); 18 19 assert(cliques == [ 20 [0, 1, 2], 21 [1, 7, 8], 22 [2, 3], 23 [3, 4, 5, 6], 24 [6, 7], 25 [9], 26 ]);
Find all maximal cliques in a graph represented by adjacencyList. The implementation is based on version 1 of the Bron-Kerbosch algorithm [1].
[1]: Bron, C.; Kerbosch, J. (1973), "Algorithm 457: finding all cliques of an undirected graph", Communications of the ACM, 16 (9): 575–577, doi:10.1145/362342.362367.