
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C / C++ Program for Dijkstra's shortest path algorithm
We are given a graph with a source vertex in the graph. And we have to find the shortest path from the source vertex to all other vertices of the graph.
The Dijikstra’s algorithm is a greedy algorithm to find the shortest path from the source vertex of the graph to the root node of the graph.
Algorithm
Step 1 : Create a set shortPath to store vertices that come in the way of the shortest path tree. Step 2 : Initialize all distance values as INFINITE and assign distance values as 0 for source vertex so that it is picked first. Step 3 : Loop until all vertices of the graph are in the shortPath. Step 3.1 : Take a new vertex that is not visited and is nearest. Step 3.2 : Add this vertex to shortPath. Step 3.3 : For all adjacent vertices of this vertex update distances. Now check every adjacent vertex of V, if sum of distance of u and weight of edge is elss the update it.
Based on this algorithm lets create a program.
Example
#include <limits.h> #include <stdio.h> #define V 9 int minDistance(int dist[], bool sptSet[]) { int min = INT_MAX, min_index; for (int v = 0; v < V; v++) if (sptSet[v] == false && dist[v] <= min) min = dist[v], min_index = v; return min_index; } int printSolution(int dist[], int n) { printf("Vertex Distance from Source\n"); for (int i = 0; i < V; i++) printf("%d \t %d\n", i, dist[i]); } void dijkstra(int graph[V][V], int src) { int dist[V]; bool sptSet[V]; for (int i = 0; i < V; i++) dist[i] = INT_MAX, sptSet[i] = false; dist[src] = 0; for (int count = 0; count < V - 1; count++) { int u = minDistance(dist, sptSet); sptSet[u] = true; for (int v = 0; v < V; v++) if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v]; } printSolution(dist, V); } int main() { int graph[V][V] = { { 0, 6, 0, 0, 0, 0, 0, 8, 0 }, { 6, 0, 8, 0, 0, 0, 0, 13, 0 }, { 0, 8, 0, 7, 0, 6, 0, 0, 2 }, { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, { 0, 0, 6, 14, 10, 0, 2, 0, 0 }, { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, { 8, 13, 0, 0, 0, 0, 1, 0, 7 }, { 0, 0, 2, 0, 0, 0, 6, 7, 0 } }; dijkstra(graph, 0); return 0; }
Output
Vertex Distance from Source 0 0 1 6 2 14 3 21 4 21 5 11 6 9 7 8 8 15
- Related Articles
- C++ Program for Dijkstra’s shortest path algorithm?
- Dijkstra's algorithm in Javascript
- Dijkstra’s Shortest Path Algorithm
- Yen's k-Shortest Path Algorithm in Data Structure
- Shortest Path algorithm in Computer Network
- Dijkstra’s algorithm to compute the shortest path through a graph
- Bellman–Ford Algorithm for Shortest Paths
- Can we use Simple Queue instead of Priority queue to implement Dijkstra’s Algorithm?
- C++ program to find out the shortest cost path in a given graph for q queries
- C program to implement Euclid’ s algorithm
- Shortest Path in Binary Matrix in C++
- Shortest Path Visiting All Nodes in C++
- Fleury’s Algorithm for printing Eulerian Path or Circuit in C++
- Print shortest path to print a string on screen in C Program.
- Shortest Path to Get All Keys in C++

Advertisements