-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstraAlgorithm.java
More file actions
55 lines (45 loc) · 1.55 KB
/
DijkstraAlgorithm.java
File metadata and controls
55 lines (45 loc) · 1.55 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
package graph;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* Finds the <b>shortest distance</b> from the source to all vertices. <br>
* It is an example of Greedy Algorithm. <br>
* We use the concept of <b>Relaxation</b>. <br>
* For ex: dist[u] + wt < dist[v], then we update the value of dist[v]. <br>
* Fails when edges weight is <b>Negative.</b>
*/
public class DijkstraAlgorithm {
// Time Complexity - O(E + E * logV)
static int[] dijkstra(int V, ArrayList<ArrayList<Pair>> adj, int S) {
PriorityQueue<Pair> pq = new PriorityQueue<>(Comparator.comparingInt(p -> p.weight));
boolean[] isVisited = new boolean[V];
int[] dist = new int[V];
for (int i = 0; i < V; i++) {
if (i != S) dist[i] = Integer.MAX_VALUE;
}
pq.add(new Pair(S, 0));
while (!pq.isEmpty()) {
Pair p = pq.poll();
int curr = p.vertex;
if (!isVisited[curr]) {
isVisited[curr] = true;
for (Pair n : adj.get(curr)) {
// Relaxation.
if (dist[curr] + n.weight < dist[n.vertex])
dist[n.vertex] = dist[curr] + n.weight;
pq.add(new Pair(n.vertex, dist[n.vertex]));
}
}
}
return dist;
}
public static class Pair {
int vertex;
int weight;
public Pair(int vertex, int weight) {
this.vertex = vertex;
this.weight = weight;
}
}
}