Arian Soleimanzadeh
  • Home
  • Blog
  • Podcasts
  • Videos
  • Contact
العربيةArabic
DeutschGerman
EnglishEnglish
فارسیPersian
한국어Korean
中文Chinese
Panel•Quick Contact

Languages

Choose your interface locale

ar

العربية

Arabic

de

Deutsch

German

en

English

English

fa

فارسی

Persian

ko

한국어

Korean

zh

中文

Chinese

Get Appointment

Drop a quick message — I’ll reply as soon as possible.

LinkedInFast response
Home/Articles/What Is Kosaraju's Algorithm and What Problem Does It Solve?
KosarajuArticle

What Is Kosaraju's Algorithm and What Problem Does It Solve?

A beginner-friendly introduction to Kosaraju's algorithm, the Strongly Connected Components problem, why SCCs matter, how the core idea works, and where the algorithm appears in real software systems.

August 19, 20269 min read3 Views
#Kosaraju#Graph Algorithms#Strongly Connected Components#SCC#DFS#Directed Graph#Algorithms#Software Engineering

Arian Soleimanzadeh

Software Engineer & Researcher

Conceptual visualization of Kosaraju's algorithm and strongly connected components in a directed graph

Arian Soleimanzadeh

AI · Code · Product

Research + Engineering
On this page
IntroductionWhat Is a Graph?What Is a Directed Graph?Where Does the Problem Begin?What Does Strongly Connected Mean?What Is a Strongly Connected Component?What Does Kosaraju's Algorithm Do?Why Are SCCs Important?1. Detecting Circular Dependencies2. Microservice Architectures3. Social Networks4. Web Link Analysis5. Compilers and Program Analysis6. Package Dependency SystemsWhy Is Ordinary DFS Not Enough?The Core Idea Behind KosarajuStep 1: Run DFSStep 2: Reverse Every EdgeStep 3: Run DFS AgainA Very Small ExampleTime ComplexityIs Kosaraju the Only SCC Algorithm?When Should You Think About Kosaraju?Cycle vs SCCWhat Should You Know Before Learning Kosaraju in Depth?Conclusion

Introduction

Many graph algorithms look complicated when we first encounter them, but most of them are designed to solve a very understandable problem. Kosaraju's algorithm is a good example.

Kosaraju's algorithm finds Strongly Connected Components, usually abbreviated as SCCs, in a directed graph.

If these terms are unfamiliar, that is fine. In this article we will not start with mathematical proofs or implementation details. Instead, we will first understand the problem itself and why we need an algorithm such as Kosaraju.


What Is a Graph?

A graph is a collection of vertices, also called nodes, connected by edges.

For example:

Code
123
A --- B
|     |
C --- D

Each letter represents a node, while the lines represent relationships between nodes.

Graphs are used throughout computer science to model systems such as:

  • Social networks
  • Roads and transportation networks
  • Communication between servers
  • Software package dependencies
  • Links between web pages
  • Program execution flow
  • Relationships between microservices

Graphs are therefore not only mathematical objects. They are one of the most useful models in software engineering.


What Is a Directed Graph?

In some graphs, relationships have a direction.

For example:

Code
1
A → B

This means we can move from A to B, but it does not automatically mean we can move from B back to A.

Such a graph is called a directed graph.

In a social network:

Code
1
Ali → Sara

may mean that Ali follows Sara. Sara does not necessarily follow Ali.

In a software architecture we might have:

Code
1
Service A → Service B

meaning that Service A depends on or calls Service B.


Where Does the Problem Begin?

Consider this graph:

Code
12345
A → B → C
↑       ↓
└───────┘

D → E

In the first part of the graph, we can travel from A to B, from B to C, and from C back to A.

Therefore, A, B, and C have a particularly strong relationship.

Starting from any one of them, we can eventually reach the other two.

D and E are different. We can travel from D to E, but there is no path from E back to D.

The set:

Code
1
{A, B, C}

forms what graph theory calls a Strongly Connected Component.


What Does Strongly Connected Mean?

Two vertices u and v are strongly connected if there is a path:

Code
1
u → ... → v

and also a path:

Code
1
v → ... → u

In other words, each vertex can reach the other.

Consider:

Code
123
A → B
↑   ↓
└── C

where the edges effectively form:

Code
123
A → B
B → C
C → A

All three vertices belong to the same strongly connected region because every vertex can eventually reach every other vertex.


What Is a Strongly Connected Component?

A Strongly Connected Component, or SCC, is a maximal group of vertices in a directed graph where every vertex can reach every other vertex in the same group.

