-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathDijkstraShortestPath.java
More file actions
83 lines (65 loc) · 2.45 KB
/
Copy pathDijkstraShortestPath.java
File metadata and controls
83 lines (65 loc) · 2.45 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
package util;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
public class DijkstraShortestPath {
// Code to find the shortest path from a source node using Dijkstra's algorithm
public static int[] dijkstraShortestPath(WeightedGraph graph, int source) {
// store all distances
int[] distance = new int[graph.getVertices().length];
Arrays.fill(distance, Integer.MAX_VALUE); // set all to infinity
PriorityQueue<GraphEdge> priorityQueue =
new PriorityQueue<>(Comparator.comparingInt(GraphEdge::getWeight));
distance[source] = 0;
priorityQueue.add(new GraphEdge(source, source, 0));
while (!priorityQueue.isEmpty()) {
GraphEdge currentEdge = priorityQueue.poll();
int destination = currentEdge.getDestination();
for (GraphEdge edge : graph.getVertices()[destination]) {
int newDistance = distance[destination] + edge.getWeight();
if (newDistance < distance[edge.getDestination()]) {
distance[edge.getDestination()] = newDistance;
priorityQueue.add(new GraphEdge(edge.getSource(), edge.getDestination(), newDistance));
}
}
}
return distance;
}
/*
* Function takes a weighted graph as a parameter.
*
*/
public static void main(String[] args) {
// Creating a graph that has 6 vertices
// 4
// 0-------- -----------3-------
// | \ / 3 \ 2
// | \ / 6 \
// | 4 2 ---------------------- 4
// | / \ /
// | / \ 1 / 3
// 1-------- ---------5---------
// 2
WeightedGraph graph = new WeightedGraph(6);
graph.addUndirectedEdge(0,1,4);
graph.addUndirectedEdge(0,2,4);
graph.addUndirectedEdge(1,2,2);
graph.addUndirectedEdge(2,3,3);
graph.addUndirectedEdge(2,4,6);
graph.addUndirectedEdge(2,5,1);
graph.addUndirectedEdge(3,4,2);
graph.addUndirectedEdge(5,4,3);
// Code to print the graph
List<GraphEdge>[] vertices = graph.getVertices();
for (int i = 0; i < vertices.length; i++) {
System.out.print("Vertex " + i + " is connected to: ");
for (GraphEdge edge : vertices[i])
System.out.print(edge.getDestination() + " ");
System.out.println();
}
for (int i : dijkstraShortestPath(graph, 0)) {
System.out.println(i);
}
}
}