-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfg.printShortestPath.adj.pq.iterative.java
78 lines (66 loc) · 2.62 KB
/
gfg.printShortestPath.adj.pq.iterative.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
class Pair{
int wt;
int des;
Pair(int des,int wt){
this.des=des;
this.wt=wt;
}
}
class Solution {
// instead of printing minimum distance from src to node we want path.
public List<Integer> shortestPath(int n, int m, int edges[][]) {
// if path exists then return list with {distance-to-n, ...path from 1 to n}
//else {-1}
// if we path actual shorted path then we have to just have to modify distance array
// In distance array we can store distance as well as parent (from where we got shorted path.)
ArrayList<ArrayList<Pair>> adj=new ArrayList<>();
for(int i=0;i<=n;i++){
adj.add(new ArrayList<>());
}
for(int i=0;i<m;i++){
int src=edges[i][0];
int des=edges[i][1];
int wt=edges[i][2];
adj.get(src).add(new Pair(des,wt));
adj.get(des).add(new Pair(src,wt));
}
PriorityQueue<Pair> pq=new PriorityQueue<>((a,b)->a.wt-b.wt);
int distance[][]=new int[n+1][2]; //{0->distance, 1->parent}
for(int i=0;i<=n;i++){
distance[i]=new int[]{(int)1e9,-1}; //initially distance is infinity and parent is -1.
}
distance[1]=new int[]{0,-1}; // our source node is 1. so mark distance as 0.
pq.add(new Pair(1,0)); //src & distance
while (!pq.isEmpty()){
Pair node=pq.poll();
for (Pair adjNode:adj.get(node.des)){
int distanceToNode=distance[node.des][0];
int wt=adjNode.wt;
int adjDistance=distance[adjNode.des][0];
if(distanceToNode+wt<adjDistance){
distance[adjNode.des][0]=distanceToNode+wt; //distance
distance[adjNode.des][1]=node.des; // parent
pq.add(new Pair(adjNode.des,distanceToNode+wt));
}
}
}
ArrayList<Integer> ans=new ArrayList<>();
if(distance[n][0]==(int)1e9){
// if final node's distance is 1e9 means it is unreachable
ans.add(-1);
return ans;
}
// we start with n.
// from n we go to its parent then parent's parent etc...
int element=n;
int i=0;
// i is used because path cannot contain more node than total nodes.
while (i<n && element!=-1){ // go till element become -1.
ans.add(0,element); // add node
element=distance[element][1]; // go to node's parent
i++;
}
ans.add(0,distance[n][0]); //add total distance in the beginning .
return ans;
}
}