forked from akshat-o5/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortest_path.java
207 lines (89 loc) · 3.48 KB
/
Shortest_path.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.util.*;
import java.io.*;
import java.lang.*;
public class DijkstraExample
{
// A utility method to compute the vertex with the distance value, which is minimum
// from the group of vertices that has not been included yet
static final int totalVertex = 9;
int minimumDistance(int distance[], Boolean spSet[])
{
// Initialize min value
int m = Integer.MAX_VALUE, m_index = -1;
for (int vx = 0; vx < totalVertex; vx++)
{
if (spSet[vx] == false && distance[vx] <= m)
{
m = distance[vx];
m_index = vx;
}
}
return m_index;
}
// A utility method to display the built distance array
void printSolution(int distance[], int n)
{
System.out.println("The shortest Distance from source 0th node to all other nodes are: ");
for (int j = 0; j < n; j++)
System.out.println("To " + j + " the shortest distance is: " + distance[j]);
}
// method that does the implementation of Dijkstra's shortest path algorithm
// for a graph that is being represented using the adjacency matrix representation
void dijkstra(int graph[][], int s)
{
int distance[] = new int[totalVertex]; // The output array distance[i] holds the shortest distance from source s to j
// spSet[j] will be true if vertex j is included in the shortest
// path tree or the shortest distance from the source s to j is finalized
Boolean spSet[] = new Boolean[totalVertex];
// Initializing all of the distances as INFINITE
// and spSet[] as false
for (int j = 0; j < totalVertex; j++)
{
distance[j] = Integer.MAX_VALUE;
spSet[j] = false;
}
// Distance from the source vertex to itself is always 0
distance[s] = 0;
// compute the shortest path for all the given vertices
for (int cnt = 0; cnt < totalVertex - 1; cnt++)
{
// choose the minimum distance vertex from the set of vertices
// not yet processed. ux is always equal to source s in the first
// iteration.
int ux = minimumDistance(distance, spSet);
// the choosed vertex is marked as true
// it means it is processed
spSet[ux] = true;
// Updating the distance value of the neighboring vertices
// of the choosed vertex.
for (int vx = 0; vx < totalVertex; vx++)
// Update distance[vx] if and only if it is not in the spSet, there is an
// edge from ux to vx, and the total weight of path from source s to
// vx through ux is lesser than the current value of distance[vx]
if (!spSet[vx] && graph[ux][vx] != -1 && distance[ux] != Integer.MAX_VALUE && distance[ux] + graph[ux][vx] < distance[vx])
{
distance[vx] = distance[ux] + graph[ux][vx];
}
}
// display the build distance array
printSolution(distance, totalVertex);
}
// main method
public static void main(String argvs[])
{
// A 9 * 9 matrix is created.
// arr[x][y] = - 1 means, there is no any edge that connects the nodes x and y directly
int grph[][] = new int[][] { { -1, 3, -1, -1, -1, -1, -1, 7, -1 },
{ 3, -1, 7, -1, -1, -1, -1, 10, 4 },
{ -1, 7, -1, 6, -1, 2, -1, -1, 1 },
{ -1, -1, 6, -1, 8, 13, -1, -1, 3 },
{ -1, -1, -1, 8, -1, 9, -1, -1, -1 },
{ -1, -1, 2, 13, 9, -1, 4, -1, 5 },
{ -1, -1, -1, -1, -1, 4, -1, 2, 5 },
{ 7, 10, -1, -1, -1, -1, 2, -1, 6 },
{ -1, 4, 1, 3, -1, 5, 5, 6, -1 } };
// creating an object of the class DijkstraExample
DijkstraExample obj = new DijkstraExample();
obj.dijkstra(grph, 0);
}
}