Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Dijkstra's Shortest Path Algorithm quickly and effectively.
220+ students studying
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.
∞
).Graph:
Nodes: A, B, C, D, E
Edges with weights:
Node | Distance from A | Visited | Path |
---|---|---|---|
A | 0 | ✔ | - |
B | 3 (via C) | ✔ | A → C → B |
C | 1 | ✔ | A → C |
D | 4 (via B) | ✔ | A → C → B → D |
E | 7 (via D) | ✔ | A → C → B → D → E |
Shortest path from A to E: A → C → B → D → E, Distance = 7
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
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}
Given the following graph:
From | To | Weight |
---|---|---|
A | B | 2 |
A | C | 5 |
B | C | 1 |
B | D | 4 |
C | D | 2 |
D | E | 1 |
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 Flashcards12 quizzes
Quizzes on Dijkstra's Shortest Path Algorithm
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Dijkstra's Shortest Path Algorithm
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Dijkstra's Shortest Path Algorithm
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Dijkstra's Shortest Path Algorithm
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Dijkstra's Shortest Path Algorithm to Deepen Your Understanding and Improve Your Mastery
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!
Report Improved Results
Recommend to friends
Students Supported
Questions answered