Tutorialspoint
Problem
Solution
Submissions

Dijkstra's Algorithm

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to find the shortest path from a source vertex to all other vertices in a weighted graph using Dijkstra's algorithm. The program should print the shortest distance from the source to every other vertex.

Example 1
  • Input:
    • Number of vertices: 5
    • Number of edges: 7
    • Edge 0: from 0 to 1, weight 9
    • Edge 1: from 0 to 2, weight 6
    • Edge 2: from 0 to 3, weight 5
    • Edge 3: from 0 to 4, weight 3
    • Edge 4: from 2 to 1, weight 2
    • Edge 5: from 2 to 3, weight 4
    • Edge 6: from 3 to 4, weight 2
    • Source vertex: 0
  • Output:
    • Shortest distance from vertex 0 to vertex 0: 0
    • Shortest distance from vertex 0 to vertex 1: 8
    • Shortest distance from vertex 0 to vertex 2: 6
    • Shortest distance from vertex 0 to vertex 3: 5
    • Shortest distance from vertex 0 to vertex 4: 3
  • Explanation:
    • Step 1: Initialize distances (0 for source, infinity for others)
    • Step 2: Select vertex with minimum distance (initially source)
    • Step 3: Update distances to adjacent vertices if shorter path found
    • Step 4: Path to vertex 1: 0→2→1 with distance 8 (6+2)
    • Step 5: Path to vertex 4: Direct from 0 with distance 3
    • Step 6: Repeat until all vertices processed
Example 2
  • Input:
    • Number of vertices: 4
    • Number of edges: 5
    • Edge 0: from 0 to 1, weight 1
    • Edge 1: from 0 to 2, weight 4
    • Edge 2: from 1 to 2, weight 2
    • Edge 3: from 1 to 3, weight 5
    • Edge 4: from 2 to 3, weight 1
    • Source vertex: 0
  • Output:
    • Shortest distance from vertex 0 to vertex 0: 0
    • Shortest distance from vertex 0 to vertex 1: 1
    • Shortest distance from vertex 0 to vertex 2: 3
    • Shortest distance from vertex 0 to vertex 3: 4
  • Explanation:
    • Step 1: Initialize distances (0 for source, infinity for others)
    • Step 2: Path to vertex 1: Direct from 0 with distance 1
    • Step 3: Path to vertex 2: 0→1→2 with distance 3 (1+2)
    • Step 4: Path to vertex 3: 0→1→2→3 with distance 4 (1+2+1)
Constraints
  • 1 ≤ V ≤ 10^3 (number of vertices)
  • 0 ≤ E ≤ 10^5 (number of edges)
  • 1 ≤ weight ≤ 10^4
  • Time Complexity: O(E log V)
  • Space Complexity: O(V + E)
ArraysGraphPriority QueueFacebookZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use an adjacency list to represent the graph.
  • Use a priority queue (min-heap) to efficiently extract the vertex with the minimum distance.
  • Maintain a distance array to keep track of the shortest distances from the source.
  • Initialize the distance of the source vertex as 0 and all other vertices as infinity.
  • Process vertices in order of increasing distance from the source.

Steps to solve by this approach:

 Step 1: Create a graph representation using adjacency list.

 Step 2: Initialize distances array with infinity for all vertices except source.
 Step 3: Use a priority queue to process vertices by their current distance.
 Step 4: Extract the vertex with minimum distance from the queue.
 Step 5: Update distances of its adjacent vertices if a shorter path is found.
 Step 6: Add vertices with updated distances back to the priority queue.
 Step 7: Return the array of shortest distances from source to all vertices.

Submitted Code :