kosaraju finding finishing time using iterative dfs
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Kosaraju's algorithm is an efficient method used to find the strongly connected components (SCCs) within a directed graph. An essential part of this algorithm is determining the finishing times of nodes, which helps in correctly ordering the processing of nodes. This article explores how to find these finishing times using an iterative depth-first search (DFS) approach. The iterative method offers several advantages, particularly regarding stack overflow issues in languages with limited stack sizes.
Understanding the Depth-First Search (DFS)
DFS is a fundamental graph traversal technique often used in graph algorithms. In DFS, traversal is conducted by exploring as far as possible along each branch before backtracking. This exploration can be performed either recursively or iteratively. While recursive implementations are straightforward, they can lead to stack overflow errors on deeply nested graphs. An iterative approach, on the other hand, utilizes an explicit stack data structure, helping to circumvent these issues.
Kosaraju's Algorithm Overview
Kosaraju's algorithm runs in two major passes:
- First Pass: Run DFS on the original graph to calculate finishing times.
- Second Pass: Run DFS on the transpose of the graph in the order of decreasing finishing times to discover SCCs.
Importance of Finishing Times
The finishing time in DFS denotes the step count when all vertices reachable from a given vertex, including the vertex itself, are completely explored. Obtaining correct finishing times is crucial for the successful execution of the second pass in Kosaraju's algorithm.
Iterative DFS to Determine Finishing Times
Data Structures
- Graph Representation: An adjacency list is typically employed for graph representation due to its space efficiency.
- Stack: Utilized to replace the call stack in recursive DFS implementations.
- Visited Array: Tracks whether a vertex is visited or not.
- Finish Stack: Stores the nodes in the order they complete processing, essentially capturing their finishing times.
Algorithm Steps
- Initialization:
- Initialize an empty
visitedarray to keep track of visited nodes. - Create an empty
finish_stack.
- Iterative DFS Implementation:
- For each node, if it hasn’t been visited, perform the following:
- Push the node onto the stack.
- While the stack is not empty:
- Peek at the node on top of the stack.
- If the node is not visited, mark it as visited.
- Explore each of the node’s neighbors:
- If a neighbor hasn't been visited, push it on the stack.
- If all neighbors are visited:
- Pop the node from the stack.
- Push it to the
finish_stack.
- Result:
- The
finish_stackwill contain nodes sorted by finishing time, with the node having the highest finishing time at the top.
Python Code Example
Visualizing iterative DFS
Imagine the following directed graph for clarity:
Related reading
- Kth smallest element in sorted matrix
- Kubernetes sort pods by age
- LabelPropagation - How to avoid division by zero?
- Laderman''s 3x3 matrix multiplication with only 23 multiplications, is it worth it?
- KSQL streams - Get data from Array of Struct
- kube-prometheus-stack issue scraping metrics
- Lamport’s (Physical) Clock Synchronization Algorithm
- Langford sequence implementation Haskell or C

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 courseTrack 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.