Critical Connections in a Network
Find every connection in a server network whose removal would cut some servers off from the others.
A network has n[a, b]connections
Example 1
Output: [[1,3]]
Explanation: servers 0, 1 and 2 are joined in a triangle, so any one of those three connections can go and the other two still link them. Server 3 is joined only by 1 - 3.
Example 2
Output: [[0,1]]
The Idea
Picture a few servers in a room, wired together with cables. A cable can be unplugged or cut, so it is worth knowing which single cable would split the network if it failed. In Example 1, servers 0, 1 and 2 are wired in a triangle, and server 3 has one cable, to server 1.
Try cutting the cables one at a time. Cut 0 - 1, and server 0 still reaches 1 by going through 2. The same is true for 1 - 2 and for 2 - 0. Cut 1 - 3, though, and server 3 is on its own. So 1 - 3 is the only critical connection.
What made the triangle cables safe? Each one is part of a loop, so the rest of the loop is a detour around it. A cable that lies on no loop has no detour. So the question becomes: which cables lie on no loop, and can we find them without cutting each cable and checking the whole network again?
We can, with one walk through the network. Start at server 0 and keep following cables to servers not visited yet, going as deep as possible before turning back. A walk like this is called a depth-first search, or DFS. Number the servers in the order the walk first reaches them, and call that number the server's visit time.
Look at the picture below. The blue arrows are the walk: 0, then 1, then 2. At server 2, the cable to 0 leads to a server visited earlier, drawn as the dashed arrow labelled "back edge". A cable like that, back to an earlier server, is called a back edge, and it closes a loop.
Server 2 has no unvisited neighbour left, so the walk returns to 1 and follows the orange arrow to server 3, which gets visit time 3. Each box also says how far back a server reaches: the smallest visit time that the server, or any server the walk reached from it, can get to with one back edge.
Server 2 reaches back to 0 through the back edge 2 to 0. Server 1 reaches back to 0 as well, because the walk reached 2 from 1. Server 3 has no cable except the one it came in by, so it reaches back only to its own visit time, 3.
That is the key insight. The walk reached 3 from 1, and server 3 cannot reach back to 1 or anything earlier, so there is no way around the cable 1 - 3 and it is critical. Server 2 reaches back to 0, earlier than 1, so the cable 1 - 2 sits on a loop and is safe.
Solution Approach
Turning that idea into code means settling five things: storing the network, what to record for each server, the test for a critical connection, which cable to ignore, and networks in several pieces. First, though, a look at why cutting each cable in turn is too slow.
Cutting each connection in turn
Why not do what we did by hand? Remove one connection, walk the network again, and see whether the walk still reaches every server it reached before. With n servers and E connections, each check takes about n + E steps, and there is one check per connection. That is about E × (n + E) steps, while the single walk with visit times needs about n + E.
Storing the network as an adjacency list
How does the walk find a server's cables quickly? Before walking, the code gives every server a list of its neighbours, the servers it has a cable to. A connection [a, b]
Visit times and how far back a server reaches
What does the walk record for each server? Two numbers, kept in two arrays. disc[u]low[u]
low[u]disc[u]disc[v]low[v]low[1]low[2]
The test for a critical connection
When is a cable critical? Take a cable u - v that the walk followed from u to v. It is critical when low[v] > disc[u]low[3]disc[1]low[2]
In graph theory a critical connection is called a bridge, and this way of finding all bridges in one walk is known as Tarjan's bridge-finding algorithm.
Ignoring the cable we came in by
Which cables count as back edges? Server 3's list holds 1, and server 1 was visited earlier. If that counted, low[3]
Networks in several pieces
What if some servers cannot reach each other at all? A walk from server 0 then never visits them. So the code tries every server in turn and starts a new walk from each one whose disc
Java Solution
The method criticalConnectionsdfs
- First, build the adjacency list
: one empty list per server, then each connection added to the lists of both its servers.adj - Create the arrays
anddisc with n entries each, and filllow with -1. Set the counterdisc to 0 and start an empty listtime for the answer.bridges - Loop over the servers. For each server
whosei is still -1, start a walk withdisc , where -1 means there is no parent.dfs(i, -1) - In
, give u the next visit time:dfs(u, parent) anddisc[u] both getlow[u] , andtime goes up by one. Then a loop goes over u's neighbourstime , skippingv .parent - If
is not visited yet, callv . When it returns, lowerdfs(v, u) tolow[u] if that is smaller, and iflow[v] , addlow[v] > disc[u] to[u, v] .bridges - If
was visited already, the cable u - v closes a loop, so lowerv tolow[u] if that is smaller.disc[v] - When every walk is done, return
.bridges
The main
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CriticalConnectionsInNetwork {
private List<List<Integer>> adj;
private int[] disc;
private int[] low;
private int time;
private List<List<Integer>> bridges;
public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (List<Integer> c : connections) { // each connection goes in both lists
adj.get(c.get(0)).add(c.get(1));
adj.get(c.get(1)).add(c.get(0));
}
disc = new int[n];
low = new int[n];
Arrays.fill(disc, -1); // -1 means not visited yet
time = 0;
bridges = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (disc[i] == -1) {
dfs(i, -1); // a new piece of the network
}
}
return bridges;
}
private void dfs(int u, int parent) {
disc[u] = low[u] = time++;
for (int v : adj.get(u)) {
if (v == parent) {
continue; // not the connection we came in on
}
if (disc[v] == -1) {
dfs(v, u);
low[u] = Math.min(low[u], low[v]);
if (low[v] > disc[u]) { // v's part cannot get back to u or earlier
bridges.add(Arrays.asList(u, v));
}
} else {
low[u] = Math.min(low[u], disc[v]); // a shortcut to an earlier server
}
}
}
public static void main(String[] args) {
CriticalConnectionsInNetwork net = new CriticalConnectionsInNetwork();
System.out.println(net.criticalConnections(4,
List.of(List.of(0, 1), List.of(1, 2), List.of(2, 0), List.of(1, 3))));
System.out.println(net.criticalConnections(2, List.of(List.of(0, 1))));
System.out.println(net.criticalConnections(4,
List.of(List.of(0, 1), List.of(1, 2), List.of(2, 3))));
System.out.println(net.criticalConnections(4,
List.of(List.of(0, 1), List.of(1, 2), List.of(2, 3), List.of(3, 0))));
System.out.println(net.criticalConnections(4, List.of(List.of(0, 1), List.of(2, 3))));
System.out.println(net.criticalConnections(6,
List.of(List.of(0, 1), List.of(1, 2), List.of(2, 0),
List.of(2, 3), List.of(3, 4), List.of(4, 5), List.of(5, 3))));
}
}
Running it prints:
[[1, 3]]
[[0, 1]]
[[2, 3], [1, 2], [0, 1]]
[]
[[0, 1], [2, 3]]
[[2, 3]]
The first line, [[1, 3]][[0, 1]][2, 3][1, 2][0, 1]
The ring on the fourth line prints [][[0, 1], [2, 3]][2, 3][2, 3]
The test must use >>=low[v] >= disc[u][[1, 3], [0, 1]]low[1]disc[0][[0, 1]][]
Complexity
The work is building the adjacency list plus one walk that visits each server once.
- Time: O(n + E), because
runs only on servers whosedfs is still -1, so each of the n servers is visited once, and each of the E connections is looked at twice, once from each end's list.disc - Space: O(n + E), because the adjacency list holds 2E entries,
anddisc hold n each, and on a chain of servers the calls tolow nest n deep.dfs
The walk itself is explained step by step in