-
Notifications
You must be signed in to change notification settings - Fork 9
/
AdjacencyMapDigraph.java
350 lines (297 loc) · 11.8 KB
/
AdjacencyMapDigraph.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
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
package graphvisualizer.graph;
import java.util.*;
/**
* An adjacency map structure for a directed graph. A double map structure is used
* to represent the directed graph. The vertices are stored in the a map. The outgoing
* and incoming edges are stored in two maps of the corresponding vertex. The double
* map structure can be used since the vertices added are unique i.e. with unique
* <code>String</code> labels. This structure provides similar performance to an
* adjacency matrix where the {@link #getEdge(Vertex u, Vertex v)} method can achieve
* O(1) by performing lookup on the first and second map respectively.
*
* @param <V> Vertex type
* @param <E> Edge type
*/
public class AdjacencyMapDigraph<V, E> implements Graph<V, E> {
/**
* Concrete implementation of {@link Vertex}. A {@link DVertex} object stores
* a {@link V} element and its edges. Edges are implemented as {@link LinkedHashMap}
* to provide O(1) lookup and also maintain the insertion order.
*/
private class DVertex implements Vertex<V> {
private V element;
private Map<Vertex<V>, Edge<E, V>> outgoingEdges, incomingEdges;
public DVertex(V element) {
this.element = element;
outgoingEdges = new LinkedHashMap<>();
incomingEdges = new LinkedHashMap<>();
}
@Override
public V element() {
return element;
}
public Map<Vertex<V>, Edge<E, V>> getOutgoingEdges() {
return outgoingEdges;
}
public Map<Vertex<V>, Edge<E, V>> getIncomingEdges() {
return incomingEdges;
}
@Override
public String toString() {
return "Vertex{" + element + "}";
}
/*
2 DVertex objects are equals if their elements are equal.
Override hashCode() if override equals()
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DVertex vertex = (DVertex) o;
return element.equals(vertex.element);
}
@Override
public int hashCode() {
return Objects.hash(element);
}
}
/**
* Concrete implementation of {@link Edge}. A {@link DEdge} object stores
* an {@link E} element and its {@link V} end vertices.
*/
private class DEdge implements Edge<E, V> {
private E element;
private Vertex<V>[] endVertices;
public DEdge(Vertex<V> u, Vertex<V> v, E element) {
this.element = element;
this.endVertices = (Vertex<V>[]) new Vertex[]{u, v};
}
@Override
public E element() {
return element;
}
@Override
public Vertex<V>[] vertices() {
return endVertices;
}
@Override
public String toString() {
return "Edge from " + endVertices[0] + " to " + endVertices[1] + " with weight of " + element;
}
/*
2 DEdge objects are equals if their end vertices elements are equal.
Override hashCode() if override equals()
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DEdge edge = (DEdge) o;
return Arrays.equals(endVertices, edge.endVertices);
}
@Override
public int hashCode() {
return Arrays.hashCode(endVertices);
}
}
private Map<V, Vertex<V>> vertices;
private Set<Edge<E, V>> edges;
/*
LinkedHashMap is used to provide a map interface and maintain the insertion order of
vertices and elements. This also provides a faster iteration in the Java for-each loop
than HashMap.
*/
public AdjacencyMapDigraph() {
this.vertices = new LinkedHashMap<>();
this.edges = new LinkedHashSet<>();
}
public synchronized void clear() {
vertices.clear();
edges.clear();
}
@Override
public int numVertices() {
return vertices.size();
}
@Override
public int numEdges() {
return edges.size();
}
/*
synchronized methods are used to prevent thread interference since the graph visualization is
run using a non-javafx thread according to the author of JavaFX SmartGraph library.
*/
@Override
public synchronized Collection<Vertex<V>> vertices() {
return vertices.values();
}
@Override
public synchronized Collection<Edge<E, V>> edges() {
return edges;
}
@Override
public synchronized Collection<Edge<E, V>> incomingEdges(Vertex<V> v) throws InvalidVertexException {
DVertex vertex = validateVertex(v);
return vertex.getIncomingEdges().values();
}
@Override
public synchronized Collection<Edge<E, V>> outgoingEdges(Vertex<V> v) throws InvalidVertexException {
DVertex vertex = validateVertex(v);
return vertex.getOutgoingEdges().values();
}
@Override
public synchronized Vertex<V> opposite(Vertex<V> v, Edge<E, V> e) throws InvalidVertexException, InvalidEdgeException {
DVertex vertex = validateVertex(v);
DEdge edge = validateEdge(e);
Vertex<V>[] endVertices = edge.vertices();
if(endVertices[0].equals(vertex)) {
return endVertices[1];
}
else if (endVertices[1].equals(vertex)) {
return endVertices[0];
}
else {
throw new InvalidEdgeException("v is not incident to this edge.");
}
}
@Override
public synchronized Vertex<V> insertVertex(V element) throws InvalidVertexException {
if (vertices.containsKey(element)) {
throw new InvalidVertexException("A vertex with this element already exists.");
}
else {
DVertex vertex = new DVertex(element);
vertices.put(element, vertex);
return vertex;
}
}
@Override
public synchronized Edge<E, V> getEdge(Vertex<V> u, Vertex<V> v) throws InvalidVertexException {
DVertex startVertex = validateVertex(u);
return startVertex.getOutgoingEdges().get(v);
}
@Override
public synchronized Edge<E, V> insertEdge(Vertex<V> u, Vertex<V> v, E element) throws InvalidVertexException, InvalidEdgeException {
if(getEdge(u, v) == null) {
DVertex startVertex = validateVertex(u);
DVertex endVertex = validateVertex(v);
DEdge edge = new DEdge(startVertex, endVertex, element);
edges.add(edge);
startVertex.getOutgoingEdges().put(endVertex, edge);
endVertex.getIncomingEdges().put(startVertex, edge);
return edge;
}
else {
throw new InvalidEdgeException("Edge from u to v exists.");
}
}
@Override
public synchronized Edge<E, V> insertEdge(V uElement, V vElement, E eElement) throws InvalidVertexException, InvalidEdgeException {
DVertex startVertex = validateVertex(vertices.get(uElement));
DVertex endVertex = validateVertex(vertices.get(vElement));
if(getEdge(startVertex, endVertex) == null) {
DEdge edge = new DEdge(startVertex, endVertex, eElement);
edges.add(edge);
startVertex.getOutgoingEdges().put(endVertex, edge);
endVertex.getIncomingEdges().put(startVertex, edge);
return edge;
}
else {
throw new InvalidEdgeException("Edge from u to v exists.");
}
}
@Override
public synchronized V removeVertex(Vertex<V> v) throws InvalidVertexException {
DVertex vertex = validateVertex(v);
List<Edge<E, V>> removedEdges = new LinkedList<>();
removedEdges.addAll(vertex.getIncomingEdges().values());
removedEdges.addAll(vertex.getOutgoingEdges().values());
for (Edge<E, V> edge : removedEdges) {
removeEdge(edge);
}
V element = v.element();
vertices.remove(v.element());
return element;
}
@Override
public synchronized E removeEdge(Edge<E, V> e) throws InvalidEdgeException {
DEdge edge = validateEdge(e);
Vertex<V>[] endVertices = edge.vertices();
DVertex startVertex = validateVertex(endVertices[0]);
DVertex endVertex = validateVertex(endVertices[1]);
startVertex.getOutgoingEdges().remove(endVertex);
endVertex.getIncomingEdges().remove(startVertex);
E element = edge.element();
edges.remove(edge);
return element;
}
public synchronized String generateRandomEdge(E randomElement) {
StringBuilder sb = new StringBuilder();
if(numEdges() == numVertices()*(numVertices() - 1)) //maximum no. of edges in digraph is n(n - 1)
return sb.append("Graph has maximum number of edges.\n").toString();
Random random;
List<V> randomVertices = new ArrayList<>(vertices.keySet());
V randomVertex;
DVertex startVertex, endVertex;
for ( ; ;) { //continue to generate a new random edge between random vertices if invalid
random = new Random();
randomVertex = randomVertices.get(random.nextInt(randomVertices.size()));
startVertex = validateVertex(vertices.get(randomVertex));
random = new Random();
randomVertex = randomVertices.get(random.nextInt(randomVertices.size()));
endVertex = validateVertex(vertices.get(randomVertex));
if (startVertex.equals(endVertex)) //self-loop is not allowed, retry
continue;
else if (getEdge(startVertex, endVertex) == null) { //a random edge from u to v does not exist
return sb.append(insertEdge(startVertex, endVertex, randomElement)).append(" is generated.\n").toString();
}
else if (getEdge(endVertex, startVertex) == null) { //a random edge from v to u does not exist
return sb.append(insertEdge(endVertex, startVertex, randomElement)).append(" is generated.\n").toString();
} //random edges exist between u and v, retry
}
}
/* validate that this vertex belongs to the graph */
private DVertex validateVertex(Vertex<V> v) throws InvalidVertexException {
if(v == null) throw new InvalidVertexException("Null vertex.");
DVertex vertex;
try {
vertex = (DVertex) v;
} catch (ClassCastException e) {
throw new InvalidVertexException("Not a vertex.");
}
if (!vertices.containsKey(vertex.element)) {
throw new InvalidVertexException("Vertex does not belong to this graph.");
}
return vertex;
}
/* validate that this edge belongs to the graph */
private DEdge validateEdge(Edge<E, V> e) throws InvalidEdgeException {
if(e == null) throw new InvalidEdgeException("Null edge.");
DEdge edge;
try {
edge = (DEdge) e;
} catch (ClassCastException ex) {
throw new InvalidVertexException("Not an adge.");
}
if (!edges.contains(edge)) {
throw new InvalidEdgeException("Edge does not belong to this graph.");
}
return edge;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(
String.format("[Graph with %d vertices and %d edges]\n", numVertices(), numEdges())
);
sb.append("--- Vertices: \n");
for (Vertex<V> v : vertices.values()) {
sb.append("\t").append(v.toString()).append("\n");
}
sb.append("\n--- Edges: \n");
for (Edge<E, V> e : edges) {
sb.append("\t").append(e.toString()).append("\n");
}
return sb.append("\n").toString();
}
}