You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+131Lines changed: 131 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,4 +66,135 @@ Output:
66
66
`Sorted array:
67
67
11 12 22 25 34 64 90`
68
68
69
+
### Dijkstra’s shortest path algorithm :
69
70
71
+
iven a graph and a source vertex in the graph, find shortest paths from source to all vertices in the given graph.
72
+
73
+
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like Prim’s MST, we generate a SPT (shortest path tree) with given source as root. We maintain two sets, one set contains vertices included in shortest path tree, other set includes vertices not yet included in shortest path tree. At every step of the algorithm, we find a vertex which is in the other set (set of not yet included) and has a minimum distance from the source.
74
+
75
+
Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from a single source vertex to all other vertices in the given graph.
76
+
Algorithm
77
+
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.
78
+
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
79
+
3) While sptSet doesn’t include all vertices
80
+
….a) Pick a vertex u which is not there in sptSet and has minimum distance value.
81
+
….b) Include u to sptSet.
82
+
….c) Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v.
83
+
84
+
`
85
+
import java.util.*;
86
+
import java.lang.*;
87
+
import java.io.*;
88
+
89
+
class ShortestPath
90
+
{
91
+
// A utility function to find the vertex with minimum distance value,
92
+
// from the set of vertices not yet included in shortest path tree
93
+
static final int V=9;
94
+
int minDistance(int dist[], Boolean sptSet[])
95
+
{
96
+
// Initialize min value
97
+
int min = Integer.MAX_VALUE, min_index=-1;
98
+
99
+
for (int v = 0; v < V; v++)
100
+
if (sptSet[v] == false && dist[v] <= min)
101
+
{
102
+
min = dist[v];
103
+
min_index = v;
104
+
}
105
+
106
+
return min_index;
107
+
}
108
+
109
+
// A utility function to print the constructed distance array
110
+
void printSolution(int dist[], int n)
111
+
{
112
+
System.out.println("Vertex Distance from Source");
113
+
for (int i = 0; i < V; i++)
114
+
System.out.println(i+" tt "+dist[i]);
115
+
}
116
+
117
+
// Funtion that implements Dijkstra's single source shortest path
118
+
// algorithm for a graph represented using adjacency matrix
119
+
// representation
120
+
void dijkstra(int graph[][], int src)
121
+
{
122
+
int dist[] = new int[V]; // The output array. dist[i] will hold
123
+
// the shortest distance from src to i
124
+
125
+
// sptSet[i] will true if vertex i is included in shortest
126
+
// path tree or shortest distance from src to i is finalized
127
+
Boolean sptSet[] = new Boolean[V];
128
+
129
+
// Initialize all distances as INFINITE and stpSet[] as false
130
+
for (int i = 0; i < V; i++)
131
+
{
132
+
dist[i] = Integer.MAX_VALUE;
133
+
sptSet[i] = false;
134
+
}
135
+
136
+
// Distance of source vertex from itself is always 0
137
+
dist[src] = 0;
138
+
139
+
// Find shortest path for all vertices
140
+
for (int count = 0; count < V-1; count++)
141
+
{
142
+
// Pick the minimum distance vertex from the set of vertices
143
+
// not yet processed. u is always equal to src in first
144
+
// iteration.
145
+
int u = minDistance(dist, sptSet);
146
+
147
+
// Mark the picked vertex as processed
148
+
sptSet[u] = true;
149
+
150
+
// Update dist value of the adjacent vertices of the
151
+
// picked vertex.
152
+
for (int v = 0; v < V; v++)
153
+
154
+
// Update dist[v] only if is not in sptSet, there is an
155
+
// edge from u to v, and total weight of path from src to
156
+
// v through u is smaller than current value of dist[v]
157
+
if (!sptSet[v] && graph[u][v]!=0 &&
158
+
dist[u] != Integer.MAX_VALUE &&
159
+
dist[u]+graph[u][v] < dist[v])
160
+
dist[v] = dist[u] + graph[u][v];
161
+
}
162
+
163
+
// print the constructed distance array
164
+
printSolution(dist, V);
165
+
}
166
+
167
+
// Driver method
168
+
public static void main (String[] args)
169
+
{
170
+
/* Let us create the example graph discussed above */
171
+
int graph[][] = new int[][]{{0, 4, 0, 0, 0, 0, 0, 8, 0},
0 commit comments