forked from effy17/matala1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDikjstraAlgorithm.java
More file actions
398 lines (338 loc) · 9.8 KB
/
Copy pathDikjstraAlgorithm.java
File metadata and controls
398 lines (338 loc) · 9.8 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
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
package ex;
import java.io.File;
import java.util.Scanner;
public class DikjstraAlgorithm {
public static void main(String[] args) {
Graph h=new Graph();
//largeEWD
long startTime = System.currentTimeMillis();
// ... do something ...
//h.readgraph("C:\\Users\\igal\\Desktop\\largeEWD.txt");
h.readTest("tinyEWD.txt","test1.txt");
// h.readgraph("mediumEWD.txt");
//h.readTest2("test1.txt");
//h.findShortestPaths(0, 7);
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println(estimatedTime);
}
public static class Graph {
static Vertex[] vertices;
static double maxSize=10;
static double maxSize1=10;
int size;
static Graph k;
public Graph() {
this.maxSize = maxSize;
vertices = new Vertex[(int)maxSize];
}
public void addVertex(int name) {
vertices[size++] = new Vertex(name);
}
public void addEdge(int sourceName, int destinationName, double weight) {
int srcIndex = sourceName;
int destiIndex = destinationName;
vertices[srcIndex].adj = new Neighbour(destiIndex, weight, vertices[srcIndex].adj);
vertices[destiIndex].indegree++;
//System.out.println();
}
public void findShortestPaths(int sourceName,int dest){
applyDikjstraAlgorith(vertices[sourceName]);
/*for(int i = 0; i < maxSize; i++){
System.out.println("Distance of "+vertices[i].name+" from Source: "+ vertices[i].cost);
}*/
System.out.println("Distance from "+vertices[sourceName].name+" to edge "+vertices[dest].name+" is:"+ vertices[dest].cost);
System.out.println(vertices[dest].put);
}
/* public void findShortestPaths2(int sourceName,int dest,int [] a){
applyDikjstraAlgorith2(vertices[sourceName], a);
for(int i = 0; i < maxSize; i++){
System.out.println("Distance of "+vertices[i].name+" from Source: "+ vertices[i].cost);
}
System.out.println("Distance from "+vertices[sourceName].name+" to edge "+vertices[dest].name+" is:"+ vertices[dest].cost);
}*/
public static void readgraph(String files){
try {
File file = new File(files);
Scanner input = new Scanner(file);
maxSize = input.nextDouble();
k=new Graph();
for(int i=0;i<maxSize;i++){
k.addVertex(i);
}
double edges=input.nextDouble();
while (input.hasNextDouble()) {
int srcIndex = (int)input.nextDouble();
int destiIndex = (int)input.nextDouble();
double c=input.nextDouble();
k.addEdge(srcIndex, destiIndex, c);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void readgraph2(String files,int [] a){
try {
File file = new File(files);
Scanner input = new Scanner(file);
maxSize = input.nextDouble();
k=new Graph();
for(int i=0;i<maxSize;i++){
k.addVertex(i);
}
double edges=input.nextDouble();
while (input.hasNextDouble()) {
int srcIndex = (int)input.nextDouble();
int destiIndex = (int)input.nextDouble();
double c=input.nextDouble();
if(!contns(srcIndex,destiIndex, a))
k.addEdge(srcIndex, destiIndex, c);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static boolean contns(int srcIndex, int destiIndex, int[] a) {
for (int i = 0; i < a.length; i++) {
if(srcIndex==a[i]||destiIndex==a[i]) return true;
}
return false;
}
public void readTest(String files,String test){
try {
File file1 = new File(test);
Scanner input = new Scanner(file1);
//maxSize = input.nextDouble();
int max = (int) input.nextDouble();
for (int i = 0; i < max; i++) {
int srcIndex = (int)input.nextDouble();
int destiIndex = (int)input.nextDouble();
int c=(int)input.nextDouble();
if(c>0){
int a []= new int [c];
for (int j = 0; j < c; j++) {
a[j]=(int)input.nextDouble();
}
readgraph2(files,a);
findShortestPaths(srcIndex,destiIndex);
}
else{
readgraph(files);
findShortestPaths(srcIndex,destiIndex);
}
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/* public void readTest2(String test){
try {
File file1 = new File(test);
Scanner input = new Scanner(file1);
//maxSize = input.nextDouble();
int max = (int) input.nextDouble();
for (int i = 0; i < max; i++) {
int srcIndex = (int)input.nextDouble();
int destiIndex = (int)input.nextDouble();
int c=(int)input.nextDouble();
if(c>0){
int a []= new int [c];
for (int j = 0; j < c; j++) {
a[j]=(int)input.nextDouble();
}
findShortestPaths2(srcIndex,destiIndex,a);
}
else{
findShortestPaths(srcIndex,destiIndex);
}
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}*/
public class Vertex {
double cost;
String put;
int name;
Neighbour adj;
int indegree;
State state;
public Vertex(int name) {
this.name = name;
cost = Integer.MAX_VALUE;
state = State.NEW;
put=name+"";
}
public int compareTo(Vertex v) {
if (this.cost == v.cost) {
return 0;
}
if (this.cost < v.cost) {
return -1;
}
return 1;
}
}
public enum State {
NEW, IN_Q, VISITED
}
public class Neighbour {
int index;
Neighbour next;
double weight;
Neighbour(int index, double weight, Neighbour next) {
this.index = index;
this.next = next;
this.weight = weight;
}
}
public void applyDikjstraAlgorith(Vertex src) {
Heap heap = new Heap((int)maxSize);
heap.add(src);
src.state = State.IN_Q;
src.cost = 0;
while (!heap.isEmpty()) {
Vertex u = heap.remove();
u.state = State.VISITED;
Neighbour temp = u.adj;
while (temp != null) {
if (vertices[temp.index].state == State.NEW) {
heap.add(vertices[temp.index]);
vertices[temp.index].state = State.IN_Q;
}
if (vertices[temp.index].cost > u.cost + temp.weight) {
vertices[temp.index].cost = (u.cost + temp.weight);
vertices[temp.index].put = (u.put +"->"+ vertices[temp.index].put);
heap.heapifyUP(vertices[temp.index]);
}
temp = temp.next;
}
}
}
/* public void applyDikjstraAlgorith2(Vertex src,int [] a) {
Heap heap = new Heap((int)maxSize);
heap.add(src);
src.state = State.IN_Q;
src.cost = 0;
while (!heap.isEmpty()) {
Vertex u = heap.remove();
u.state = State.VISITED;
Neighbour temp = u.adj;
while (temp != null) {
if(!contns(temp.index,a)){
if (vertices[temp.index].state == State.NEW) {
heap.add(vertices[temp.index]);
vertices[temp.index].state = State.IN_Q;
}
if (vertices[temp.index].cost > u.cost + temp.weight) {
vertices[temp.index].cost = (u.cost + temp.weight);
heap.heapifyUP(vertices[temp.index]);
}
}
temp = temp.next;
}
}
}
*/
private static boolean contns(int index, int[] a) {
for (int i = 0; i < a.length; i++) {
if(index==a[i]) return true;
}
return false;
}
public static class Heap {
private Vertex[] heap;
private int maxSize;
private int size;
public Heap(int maxSize) {
this.maxSize = maxSize;
heap = new Vertex[maxSize];
}
public void add(Vertex u) {
heap[size++] = u;
heapifyUP(size - 1);
}
public void heapifyUP(Vertex u) {
for (int i = 0; i < maxSize; i++) {
if (u == heap[i]) {
heapifyUP(i);
break;
}
}
}
public void heapifyUP(int position) {
int currentIndex = position;
Vertex currentItem = heap[currentIndex];
int parentIndex = (currentIndex - 1) / 2;
Vertex parentItem = heap[parentIndex];
while (currentItem.compareTo(parentItem) == -1) {
swap(currentIndex, parentIndex);
currentIndex = parentIndex;
if (currentIndex == 0) {
break;
}
currentItem = heap[currentIndex];
parentIndex = (currentIndex - 1) / 2;
parentItem = heap[parentIndex];
}
}
public Vertex remove() {
Vertex v = heap[0];
swap(0, size - 1);
heap[size - 1] = null;
size--;
heapifyDown(0);
return v;
}
public void heapifyDown(int postion) {
if (size == 1) {
return;
}
int currentIndex = postion;
Vertex currentItem = heap[currentIndex];
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
int childIndex;
if (heap[leftChildIndex] == null) {
return;
}
if (heap[rightChildIndex] == null) {
childIndex = leftChildIndex;
} else if (heap[rightChildIndex].compareTo(heap[leftChildIndex]) == -1) {
childIndex = rightChildIndex;
} else {
childIndex = leftChildIndex;
}
Vertex childItem = heap[childIndex];
while (currentItem.compareTo(childItem) == 1) {
swap(currentIndex, childIndex);
currentIndex = childIndex;
currentItem = heap[currentIndex];
leftChildIndex = 2 * currentIndex + 1;
rightChildIndex = 2 * currentIndex + 2;
if (heap[leftChildIndex] == null) {
return;
}
if (heap[rightChildIndex] == null) {
childIndex = leftChildIndex;
} else if (heap[rightChildIndex].compareTo(heap[leftChildIndex]) == -1) {
childIndex = rightChildIndex;
} else {
childIndex = leftChildIndex;
}
}
}
public void swap(int index1, int index2) {
Vertex temp = heap[index1];
heap[index1] = heap[index2];
heap[index2] = temp;
}
public boolean isEmpty() {
return size == 0;
}
}
}
}