Maximum Clique Search Algorithm
Prerequisites: Clique
Introduction
In computer science, the clique problem refers to the computational problem of finding cliques (subsets of vertices that are all adjacent to each other, also known as complete subgraphs) in a given graph.
The clique problem also manifests in real life. For example, consider a social network where vertices represent users and edges represent mutual acquaintance between two users. Then finding a clique corresponds to finding a group of people who all know each other.
If we want to find the largest group of mutually acquainted people in this social network, we need to use a maximum clique search algorithm.
We have already introduced the concept of maximal clique. A maximum clique is a maximal clique with the largest number of vertices.
Explanation
The idea is to use recursion and backtracking, maintaining a list of vertices. Each time a vertex is added, we check whether the current set of vertices still forms a clique. If adding the vertex would break the clique property, we backtrack to a state that satisfies the condition and try adding a different vertex.
The reason for using a backtracking strategy is that we do not know in advance whether a vertex \(v\) will ultimately be part of the maximum clique. If the recursive algorithm chooses \(v\) as a member of the maximum clique but fails to find a maximum clique, it should backtrack and search for a solution without \(v\).
Process
The Bron–Kerbosch algorithm optimizes this idea. Its basic form recursively searches using three sets: \(R\), \(P\), and \(X\). The steps are as follows:
- Initialize sets \(R\) and \(X\) as empty, and set \(P\) as the set of all vertices in the graph.
- Each time, take a vertex \(v\) from set \(P\). When \(P\) is empty, there are two cases:
- Set \(R\) is a maximum clique, and set \(X\) is empty.
- No maximum clique is found, then backtrack.
- For each vertex \(v\) taken from set \(P\), perform the following:
- Add vertex \(v\) to set \(R\), then recursively call the algorithm on sets \(R\), \(P\), and \(X\).
- Remove vertex \(v\) from set \(P\) and add it to set \(X\).
- If both sets \(P\) and \(X\) are empty, then set \(R\) is a maximum clique.
This method can be further optimized. To save time and allow the algorithm to backtrack faster, a pivot vertex can be selected for searching. Another optimization is to sort all vertices at the beginning and enumerate them in order of their indices to avoid duplication.
Implementation
Pseudocode
1 2 3 4 5 6 7 8 9 10 11 | |
C++ Implementation
Implementation Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
Example Problems
POJ 2989: All Friends
Problem summary: Given \(n\) people and \(m\) pairs of friends, find the number of maximum cliques.
Idea: Template problem, requires using the Bron–Kerbosch algorithm.
Pseudocode:
1 2 3 4 5 6 7 8 | |
To save time and allow the algorithm to backtrack faster, we can optimize by selecting a pivot vertex \(v\).
We know that the algorithm described above will inevitably repeat many calculations of previously computed maximal cliques before backtracking.
Taking the three sets \(R\), \(P\), and \(X\) mentioned earlier as an example:
Consider the following: take a vertex \(u\) from the set \(P\cup X\). To form a maximal clique with set \(R\), the vertices chosen must be from \(P\cap N(u)\) (where \(N(u)\) represents the neighbors of \(u\)).
If after choosing \(u\), we could also choose a neighbor \(v\) of \(u\) to join the maximal clique, then we only need to choose \(u\). This reduces redundant calculations for \(v\) later. We only need to consider vertices that are not adjacent to \(u\).
C++ implementation with optimization:
Implementation Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
Exercises
References
- Clique problem - Wikipedia
- Maximal Clique and Maximum Clique in Undirected Graphs (Bron–Kerbosch Algorithm)
- Maximum Clique Problem — Bron–Kerbosch Algorithm
- Maximum Clique Problem
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用