2

The Bron–Kerbosch algorithm is a method for listing all maximal cliques of a graph. I recently implemented the algorithm successfully just for fun. The downside is that the algorithm is recursive, and thus can only be ran on tiny graphs until the stack overflows.

It should be possible to make the algorithm purely iterative. Consider the basic version (no pivoting) on Wikipedia. How would the iterative version of the algorithm look like in pseudocode? Is there a description somewhere?

I am imagining a stack data structure to simulate the recursion. I should also have a loop in which I test for emptiness of P and X, but I am not seeing a complete answer.

Gideon
  • 433
  • 4
  • 15

1 Answers1

6

The recursive version is given in Wikipedia as this:

BronKerbosch1(R, P, X):
   if P and X are both empty:
       report R as a maximal clique
   for each vertex v in P:
       BronKerbosch1(R ⋃ {v}, P ⋂ N(v), X ⋂ N(v))
       P := P \ {v}
       X := X ⋃ {v}

To simulate recursion, we just need to keep track of the three variables using a stack:

BronKerbosch(P):
    S := empty stack
    S.push({}, P, {})
    while S is not empty:
        R, P, X := S.pop()
        if P and X are both empty:   
            report R as a maximal clique            
        if P is not empty:
            v := some vertex in P
            S.push(R, P \ {v}, X ⋃ {v})
            S.push(R ⋃ {v}, P ⋂ N(v), X ⋂ N(v))
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • Hi, Janne, could you glance in https://stackoverflow.com/q/76141667/868905 when you have time? – ttnphns Apr 30 '23 at 14:08
  • @ttnphns In the pivoting version, the choice of pivot is the fourth variable you need to push and pop. – Janne Karila May 02 '23 at 05:37
  • Janne, what exactly should I push & pop? The pivot vertex u itself or the P \ N(u) set that is the source of v? And where the line "Choose u" must be placed in the code? – ttnphns May 02 '23 at 07:43
  • Janne, I've answered my question in the link, following the above hint from you. Can you visit there? Perhaps you can bring in improvements. Thank you. – ttnphns May 08 '23 at 16:33