-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstras_algo.java
154 lines (126 loc) · 3.42 KB
/
Dijkstras_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
*
* @author pulkit4tech
*/
//Adjancy matrix solution
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Dijkstras_algo implements Runnable {
BufferedReader c;
PrintWriter pout;
// static long mod = 1000000007;
public void run() {
try {
c = new BufferedReader(new InputStreamReader(System.in));
pout = new PrintWriter(System.out, true);
solve();
pout.close();
} catch (Exception e) {
pout.close();
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
new Thread(new Dijkstras_algo()).start();
}
void solve() throws Exception {
dijkstras();
}
private void dijkstras() {
int V = 9;
Graph g = new Graph(V);
addEdge(0, 1, 4);
addEdge(0, 7, 8);
addEdge(1, 2, 8);
addEdge(1, 7, 11);
addEdge(2, 3, 7);
addEdge(2, 8, 2);
addEdge(2, 5, 4);
addEdge(3, 4, 9);
addEdge(3, 5, 14);
addEdge(4, 5, 10);
addEdge(5, 6, 2);
addEdge(6, 7, 1);
addEdge(6, 8, 6);
addEdge(7, 8, 7);
dijkstra_algo(g, 0);
}
boolean adjmatrix[][];
long distance[][];
class Heap{
int vertex;
long dist;
}
PriorityQueue<Heap> getPriorityQueue(int v){
PriorityQueue<Heap> pq = new PriorityQueue<>(new Comparator<Heap>() {
@Override
public int compare(Heap o1, Heap o2) {
// TODO Auto-generated method stub
if(o1.dist>o2.dist)
return 1;
else if(o1.dist==o2.dist)
return 0;
else
return -1;
}
});
return pq;
}
void dijkstra_algo(Graph g,int src){
int V = g.V;
long dist [] = new long [V];
boolean check[] = new boolean[V];
Arrays.fill(dist, Long.MAX_VALUE);
dist[src] = 0;
PriorityQueue<Heap> heap = getPriorityQueue(V);
for(int v=0;v<V;v++){
Heap hp = new Heap();
hp.vertex = v;
hp.dist = dist[v];
heap.add(hp);
}
while(!heap.isEmpty()){
//extract vertex with min distance
Heap min = heap.poll();
check[min.vertex] = true;
//get adjacent vertex
PriorityQueue<Heap> tempheap = getPriorityQueue(heap.size());
while(!heap.isEmpty()){
Heap temp = heap.poll();
if(!check[temp.vertex]&&adjmatrix[min.vertex][temp.vertex]&&
distance[min.vertex][temp.vertex]+dist[min.vertex]<dist[temp.vertex]){
dist[temp.vertex] = dist[min.vertex] + distance[min.vertex][temp.vertex];
temp.dist = dist[temp.vertex];
}
tempheap.add(temp);
}
heap = tempheap;
}
printArr(dist,V);
}
void printArr(long dist[], int n)
{
pout.println("Vertex Distance from Source");
for (int i = 0; i < n; ++i)
pout.print(i+" \t\t "+dist[i]+"\n");
}
class Graph{
int V;
public Graph(int V) {
adjmatrix = new boolean[V][V];
distance = new long[V][V];
this.V = V;
}
}
void addEdge(int src,int dest,int wt){
adjmatrix[src][dest] = true;
adjmatrix[dest][src] = true;
distance[src][dest] = wt;
distance[dest][src] = wt;
}
}