-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDijkstra.java
More file actions
240 lines (202 loc) · 8.07 KB
/
Copy pathDijkstra.java
File metadata and controls
240 lines (202 loc) · 8.07 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
package algorithms.shortestpath;
import java.util.*;
/**
*
* https://java2blog.com/dijkstra-java/
*
*/
public class Dijkstra {
public static void main(String[] args) {
directedExample();
undirectedExample();
}
/**
* 6
* (E) ---------------- (B)
* / | \ |
* 10 / | \ 21 | 7
* / | \ |
* (F) | 11 (A) (C)
* \ | /
* 7 \ | / 18
* \ | /
* (D)
*
*/
private static void undirectedExample() {
Node nodeA = new Node("A");
Node nodeB = new Node("B");
Node nodeC = new Node("C");
Node nodeD = new Node("D");
Node nodeE = new Node("E");
Node nodeF = new Node("F");
nodeA.addNeighbour(new Edge(18, nodeA, nodeD));
nodeA.addNeighbour(new Edge(21, nodeA, nodeE));
nodeB.addNeighbour(new Edge(7, nodeB, nodeC));
nodeB.addNeighbour(new Edge(6, nodeB, nodeE));
nodeC.addNeighbour(new Edge(7, nodeC, nodeB)); // B-C
nodeD.addNeighbour(new Edge(18, nodeD, nodeA)); // A-D
nodeD.addNeighbour(new Edge(11, nodeD, nodeE));
nodeD.addNeighbour(new Edge(7, nodeD, nodeF));
nodeE.addNeighbour(new Edge(21, nodeE, nodeA)); // A-E
nodeE.addNeighbour(new Edge(6, nodeE, nodeB)); // B-E
nodeE.addNeighbour(new Edge(11, nodeE, nodeD)); // D-E
nodeE.addNeighbour(new Edge(10, nodeE, nodeF));
nodeF.addNeighbour(new Edge(7, nodeF, nodeD)); // D-F
nodeF.addNeighbour(new Edge(10, nodeF, nodeE)); // E-F
Dijkstra shortestPath = new Dijkstra();
shortestPath.computeShortestPaths(nodeA);
System.out.println("from A to B, Shortest Path is : "+shortestPath.getShortestPathTo(nodeB) + " and min. distance is : "+nodeB.getTotalWeight());
System.out.println("from A to C, Shortest Path is : "+shortestPath.getShortestPathTo(nodeC) + " and min. distance is : "+nodeC.getTotalWeight());
System.out.println("from A to D, Shortest Path is : "+shortestPath.getShortestPathTo(nodeD) + " and min. distance is : "+nodeD.getTotalWeight());
System.out.println("from A to E, Shortest Path is : "+shortestPath.getShortestPathTo(nodeE) + " and min. distance is : "+nodeE.getTotalWeight());
System.out.println("from A to F, Shortest Path is : "+shortestPath.getShortestPathTo(nodeF) + " and min. distance is : "+nodeF.getTotalWeight());
}
/**
*
* 10
* (A) -------> (C)
* | / | \
* | / | \
* 17 | 5 / 9 | \ 11
* | / | \
* ˅ ˅ ˅ ˅
* (B) -------> (D) ---> (E)
* 17 6
*
*/
private static void directedExample() {
Node nodeA = new Node("A");
Node nodeB = new Node("B");
Node nodeC = new Node("C");
Node nodeD = new Node("D");
Node nodeE = new Node("E");
nodeA.addNeighbour(new Edge(10, nodeA, nodeC));
nodeA.addNeighbour(new Edge(17, nodeA, nodeB));
nodeC.addNeighbour(new Edge(5, nodeC, nodeB));
nodeC.addNeighbour(new Edge(9, nodeC, nodeD));
nodeC.addNeighbour(new Edge(11, nodeC, nodeE));
nodeB.addNeighbour(new Edge(1, nodeB, nodeD));
nodeD.addNeighbour(new Edge(6, nodeD, nodeE));
Dijkstra shortestPath = new Dijkstra();
shortestPath.computeShortestPaths(nodeA);
System.out.println("from A to B, Shortest Path is : "+shortestPath.getShortestPathTo(nodeB) + " and min. distance is : "+nodeB.getTotalWeight());
System.out.println("from A to C, Shortest Path is : "+shortestPath.getShortestPathTo(nodeC) + " and min. distance is : "+nodeC.getTotalWeight());
System.out.println("from A to D, Shortest Path is : "+shortestPath.getShortestPathTo(nodeD) + " and min. distance is : "+nodeD.getTotalWeight());
System.out.println("from A to E, Shortest Path is : "+shortestPath.getShortestPathTo(nodeE) + " and min. distance is : "+nodeE.getTotalWeight());
}
public void computeShortestPaths(Node sourceNode) {
sourceNode.setTotalWeight(0);
Queue<Node> pq = new PriorityQueue<>();
pq.add(sourceNode);
sourceNode.setVisited(true);
while ( ! pq.isEmpty()) {
Node currentNode = pq.poll();
for (Edge edge : currentNode.getAdjacencyList()) {
Node destinationNode = edge.getDestinationNode();
if ( ! destinationNode.isVisited()) {
double currTotalWeight = currentNode.getTotalWeight() + edge.getWeight();
// edge relaxation
if (currTotalWeight < destinationNode.getTotalWeight()) {
pq.remove(destinationNode);
destinationNode.setTotalWeight(currTotalWeight);
destinationNode.setPrevNode(currentNode);
pq.add(destinationNode);
}
}
}
currentNode.setVisited(true);
}
}
public List<Node> getShortestPathTo(Node destinationNode) {
List<Node> path = new ArrayList<>();
for (Node node=destinationNode; node != null; node = node.getPrevNode()) {
path.add(node);
}
Collections.reverse(path);
return path;
}
public static class Node implements Comparable<Node> {
private String name;
private List<Edge> adjacencyList;
private boolean visited;
private Node prevNode;
private double totalWeight;
public Node(String name) {
this.name = name;
this.adjacencyList = new ArrayList<>();
this.totalWeight = Double.MAX_VALUE;
}
public void addNeighbour (Edge edge) {
this.adjacencyList.add(edge);
}
@Override
public String toString() {
return this.name;
}
@Override
public int compareTo(Node otherNode) {
// returns -1 when this.totalWeight < otherNode.getTotalWeight()
// returns 1 when this.totalWeight > otherNode.getTotalWeight()
return Double.compare(this.totalWeight, otherNode.getTotalWeight());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Edge> getAdjacencyList() {
return adjacencyList;
}
public void setAdjacencyList(List<Edge> adjacencyList) {
this.adjacencyList = adjacencyList;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public Node getPrevNode() {
return prevNode;
}
public void setPrevNode(Node prevNode) {
this.prevNode = prevNode;
}
public double getTotalWeight() {
return totalWeight;
}
public void setTotalWeight(double totalWeight) {
this.totalWeight = totalWeight;
}
}
public static class Edge {
private double weight;
private Node sourceNode;
private Node destinationNode;
public Edge(double weight, Node sourceNode, Node destinationNode) {
this.weight = weight;
this.sourceNode = sourceNode;
this.destinationNode = destinationNode;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Node getSourceNode() {
return sourceNode;
}
public void setSourceNode(Node sourceNode) {
this.sourceNode = sourceNode;
}
public Node getDestinationNode() {
return destinationNode;
}
public void setDestinationNode(Node destinationNode) {
this.destinationNode = destinationNode;
}
}
}