Photo AI

Last Updated Sep 27, 2025

Dijkstra's Shortest Path Algorithm Simplified Revision Notes

Revision notes with simplified explanations to understand Dijkstra's Shortest Path Algorithm quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

265+ students studying

Dijkstra's Shortest Path Algorithm

Overview

Dijkstra's Shortest Path Algorithm is a graph traversal algorithm used to find the shortest path from a source node to all other nodes in a weighted graph. The algorithm guarantees the shortest path for graphs with non-negative edge weights.

How Dijkstra's Algorithm Works

  • Input: A weighted graph, a starting node (source).
  • Output: The shortest path from the source node to all other nodes.

Steps:

  1. Initialise:
  • Set the distance to the source node as 0 and all other nodes as infinity ().
  • Mark all nodes as unvisited.
  1. Visit Nodes:
  • Start with the source node.
  • For each unvisited neighbour, calculate the tentative distance from the source.
  • If this distance is smaller than the previously recorded distance, update it.
  1. Mark Node as Visited:
  • Once all neighbours of the current node are evaluated, mark it as visited. A visited node will not be revisited.
  1. Repeat:
  • Move to the unvisited node with the smallest tentative distance.
  • Repeat steps 2–4 until all nodes are visited or the shortest path to the target node is found (if only specific node distances are required).

Properties of Dijkstra's Algorithm

  • Greedy Algorithm: Always selects the node with the smallest known distance.
  • Time Complexity:
    • O(V²) for simple implementation using an adjacency matrix.
    • O((V + E) log V) with a priority queue (using a binary heap).
  • Space Complexity: O(V + E) for adjacency list storage.
infoNote

Example: Finding the Shortest Path

Graph:

Nodes: A, B, C, D, E

Edges with weights:

  • A → B: 4
  • A → C: 1
  • B → D: 1
  • C → B: 2
  • C → D: 5
  • D → E: 3 Step-by-Step Execution:
NodeDistance from AVisitedPath
A0-
B3 (via C)A → C → B
C1A → C
D4 (via B)A → C → B → D
E7 (via D)A → C → B → D → E

Shortest path from A to E: A → C → B → D → E, Distance = 7

Pseudocode for Dijkstra's Algorithm

METHOD Dijkstra(graph, source)
    dist ← array of size graph.length filled with ∞
    dist[source] ← 0
    visited ← empty set

    WHILE there are unvisited nodes
        current ← node with smallest distance in dist
        visited.add(current)

        FOR each neighbour of current
            IF neighbour NOT IN visited
                tentative_distance ← dist[current] + weight(current, neighbor)
                IF tentative_distance < dist[neighbour]
                    dist[neighbour] ← tentative_distance
    ENDWHILE

    RETURN dist

Python Implementation

import heapq  # Priority queue for efficient node selection

def dijkstra(graph, source):
    # Initialise distances and priority queue
    dist = {node: float('inf') for node in graph}
    dist[source] = 0
    priority_queue = [(0, source)]  # (distance, node)

    while priority_queue:
        current_distance, current_node = heapq.heappop(priority_queue)

        for neighbor, weight in graph[current_node]:
            distance = current_distance + weight
            if distance < dist[neighbor]:
                dist[neighbor] = distance
                heapq.heappush(priority_queue, (distance, neighbor))

    return dist

# Example graph as an adjacency list
graph = {
    'A': [('B', 4), ('C', 1)],
    'B': [('D', 1)],
    'C': [('B', 2), ('D', 5)],
    'D': [('E', 3)],
    'E': []
}

# Compute shortest paths from source 'A'
distances = dijkstra(graph, 'A')
print(distances)  # Output: {'A': 0, 'B': 3, 'C': 1, 'D': 4, 'E': 7}

Tracing Dijkstra's Algorithm

Given the following graph:

FromToWeight
AB2
AC5
BC1
BD4
CD2
DE1
  1. Start at A (distance = 0).
  2. Visit B (distance = 2 via A).
  3. Visit C (distance = 3 via B).
  4. Visit D (distance = 5 via C).
  5. Visit E (distance = 6 via D).

Note Summary

infoNote

Common Mistakes

  1. Incorrect Initialisation of Distances: Forgetting to set the source node's distance to 0.
  2. Not Updating Tentative Distances Properly: Failing to update distances correctly when a shorter path is found.
  3. Revisiting Nodes: Revisiting already visited nodes, leads to incorrect or inefficient execution.
  4. Misunderstanding Priority Queue: Not using a priority queue correctly, leading to suboptimal performance.
infoNote

Key Takeaways

  • Dijkstra's Algorithm efficiently finds the shortest path in a weighted graph with non-negative edges.
  • It uses a greedy approach, selecting the node with the smallest tentative distance.
  • Time Complexity varies based on implementation:
  • Basic: O(V²).
  • Optimized (using priority queue): O((V + E) log V).
  • Correct initialization and accurate distance updates are crucial for the algorithm's success.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Dijkstra's Shortest Path Algorithm

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

120 flashcards

Flashcards on Dijkstra's Shortest Path Algorithm

Revise key concepts with interactive flashcards.

Try Computer Science Flashcards

12 quizzes

Quizzes on Dijkstra's Shortest Path Algorithm

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Dijkstra's Shortest Path Algorithm

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Dijkstra's Shortest Path Algorithm

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Dijkstra's Shortest Path Algorithm

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Dijkstra's Shortest Path Algorithm you should explore

Discover More Revision Notes Related to Dijkstra's Shortest Path Algorithm to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Algorithms for the Main Data Structures

Stacks

user avatar
user avatar
user avatar
user avatar
user avatar

462+ studying

183KViews

96%

114 rated

Algorithms for the Main Data Structures

Queues

user avatar
user avatar
user avatar
user avatar
user avatar

236+ studying

190KViews

96%

114 rated

Algorithms for the Main Data Structures

Trees

user avatar
user avatar
user avatar
user avatar
user avatar

457+ studying

190KViews

96%

114 rated

Algorithms for the Main Data Structures

Linked List

user avatar
user avatar
user avatar
user avatar
user avatar

255+ studying

187KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered