-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra_algo.java
103 lines (101 loc) · 3.55 KB
/
Dijkstra_algo.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.company;
import java.util.*;
public class Dijkstra_algo {
void print_path(Map<Integer,Integer> parent,int source,int dest,ArrayList<Integer> weights){
System.out.println("From "+source+" to "+dest);
Stack<Integer> s = new Stack<>();
int end=0;
while (end!=-10){
end = parent.get(dest);
if(end!=-10){
s.push(end);
s.push(dest);
}
dest = end;
}
int dest1;
while (!s.isEmpty()){
dest1 = s.pop();
end = s.pop();
System.out.println(end+"->"+dest1);
}
int sum=0;
for(int i = 0; i < weights.size(); i++)
sum += weights.get(i);
System.out.println("Weight of path = "+sum+"\n");
}
void dijkstra_algo(int G[][],int s){
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
Map<Integer,Integer> parent = new HashMap<>();
ArrayList<Integer> chain = new ArrayList<>();
HashMap<Integer,Integer> dist = new HashMap<>();
HashMap<Integer,Integer> values = new HashMap<>();
HashMap<Integer,Integer> edges = new HashMap<>();
HashMap<Integer,Integer> path_val = new HashMap<>();
int index = s;
System.out.println("Shortest path :\n");
for (int i=0;i<G.length;i++)
{
if(i==s){
dist.put(s,0);
edges.put(i,0);
continue;
}
edges.put(i,0);
dist.put(i,Integer.MAX_VALUE);
}
parent.put(s,-10);
while (edges.size()!=0){
if(index==-10){
index = edges.get(0);
s=index;
edges.clear();
dist.clear();
for (int i=0;i<G.length;i++)
{
if(i==s){
dist.put(s,0);
edges.put(i,0);
continue;
}
edges.put(i,0);
dist.put(i,Integer.MAX_VALUE);
}
System.out.printf("The program will restart from node (%d) as it hava no in degree \n\n",index);
chain.clear();
values.clear();
parent.clear();
parent.put(s,-10);
path_val.clear();
}
for (int j=0;j<G.length;j++){
if(G[index][j]!=0&&edges.containsKey(j)&&j!=index){
if(dist.get(j)>=dist.get(index)+G[index][j]){
dist.put(j,(dist.get(index)+G[index][j]));
priorityQueue.add(dist.get(j));
parent.put(j,index);
values.put(dist.get(j),j);
path_val.put(dist.get(j),G[index][j]);
}
}
}
if(priorityQueue.isEmpty()){
edges.remove(index);
index = parent.get(index);
if(chain.size()!=0)
chain.remove(chain.size()-1);
continue;
}
edges.remove(index);
int min = priorityQueue.poll();
int source =index;
chain.add(path_val.get(min));
index=values.get(min);
path_val.clear();
values.remove(min);
print_path(parent,s,index,chain);
edges.remove(source);
priorityQueue.clear();
}
}
}