-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkDirected.java
executable file
·405 lines (342 loc) · 13.5 KB
/
NetworkDirected.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package com.example.graph;
import com.example.arrayList.ArrayUnorderedList;
import com.example.arrayStack.ArrayStack;
import com.example.binaryTree.LinkedHeapMin;
import com.example.exceptions.*;
import com.example.interfaces.*;
import com.example.list.*;
import com.example.queue.LinkedQueue;
import java.util.Iterator;
/**
* @param <T>
* @author Micael
*/
public class NetworkDirected<T> implements NetworkADT<T> {
protected static final int DEFAULT_CAPACITY = 100;
protected static final int DEFAULT_WEIGHT = 1;
protected GraphVertex<T, GraphEdge>[] vertices;
protected int numberOfVertices;
protected int numberOfEdges;
public NetworkDirected(int capacity) {
this.vertices = (GraphVertex<T, GraphEdge>[]) new GraphVertex[capacity];
this.numberOfVertices = 0;
this.numberOfEdges = 0;
}
public NetworkDirected() {
this(DEFAULT_CAPACITY);
}
/**
* {@inheritDoc }
*/
@Override
public void addEdge(T vertex1, T vertex2, double weight) throws ElementNotFoundException, VertexConnectionExeception {
int indexVertex1;
int indexVertex2;
if (weight < 0) {
throw new VertexConnectionExeception(VertexConnectionExeception.NEGATIVE_WEIGHT);
}
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.ALREADY_CONNECTED);
}
this.vertices[indexVertex1].neighbors.add(new GraphEdge(indexVertex2, weight));
this.numberOfEdges++;
}
/**
* {@inheritDoc }
*/
@Override
public void addEdge(T vertex1, T vertex2) throws ElementNotFoundException, VertexConnectionExeception {
addEdge(vertex1, vertex2, DEFAULT_WEIGHT);
}
/**
* {@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 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);
}
//Remove connections from all vertex to the removed vertex
for (int i = 0; i < this.numberOfVertices; i++) {
try {
this.vertices[i].neighbors.remove(new GraphEdge(indexVertex, DEFAULT_WEIGHT));
this.numberOfEdges--;//Decrement number of edges
} catch (ElementNotFoundException | EmptyCollectionException ignored) {
}
}
//removes 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 {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
int indexVertex1 = getIndex(vertex1);
int indexVertex2 = getIndex(vertex2);
if (!isIndexValid(indexVertex1) || !isIndexValid(indexVertex2)) {
throw new ElementNotFoundException(ElementNotFoundException.ELEMENT_NOT_FOUND);
}
if (!NetworkDirected.this.isNeighbor(indexVertex1, indexVertex2)) {
throw new VertexConnectionExeception(VertexConnectionExeception.NO_CONNECTION);
}
try {
this.vertices[indexVertex1].neighbors.remove(new GraphEdge(indexVertex2, DEFAULT_WEIGHT));
} 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 (int i = 0; (i < this.numberOfVertices) && !found; i++) {
if (this.isNeighbor(indexCurrent, i) && !visited[i]) {
traversalStack.push(i);
resultList.addToRear(this.vertices[i].element);
visited[i] = true;
found = true;
}
}
if (!found && !traversalStack.isEmpty()) {
traversalStack.pop();
}
} catch (EmptyCollectionException ignored) {
}
}
return resultList.iterator();
}
/**
* Class for the shortest path algorithm
*/
protected class ShortestPath {
private double pathLenght;
ShortestPath() {
this.pathLenght = Double.POSITIVE_INFINITY;
}
UnorderedListADT<T> getPath(T startVertex, T targetVertex) {
int indexStart = getIndex(startVertex);
int indexTarget = getIndex(targetVertex);
boolean found = false;
GraphEdgePredecessor[] predecessors = new GraphEdgePredecessor[numberOfVertices];
HeapMinADT<GraphEdgePredecessor> minHeap = new LinkedHeapMin<>();
UnorderedListADT<T> resultList = new LinkedUnorderedList<>();
if (!isIndexValid(indexStart) || !isIndexValid(indexTarget) || indexStart == indexTarget) {
return resultList;
}
predecessors[indexStart] = new GraphEdgePredecessor(indexStart, 0, -1);
minHeap.addElement(predecessors[indexStart]);
while (!found && !minHeap.isEmpty()) {
try {
GraphEdgePredecessor currentVertex = minHeap.removeMin();
int indexCurrentVertex = currentVertex.indexVertex;
if (!currentVertex.visited) {
predecessors[indexCurrentVertex].visited = true;
for (GraphEdge neighbor : vertices[indexCurrentVertex].neighbors) {
int indexNeighbor = neighbor.indexVertex;
double costToNext = currentVertex.weight + neighbor.weight;
if (predecessors[indexNeighbor] == null) {
predecessors[indexNeighbor] = new GraphEdgePredecessor(indexNeighbor, Double.POSITIVE_INFINITY, indexCurrentVertex);
}
if (!predecessors[indexNeighbor].visited) {
minHeap.addElement(new GraphEdgePredecessor(indexNeighbor, costToNext, indexCurrentVertex));
if (costToNext < predecessors[indexNeighbor].weight) {
predecessors[indexNeighbor].indexPredecessor = indexCurrentVertex;
predecessors[indexNeighbor].weight = costToNext;
}
}
}
if (indexCurrentVertex == indexTarget) {
found = true;
}
}
} catch (EmptyCollectionException ignored) {
}
}
if (!found) {
return resultList;
}
int indexPrecedor = indexTarget;
while (indexPrecedor != -1) {
resultList.addToFront(vertices[indexPrecedor].element);
indexPrecedor = predecessors[indexPrecedor].indexPredecessor;
}
this.pathLenght = predecessors[indexTarget].weight;
return resultList;
}
double getPathWeight(T startVertex, T targetVertex) {
getPath(startVertex, targetVertex);
double path = pathLenght;
pathLenght = Double.POSITIVE_INFINITY;
return path;
}
}
/**
* {@inheritDoc }
*/
@Override
public double shortestPathWeight(T vertex1, T vertex2) {
return new ShortestPath().getPathWeight(vertex1, vertex2);
}
/**
* {@inheritDoc }
*/
@Override
public Iterator<T> iteratorShortestPath(T startVertex, T targetVertex) {
return new ShortestPath().getPath(startVertex, targetVertex).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 (GraphEdge neighbor : this.vertices[index].neighbors) {
int indexNeighbor = neighbor.indexVertex;
if (!visited[indexNeighbor]) {
traversalQueue.enqueue(indexNeighbor);
visited[indexNeighbor] = 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;
}
protected void shiftDelete(int indexVertex) {
for (int i = indexVertex; i < this.numberOfVertices - 1; i++) {
this.vertices[i] = this.vertices[i + 1];//shift vertex to the left
}
this.vertices[this.numberOfVertices - 1] = null;
for (int i = 0; i < this.numberOfVertices - 1; i++) {
Iterator<GraphEdge> itrNeighbors = vertices[i].neighbors.iterator();//Save the connections
vertices[i].neighbors = new LinkedOrderedList<>();//Clear connections
while (itrNeighbors.hasNext()) {
GraphEdge neighbor = itrNeighbors.next();
if (neighbor.indexVertex > indexVertex) {//if it is necessary to correct the connection
this.vertices[i].neighbors.add(new GraphEdge(neighbor.indexVertex - 1, neighbor.weight));
} else {
this.vertices[i].neighbors.add(neighbor);
}
}
}
}
protected int getIndex(T vertex) {
int i = 0;
int indexVertex = -1;
while (i < this.numberOfVertices && indexVertex == -1) {
if (this.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, GraphEdge>[] temp = (GraphVertex<T, GraphEdge>[]) 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 this.vertices[vertex1].neighbors.contains(new GraphEdge(vertex2, DEFAULT_WEIGHT));
}
}