For example:

Code
1234567
A → B → C
↑       ↓
└───────┘

D → E → F
↑       ↓
└───────┘

This graph contains two SCCs:

Code
12
SCC 1 = {A, B, C}
SCC 2 = {D, E, F}

There may even be an edge from one component to the other. They remain separate SCCs as long as mutual reachability does not exist between the two groups.


What Does Kosaraju's Algorithm Do?

Kosaraju's algorithm finds every SCC in a directed graph.

If a graph contains hundreds of thousands of vertices, Kosaraju can determine which vertices belong to mutually reachable groups.

For example, given:

Code
123456
A → B
B → C
C → A
C → D
D → E
E → D

the result is:

Code
12
SCC 1 = {A, B, C}
SCC 2 = {D, E}

A simple definition is therefore:

Kosaraju's algorithm partitions a directed graph into groups whose vertices are mutually reachable through directed paths.


Why Are SCCs Important?

The problem may initially seem theoretical, but strongly connected components appear in many real systems.

1. Detecting Circular Dependencies

Suppose a software system contains:

Code
123
Module A → Module B
Module B → Module C
Module C → Module A

The modules form a circular dependency.

None of them is truly independent because each one indirectly depends on another member of the cycle.

These modules belong to the same SCC.

SCC analysis can therefore help identify circular dependency groups in large codebases.


2. Microservice Architectures

Imagine:

Code
123
User Service → Payment Service
Payment Service → Notification Service
Notification Service → User Service

These services form a dependency cycle.

In an architecture containing dozens or hundreds of services, finding such structures manually becomes difficult.

By representing services as graph vertices and service dependencies as directed edges, SCC algorithms can expose cyclic dependency groups.


3. Social Networks

If an edge A → B means user A follows user B, a large social network becomes a directed graph.

Some groups of users may be mutually reachable through chains of follower relationships.

SCC analysis provides one of the basic tools for understanding such directed network structures.

Real social-network analysis is usually far more complex, but strong connectivity remains a fundamental graph concept.


4. Web Link Analysis

Web pages can also be represented as a directed graph:

Code
123
Page A → Page B
Page B → Page C
Page C → Page A

Every hyperlink is a directed edge.

SCCs can identify groups of pages in which users or crawlers can move from one page to another and eventually return through available links.


5. Compilers and Program Analysis

Graphs appear frequently in compilers and static-analysis systems, including:

  • Control-flow graphs
  • Call graphs
  • Dependency graphs

These graphs may contain cycles.

SCC decomposition helps identify portions of the program that are mutually dependent or cyclically connected.


6. Package Dependency Systems

Consider package dependencies:

Code
123
Package A → Package B
Package B → Package C
Package C → Package A

This is a dependency cycle.

Large dependency graphs may contain thousands of packages and modules. SCC algorithms provide an efficient way to identify groups involved in cyclic dependencies.


Why Is Ordinary DFS Not Enough?

You may wonder why we cannot simply run Depth-First Search and treat every visited group as an SCC.

The reason is that in a directed graph, one-way reachability does not imply strong connectivity.

Consider:

Code
1
A → B → C

A DFS starting from A visits all three vertices.

However, they do not belong to the same SCC because C cannot travel back to B or A.

Therefore:

Code
1
Reachable ≠ Strongly Connected

Kosaraju's algorithm uses a clever ordering and a reversed graph to distinguish ordinary reachability from mutual reachability.


The Core Idea Behind Kosaraju

We will study the full procedure in later articles, but its high-level idea has three major steps.

Step 1: Run DFS

First, DFS is executed on the original graph and vertices are recorded according to their finishing order.

Step 2: Reverse Every Edge

We create the transpose graph.

If the original graph contains:

Code
1
A → B

the transpose contains:

Code
1
A ← B

Step 3: Run DFS Again

DFS is executed on the transpose graph, but vertices are selected according to the special order obtained from the first DFS.

Each traversal in this second phase reveals one strongly connected component.

The overall idea is:

Code
1234567891011
Original Graph
      ↓
     DFS
      ↓
Finish Order
      ↓
Transpose Graph
      ↓
     DFS
      ↓
     SCCs

Later articles in this series will explain why this procedure works mathematically.


A Very Small Example

Consider:

Code
1234567
A → B → C
↑       ↓
└───────┘

C → D
D → E
E → D

The first region contains:

Code
1
A → B → C → A

so:

Code
1
{A, B, C}

forms one SCC.

We also have:

Code
12
D → E
E → D

