forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBronKerbosch.java
More file actions
114 lines (102 loc) · 3.89 KB
/
BronKerbosch.java
File metadata and controls
114 lines (102 loc) · 3.89 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.thealgorithms.graph;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Implementation of the Bron–Kerbosch algorithm with pivoting for enumerating all maximal cliques
* in an undirected graph.
*
* <p>The input graph is represented as an adjacency list where {@code adjacency.get(u)} returns the
* set of vertices adjacent to {@code u}. The algorithm runs in time proportional to the number of
* maximal cliques produced and is widely used for clique enumeration problems.</p>
*
* @author <a href="https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm">Wikipedia: Bron–Kerbosch algorithm</a>
*/
public final class BronKerbosch {
private BronKerbosch() {
}
/**
* Finds all maximal cliques of the provided graph.
*
* @param adjacency adjacency list where {@code adjacency.size()} equals the number of vertices
* @return a list containing every maximal clique, each represented as a {@link Set} of vertices
* @throws IllegalArgumentException if the adjacency list is {@code null}, contains {@code null}
* entries, or references invalid vertices
*/
public static List<Set<Integer>> findMaximalCliques(List<Set<Integer>> adjacency) {
if (adjacency == null) {
throw new IllegalArgumentException("Adjacency list must not be null");
}
int n = adjacency.size();
List<Set<Integer>> graph = new ArrayList<>(n);
for (int u = 0; u < n; u++) {
Set<Integer> neighbors = adjacency.get(u);
if (neighbors == null) {
throw new IllegalArgumentException("Adjacency list must not contain null sets");
}
Set<Integer> copy = new HashSet<>();
for (int v : neighbors) {
if (v < 0 || v >= n) {
throw new IllegalArgumentException("Neighbor index out of bounds: " + v);
}
if (v != u) {
copy.add(v);
}
}
graph.add(copy);
}
Set<Integer> r = new HashSet<>();
Set<Integer> p = new HashSet<>();
Set<Integer> x = new HashSet<>();
for (int v = 0; v < n; v++) {
p.add(v);
}
List<Set<Integer>> cliques = new ArrayList<>();
bronKerboschPivot(r, p, x, graph, cliques);
return cliques;
}
private static void bronKerboschPivot(Set<Integer> r, Set<Integer> p, Set<Integer> x, List<Set<Integer>> graph, List<Set<Integer>> cliques) {
if (p.isEmpty() && x.isEmpty()) {
cliques.add(new HashSet<>(r));
return;
}
int pivot = choosePivot(p, x, graph);
Set<Integer> candidates = new HashSet<>(p);
if (pivot != -1) {
candidates.removeAll(graph.get(pivot));
}
for (Integer v : candidates) {
r.add(v);
Set<Integer> newP = intersection(p, graph.get(v));
Set<Integer> newX = intersection(x, graph.get(v));
bronKerboschPivot(r, newP, newX, graph, cliques);
r.remove(v);
p.remove(v);
x.add(v);
}
}
private static int choosePivot(Set<Integer> p, Set<Integer> x, List<Set<Integer>> graph) {
int pivot = -1;
int maxDegree = -1;
Set<Integer> union = new HashSet<>(p);
union.addAll(x);
for (Integer v : union) {
int degree = graph.get(v).size();
if (degree > maxDegree) {
maxDegree = degree;
pivot = v;
}
}
return pivot;
}
private static Set<Integer> intersection(Set<Integer> base, Set<Integer> neighbors) {
Set<Integer> result = new HashSet<>();
for (Integer v : base) {
if (neighbors.contains(v)) {
result.add(v);
}
}
return result;
}
}