-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
64 lines (57 loc) · 1.79 KB
/
Copy pathGraph.java
File metadata and controls
64 lines (57 loc) · 1.79 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
import java.util.*;
public class Graph {
GraphNode[] node;
int size;
public Graph(int n) {
size = n;
node = new GraphNode[size];
for(int i=0; i<n; i++) {
node[i] = new GraphNode(i);
}
}
public void addEdge(int from, int to, int w) {
node[from].adjList.add(node[to]);
node[to].adjList.add(node[from]);
node[from].weights.add(w);
node[to].weights.add(w);
}
public void print() {
for(int i=0; i<this.size; i++) {
System.out.print(i+": ");
node[i].print();
System.out.println();
}
}
public void reset() {
for(int i=0; i<size; i++) {
node[i].visited = false;
node[i].d = Integer.MAX_VALUE;
node[i].reset();
}
}
public void Dijkstra(int i) {
reset();
ArrayDeque<GraphNode> q = new ArrayDeque<GraphNode>();
q.add(node[i]);
node[i].visited = true;
node[i].d = 0;
node[i].Dijkstra(q);
}
public void ShortestPath(int s) {
Dijkstra(s);
if(node[size-1].d==Integer.MAX_VALUE) {
System.out.println("NO PATH EXIST");
}
else {
System.out.println();
System.out.println("*****************************************************************************************************************************************");
System.out.println("STATION CODE : "+(node[s].label+1));
System.out.println();
System.out.println("DESTINATION CODE : "+(node[size-1].label+1));
System.out.println();
System.out.println("MOST EFFICIENT PATH WILL COST : "+(node[size-1].d));
System.out.println("*****************************************************************************************************************************************");
//System.out.println("Shortest path from "+(node[s].label+1)+" to "+node[size-1].label+1+": "+node[size-1].d);
}
}
}