so:

Code
1
{D, E}

forms another SCC.

Although C can reach D, D and E cannot return to A, B, or C.

Therefore the components must remain separate.

Kosaraju's output is:

Code
12
SCC 1 = {A, B, C}
SCC 2 = {D, E}

Time Complexity

One important advantage of Kosaraju's algorithm is its efficiency.

With an adjacency-list representation, the running time is:

Code
1
O(V + E)

where:

  • V is the number of vertices.
  • E is the number of edges.

This is essentially linear in the size of the graph, making the algorithm practical even for large sparse graphs.


Is Kosaraju the Only SCC Algorithm?

No.

Other well-known algorithms for finding strongly connected components include:

  • Tarjan's Algorithm
  • Gabow's Algorithm

Tarjan's algorithm also runs in O(V + E) time and finds SCCs using one primary DFS traversal.

Kosaraju is often particularly useful for learning because its two-pass structure is conceptually straightforward.


When Should You Think About Kosaraju?

Whenever a problem involves a directed graph and mentions ideas such as the following, SCC algorithms should come to mind:

  • Groups in which every node can reach every other node
  • Circular dependencies
  • Mutual reachability
  • Cycles between modules
  • Mutually reachable services
  • Strong connectivity
  • Decomposing a directed graph into components

Kosaraju is one of the main algorithms to consider in such situations.


Cycle vs SCC

A cycle and an SCC are related, but they are not identical concepts.

A cycle is a closed path such as:

Code
1
A → B → C → A

An SCC, however, is a maximal set of mutually reachable vertices.

A single SCC may contain many different cycles.

Therefore, SCC decomposition describes a broader structure than simply detecting one cycle.


What Should You Know Before Learning Kosaraju in Depth?

To understand the algorithm completely, it helps to know:

  1. Graphs and directed graphs
  2. Vertices and edges
  3. Paths and reachability
  4. Depth-First Search
  5. DFS finishing time
  6. Graph transpose
  7. Strongly connected components

The next articles in this series will develop these concepts step by step.


Conclusion

Kosaraju's algorithm finds Strongly Connected Components in directed graphs.

An SCC is a group of vertices in which every vertex can reach every other vertex through directed paths.

This concept appears in software architecture, dependency analysis, social networks, compiler design, microservices, web graphs, and many other areas.

Kosaraju's high-level strategy uses two DFS passes and a transposed graph, while achieving O(V + E) running time with an adjacency-list representation.

The central idea to remember is:

Kosaraju is not merely a cycle-detection algorithm. It reveals the internal structure of a directed graph by partitioning it into maximal groups of mutually reachable vertices.

In the next article, we will study Strongly Connected Components in greater depth before examining Kosaraju's execution step by step.

On this page
IntroductionWhat Is a Graph?What Is a Directed Graph?Where Does the Problem Begin?What Does Strongly Connected Mean?What Is a Strongly Connected Component?What Does Kosaraju's Algorithm Do?Why Are SCCs Important?1. Detecting Circular Dependencies2. Microservice Architectures3. Social Networks4. Web Link Analysis5. Compilers and Program Analysis6. Package Dependency SystemsWhy Is Ordinary DFS Not Enough?The Core Idea Behind KosarajuStep 1: Run DFSStep 2: Reverse Every EdgeStep 3: Run DFS AgainA Very Small ExampleTime ComplexityIs Kosaraju the Only SCC Algorithm?When Should You Think About Kosaraju?Cycle vs SCCWhat Should You Know Before Learning Kosaraju in Depth?Conclusion

Article details

Publication metadata, reading time and live view information.

Published

August 19, 2026

Updated

August 19, 2026

Reading time

9 min read

Views

3

Author

Arian Soleimanzadeh

Previous article

B2B CRM vs. B2C CRM: From Sales Processes to Software Architecture

Next article

What Is Hamming Distance? From the Core Idea to Implementation and Real-World Uses

Let’s build something clean, fast, and beautiful.

Quick contact for collaborations, consulting, or product work.

Quick ContactEmail Me
Arian Soleimanzadeh

Personal portfolio focused on modern web engineering, UI systems, and practical AI products — clean code, clean design.

Quick Links

  • About
  • Blog
  • Projects
  • Contact

Contact

  • info@ariansoleimanzadeh.site
  • soleimanzadeh.a.work@gmail.com

Availability: Weekdays

Typically replies within 24h.

Newsletter

Get updates on posts, projects, and new releases.

© 2026 ariansoleimanzadeh.site — All rights reserved.

LinkedIn