Skip to content

Commit 1d9bfdd

Browse files
Dijkstra's single source shortest path
Like Prim’s MST, generate a SPT (shortest path tree) with a given source as a root. Maintain two sets, one set contains vertices included in the shortest-path tree, other set includes vertices not yet included in the shortest-path tree. At every step of the algorithm, find a vertex that is in the other set (set not yet included) and has a minimum distance from the source.
1 parent cc987bb commit 1d9bfdd

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// C++ program for Dijkstra's single source shortest path
2+
// algorithm. The program is for adjacency matrix
3+
// representation of the graph
4+
/*
5+
Description
6+
7+
//Like Prim’s MST, generate a SPT (shortest path tree) with a given source as a root.
8+
//Maintain two sets, one set contains vertices included in the shortest-path tree, other set includes vertices not yet included in the shortest-path tree.
9+
//At every step of the algorithm, find a vertex that is in the other set (set not yet included) and has a minimum distance from the source.
10+
*/
11+
#include <iostream>
12+
using namespace std;
13+
#include <limits.h>
14+
15+
// Number of vertices in the graph
16+
#define V 9
17+
18+
// A utility function to find the vertex with minimum
19+
// distance value, from the set of vertices not yet included
20+
// in shortest path tree
21+
int minDistance(int dist[], bool sptSet[])
22+
{
23+
24+
// Initialize min value
25+
int min = INT_MAX, min_index;
26+
27+
for (int v = 0; v < V; v++)
28+
if (sptSet[v] == false && dist[v] <= min)
29+
min = dist[v], min_index = v;
30+
31+
return min_index;
32+
}
33+
34+
// A utility function to print the constructed distance
35+
// array
36+
void printSolution(int dist[])
37+
{
38+
cout << "Vertex \t Distance from Source" << endl;
39+
for (int i = 0; i < V; i++)
40+
cout << i << " \t\t\t\t" << dist[i] << endl;
41+
}
42+
43+
// Function that implements Dijkstra's single source
44+
// shortest path algorithm for a graph represented using
45+
// adjacency matrix representation
46+
void dijkstra(int graph[V][V], int src)
47+
{
48+
int dist[V]; // The output array. dist[i] will hold the
49+
// shortest
50+
// distance from src to i
51+
52+
bool sptSet[V]; // sptSet[i] will be true if vertex i is
53+
// included in shortest
54+
// path tree or shortest distance from src to i is
55+
// finalized
56+
57+
// Initialize all distances as INFINITE and stpSet[] as
58+
// false
59+
for (int i = 0; i < V; i++)
60+
dist[i] = INT_MAX, sptSet[i] = false;
61+
62+
// Distance of source vertex from itself is always 0
63+
dist[src] = 0;
64+
65+
// Find shortest path for all vertices
66+
for (int count = 0; count < V - 1; count++) {
67+
// Pick the minimum distance vertex from the set of
68+
// vertices not yet processed. u is always equal to
69+
// src in the first iteration.
70+
int u = minDistance(dist, sptSet);
71+
72+
// Mark the picked vertex as processed
73+
sptSet[u] = true;
74+
75+
// Update dist value of the adjacent vertices of the
76+
// picked vertex.
77+
for (int v = 0; v < V; v++)
78+
79+
// Update dist[v] only if is not in sptSet,
80+
// there is an edge from u to v, and total
81+
// weight of path from src to v through u is
82+
// smaller than current value of dist[v]
83+
if (!sptSet[v] && graph[u][v]
84+
&& dist[u] != INT_MAX
85+
&& dist[u] + graph[u][v] < dist[v])
86+
dist[v] = dist[u] + graph[u][v];
87+
}
88+
89+
// print the constructed distance array
90+
printSolution(dist);
91+
}
92+
93+
// driver's code
94+
int main()
95+
{
96+
97+
/* Let us create the example graph discussed above */
98+
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
99+
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
100+
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
101+
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
102+
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
103+
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
104+
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
105+
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
106+
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
107+
108+
// Function call
109+
dijkstra(graph, 0);
110+
111+
return 0;
112+
}
113+
114+
//Time Complexity: O(V2)
115+
//Auxiliary Space: O(V)

0 commit comments

Comments
 (0)