-
Notifications
You must be signed in to change notification settings - Fork 5
/
BronKerbosch.java
108 lines (99 loc) · 3.94 KB
/
BronKerbosch.java
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
import java.awt.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
/*
* Given a set of friends, this class will find the maximal cliques using the
* Bron-Kerbosch algorithm
*/
public class BronKerbosch {
private Set<Set<Friend>> cliques;
public BronKerbosch(){}
/*
* Find the maximal cliques in a given set of friends
*
* @return the set of all maximal cliques (each of which is in turn a set
* of friends) present in the graph.
*
*/
public static void main (String[] args) {
FakebookReader fr = new FakebookReader("50.json");
BronKerbosch bk = new BronKerbosch();
Set<Set<Friend>> cliques = bk.maxCliques(fr.JustFriends);
for(Set<Friend> clique : cliques) {
System.out.println("-----");
for(Friend f: clique) {
System.out.println(f.name);
}
}
}
public Set<Set<Friend>> maxCliques(Set<Friend> people){
cliques = new HashSet();
ArrayList<Friend> potential_clique = new ArrayList<Friend>();
ArrayList<Friend> candidates = new ArrayList<Friend>();
ArrayList<Friend> already_found = new ArrayList<Friend>();
candidates.addAll(people);
findCliques(potential_clique,candidates,already_found);
return cliques;
}
private void findCliques(ArrayList<Friend> potential_clique, ArrayList<Friend> candidates, ArrayList<Friend> already_found) {
ArrayList<Friend> candidates_array = new ArrayList(candidates);
if (!end(candidates, already_found)) {
// for each candidate_node in candidates do
for (Friend candidate : candidates_array) {
ArrayList<Friend> new_candidates = new ArrayList<Friend>();
ArrayList<Friend> new_already_found = new ArrayList<Friend>();
// move candidate node to potential_clique
potential_clique.add(candidate);
candidates.remove(candidate);
// create new_candidates by removing nodes in candidates not
// connected to candidate node
for (Friend new_candidate : candidates) {
if (candidate.friends.containsKey(new_candidate))
{
new_candidates.add(new_candidate);
}
}
// create new_already_found by removing nodes in already_found
// not connected to candidate node
for (Friend new_found : already_found) {
if (candidate.friends.containsKey(new_found)) {
new_already_found.add(new_found);
}
}
// if new_candidates and new_already_found are empty
if (new_candidates.isEmpty() && new_already_found.isEmpty()) {
// potential_clique is maximal_clique
cliques.add(new HashSet<Friend>(potential_clique));
}
else {
findCliques(
potential_clique,
new_candidates,
new_already_found);
}
// move candidate_node from potential_clique to already_found;
already_found.add(candidate);
potential_clique.remove(candidate);
}
}
}
private boolean end(ArrayList<Friend> candidates, ArrayList<Friend> already_found)
{
// if a node in already_found is connected to all nodes in candidates
boolean end = false;
int edgecounter;
for (Friend found : already_found) {
edgecounter = 0;
for (Friend candidate : candidates) {
if (found.friends.containsKey(candidate)) {
edgecounter++;
}
}
if (edgecounter == candidates.size()) {
end = true;
}
}
return end;
}
}