-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
369 lines (312 loc) · 9.11 KB
/
Graph.java
File metadata and controls
369 lines (312 loc) · 9.11 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package npc;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* This class represents a graph.
* Each graph has vertices represented by integers from 0 to numVertices—1, inclusive.
*/
class Graph {
/***** 1. PUBLIC STATIC API *****/
/**
* Create all graphs from a file
* @param filename
* @return An ArrayList of graphs.
*/
static List<Graph> makeAllGraphsFromFile (String filename) {
Scanner file = null;
try { file = new Scanner(new File(filename)); }
catch (FileNotFoundException e) {}
List<Graph> graphs = new ArrayList<Graph>();
for (int i = 1; ; i++) {
int order = file.nextInt();
if (order > 0) graphs.add(new Graph(i, order, file));
else break;
}
return graphs;
}
/***** 2. PUBLIC API *****/
/*** 2.1 Creation API (creating new graphs) ***/
/**
* Create a new, blank graph with a given number of vertices and no edges.
* @param numVertices
*/
Graph (int numVertices) {
matrix = new ArrayList<>();
this.numVertices = numVertices;
}
/**
* Create a new, empty graph.
*/
Graph () { this(0); }
/*** 2.2 Reading API (read information about the graph) ***/
/**
* Get all edges, represented as a HashSet. Each entry in the HashSet represents a single edge, using an array of
* two integers (the two vertices connected by this edge). Each edge will be ordered with the smaller vertex first.
* @return
*/
Set<int[]> getEdges () {
Set<int[]> edges = new HashSet<>();
for (int v1 = 0; v1 < numVertices; v1++)
for (int v2 = v1 + 1; v2 < numVertices; v2++)
if (areConnected(v1, v2))
edges.add(new int[]{v1, v2});
return edges;
}
/**
* Get the numVertices of (number of vertices in) this graph.
* @return
*/
int getNumVertices () { return numVertices; }
/**
* Get all vertices in this graph, i.e., creates a set with integers ordered from 0 to numVertices—1.
* @return
*/
Set<Integer> getVertices () {
return IntStream
.range(0, numVertices)
.boxed()
.collect(Collectors.toSet());
}
/**
* Get the adjacent vertices of a given vertex.
* @param vertex
* @return
*/
Set<Integer> getAdjacentVertices (int vertex) {
return getVertices()
.stream()
.filter(v -> areConnected(vertex, v))
.collect(Collectors.toSet());
}
/**
* Generate a string representation of this graph suitable for console display, including the index, number of
* vertices, and number of edges of this graph.
*/
public String toString () { return toString(false); }
/**
* Check whether two vertices are connected.
* @param v1
* @param v2
* @return
*/
boolean areConnected (int v1, int v2) { return matrix.get(v1).get(v2); }
/**
* Find the largest clique in the graph. Returns a HashSet of integer vertices.
* @return
*/
Set<Integer> findLargestClique () {
bronKerbosch(
set(),
getVertices(),
set()
);
return new TreeSet<Integer>(
maximalCliques
.stream()
.reduce(null, (curMaxClique, thisClique) ->
curMaxClique == null || thisClique.size() > curMaxClique.size() ? thisClique : curMaxClique
)
);
}
/*** 2.3 Modification API (modifying the graph) ***/
/**
* Create a connection between two vertices. Returns itself (chainable).
* @param v1
* @param v2
* @return
*/
Graph addEdge (int v1, int v2) { return setMatrix(v1, v2, true); }
/**
* Append a new vertex to the graph, not connected to any other vertices.
* @return The number of the new vertex
*/
int appendVertex () {
addVertex(numVertices);
return numVertices - 1;
}
/**
* Add a vertex to the graph with a specific number.
* IMPORTANT: Note that this adds 1 to the number of all higher-numbered vertices.
* @param vertex
* @return
*/
Graph addVertex (int vertex) {
numVertices++;
for (List<Boolean> row: matrix) row.add(vertex, false);
matrix.add(vertex, new ArrayList<>(Collections.nCopies(numVertices, false)));
return this;
}
/**
* Remove an edge from the graph.
* @param v1
* @param v2
* @return
*/
Graph removeEdge (int v1, int v2) { return setMatrix(v1, v2, false); }
/**
* Remove a vertex from the graph.
* IMPORTANT: Note that this subtracts 1 from the number of all higher-numbered vertices.
* @param vertex
* @return
*/
Graph removeVertex (int vertex) {
matrix.remove(vertex);
for (List<Boolean> row: matrix) row.remove(vertex);
--numVertices;
return this;
}
public Graph invertGraph(){
for(int i=0;i<numVertices;i++){
for(int j=0;j<numVertices;j++){
if(i != j){ //don't make a vertex connected to itself
this.matrix.get(i).set(j, !areConnected(i, j));
}
}
}
return this;
}
/**
* Print debugging information about the graph, including a URL to visualize it. Omit the parameter to end execution
* immediately, or pass `true` to continue execution.
* @param noDie
*/
void debug (boolean noDie) {
System.out.println();
System.out.println("Debugging graph " + this.toString(true) + ":");
System.out.println("To visualize this graph, copy the following lines and paste on");
System.out.println("csacademy.com/app/graph_editor");
System.out.println();
for (int v1 = 0; v1 < numVertices; v1++)
System.out.println(v1);
for (int v1 = 0; v1 < numVertices; v1++)
for (int v2 = v1 + 1; v2 < numVertices; v2++)
if (areConnected(v1, v2))
System.out.println(v1 + " " + v2);
System.out.println();
System.out.print("Debugging output complete. ");
if (noDie) {
System.out.println("Continuing execution.");
System.out.println("To terminate execution, remove the parameter to the debug() method.");
System.out.println();
}
else {
System.out.println("Stopping execution.");
System.out.println("To continue execution, pass `true` to the debug() method.");
System.exit(0);
}
}
void debug () { debug(false); }
/***** 3. PRIVATE INSTANCE FIELDS *****/
/**
* A number >= 1 representing the index of this graph.
*/
private int index;
/**
* The number of vertices that this graph contains.
*/
private int numVertices;
/**
* A (numVertices * numVertices) 2D array representing an adjacency matrix storing pairs of vertices that are
* connected. It is symmetric (i.e. matrix[A][B] == matrix[B][A]), and vertices are NOT marked as being connected to
* themselves (i.e. matrix[A][A] == false).
*/
private List<List<Boolean>> matrix;
/**
* Used to accumulate the maximal cliques found by the Bron-Kerbosch algorithm as it recurses. After the algorithm
* completes, the largest clique in this set is reported as the maximum clique. Note that this set contains
* maxiMAL cliques, which are not all necessarily maxiMUM cliques.
*/
private Set<Set<Integer>> maximalCliques = new HashSet<>();
/***** 4. PRIVATE API *****/
/*** 4.1 Private graph methods ***/
/**
* Create a new graph using a file reference.
* @param index
* @param numVertices
* @param file
*/
private Graph (int index, int numVertices, Scanner file) {
this(numVertices);
this.index = index;
for (int v1 = 0; v1 < numVertices; v1++) {
matrix.add(new ArrayList<>());
for (int v2 = 0; v2 < numVertices; v2++)
matrix.get(v1).add(file.nextInt() == 1 && v1 != v2);
}
}
/**
* Recursor for the Bron-Kerbosch clique-finder algorithm.
* @param R
* @param P
* @param X
*/
private void bronKerbosch (Set<Integer> R, Set<Integer> P, Set<Integer> X) {
if (P.isEmpty() && X.isEmpty()) maximalCliques.add(R);
for (Iterator<Integer> PIter = P.iterator(); PIter.hasNext();) {
int v = PIter.next();
Set<Integer>
thisR = clone(R),
neighborhood = getAdjacentVertices(v);
thisR.add(v);
bronKerbosch(thisR, intersect(P, neighborhood), intersect(X, neighborhood));
PIter.remove();
X.add(v);
}
}
/**
* Set two symmetric cells in the matrix to be either true or false.
* @param v1
* @param v2
* @param connected
* @return
*/
private Graph setMatrix (int v1, int v2, boolean connected) {
this.matrix.get(v1).set(v2, connected);
this.matrix.get(v2).set(v1, connected);
return this;
}
/*** 4.2 Private helper methods ***/
/**
* Create a new EMPTY Integer HashSet.
* @return
*/
private Set<Integer> set () { return new HashSet<>(); }
/**
* Clone an Integer HashSet.
* @param c
* @return
*/
private Set<Integer> clone (Collection<Integer> c) { return new HashSet<>(c); }
/**
* Find the intersection of two Integer HashSets.
* @param s1
* @param s2
* @return
*/
private Set<Integer> intersect (Set<Integer> s1, Set<Integer> s2) {
Set<Integer> intersection = clone(s1);
intersection.retainAll(s2);
return intersection;
}
/**
* Generate a string representation of the graph
* @param withLabels
* @return
*/
private String toString (boolean withLabels) {
return
"G" + index + " ( " + numVertices + (withLabels ? " vertices" : "") + ", " + getEdges().size() +
(withLabels ? " edges" : "") + ")";
}
}