Javatpoint Logo
Javatpoint Logo

Tarjan's Algorithm to Find Strongly Connected Components

Introduction

In this article, we'll delve into Tarjan's Algorithm, figure out its inward operations, and execute it in C.

Strongly Connected Components are fundamental designs in graph theory, addressing subsets of vertices where every vertex is reachable from every vertex inside the subset. Recognizing Strongly Connected Components in a directed graph is vital for different applications, for example, improving organization streams, circuit design, and resolving dependency connections. Tarjan's Algorithm gives an effective method for tracking down these Strongly Connected Components.

Introduction to Tarjan's Algorithm

Named after its creator, Robert Tarjan, Tarjan's Algorithm is a depth-first search-based calculation used to track down strongly associated parts in a directed graph. It proficiently recognizes SCCs while traversing the graph just a single time. This algorithm is liked for its simplicity, proficiency, and capacity actually to deal with large graphs.

Understanding the Algorithm

Tarjan's Algorithm utilizes DFS traversal to investigate the graph and distinguish SCCs. It maintains a few data structures during the traversal:

  • Stack: A stack is utilized to monitor visited vertices in the current DFS traversal.
  • Discovery Time and Low-link Value: Every vertex is assigned a discovery time demonstrating when it was first experienced during the DFS traversal. Furthermore, every vertex is assigned a low-link value, which addresses the smallest discovery time reachable from that vertex.

The algorithm continues as follows

  • Start a DFS traversal from an arbitrary starting vertex.
  • As the traversal advances, allocate discovery times and low-link values to every vertex.
  • Keep a stack of visited vertices to monitor the current traversal path.
  • While backtracking during DFS, check for vertices with lower low-link values reachable from the current vertex. This demonstrates the discovery of a strongly associated component.
  • When a strongly associated component is recognized, pop vertices off the stack until the current vertex is reached. These popped vertices form one strongly associated component.
  • Proceed with DFS traversal until all vertices are visited.

Code

Output:

Tarjan's Algorithm to Find Strongly Connected Components

Code Explanation

Header Files and Macros

  • h and stdlib.h are incorporated for standard input/output and memory allocation functions.
  • MAX_VERTICES is characterized as the maximum number of vertices in the graph. The min function is a helper function to track down the minimum of two numbers.

Global Variables

  • Arrays are proclaimed to store the graph, discovery times, low-link values, stack, visited vertices, and whether a vertex is on the stack.
  • stack_top monitors the top component index in the stackand time tracks the disclosure time during DFS traversal.

tarjan_scc Function

  • This function plays out a DFS traversal from a given vertex v and recognizes SCCs utilizing Tarjan's Algorithm.
  • It allows discovery times and low-link values to every vertex.
  • The function keeps a stack to monitor the current traversal path.
  • In the event that a back edge is experienced during traversal, it updates the low-link value of the current vertex accordingly.
  • At the point when a vertex is viewed as the root of a SCC, it pops vertices off the stack until the root is reached, printing the SCC.

main function

  • It takes input for the number of vertices and edges in the graph.
  • Then, at that point, it takes input for the edges and checks them in the adjacency matrix representation of the graph.
  • It repeats through every vertex and calls tarjan_scc on the off chance that the vertex has not been visited at this point.
  • Finally, it prints the SCCs tracked down in the graph.

Conclusion

Tarjan's Algorithm gives an efficient solution for tracking down strongly connected parts in a directed graph. By utilizing depth-first search traversal and maintaining relevant data structures, it can actually recognize SCCs. The C execution provided in this article offers a functional comprehension of how the algorithm functions and how it can be applied to real-world issues.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA