Back to Data Structures & Algorithms
2026-02-195 min read

Dijkstra's Algorithm (Data Structures & Algorithms)

Learn Dijkstra's Algorithm (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on Dijkstra's Algorithm! Understanding this fundamental data structure and algorithm is crucial for solving shortest path problems in various fields, including computer science, operations research, engineering, and transportation networks. Mastering Dijkstra's Algorithm can give you an edge in job interviews, coding challenges, and real-world programming scenarios.

This tutorial will provide a thorough explanation of the algorithm, along with examples, practice questions, and common mistakes to help you become proficient in using Dijkstra's Algorithm.

Prerequisites

To fully grasp this tutorial, you should have a good understanding of the following concepts:

  1. Basic Python syntax and control structures (if statements, for loops)
  2. Data Structures: Lists, Dictionaries
  3. Graphs and Adjacency List Representation
  4. Breadth-First Search (BFS) Algorithm
  5. Understanding of sets (optional but recommended)

Core Concept

Dijkstra's Algorithm is a popular algorithm used to find the shortest paths between nodes in a graph using the greedy approach. It works by iteratively marking the smallest unvisited node and updating distances as we explore the graph.

Key components:

  1. Graph: A collection of vertices (nodes) and edges that represent connections between them.
  2. Adjacency List: A way to represent a graph using lists, where each vertex has a list of its adjacent vertices and the corresponding edge weights.
  3. Distance Array: An array storing the shortest known distance from the starting node (source) to every other node in the graph. Initially, all distances are set to infinity except for the source node's distance, which is set to 0.
  4. Visited Set: A set to keep track of the nodes that have been visited during the algorithm's execution.
  5. Unvisited Set: A set containing all the unvisited nodes in the graph at each step.
  6. Current Min Distance: The smallest distance found so far from the source node to any unvisited node.
  7. Neighbor: A function that returns a list of adjacent nodes for a given node.

Algorithm Steps:

  1. Initialize the distance array and visited set.
  2. While there are still unvisited nodes:
  • Find the unvisited node with the smallest current minimum distance.
current_node = min(unvisited, key=lambda node: distances[node] if node in distances else float('inf'))
  • Mark this node as visited.
visited.add(current_node)
  • Update the distances of its neighboring nodes that have not yet been visited, if a shorter path is found.
for neighbor, weight in graph[current_node].items():
new_distance = distances[current_node] + weight
if neighbor not in visited and new_distance < distances[neighbor]:
distances[neighbor] = new_distance
  1. Once all nodes have been visited, the shortest path from the source to every other node will be stored in the distance array.

Worked Example

Let's apply Dijkstra's Algorithm to solve a simple example:

graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'D': 2, 'E': 5},
'C': {'A': 4, 'D': 3},
'D': {'B': 2, 'C': 3, 'E': 1},
'E': {'B': 5, 'D': 1}
}
source = 'A'

def dijkstra(graph, source):

Initialize the distance array and visited set.

distances = {node: float('inf') for node in graph}

distances[source] = 0

visited = set()

current_min_distance = float('inf')

while len(visited) < len(graph):

Find the unvisited node with the smallest current minimum distance.

current_node = min(unvisited, key=lambda node: distances[node] if node in distances else float('inf'))

visited.add(current_node)

current_min_distance = current_max_distance = float('inf')

Update the distances of its neighboring nodes that have not yet been visited, if a shorter path is found.

for neighbor, weight in graph[current_node].items():

if neighbor not in visited:

new_distance = distances[current_node] + weight

if new_distance < distances[neighbor]:

distances[neighbor] = new_distance

current_min_distance = min(current_min_distance, new_distance)

return distances

print(dijkstra(graph, source))


Output:

{'B': 1, 'C': 4, 'D': 3, 'E': 5}

Common Mistakes

1. Misunderstanding the algorithm's purpose and its applicability

  • Dijkstra's Algorithm is used to find the shortest path between nodes in a weighted graph, not an unweighted one.

2. Incorrect initialization of distance array

  • The initial values for all distances should be set to infinity except for the source node's distance, which should be set to 0.

3. Failing to update the current minimum distance when visiting a new node

  • When updating the distances of neighboring nodes, don't forget to check if the new distance is shorter than the current one.

4. Incorrect handling of negative edge weights (if applicable)

  • If the graph contains negative edge weights, Dijkstra's Algorithm may not always find the shortest path if there are negative cycles in the graph. To handle this, you can use a modified version of Dijkstra's Algorithm called Bellman-Ford Algorithm.

Common Mistakes (continued)

5. Incorrect priority queue implementation

  • When implementing Dijkstra's Algorithm using a priority queue, make sure that the queue is properly implemented to maintain the correct order of nodes based on their current minimum distances.

Practice Questions

  1. Modify the example above to find the shortest path between 'E' and all other nodes in the graph.
  2. Implement Dijkstra's Algorithm for a directed cyclic graph, ensuring that it correctly handles cycles without getting stuck in an infinite loop.
  3. Solve the following graph using Dijkstra's Algorithm:
graph = {
'A': {'B': 1, 'C': 2},
'B': {'A': 1, 'D': 5, 'E': 3},
'C': {'A': 2, 'D': 4, 'F': 6},
'D': {'B': 5, 'C': 4, 'E': 2},
'E': {'B': 3, 'D': 2, 'F': 1},
'F': {'C': 6, 'E': 1}
}

FAQ

Q: What is the time complexity of Dijkstra's Algorithm?

A: The time complexity of Dijkstra's Algorithm is O(E log V), where E is the number of edges and V is the number of vertices in the graph.

Q: Can Dijkstra's Algorithm handle negative edge weights?

A: Yes, but it may not always find the shortest path if there are negative cycles in the graph.

Q: How does Dijkstra's Algorithm compare to other shortest path algorithms like Bellman-Ford and Floyd-Warshall?

A: Dijkstra's Algorithm is more efficient than Bellman-Ford for graphs without negative edge weights, but it may not handle negative cycles. Floyd-Warshall, on the other hand, can handle both positive and negative edge weights and find the shortest paths between all pairs of nodes in a graph.

Q: Is there a way to optimize Dijkstra's Algorithm for sparse graphs (graphs with many vertices and few edges)?

A: Yes, for sparse graphs, you can use data structures like Fibonacci heaps or priority queues to improve the algorithm's performance. These data structures allow for faster insertion and deletion of elements, which is beneficial when dealing with large graphs.

Dijkstra's Algorithm (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn