Def: Bridge is an edge, when removed, will disconnect the graph (or increase the number of connected components by 1).
One observation regarding bridges in graph; none of the edges that belong to a loop can be a bridge. So in a graph such as A--B--C--A
, removing any of the edge A--B
, B--C
and C--A
will not disconnect the graph. But, for an undirected graph, the edge A--B
implies B--A
; and this edge could still be a bridge, where the only loop it is in is A--B--A
. So, we should consider only those loops formed by a back edge. This is where the parent information you’ve passed in the function argument helps. It will help you to not use the loops such as A--B--A
.
Now to identify the back edge (or the loop), A--B--C--A
we use the low
and pre
arrays. The array pre
is like the visited
array in the dfs algorithm; but instead of just flagging that the vertex as visited, we identify each vertex with a different number (according to its position in the dfs tree). The low
array helps to identify if there is a loop. The low
array identifies the lowest numbered (from pre
array) vertex that the current vertex can reach.
Lets work through this graph A--B--C--D--B
.
Starting at A
dfs: ^ ^ ^ ^ ^
pre: 0 -1 -1 -1 -1 0--1 -1 -1 1 0--1--2 -1 1 0--1--2--3 1 0--1--2--3--1
graph: A--B--C--D--B A--B--C--D--B A--B--C--D--B A--B--C--D--B A--B--C--D--B
low: 0 -1 -1 -1 -1 0--1 -1 -1 1 0--1--2 -1 1 0--1--2--3 1 0--1--2--3->1
At this point, you’ve encountered a cycle/loop in graph. In your code if (pre[w] == -1)
will be false this time. So, you’ll enter the else part. The if statement there is checking if B
is the parent vertex of D
. It is not, so D
will absorb B
‘s pre
value into low
. Continuing the example,
dfs: ^
pre: 0--1--2--3
graph: A--B--C--D
low: 0--1--2--1
This low
value of D
propagates back to C
through the code low[v] = Math.min(low[v], low[w]);
.
dfs: ^ ^ ^
pre: 0--1--2--3--1 0--1--2--3--1 0--1--2--3--1
graph: A--B--C--D--B A--B--C--D--B A--B--C--D--B
low: 0--1--1--1--1 0--1--1--1--1 0--1--1--1--1
Now, that the cycle/loop is identified, we note that the vertex A
is not part of the loop. So, you print out A--B
as a bridge. The code low['B'] == pre['B']
means an edge to B
will be a bridge. This is because, the lowest vertex we can reach from B
is B
itself.
Hope this explanation helps.