Explanation and Implementation with C++ program (2024)

  • Home
  • DS & Algo. ▾
    • Data Structure
    • Algorithms
    • Coding Problems
  • Languages ▾
    • C
    • C++
    • C++ STL
    • Java
    • Python
    • Scala
    • Ruby
    • C#.Net
    • Golang
    • Android
    • Kotlin
    • SQL
  • Web. ▾
    • JavaScript
    • CSS
    • jQuery
    • PHP
    • Node.Js
    • AdonisJs
    • VueJS
    • Ajax
    • HTML
    • Django
  • Programs ▾
    • C
    • C++
    • Data Structure
    • Java
    • C#.Net
    • VB.Net
    • Python
    • PHP
    • Golang
    • Scala
    • Swift
    • Rust
    • Ruby
    • Kotlin
    • C Interview Programs
  • Aptitude ▾
    • C Aptitude
    • C++ Aptitude
    • Java Aptitude
    • C# Aptitude
    • PHP Aptitude
    • Linux Aptitude
    • DBMS Aptitude
    • Networking Aptitude
    • AI Aptitude
    • More...
  • Interview ▾
    • Golang
    • MIS Executive
    • DBMS
    • C
    • Embedded C
    • Java
    • SEO
    • HR
  • Find Output ▾
    • C
    • C++
    • C#.Net
    • Java
    • Go
    • PHP
    • More...
  • MCQs ▾
    • Web Technologie MCQs
    • CS Subjects MCQs
    • Databases MCQs
    • Programming MCQs
    • Testing Software MCQs
    • Digital Mktg Subjects MCQs
    • Cloud Computing S/W MCQs
    • Engineering Subjects MCQs
    • Commerce MCQs
    • More MCQs...
  • CS Subjects ▾
    • Machine Learning/AI
    • Operating System
    • Computer Network
    • Software Engineering
    • Discrete Mathematics
    • Digital Electronics
    • Data Mining
    • MIS
    • DBMS
    • Embedded Systems
    • Cryptography
    • CS Fundamental
    • More Tutorials...
  • More ▾
    • Tech Articles
    • Puzzles
    • Full Forms
    • Code Examples
    • Blogs
    • Guest Post
    • Programmer's Calculator
    • XML Sitemap Generator
    • About
    • Contact

Home »C++ programming language

Dijkstra's Algorithm: In this tutorial, we will learn about Dijkstra's algorithm, why it is used, and the implementation of Dijkstra's algorithm with the help of a C++ program.By Shubham Singh Rajawat Last updated : August 06, 2023

Dijkstra's Algorithm

Dijkstra's algorithm aka the shortest path algorithm is used to find the shortest path in a graph that covers all the vertices.

Given a graph with the starting vertex.

Dijkstra's Algorithm Implementation Steps

The steps to implement Dijkstra's algorithm are:

  • Initially Dset contains src: dist[s]=0 dist[v]= ∞
  • Set Dset to initially empty
  • While all the elements in the graph are not added to 'Dset'
    A. Let 'u' be any vertex not in 'Dset' and has minimum label dist[u]B. Add 'u' to DsetC. Update the label of the elements adjacent to u For each vertex 'v' adjacent to u If 'v' is not in 'Dset' then If dist[u]+weight(u,v)<dist[v] then Dist[v]=dist[u]+weight(u,v)

Dijkstra's Algorithm Example with Explanation

Let us understand this with the help of an example:

Explanation and Implementation with C++ program (4)

Initially Dset is empty and the distance of all the vertices is set to infinity except the source which is set to zero. First we find the vertex with minimum distance. The vertex ‘A’ got picked as it is the source so update Dset for A.

Explanation and Implementation with C++ program (5)Explanation and Implementation with C++ program (6)

Now update the distance of its adjacent vertices which B and C. Now find the vertex whose distance is minimum which is C.

Explanation and Implementation with C++ program (7)Explanation and Implementation with C++ program (8)

So update Dset for C and update the distance of its adjacent vertices. Find the vertex with minimum distance which is B.

Explanation and Implementation with C++ program (9)Explanation and Implementation with C++ program (10)

Update Dset for B and update distance of its adjacent vertices and then find the vertex with minimum distance which is G.

Explanation and Implementation with C++ program (11)Explanation and Implementation with C++ program (12)

Update Dset for G and update distance of its adjacent vertices and find the vertex with minimum distance which is E.

Explanation and Implementation with C++ program (13)Explanation and Implementation with C++ program (14)

Update Dset for E and update distance of its adjacent vertices and find the vertex with minimum distance which is F.

Explanation and Implementation with C++ program (15)Explanation and Implementation with C++ program (16)

Update Dset for F and update distance of its adjacent vertices and find the vertex with minimum distance which is D.

Explanation and Implementation with C++ program (17)Explanation and Implementation with C++ program (18)

Update Dset for D.

Explanation and Implementation with C++ program (19)

As all the vertices are part of Dset so we got the shortest path which contains all the vertices. So the graph for shortest path from vertex A is

Explanation and Implementation with C++ program (20)

C++ Program to Implement Dijkstra's Algorithm

#include <iostream>#include <climits> // Used for INT_MAXusing namespace std;// It is the total no of verteices in the graph#define vertex 7// A method to find the vertex with minimum distance// which is not yet included in Dsetint minimumDist(int dist[], bool Dset[]){ // initialize min with the maximum possible value // as infinity does not exist int min = INT_MAX, index; for (int v = 0; v < vertex; v++) { if (Dset[v] == false && dist[v] <= min) { min = dist[v]; index = v; } } return index;}// Method to implement shortest path algorithmvoid dijkstra(int graph[vertex][vertex], int src){ int dist[vertex]; bool Dset[vertex]; // Initialize distance of all the vertex // to INFINITY and Dset as false for (int i = 0; i < vertex; i++) { dist[i] = INT_MAX; Dset[i] = false; } // Initialize the distance of the source vertec to zero dist[src] = 0; for (int c = 0; c < vertex; c++) { // u is any vertex that is not yet included in // Dset and has minimum distance int u = minimumDist(dist, Dset); // If the vertex with minimum distance found // include it to Dset Dset[u] = true; // Update dist[v] if not in Dset and their is a path from src to v // through u that has distance minimum than current value of dist[v] for (int v = 0; v < vertex; v++) { if (!Dset[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v]; } } cout << "Vertex\t\tDistance from source" << endl; // will print the vertex with their distance // from the source to the console for (int i = 0; i < vertex; i++) { char c = 65 + i; cout << c << "\t\t" << dist[i] << endl; }}// Main programint main(){ int graph[vertex][vertex] = { { 0, 5, 3, 0, 0, 0, 0 }, { 0, 0, 2, 0, 3, 0, 1 }, { 0, 0, 0, 7, 7, 0, 0 }, { 2, 0, 0, 0, 0, 6, 0 }, { 0, 0, 0, 2, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0 } }; dijkstra(graph, 0); return 0;}

Output

Vertex Distance from sourceA 0B 5C 3D 9E 7F 8G 6

Related Tutorials

  • Quick Sort in C++ with Algorithm, Example
  • Merge Sort in C++ with Example
  • Counting Sort with C++ Example
  • Implement shell sort using C++ program
  • C++ print Postorder traversal from Preorder and Inorder traversal of a tree
  • Find in-order Successor and Predecessor in a BST using C++ program
  • Maximum Sum Helix path (using C++ program)
  • Implement in-order traversal using C++ program
  • Implement post-order traversal using C++ program
  • Implement pre-order traversal using C++ program

Comments and Discussions!

Load comments ↻


Explanation and Implementation with  C++ program (2024)

References

Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6776

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.