-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph.java
More file actions
268 lines (228 loc) · 7.13 KB
/
Copy pathGraph.java
File metadata and controls
268 lines (228 loc) · 7.13 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
/**
* Taken from https://gist.github.com/imamhidayat92/dff60e5554020bd58b64
* With some modification
*
* Credit to imamhidayat92
* and Ray Andrew <raydreww@gmail.com>
*/
import java.util.*;
/**
* A simple undirected and unweighted graph implementation.
*
* @param <Long> The type that would be used as vertex.
*/
public class Graph {
final private HashMap<Long, Set<Long>> adjacencyList;
final private int MAX_SIZE = 4000000;
/**
* Create new Graph object.
*/
public Graph() {
this.adjacencyList = new HashMap<>(MAX_SIZE);
}
/**
* Add new vertex to the graph.
*
* @param v The vertex object.
*/
public void addVertex(Long v) {
if (!this.adjacencyList.containsKey(v)) {
this.adjacencyList.put(v, new HashSet<Long>());
}
}
/**
* Remove the vertex v from the graph.
*
* @param v The vertex that will be removed.
*/
public void removeVertex(Long v) {
if (!this.adjacencyList.containsKey(v)) {
throw new IllegalArgumentException("Vertex doesn't exist.");
}
this.adjacencyList.remove(v);
for (Long u : this.getAllVertices()) {
this.adjacencyList.get(u).remove(v);
}
}
/**
* Add new edge between vertex. Adding new edge from u to v will automatically
* add new edge from v to u since the graph is undirected.
*
* @param v Start vertex.
* @param u Destination vertex.
*/
public void addEdge(Long v, Long u) {
if (!this.adjacencyList.containsKey(v) || !this.adjacencyList.containsKey(u)) {
// throw new IllegalArgumentException();
this.addVertex(u);
this.addVertex(v);
}
this.adjacencyList.get(v).add(u);
this.adjacencyList.get(u).add(v);
}
/**
* Remove the edge between vertex. Removing the edge from u to v will
* automatically remove the edge from v to u since the graph is undirected.
*
* @param v Start vertex.
* @param u Destination vertex.
*/
public void removeEdge(Long v, Long u) {
if (!this.adjacencyList.containsKey(v) || !this.adjacencyList.containsKey(u)) {
throw new IllegalArgumentException();
}
this.adjacencyList.get(v).remove(u);
this.adjacencyList.get(u).remove(v);
}
/**
* Check adjacency between 2 vertices in the graph.
*
* @param v Start vertex.
* @param u Destination vertex.
* @return <tt>true</tt> if the vertex v and u are connected.
*/
public boolean isAdjacent(Long v, Long u) {
return this.adjacencyList.get(v).contains(u);
}
/**
* Get all vertices in the graph.
*
* @return An Iterable for all vertices in the graph.
*/
public Iterable<Long> getAllVertices() {
return this.adjacencyList.keySet();
}
/**
* Get connected vertices of a vertex.
*
* @param v The vertex.
* @return An iterable for connected vertices.
*/
public Iterable<Long> getNeighbors(Long v) {
return this.adjacencyList.get(v);
}
/**
* Get connected vertices of a vertex.
*
* @param v The vertex.
* @return Degree count for vertex v.
*/
public long degree(Long v) {
long counter = 0;
Iterator<Long> neighborIterator = getNeighbors(v).iterator();
while (neighborIterator.hasNext()) {
neighborIterator.next();
counter++;
}
return counter;
}
/**
* Get first neighbor with min grade
*
* @param vertices List of vertices.
* @param v vertex.
* @return first neighbor with min grade.
*/
private Integer firstNeighborIndex(List<Long> vertices, Long v) { // fni
int index, min = vertices.size();
Iterator<Long> neighborIterator = getNeighbors(v).iterator();
while (neighborIterator.hasNext()) {
index = vertices.indexOf(neighborIterator.next());
if (min > index) {
min = index;
}
}
return min;
}
/**
* Get first neighbor with min grade of specific vertex
*
* @param vertices List of vertices.
* @param v vertex.
* @param j vertex to be compared with.
* @return first neighbor with min grade of vertex v.
*/
private Integer firstNeighborWithMinDegree(List<Long> vertices, Long v, Integer j) { // nni
int index, min = vertices.size();
Iterator<Long> neighborIterator = getNeighbors(v).iterator();
while (neighborIterator.hasNext()) {
index = vertices.indexOf(neighborIterator.next());
if (min > index && index > j) {
min = index;
}
}
return min;
}
public double countTrianglesWithPartition(int p) {
if (p <= 1) {
throw new Error("Partition must be more than 1");
}
List<Long> vertices = new ArrayList<>(MAX_SIZE);
Iterator<Long> verticesIterator = getAllVertices().iterator();
verticesIterator.forEachRemaining(vertices::add);
java.util.Collections.sort(vertices, new java.util.Comparator<Long>() {
@Override
public int compare(Long o1, Long o2) {
return (degree(o1) > degree(o2) ? -1 : (degree(o1) == degree(o2) ? 0 : 1));
}
});
// algorithm compact-forward
double counter = 0.0;
int l;
Iterator<Long> neighborsIterator;
Long templ;
Long tempk;
Long tempi;
for (int i = 0; i < vertices.size(); i++) {
neighborsIterator = getNeighbors(vertices.get(i)).iterator();
while (neighborsIterator.hasNext()) {
l = vertices.indexOf(neighborsIterator.next());
if (l < i) {
Integer j = firstNeighborIndex(vertices, vertices.get(i));
Integer k = firstNeighborIndex(vertices, vertices.get(l));
while ((j < l) && (k < l)) {
if (j < k) {
j = firstNeighborWithMinDegree(vertices, vertices.get(i), j);
} else {
if (k < j) {
k = firstNeighborWithMinDegree(vertices, vertices.get(l), k);
} else {
templ = vertices.get(l);
tempk = vertices.get(k);
tempi = vertices.get(i);
if (templ % p == tempk % p && tempk % p == tempi % p) {
counter = counter + (1.0 / (p - 1));
} else {
counter = counter + 1.0;
} // counting of the others
j = firstNeighborWithMinDegree(vertices, tempi, j);
k = firstNeighborWithMinDegree(vertices, templ, k);
}
}
}
}
}
}
return counter;
}
// public static void main(String[] args) {
// Graph graph = new Graph();
// graph.addEdge(new Long(1), new Long(2));
// graph.addEdge(new Long(2), new Long(1));
// graph.addEdge(new Long(2), new Long(3));
// graph.addEdge(new Long(3), new Long(4));
// graph.addEdge(new Long(4), new Long(3));
// graph.addEdge(new Long(3), new Long(1));
// graph.addEdge(new Long(13), new Long(12));
// graph.addEdge(new Long(12), new Long(13));
// graph.addEdge(new Long(12), new Long(14));
// graph.addEdge(new Long(12), new Long(15));
// graph.addEdge(new Long(14), new Long(15));
// graph.addEdge(new Long(16), new Long(17));
// Iterator<Long> graphIterator = graph.getAllVertices().iterator();
// while (graphIterator.hasNext()) {
// System.out.println(graphIterator.next());
// }
// System.out.println(graph.countTrianglesWithPartition(2));
// }
}