-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clique.java
179 lines (161 loc) · 4.78 KB
/
Clique.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
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
import java.util.*;
/**
* Abstraction of a Clique - A solution to the clique problem.
*
* @author Shalin Shah
* Email: [email protected]
*/
public class Clique
{
/* Possible additions */
public LinkedHashSet pa;
/* The vertices that make up the clique */
public LinkedHashSet clique;
/* A TreeMap of possible additions sorted according to their degrees */
public TreeMap sortedPA;
/* The graph on which the algorithm runs */
public Graph graph;
/* A random access list of the vertices that make up the clique */
public List cliqueList;
/** Creates a new instance of Clique */
public Clique(int firstVertex, Graph graph)
{
pa = new LinkedHashSet();
clique = new LinkedHashSet();
sortedPA = new TreeMap();
cliqueList = new ArrayList();
clique.add(new Integer(firstVertex));
cliqueList.add(new Integer(firstVertex));
this.graph = graph;
for(int i=0; i<Constants.NUMBER_NODES; i++)
{
if(i == firstVertex)
{
continue;
}
if(getEdge(i, firstVertex, graph) == 1)
{
pa.add(new Integer(i));
sortedPA.put(graph.nodes[i], graph.nodes[i]);
}
}
}
/*
* Get an edge from the graph
*/
public static int getEdge(int i, int j, Graph graph)
{
if(graph.nodes[i].list.contains(j))
{
return 1;
}
return 0;
}
/*
* Add a vertex to the clique and update the possible additions list.
*/
public void addVertex(int vertex)
{
clique.add(new Integer(vertex));
cliqueList.add(new Integer(vertex));
removeFromSortedPA(graph.nodes[vertex]);
pa.remove(new Integer(vertex));
for(Iterator it = pa.iterator(); it.hasNext();)
{
int pavertex = ((Integer)it.next()).intValue();
if(getEdge(pavertex, vertex, graph) == 0)
{
it.remove();
removeFromSortedPA(graph.nodes[pavertex]);
}
else if(getEdge(pavertex, vertex, graph) == 1)
{
// do nothing
}
else
{
System.out.println("Invalid Matrix in addVertex of Clique");
System.exit(1);
}
}
}
/*
* Remove a vertex from the clique and update the possible additions list.
*/
public void removeVertex(int vertex)
{
if(!clique.contains(new Integer(vertex)))
return;
clique.remove(new Integer(vertex));
cliqueList.remove(new Integer(vertex));
for(int i=0; i<Constants.NUMBER_NODES; i++)
{
if(clique.contains(new Integer(i)))
{
continue;
}
else
{
Iterator it = clique.iterator();
boolean flag = true;
while(it.hasNext())
{
int ver = ((Integer)it.next()).intValue();
if(getEdge(i, ver, graph) == 0)
{
flag = false;
break;
}
}
if(flag)
{
pa.add(new Integer(i));
sortedPA.put(graph.nodes[i], graph.nodes[i]);
}
}
}
}
/* Linear time remove method for TreeMap */
private void removeFromSortedPA(Node node)
{
Set set = sortedPA.keySet();
Iterator it = set.iterator();
while(it.hasNext())
{
if(it.next().equals(node))
{
it.remove();
break;
}
}
}
/*
* Compute a list of nodes based on degrees in the induced subgraph of
* possible additions (PA)
*/
public List computeSortedList()
{
List sortedList = new ArrayList();
Iterator it = pa.iterator();
while(it.hasNext())
{
int node1 = ((Integer)it.next()).intValue();
int reach = 0;
Iterator itt = pa.iterator();
while(itt.hasNext())
{
int node2 = ((Integer)itt.next()).intValue();
if(getEdge(node1, node2, graph) == 1)
{
reach++;
}
}
SortedListNode n = new SortedListNode();
n.reach = reach;
n.node = node1;
sortedList.add(n);
}
Collections.sort(sortedList);
return sortedList;
}
}