-
Notifications
You must be signed in to change notification settings - Fork 636
Expand file tree
/
Copy pathCPM.java
More file actions
277 lines (216 loc) · 7.83 KB
/
CPM.java
File metadata and controls
277 lines (216 loc) · 7.83 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.plugin;
import org.gephi.graph.api.*;
import org.gephi.utils.longtask.spi.LongTask;
import org.gephi.utils.progress.ProgressTicket;
import org.openide.util.Lookup;
import java.util.*;
public class CPM implements org.gephi.statistics.spi.Statistics, LongTask {
private String report = "";
private boolean cancel = false;
private ProgressTicket progressTicket;
private int k = 0;
private Set<Set<Node>> Cliques = new HashSet<Set<Node>>();
GenQueue<TreeSet<Node>> Bk = new GenQueue<TreeSet<Node>>();
public class SortByID implements Comparator<Node> {
public int compare(Node n1, Node n2) {
if (n1.getStoreId() > n2.getStoreId()) {
return 1;
} else {
return -1;
}
}
}
//<editor-fold defaultstate="collapsed" desc="Queue Implementation">
public Object getLastElement(final Collection c) {
/*
final Iterator itr = c.iterator();
Object lastElement = itr.next();
while (itr.hasNext()) {
lastElement = itr.next();
}
return lastElement;
*/
return null;
}
class GenQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
public void enqueue(E item) {
list.addLast(item);
}
public E dequeue() {
return list.pollFirst();
}
public boolean hasItems() {
return !list.isEmpty();
}
public int size() {
return list.size();
}
public void addItems(GenQueue<? extends E> q) {
while (q.hasItems()) {
list.addLast(q.dequeue());
}
}
}
//</editor-fold>
private Vector<Node> getLargerIndexNodes(Graph g, Node vi) {
Vector<Node> output = new Vector<Node>();
for (Node n : g.getNodes()) {
boolean b1 = n.getStoreId() > vi.getStoreId(),
b2 = g.getEdge(n, vi) != null,
b3 = g.getEdge(vi, n) != null;
if (b1 && (b2 || b3)) {
output.addElement(n);
}
}
return output;
}
private boolean checkBk1IsClique(Graph g, TreeSet<Node> Bk1) {
for (Node firstNode : Bk1) {
for (Node secondNode : Bk1) {
if (firstNode == secondNode) {
continue;
}
if (g.getEdge(firstNode, secondNode) == null &&
g.getEdge(secondNode, firstNode) == null) { //One edge is missing in the Bk+1 clique
return false;
}
}
}
return true;
}
Random r = new Random();
@Override
public void execute(GraphModel gm) {
/*for (int i = 0; i < 2; i++) {
Graph graph = gm.getGraphVisible();
Node node = gm.factory().newNode();
node.setLabel(String.valueOf(r.nextInt()));
node.setX(r.nextInt(500));
node.setY(r.nextInt(500));
node.setSize(10f);
graph.addNode(node);
}*/
Graph g = gm.getGraphVisible();
g.readLock();
//Firstly add each node as an item in Bk
int count = 0;
TreeSet<Node> tmp;
for (Node n : g.getNodes()) {
count++;
//Trick: if the node's degree is less than k-1, it can not involve in k-clique
if (g.getDegree(n) >= k - 1) {
tmp = new TreeSet<Node>(new SortByID());
tmp.add(n);
Bk.enqueue(tmp); //Add the B1 (node itself) to the queue
}
}
//Now start the iterative process for finding cliques
tmp = Bk.dequeue();
while (tmp != null) {
if (cancel) {
//Empty variables
Bk.list.clear();
tmp.clear();
Cliques.clear();
return;
}
//Search for Bk+1
Node vi = tmp.last(); //(Node) getLastElement(tmp);
Vector<Node> largerIndexes = getLargerIndexNodes(g, vi);
for (Node vj : largerIndexes) {
TreeSet<Node> Bk1 = new TreeSet<Node>(new SortByID());
Bk1.addAll(tmp); //Clone current Bk into Bk+1
Bk1.add(vj);
if (Bk1.size() <= getK() && checkBk1IsClique(g, Bk1)) {
if (Bk1.size() == getK()) { //A clique of size k found. Finish expanding this Bk+1 here.
Cliques.add(Bk1);
} else if (Bk1.size() < getK()) {
Bk.enqueue(Bk1); //k should be checked for finding cliques of size k.
} else { //Clique with larger size will be omitted.
report += "<br>Larger Clique Found. It should not be here<br>";
}
}
}
tmp = Bk.dequeue(); //Check next item
}
g.readUnlock();
//Algorithm finished.
//Write the output
report += "Clique Detection started. Nodes with <b>" + (k - 1) + "</b> edges will not be included.";
report += "<br><br>";
report += "Found Cliques of size " + getK() + ".<br>Now making new graph ...<br>Clearing old graph ...";
//edit the graph
g.clear();
report += " [+]<br>Creating new nodes ...";
gm = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
int nID = 0;
Set<Node> nodes = new HashSet<Node>();
for (Set<Node> firstClique : Cliques) { //Create the nodes
Node firstNode = gm.factory().newNode(String.valueOf(nID++));
firstNode.setX(-500 + r.nextInt(1000));
firstNode.setY(-500 + r.nextInt(1000));
firstNode.setSize(8f);
String nodeLabel = "";
for (Node n : firstClique) {
nodeLabel += n.getLabel() + ",";
}
nodeLabel = nodeLabel.substring(0, nodeLabel.length() - 1); //remove last ,
firstNode.setLabel(nodeLabel);
nodes.add(firstNode);
}
report += "[+]<br>Detecting and creating the edges ...";
HashSet<Edge> edges = new HashSet<Edge>();
for (Node vi : nodes) {
for (Node vj : nodes) {
if ((vi != vj) && (getSharedNodes(vi, vj) == k - 1)) {
if (g.isDirected()) {
edges.add(gm.factory().newEdge(vi, vj, true));
} else {
edges.add(gm.factory().newEdge(vi, vj, false));
}
}
}
}
report += "[+]<br>Redrawing new graph ...";
for (Node n : nodes) {
g.addNode(n);
}
for (Edge e : edges) {
g.addEdge(e);
}
report += "[+]<br>Done!<br><br><br>Palla, Gergely, Imre Derényi, Illés Farkas, and Tamás Vicsek. \"Uncovering the overlapping community structure of complex networks in nature and society.\" Nature 435, no. 7043 (2005): 814-818";
}
private int getSharedNodes(Node vi, Node vj) {
String[] firstCliqueNodes = vi.getLabel().split(",");
String[] secondCliqueNodes = vj.getLabel().split(",");
int sharedNodes = 0;
for (String n1 : firstCliqueNodes) {
for (String n2 : secondCliqueNodes) {
if (n1.equals(n2)) {
sharedNodes++;
}
}
}
return sharedNodes;
}
@Override
public String getReport() {
return report;
}
@Override
public boolean cancel() {
cancel = true;
return true;
}
@Override
public void setProgressTicket(ProgressTicket pt) {
this.progressTicket = pt;
}
public int getK() {
return k;
}
public void setK(int k) {
this.k = k;
}
}