Skip to content

Commit 5a3f6b0

Browse files
authored
Merge pull request cp-algorithms#1296 from pr4-kp/master
Updates to Bridge Searching in O(N+M)
2 parents 8d9bddc + 7324c91 commit 5a3f6b0

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/graph/bridge-searching.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,26 @@ Pick an arbitrary vertex of the graph $root$ and run [depth first search](depth-
2222

2323
Now we have to learn to check this fact for each vertex efficiently. We'll use "time of entry into node" computed by the depth first search.
2424

25-
So, let $tin[v]$ denote entry time for node $v$. We introduce an array $low$ which will let us check the fact for each vertex $v$. $low[v]$ is the minimum of $tin[v]$, the entry times $tin[p]$ for each node $p$ that is connected to node $v$ via a back-edge $(v, p)$ and the values of $low[to]$ for each vertex $to$ which is a direct descendant of $v$ in the DFS tree:
25+
So, let $\mathtt{tin}[v]$ denote entry time for node $v$. We introduce an array $\mathtt{low}$ which will let us store the node with earliest entry time found in the DFS search that a node $v$ can reach with a single edge from itself or its descendants. $\mathtt{low}[v]$ is the minimum of $\mathtt{tin}[v]$, the entry times $\mathtt{tin}[p]$ for each node $p$ that is connected to node $v$ via a back-edge $(v, p)$ and the values of $\mathtt{low}[to]$ for each vertex $to$ which is a direct descendant of $v$ in the DFS tree:
2626

27-
$$low[v] = \min \begin{cases} tin[v] \\ tin[p]& \text{ for all }p\text{ for which }(v, p)\text{ is a back edge} \\ low[to]& \text{ for all }to\text{ for which }(v, to)\text{ is a tree edge} \end{cases}$$
27+
$$\mathtt{low}[v] = \min \left\{
28+
\begin{array}{l}
29+
\mathtt{tin}[v] \\
30+
\mathtt{tin}[p] &\text{ for all }p\text{ for which }(v, p)\text{ is a back edge} \\
31+
\mathtt{low}[to] &\text{ for all }to\text{ for which }(v, to)\text{ is a tree edge}
32+
\end{array}
33+
\right\}$$
2834

29-
Now, there is a back edge from vertex $v$ or one of its descendants to one of its ancestors if and only if vertex $v$ has a child $to$ for which $low[to] \leq tin[v]$. If $low[to] = tin[v]$, the back edge comes directly to $v$, otherwise it comes to one of the ancestors of $v$.
35+
Now, there is a back edge from vertex $v$ or one of its descendants to one of its ancestors if and only if vertex $v$ has a child $to$ for which $\mathtt{low}[to] \leq \mathtt{tin}[v]$. If $\mathtt{low}[to] = \mathtt{tin}[v]$, the back edge comes directly to $v$, otherwise it comes to one of the ancestors of $v$.
3036

31-
Thus, the current edge $(v, to)$ in the DFS tree is a bridge if and only if $low[to] > tin[v]$.
37+
Thus, the current edge $(v, to)$ in the DFS tree is a bridge if and only if $\mathtt{low}[to] > \mathtt{tin}[v]$.
3238

3339
## Implementation
3440

3541
The implementation needs to distinguish three cases: when we go down the edge in DFS tree, when we find a back edge to an ancestor of the vertex and when we return to a parent of the vertex. These are the cases:
3642

37-
- $visited[to] = false$ - the edge is part of DFS tree;
38-
- $visited[to] = true$ && $to \neq parent$ - the edge is back edge to one of the ancestors;
43+
- $\mathtt{visited}[to] = false$ - the edge is part of DFS tree;
44+
- $\mathtt{visited}[to] = true$ && $to \neq parent$ - the edge is back edge to one of the ancestors;
3945
- $to = parent$ - the edge leads back to parent in DFS tree.
4046

4147
To implement this, we need a depth first search function which accepts the parent vertex of the current node.
@@ -101,3 +107,4 @@ Note that this implementation malfunctions if the graph has multiple edges, sinc
101107
* [SPOJ - Critical Edges](http://www.spoj.com/problems/EC_P/)
102108
* [Codeforces - Break Up](http://codeforces.com/contest/700/problem/C)
103109
* [Codeforces - Tourist Reform](http://codeforces.com/contest/732/problem/F)
110+
* [Codeforces - Non-academic problem](https://codeforces.com/contest/1986/problem/F)

0 commit comments

Comments
 (0)