Tutorialspoint
Problem
Solution
Submissions

Critical Connections in a Network

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to find all critical connections in a network. A critical connection is an edge that, if removed, will make some nodes unable to reach each other. Given a network with n nodes labeled from 0 to n-1 and a list of connections (edges) where each connection is represented by a pair [u, v] indicating that there is an undirected edge between nodes u and v, return all critical connections in the network in any order.

Example 1
  • Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
  • Output: [[1,3]]
  • Explanation:
    • The edge [1,3] is the only critical connection in the network.
    • If this edge is removed, node 3 will be disconnected from the rest of the network.
Example 2
  • Input: n = 5, connections = [[0,1],[1,2],[2,0],[1,3],[3,4]]
  • Output: [[1,3],[3,4]]
  • Explanation:
    • The edges [1,3] and [3,4] are critical connections.
    • If either is removed, the network will have disconnected components.
Constraints
  • 1 ≤ n ≤ 10^5
  • 1 ≤ connections.length ≤ 10^5
  • connections[i][0] != connections[i][1]
  • No repeated connections
  • Time Complexity: O(V + E) where V is number of vertices and E is number of edges
  • Space Complexity: O(V + E)
AlgorithmsEYPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use Tarjan's algorithm to find bridges in the graph
  • Implement a DFS traversal to discover and classify edges
  • Keep track of discovery times and lowest reachable vertex
  • An edge is a bridge (critical connection) if the lowest reachable vertex from v is greater than the discovery time of u
  • Return all identified bridges as the answer

Steps to solve by this approach:

 Step 1: Build an adjacency list representation of the graph.
 Step 2: Initialize arrays to track discovery time and lowest reachable node for each vertex.
 Step 3: Perform a depth-first search (DFS) traversal of the graph.
 Step 4: For each vertex, track the discovery time and update the lowest reachable node.
 Step 5: During DFS, when we visit a node's neighbor, we update the lowest reachable node.
 Step 6: If the lowest reachable from a neighbor is greater than the discovery time of the current node, the edge is a bridge.
 Step 7: Collect and return all bridge edges identified during the traversal.

Submitted Code :