Dijkstra's Algorithm
visited set
graph theory
pathfinding
algorithm explanation

What is the purpose of the visited set in Dijkstra?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Dijkstra's algorithm is a well-known algorithm for finding the shortest path between nodes in a graph, particularly in graphs with non-negative edge weights. One critical component of this algorithm is the "visited set." Understanding the purpose and implementation of the visited set is essential for correctly employing Dijkstra's algorithm.

Purpose of the Visited Set

The visited set is an auxiliary data structure used in Dijkstra's algorithm to ensure that each node in the graph is processed at most once. Its primary function is to keep track of which nodes have already been visited and, therefore, have their shortest path from the source node determined.

Key Functions

  1. Prevention of Redundant Operations: Once a node has been visited and its minimal distance from the source node established, it's added to the visited set. The presence of a node in this set indicates that future edge relaxations involving this node are unnecessary, saving computational resources.
  2. Algorithm Termination Check: The visited set helps in determining when the algorithm should terminate. When all nodes have been visited, the algorithm can conclude, ensuring that all reachable nodes have the shortest paths calculated.
  3. Aiding Priority Queue Operations: The visited set works alongside the priority queue (often implemented with a min-heap) to ensure that nodes are not revisited. The priority queue might have multiple entries for the same node at varying distances due to different paths. The visited set filters older, obsolete entries when the node has already been processed.

Technical Explanation

Dijkstra's algorithm starts by initializing all node distances to infinity, except the source node, which is set to zero. It uses a priority queue to repeatedly extract the node with the smallest known distance. For each extracted node, the algorithm iterates over its adjacent nodes, updating their distances if a shorter path is found.

Here's where the visited set comes into play:

  1. Node Extraction: The algorithm extracts a node with the smallest tentative distance from the priority queue.
  2. Visit Check: Before processing the extracted node's neighbors, the algorithm checks if the node is in the visited set. If true, it skips processing to avoid redundant work.
  3. Updating Neighbors: If the node isn't visited, the algorithm proceeds to relax the edges of this node. For each neighbor, if a shorter path is found through the current node, the algorithm updates the neighbor's distance and inserts this updated distance into the priority queue.
  4. Mark as Visited: Once the node's neighbors are processed, it's added to the visited set, marking it as processed.

Example

Consider a graph with nodes represented as integers and edges with specific weights. When running Dijkstra's algorithm from node 1:

• Initialize source node distance: dist[1]=0\text{dist}[1] = 0; all others set to \infty. • Priority Queue (PQ) starts with 1. • Extract 1, update neighbors, insert their distances into PQ. • Add 1 to the visited set. • Continue this process, skipping nodes already in the visited set.

Table of Key Points

FunctionDescription
Redundant Operation PreventionEnsures nodes are processed only once, enhancing efficiency.
Termination CheckDetermines when all nodes have shortest paths and the algorithm can cease.
Priority Queue CoordinationFilters unnecessary or outdated entries from the priority queue.

Additional Details

Complexity Considerations

The use of a visited set allows Dijkstra's algorithm to run efficiently: • Without the visited set, nodes might be processed multiple times, increasing complexity. • With a visited set, the algorithm achieves a time complexity of O((V+E)logV)O((V + E) \log V) using a min-heap-based priority queue for VV nodes and EE edges.

Implementation Tips

• The visited set is often implemented as a simple hash set (such as a Python set ), enabling O(1)O(1) complexity for membership checks. • It's vital to ensure that only nodes with finalized shortest paths are added to the visited set.

In conclusion, the visited set is a fundamental component that enhances the efficiency and correctness of Dijkstra's algorithm by preventing redundant computations and ensuring proper termination. Proper implementation of the visited set is crucial for leveraging the full benefits of Dijkstra's approach to finding shortest paths in weighted graphs.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.