I have looked at the other answers to this but I still don't get it. (for instance: Why is the clique problem NP-complete?)
The general clique problem is defined as
$\text{CLIQUE} = \left\{ (G, k) | G = (V, E) \in \mathcal{G} \land \exists V_c \subseteq V . |V_c| \geqslant k \land V_c \text{ is clique in } G \right\}$
A clique of a given size can be found in polynomial time, for instance by this code:
# Iterates over all nodes. If node has at least size -1 neighbours,
# compares neighbour's neighbours with neighbours.
def CLIQUE(graph, size):
for node in graph.nodes:
if degree(graph, node) >= size - 1:
neighbours = graph.edges[node]
intersection = neighbours.union(set(node))
for neighbour in neighbours:
print neighbour
print graph.edges[neighbour].union(neighbour)
intersection = intersection.intersection(graph.edges[neighbour].union(neighbour))
print intersection
if len(intersection) >= size:
return True
return False
(rest of the program)
class Graph:
def __init__(self):
self.nodes = set()
self.edges = dict()
def from_file(self, deffile):
with open(deffile, 'r') as f:
for line in f:
n1, n2 = line.split()
self.nodes.add(n1)
self.nodes.add(n2)
if n1 in self.edges:
self.edges[n1].add(n2)
else:
self.edges[n1] = set(n2)
if n2 not in self.edges:
self.edges[n2] = set()
def degree(graph, node):
return len(graph.edges[node])
So, given that the clique problem is 'Is there q clique of at least size k in G' and the above code returns true once it finds a clique of (at least) k nodes and runs in $\mathcal{O}(n^2)$, as it compares every node with every other node in the worst case, I don't understand how that code does not do what the general clique problem does... because apparenrtly it does not, as it runs in polynomial time and the general clique problem does not.