14. Find all Critical Connections in the Graph
The problem can be found at the following link: Question Link
My Approach
To find all critical connections in the graph, I used Tarjan's algorithm, which is based on depth-first search (DFS). Here's a brief explanation of the approach:
Initialize variables
timer
,vis
,dis
,low
, andans
.Implement a depth-first search (DFS) function to traverse the graph.
During DFS traversal, mark the visited nodes and update the discovery time (
dis
) and lowest reachable node (low
) for each node.If a backward edge is found (i.e.,
low[it] > dis[node]
), it indicates a critical connection. Store the edge in theans
vector.Sort the
ans
vector to maintain order.Return the
ans
vector containing all critical connections.
Note:
I personally recommend this video for detailed learning about Tarjan's algorithm.
Time and Auxiliary Space Complexity
Time Complexity : The time complexity of Tarjan's algorithm is O(V + E), where V is the number of vertices and E is the number of edges in the graph.
Auxiliary Space Complexity : The auxiliary space complexity is
O(V + E)
, where V is the number of vertices and E is the number of edges in the graph. This space is utilized for maintaining thevis
,dis
,low
, andans
vectors.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated