![]() |
VOOZH | about |
Given an undirected Connected graph of V vertices and E edges. A critical connection is an edge that, if removed, will make some nodes unable to reach some other nodes, the task is to find all critical connections in the graph.
Note: There are many possible orders for the answer. You are supposed to print the edges in sorted order, and also an edge should be in sorted order too. So if there's an edge between nodes 1 and 2, you should print it like (1,2) and not (2,1).
Examples:
Input:
👁 ImageOutput: 0 1 0 2 Explanation: Both the edges in the graph are Crtical connections.
Input:
👁 ImageOutput: 2 3 Explanation: The edge between nodes 2 and 3 is the only Critical connection in the given graph.
Approach: To solve the problem follow the below idea:
- An edge is a critical connection, if and only if it is not in a cycle.
- So, if we know how to find cycles, and discard all edges in the cycles, then the remaining connections are a complete collection of critical connections.
Follow the steps to solve the problem:
dfs function returns the minimum rank it finds. During a step of search from node u to its neighbor v, if dfs(v) returns something smaller than or equal to rank(u), then u knows its neighbor v helped it to find a cycle back to u or u's ancestor. So u knows it should discard the edge (u, v) which is in a cycle.Below is the Implementation of the above approach:
0 1 0 2
Time Complexity: O(V) + ElogE
Auxiliary Space : O(V)