-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphDirected.java
executable file
·351 lines (294 loc) · 11 KB
/
GraphDirected.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
351
package com.example.graph;
import com.example.arrayList.ArrayUnorderedList;
import com.example.arrayQueue.CircularArrayQueue;
import com.example.arrayStack.ArrayStack;
import com.example.exceptions.*;
import com.example.interfaces.*;
import com.example.list.LinkedOrderedList;
import com.example.queue.LinkedQueue;
import java.util.Iterator;
public class GraphDirected<T> implements GraphADT<T> {
protected static final int DEFAULT_CAPACITY = 100;
protected GraphVertex<T, Integer>[] vertices;
protected int numberOfVertices;
protected int numberOfEdges;
public GraphDirected(int capacity) {
this.vertices = (GraphVertex<T, Integer>[]) new GraphVertex[capacity];
this.numberOfVertices = 0;
this.numberOfEdges = 0;
}
public GraphDirected() {
this(DEFAULT_CAPACITY);
}
/**
* {@inheritDoc }
*/
@Override
public void addVertex(T vertex) throws ElementAlreadyExistsException {
if (exists(vertex)) {
throw new ElementAlreadyExistsException(ElementAlreadyExistsException.ELEMENT_ALREADY_EXISTS);
}
if (this.numberOfVertices == this.vertices.length) {
expandCapacity();
}
this.vertices[this.numberOfVertices++] = new GraphVertex<>(vertex);
}
/**
* {@inheritDoc }
*/
@Override
public void addEdge(T vertex1, T vertex2) throws ElementNotFoundException, VertexConnectionExeception {
int indexVertex1 = getIndex(vertex1);
int indexVertex2 = getIndex(vertex2);
if (!isIndexValid(indexVertex1) || !isIndexValid(indexVertex2)) {
throw new ElementNotFoundException(ElementNotFoundException.ELEMENT_NOT_FOUND);
}
if (isNeighbor(indexVertex1, indexVertex2)) {
throw new VertexConnectionExeception(VertexConnectionExeception.ALREADY_CONNECTED);
}
this.vertices[indexVertex1].neighbors.add(indexVertex2);
this.numberOfEdges++;
}
/**
* {@inheritDoc }
*/
@Override
public void removeVertex(T vertex) throws EmptyCollectionException, ElementNotFoundException {
int indexVertex;
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
indexVertex = getIndex(vertex);
if (!isIndexValid(indexVertex)) {
throw new ElementNotFoundException(ElementNotFoundException.ELEMENT_NOT_FOUND);
}
//removes the incident edges on the removed vertex
for (int i = 0; i < this.numberOfVertices; i++) {
try {
this.vertices[i].neighbors.remove(indexVertex);
this.numberOfEdges--; //decrement number of edges
} catch (ElementNotFoundException | EmptyCollectionException ignored) {
}
}
//Decrement the number of edges going from removed vertex
this.numberOfEdges -= this.vertices[indexVertex].neighbors.size();
shiftDelete(indexVertex);
this.numberOfVertices--;
}
/**
* {@inheritDoc }
*/
@Override
public void removeEdge(T vertex1, T vertex2) throws EmptyCollectionException, ElementNotFoundException, VertexConnectionExeception {
int indexVertex1;
int indexVertex2;
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
indexVertex1 = getIndex(vertex1);
indexVertex2 = getIndex(vertex2);
if (!isIndexValid(indexVertex1) || isIndexValid(indexVertex2)) {
throw new ElementNotFoundException(ElementNotFoundException.ELEMENT_NOT_FOUND);
}
if (!isNeighbor(indexVertex1, indexVertex2)) {
throw new VertexConnectionExeception(VertexConnectionExeception.NO_CONNECTION);
}
try {
this.vertices[indexVertex1].neighbors.remove(indexVertex2);
} catch (EmptyCollectionException ignored) {
}
this.numberOfEdges--;
}
/**
* {@inheritDoc }
*/
@Override
public Iterator<T> iteratorBFS(T startVertex) {
return BFSList(startVertex).iterator();
}
/**
* {@inheritDoc }
*/
@Override
public Iterator<T> iteratorDFS(T startVertex) {
int indexCurrent;
boolean found;
ArrayStack<Integer> traversalStack = new ArrayStack<>(this.numberOfVertices);
ArrayUnorderedList<T> resultList = new ArrayUnorderedList<>(this.numberOfVertices);
boolean[] visited = new boolean[this.numberOfVertices];
int indexVertex = getIndex(startVertex);
if (!isIndexValid(indexVertex)) {
return resultList.iterator();
}
traversalStack.push(indexVertex);
resultList.addToRear(this.vertices[indexVertex].element);
visited[indexVertex] = true;
while (!traversalStack.isEmpty()) {
try {
indexCurrent = traversalStack.peek();
found = false;
for (Integer neighbor : this.vertices[indexCurrent].neighbors) {
if (!visited[neighbor]) {
traversalStack.push(neighbor);
resultList.addToRear(this.vertices[neighbor].element);
visited[neighbor] = true;
found = true;
}
}
if (!found && !traversalStack.isEmpty()) {
traversalStack.pop();
}
} catch (EmptyCollectionException ignored) {
}
}
return resultList.iterator();
}
/**
* {@inheritDoc }
*/
@Override
public Iterator<T> iteratorShortestPath(T startVertex, T targetVertex) {
Integer indexCurrent;
boolean targetFound = false;
int[] predecessor = new int[this.numberOfVertices];
boolean[] visited = new boolean[this.numberOfVertices];
QueueADT<Integer> traversalQueue = new CircularArrayQueue<>(this.numberOfVertices);
UnorderedListADT<T> resultList = new ArrayUnorderedList<>(this.numberOfVertices);
int indexStart = getIndex(startVertex);
int indexTarget = getIndex(targetVertex);
if (!isIndexValid(indexStart) && !isIndexValid(indexTarget)) {
return resultList.iterator();
}
traversalQueue.enqueue(indexStart);
predecessor[indexStart] = -1;
visited[indexStart] = true;
while (!targetFound && !traversalQueue.isEmpty()) {
try {
indexCurrent = traversalQueue.dequeue();
for (int i = 0; i < this.numberOfVertices; i++) {
if (isNeighbor(indexCurrent, i) && !visited[i]) {
predecessor[i] = indexCurrent;
traversalQueue.enqueue(i);
visited[i] = true;
}
}
} catch (EmptyCollectionException ignored) {
}
}
int indexPrecedor = predecessor[indexTarget];
while (!(vertices[indexPrecedor].element.equals(startVertex))) {
resultList.addToFront(vertices[indexPrecedor].element);
indexPrecedor = predecessor[indexPrecedor];
}
resultList.addToRear(targetVertex);
resultList.addToFront(startVertex);
return resultList.iterator();
}
protected UnorderedListADT<T> BFSList(T startVertex) {
int indexStart = getIndex(startVertex);
QueueADT<Integer> traversalQueue = new LinkedQueue<>();
UnorderedListADT<T> resultList = new ArrayUnorderedList<>();
boolean[] visited = new boolean[this.numberOfVertices];
if (!isIndexValid(indexStart)) {
return resultList;
}
traversalQueue.enqueue(indexStart);
visited[indexStart] = true;
while (!traversalQueue.isEmpty()) {
try {
int index = traversalQueue.dequeue();
resultList.addToRear(this.vertices[index].element);
for (Integer neighbor : this.vertices[index].neighbors) {
if (!visited[neighbor]) {
traversalQueue.enqueue(neighbor);
visited[neighbor] = true;
}
}
} catch (EmptyCollectionException ignored) {
}
}
return resultList;
}
/**
* {@inheritDoc }
*/
@Override
public boolean isEmpty() {
return this.numberOfVertices == 0;
}
/**
* {@inheritDoc }
*/
@Override
public boolean isConnected() {
boolean isConnected = true;
int count = BFSList(this.vertices[0].element).size();
int i = 1;
while (isConnected && i < this.numberOfVertices) {
if (BFSList(this.vertices[i].element).size() != count) {
isConnected = false;
}
i++;
}
return isConnected;
}
/**
* {@inheritDoc }
*/
@Override
public int size() {
return this.numberOfVertices;
}
/**
* Makes a shift left in array of vertices, and fix the resulting invalid
* connections after the shift
*
* @param indexVertex start shift index on the left
*/
protected void shiftDelete(int indexVertex) {
for (int i = indexVertex; i < this.numberOfVertices - 1; i++) {
vertices[i] = vertices[i + 1];//shift vertex to the left
}
this.vertices[this.numberOfVertices - 1] = null;
for (int i = 0; i < this.numberOfVertices - 1; i++) {
Iterator<Integer> itrNeighbors = vertices[i].neighbors.iterator();//Save the connections
vertices[i].neighbors = new LinkedOrderedList<>();//Clear connections
while (itrNeighbors.hasNext()) {
int neighbor = itrNeighbors.next();
if (neighbor > indexVertex) {//if it is necessary to correct the connection
vertices[i].neighbors.add(neighbor - 1);
} else {
vertices[i].neighbors.add(neighbor);
}
}
}
}
protected int getIndex(T vertex) {
int i = 0;
int indexVertex = -1;
while (i < numberOfVertices && indexVertex == -1) {
if (vertices[i].element.equals(vertex)) {
indexVertex = i;
}
i++;
}
return indexVertex;
}
protected boolean exists(T vertex) {
return isIndexValid(getIndex(vertex));
}
protected boolean isIndexValid(int indexVertex) {
return indexVertex >= 0;
}
protected void expandCapacity() {
GraphVertex<T, Integer>[] temp = (GraphVertex<T, Integer>[]) new GraphVertex[this.vertices.length * 2];
System.arraycopy(this.vertices, 0, temp, 0, this.numberOfVertices);
this.vertices = temp;
}
protected boolean isNeighbor(T vertex1, T vertex2) {
return isNeighbor(getIndex(vertex1), getIndex(vertex2));
}
protected boolean isNeighbor(int vertex1, int vertex2) {
return vertices[vertex1].neighbors.contains(vertex2);
}
}