-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathdijkstra_algorithm.java
79 lines (64 loc) · 2.2 KB
/
dijkstra_algorithm.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
import java.util.*;
class DijkstraAlgorithm {
private int V; // Number of vertices
private List<List<Node>> graph;
public DijkstraAlgorithm(int V) {
this.V = V;
graph = new ArrayList<>(V);
for (int i = 0; i < V; i++) {
graph.add(new ArrayList<>());
}
}
public void addEdge(int source, int destination, int weight) {
Node node = new Node(destination, weight);
graph.get(source).add(node);
}
public void dijkstra(int source) {
int[] distance = new int[V];
Arrays.fill(distance, Integer.MAX_VALUE);
distance[source] = 0;
PriorityQueue<Node> minHeap = new PriorityQueue<>(V, Comparator.comparingInt(a -> a.weight));
minHeap.add(new Node(source, 0));
while (!minHeap.isEmpty()) {
int u = minHeap.poll().node;
for (Node neighbor : graph.get(u)) {
int v = neighbor.node;
int weight = neighbor.weight;
if (distance[u] != Integer.MAX_VALUE && distance[u] + weight < distance[v]) {
distance[v] = distance[u] + weight;
minHeap.add(new Node(v, distance[v]));
}
}
}
printShortestPaths(distance, source);
}
private void printShortestPaths(int[] distance, int source) {
System.out.println("Shortest distances from source vertex " + source + ":");
for (int i = 0; i < V; i++) {
System.out.println("Vertex " + i + ": " + distance[i]);
}
}
public static void main(String[] args) {
int V = 6;
DijkstraAlgorithm graph = new DijkstraAlgorithm(V);
graph.addEdge(0, 1, 4);
graph.addEdge(0, 2, 2);
graph.addEdge(1, 3, 3);
graph.addEdge(1, 2, 5);
graph.addEdge(2, 3, 7);
graph.addEdge(3, 4, 2);
graph.addEdge(4, 0, 4);
graph.addEdge(4, 1, 4);
graph.addEdge(4, 5, 6);
int sourceVertex = 0;
graph.dijkstra(sourceVertex);
}
static class Node {
int node;
int weight;
public Node(int node, int weight) {
this.node = node;
this.weight = weight;
}
}
}