def find_cliques_recursive(G):
 if (len(G) == 0):
  return iter([])
 adj = {u: {v for v in G[u] if (v != u)} for u in G}
 Q = []
 def expand(subg, cand):
  u = max(subg, key=(lambda u: len((cand & adj[u]))))
  for q in (cand - adj[u]):
   cand.remove(q)
   Q.append(q)
   adj_q = adj[q]
   subg_q = (subg & adj_q)
   if (not subg_q):
    (yield Q[:])
   else:
    cand_q = (cand & adj_q)
    if cand_q:
     for clique in expand(subg_q, cand_q):
      (yield clique)
   Q.pop()
 return expand(set(G), set(G))

def find_cliques_recursive ( G ) : if ( len ( G ) == 0 ) : return iter ( [ ] ) adj = { u : { v for v in G [ u ] if ( v != u ) } for u in G } Q = [ ] def expand ( subg , cand ) : u = max ( subg , key = ( lambda u : len ( ( cand & adj [ u ] ) ) ) ) for q in ( cand - adj [ u ] ) : cand . remove ( q ) Q . append ( q ) adj_q = adj [ q ] subg_q = ( subg & adj_q ) if ( not subg_q ) : ( yield Q [ : ] ) else : cand_q = ( cand & adj_q ) if cand_q : for clique in expand ( subg_q , cand_q ) : ( yield clique ) Q . pop ( ) return expand ( set ( G ) , set ( G ) )
returns all maximal cliques in a graph .