Kosaraju Algorithm
What is Kosaraju Algorithm?
In order to locate strongly connected components (SCCs) in a directed graph, Kosaraju's technique uses linear time. In 1978, Indian computer scientist Sharir Kosaraju put up the idea. The algorithm has an O(V + E) time complexity, where V is the number of graph vertices and E is the number of graph edges.
The steps of kosaraju's algorithm are as follows:
 Do a depthfirst traversal (DFS) of the provided directed graph using an empty stack (or any other data structure that permits DFS). Push each visited vertex onto the stack during the DFS after all of its neighbouring vertices have been seen.
 Reverse the direction of all the graph's edges to produce a new graph with the same vertices but reversing the direction of its edges. Just switching the source and destination of each edge will accomplish this.
 One by one, pop vertices from the stack, and then execute a DFS on the reversed graph, starting at each popped vertices. You may determine the strongly connected component linked to each vertex by looking at the collection of vertices visited during each DFS traverse.
 Till the stack is empty, keep performing step 3. You will get a new strongly linked component each time you run a DFS traverse on the inverted graph starting from a vertex that was popped from the stack.
 Kosaraju's approach produces the strongly connected components that were discovered in step 3 as its output. They stand in for the directed graph's mutually accessible groups of vertices.
For discovering strongly connected components in directed graphs, Kosaraju's approach is effective and often used. It has uses in a variety of fields, including social network analysis, network analysis, and compilers, among others.
Working of Kosaraju's Algorithm
In a directed network, Kosaraju's approach locates strongly connected components (SCCs) in linear time. A "forward" stage and a "reverse" stage are how it operates. Here is a general description of how the algorithm functions:
Forward Stage
 Start at any vertex in the graph and traverse it in depth first (DFS). Each vertex receives a "finish time" from this traverse, which denotes the order in which vertices complete the DFS.
 Record the vertices in the order of their finish times, usually by stacking them as they are completed.
Backward Stage
 Transpose the original graph by reversing the direction of each edge in the graph.
 Pop vertices in the order of their finish timings from the stack acquired in the forward step.
 Run a DFS on the transposed graph, only visiting unvisited vertices while starting from each popped vertex. A strongly linked component in the original graph is identified by the DFS from each popped vertex in this stage.
Output
 The results of Kosaraju's algorithm are the tightly related components found in the backward step.
Why do we use Kosaraju's Algorithm?
In a directed graph, strongly connected components (SCCs) are discovered using Kosaraju's approach. SCCs are collections of vertices in a directed graph where every vertex has a directed edge connecting it to every other vertex in the same component.
Kosaraju's algorithm is employed in practise for a number of reasons, including:
 Efficient Time Complexity: Kosaraju's approach has an efficient time complexity of O(V + E), where V is the number of graph vertices and E is the number of graph edges. The fact that it can locate SCCs in linear time makes it particularly effective for huge graphs. Because of its efficiency, Kosaraju's technique is well suited for realworld applications where graph sizes might be rather big.
 Simplicity and Ease of Implementation: As compared to other algorithms for locating SCCs, such as Tarjan's algorithm or Gabow's algorithm, Kosaraju's technique is relatively easy to comprehend and put into practise. It employs a simple twostep methodology that combines depthfirst traversal and reverse graph traversal, making it simple to implement in a variety of programming languages.
 Strongly Connected Components: SCCs have significant uses in a variety of fields, including database management systems, network analysis, social network analysis, and compilers, among others. Insights into the structure, connectivity, and behaviour of directed graphs can be gained by identifying SCCs, and these insights can be helpful in resolving practical issues.
 Robustness: Because each strongly connected component is treated as an independent subgraph, Kosaraju's approach is able to handle unconnected components. Even if a directed network has many disconnected components or certain vertices are inaccessible from other vertices, it can still find all SCCs in that graph.
 Flexibility: As a flexible tool for graph analysis and processing, Kosaraju's approach can be simply modified to accommodate variations of the fundamental problem, such as finding the largest SCC, finding the k largest SCCs, or discovering SCCs with extra constraints.
In conclusion, Kosaraju's approach is frequently used for discovering strongly linked components in directed graphs because of its effectiveness, simplicity, and application in a variety of fields.
Code implementation
Let's take an example for implementation of kosaraju's algorithm in Python:
Output
Strongly Connected Components:
[1, 3, 2]
[4, 6, 5]
Explanation:
In this implementation, an adjacency list is used by the Graph class to represent a directed graph. The graph's edges are added using the add edge method. With the DFS method, vertices are pushed onto a stack in finishing order after a depthfirst traversal.
A new graph with all edge directions reversed is the result of the get reversed graph method. To find components that are highly related, the DFS SCC technique traverses the reversed graph from depth to depth.
Kosaraju's approach is finally put into practise by combining these phases, and the kosaraju method then returns a list of the graph's strongly connected components.
Limitation of Kosaraju's Algorithm
Like any other algorithm, Kosaraju's has several drawbacks, such as:
 Memory usage: Kosaraju's technique calls for the graph to be stored as an adjacency list, which can use up a lot of memory for big graphs with millions or billions of vertices and edges. This may restrict its applicability to graphs with memory limitations or that cannot fit in memory.
 Computational complexity for dense graphs: Kosaraju's approach has a computational cost for dense graphs of O(V + E), although it might not work well for dense graphs with a lot of edges. Compared to previous SCC techniques made expressly for dense networks, the algorithm may take longer to perform in dense graphs where E is close to V2.
 Not parallelizable: Because Kosaraju's approach is fundamentally sequential and difficult to parallelize, its performance on distributed systems or parallel computing architectures may be constrained. For processing big graphs in distributed or parallel contexts, where parallelism is necessary for effective computation, this can be a hindrance.
 Lack of edge information: Edge information is lacking because Kosaraju's approach only considers the connectedness of vertices and ignores any other information about the edges, such as edge weights, directions, or labels. This may be a drawback in situations where the analysis or processing of strongly connected components depends on edge properties or semantics.
 Limited to directed graphs: Kosaraju's approach is restricted to directed graphs and cannot be directly applied to undirected graphs or other kinds of graphs, such as weighted graphs, multigraphs, or hypergraphs. It is intended exclusively for directed graphs. Without adjustments, it is not suited for locating connected components in undirected graphs.
 Output Format: A list of tightly connected components is the output format of Kosaraju's algorithm, which may not be the most practical or effective representation for all applications. For instance, additional processing can be necessary on Kosaraju's algorithm's output if the objective is to identify the largest strongly connected component or carry out other particular studies on the SCCs.
 Not the best option for any SCCrelated task: While Kosaraju's technique is effective at locating strongly connected components; it might not be the best option for other related tasks like locating the longest path in an SCC or figuring out whether or not a pair of adjacent vertices in an SCC can be reached. For certain particular jobs, different algorithms or methodologies might be better appropriate.
