Skip to content

Commit ef884a3

Browse files
committed
Translate "Finding Bridges Online"
1 parent 2e145c8 commit ef884a3

File tree

4 files changed

+270
-2
lines changed

4 files changed

+270
-2
lines changed

src/data_structures/disjoint_set_union.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ Indeed, for rooting a tree at vertex $v$ we must go from the vertex to the old r
512512
513513
However in reality it isn't so bad, we can just re-root the smaller of the two trees similar to the ideas in the previous sections, and get $O(\log n)$ on average.
514514
515-
More details (including proof of the time complexity) will be in the article **Online bridge finding in a graph in $O(\log n)$ on average** (currently not yet translated).
515+
More details (including proof of the time complexity) can be found in the article [Finding Bridges Online](./graph/bridge-searching-online.html).
516516
517517
## Historical retrospective
518518

src/graph/bridge-searching-online.md

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
<!--?title Finding Bridges Online -->
2+
# Finding Bridges Online
3+
4+
We are given an undirected graph.
5+
A bridge is an edge whose removal makes the graph disconnected (or, more precisely, increases the number of connected components).
6+
Our task is to find all the bridges in the given graph.
7+
8+
Informally this task can be put as follows:
9+
we have to find all the "important" roads on the given road map, i.e. such roads that the removal of any of them will lead to some cities being unreachable from others.
10+
11+
There is already the article [Finding Bridges in $O(N+M)$](./graph/bridge-searching.html) which solves this task with a [Depth First Search](./graph/depth-first-search.html) traversal.
12+
This algorithm will be much more complicated, but it has one big advantage:
13+
the algorithm described in this article works online, which means that the input graph doesn't have to be not known in advance.
14+
The edges are added once at a time, and after each addition the algorithm recounts all the bridges in the current graph.
15+
In other words the algorithm is designed to work efficiently on a dynamic, changing graph.
16+
17+
More rigorously the statement of the problem is as follows:
18+
Initially the graph is empty and consists of $n$ vertices.
19+
Then we receive pairs of vertices $(a, b)$, which denote an edge added to the graph.
20+
After each received edge, i.e. after adding each edge, output the current number of bridges in the graph.
21+
22+
It is also possible to maintain a list of all bridges as well as explicitly support the 2-edge-connected components.
23+
24+
The algorithm described below works in $O(n \log n + m)$ time, where $m$ is the number of edges.
25+
The algorithm is based on the data structure [Disjoint Set Union](./data_structures/disjoint_set_union.html).
26+
However the implementation in this article takes $O(n \log n + m \log n)$ time, because it uses the simplified version of the DSU without Union by Rank.
27+
28+
## Algorithm
29+
30+
First lets define a $k$-edge-connected component:
31+
it is a connected component that remains connected whenever you remove fewer than $k$ edges.
32+
33+
It is very easy to see, that the bridges partition the graph into 2-edge-connected components.
34+
If we compress each of those 2-edge-connected components into vertices and only leave the bridges as edges in the compressed graph, then we obtain an acyclic graph, i.e. a forest.
35+
36+
The algorithm described below maintains this forest explicitly as well as the 2-edge-connected components.
37+
38+
It is clear that initially, when the graph is empty, it contains $n$ 2-edge-connected components, which by themselves are not connect.
39+
40+
When adding the next edge $(a, b)$ there can occur three situations:
41+
42+
* Both vertices $a$ and $b$ are in the same 2-edge-connected component - then this edge is not a bridge, and does not change anything in the forest structure, so we can just skip this edge.
43+
44+
Thus, in this case the number of bridges does not change.
45+
46+
* The vertices $a$ and $b$ are in completely different connected components, i.e. each one is part of a different tree.
47+
In this case, the edge $(a, b)$ becomes a new bridge, and these two trees are combined into one (and all the old bridges remain).
48+
49+
Thus, in this case the number of bridges increases by one.
50+
51+
* The vertices $a$ and $b$ are in one connected component, but in different 2-edge-connected components.
52+
In this case, this edge forms a cycle along with some of the old bridges.
53+
All these bridges end being bridges, and the resulting cycle must be compressed into a new 2-edge-connected component.
54+
55+
Thus, in this case the number of bridges decreases by two or more.
56+
57+
Consequently the whole task is reduced to the effective implementation of all these operations over the forest of 2-edge-connected components.
58+
59+
## Data Structures for storing the forest
60+
61+
The only data structure that we need is [Disjoint Set Union](./data_structures/disjoint_set_union.html).
62+
In fact we will make two copies of this structure:
63+
one will be to maintain the connected components, the other to maintain the 2-edge-connected components.
64+
And in addition we store the structure of the trees in the forest of 2-edge-connected components via pointers:
65+
Each 2-edge-connected component will store the index `par[]` of its ancestor in the tree.
66+
67+
We will now consistently disassemble every operation that we need to learn to implement:
68+
69+
* Check whether the two vertices lie in the same connected / 2-edge-connected component.
70+
It is done with the usual DSU algorithm, we just find and compare the representatives of the DSUs.
71+
72+
* Joining two trees for some edge $(a, b)$.
73+
Since it could turn out that neither the vertex $a$ nor the vertex $b$ are the roots of their trees, the only way to connect these two trees is to re-root one of them.
74+
For example you can re-root the tree of vertex $a$, and then attach it to another tree by setting the ancestor of $a$ to $b$.
75+
76+
However the question about the effectiveness of the re-rooting operation arises:
77+
in order to re-root the tree with the root $r$ to the vertex $v$, it is necessary to necessary to visit all vertices on the path between $v$ and $r$ and redirect the pointers `par[]` in the opposite direction, and also change the references to the ancestors in the DSU that is responsible for the connected components.
78+
79+
Thus, the cost of re-rooting is $O(h)$, where $h$ is the height of the tree.
80+
You can make an even worse estimate by saying that the cost is $O(\text{size})$ where $\text{size}$ is the number of vertices in the tree.
81+
The final complexity will not differ.
82+
83+
We now apply a standard technique: we re-root the tree that contains fewer vertices.
84+
Then it is intuitively clear that the worst case is when two trees of approximately equal sizes are combined, but then the result is a tree of twice the size.
85+
This does not allow this situation to happen many times.
86+
87+
In general the total cost can be written in the form of a recurrence:
88+
$$T(n) = \max_{k = 1 \ldots n-1} \left\\{ T(k) + T(n - k) + O(\min(k, n - k))\right\\}$$
89+
$T(n)$ is the number of operations necessary to obtain a tree with $n$ vertices by means of re-rooting and unifying trees.
90+
A tree of size $n$ can be created by combining two smaller trees of size $k$ and $n - k$.
91+
This recurrence is has the solution $T(n) = O (n \log n)$.
92+
93+
Thus, the total time spent on all re-rooting operations will be $O(n \log n)$ if we always re-root the smaller of the two trees.
94+
95+
We will have to maintain the size of each connected component, but the data structure DSU makes this possible without difficulty.
96+
97+
* Searching for the cycle formed by adding a new edge $(a, b)$.
98+
Since $a$ and $b$ are already connected in the tree we need to find the [Lowest Common Ancestor](./graph/lca.html) of the vertices $a$ and $b$.
99+
The cycle will consist of the paths from $b$ to the LCA, from the LCA to $b$ and the edge $a$ to $b$.
100+
101+
After finding the cycle we compress all vertices of the detected cycle into one vertex.
102+
This means that we already have a complexity proportional to the cycle length, which means that we also can use any LCA algorithm proportional to the length, and don't have to use any fast one.
103+
104+
Since all information about the structure of the tree is available is the ancestor array `par[]`, the only reasonable LCA algorithm is the following:
105+
mark the vertices $a$ and $b$ as visited, then we go to their ancestors `par[a]` and `par[b]` and mark them, then advance to their ancestors and so on, until we reach an already marked vertex.
106+
This vertex is the LCA that we are looking for, and we can find the vertices on the cycle by traversing the path from $a$ and $b$ to the LCA again.
107+
108+
It is obvious that the complexity of this algorithm is proportional to the length of the desired cycle.
109+
110+
* Compression of the cycle by adding a new edge $(a, b)$ in a tree.
111+
112+
We need to create a new 2-edge-connected component, which will consist of all vertices of the detected cycle (also the detected cycle itself could consist of some 2-edge-connected components, but this does not change anything).
113+
In addition it is necessary to compress them in such a way that the structure of the tree is not disturbed, and all pointers `par[]` and two DSUs are still correct.
114+
115+
The easiest way to achieve this is to compress all the vertices of the cycle to their LCA.
116+
In fact the LCA is the highest of the vertices, i.e. its ancestor pointer `par[]` remains unchanged.
117+
For all the other vertices of the loop the ancestors do not need to be updated, since these vertices simply cease to exists.
118+
But in the DSU of the 2-edge-connected components all these vertices will simply point to the LCA.
119+
120+
We will implement the DSU of the 2-edge-connected components without the Union by rank optimization, therefore we will get the complexity $O(\log n)$ on average per query.
121+
To achieve the complexity $O(1)$ on average per query, we need to combine the vertices of the cycle according to Union by rank, and then assign `par[]` accordingly.
122+
123+
## Implementation
124+
125+
Here is the final implementation of the whole algorithm.
126+
127+
As mentioned before, for the sake of simplicity the DSU of the 2-edge-connected components is written without Union by rank, therefore the resulting complexity will be $O(\log n)$ on average.
128+
129+
Also in this implementation the bridges themselves are not stored, only their count `bridges`.
130+
However it will not be difficult to create a `set` of all bridges.
131+
132+
Initially you call the function `init()`, which initializes the two DSUs (creating a separate set for each vertex, and setting the size equal to one), and sets the ancestors `par`.
133+
134+
The main function is `add_edge(a, b)`, which processes and adds a new edge.
135+
136+
```cpp
137+
vector<int> par, dsu_2ecc, dsu_cc, dsu_cc_size;
138+
int bridges;
139+
int lca_iteration;
140+
vector<int> last_visit;
141+
142+
void init(int n) {
143+
par.resize(n);
144+
dsu_2ecc.resize(n);
145+
dsu_cc.resize(n);
146+
dsu_cc_size.resize(n);
147+
lca_iteration = 0;
148+
last_visit.assign(n, 0);
149+
for (int i=0; i<n; ++i) {
150+
dsu_2ecc[i] = i;
151+
dsu_cc[i] = i;
152+
dsu_cc_size[i] = 1;
153+
par[i] = -1;
154+
}
155+
bridges = 0;
156+
}
157+
158+
int find_2ecc(int v) {
159+
if (v == -1)
160+
return -1;
161+
return dsu_2ecc[v] == v ? v : dsu_2ecc[v] = find_2ecc(dsu_2ecc[v]);
162+
}
163+
164+
int find_cc(int v) {
165+
v = find_2ecc(v);
166+
return dsu_cc[v] == v ? v : dsu_cc[v] = find_cc(dsu_cc[v]);
167+
}
168+
169+
void make_root(int v) {
170+
v = find_2ecc(v);
171+
int root = v;
172+
int child = -1;
173+
while (v != -1) {
174+
int p = find_2ecc(par[v]);
175+
par[v] = child;
176+
dsu_cc[v] = root;
177+
child = v;
178+
v = p;
179+
}
180+
dsu_cc_size[root] = dsu_cc_size[child];
181+
}
182+
183+
void merge_path (int a, int b) {
184+
++lca_iteration;
185+
vector<int> path_a, path_b;
186+
int lca = -1;
187+
while (lca == -1) {
188+
if (a != -1) {
189+
a = find_2ecc(a);
190+
path_a.push_back(a);
191+
if (last_visit[a] == lca_iteration)
192+
lca = a;
193+
last_visit[a] = lca_iteration;
194+
a = par[a];
195+
}
196+
if (b != -1) {
197+
path_b.push_back(b);
198+
b = find_2ecc(b);
199+
if (last_visit[b] == lca_iteration)
200+
lca = b;
201+
last_visit[b] = lca_iteration;
202+
b = par[b];
203+
}
204+
205+
}
206+
207+
for (int v : path_a) {
208+
dsu_2ecc[v] = lca;
209+
if (v == lca)
210+
break;
211+
--bridges;
212+
}
213+
for (int v : path_b) {
214+
dsu_2ecc[v] = lca;
215+
if (v == lca)
216+
break;
217+
--bridges;
218+
}
219+
}
220+
221+
void add_edge(int a, int b) {
222+
a = find_2ecc(a);
223+
b = find_2ecc(b);
224+
if (a == b)
225+
return;
226+
227+
int ca = find_cc(a);
228+
int cb = find_cc(b);
229+
230+
if (ca != cb) {
231+
++bridges;
232+
if (dsu_cc_size[ca] > dsu_cc_size[cb]) {
233+
swap(a, b);
234+
swap(ca, cb);
235+
}
236+
make_root(a);
237+
par[a] = dsu_cc[a] = b;
238+
dsu_cc_size[cb] += dsu_cc_size[a];
239+
} else {
240+
merge_path(a, b);
241+
}
242+
}
243+
```
244+
245+
The DSU for the 2-edge-connected components is stored in the vector `dsu_2ecc`, and the function returning the representative is `find_2ecc(v)`.
246+
This function is used many times in the rest of the code, since after the compression of several vertices into one all these vertices cease to exist, and instead only the leader has the correct ancestor `par` in the forest of 2-edge-connected components.
247+
248+
The DSU for the connected components is stored in the vector `dsu_cc`, and there is also an additional vector `dsu_cc_size` to store the component sizes.
249+
The function `find_cc(v)` returns the leader of the connectivity component (which is actually the root of the tree).
250+
251+
The re-rooting of a tree `make_root(v)` works as descibed above:
252+
if traverses from the vertex $v$ via the ancestors to the root vertex, each time redirecting the ancestor `par` in the opposite direction.
253+
The link to the representative of the connected component `dsu_cc` is also updated, so that it points to the new root vertex.
254+
After re-rooting we have to assign the new root the correct size of the connected component.
255+
Also we have to be careful that we call `find_2ecc()` to get the representatives of the 2-edge-connected component, rather than some other vertex that have already been compressed.
256+
257+
The cycle finding and compression function `merge_path(a, b)` is also implemented as descibed above.
258+
It searches for the LCA of $a$ and $b$ be rising these nodes in parallel, until we meet a vertex for the second time.
259+
For efficiency purposes we choose a unique identifier for each LCA finding call, and mark the traversed vertices with it.
260+
This works in $O(1)$, while other approaches like using $set$ perform worse.
261+
The passed paths are stored in the vectors `path_a` and `path_b`, and we use them to walk through them a second time up to the LCA, thereby obtaining all vertices of the cycle.
262+
All the vertices of the cycle get compressed by attaching them to the LCA, hence the average complexity is $O(\log n)$ (since we don't use Union by rank).
263+
All the edges we pass have been bridges, so we subtract 1 for each edge in the cycle.
264+
265+
Finally the query function `add_edge(a, b)` determines the connected components in which the vertices $a$ and $b$ lie.
266+
If they lie in different connectivity components, then a smaller tree is re-rooted and then attached to the larger tree.
267+
Otherwise if the vertices $a$ and $b$ lie in one tree, but in different 2-edge-connected components, then the function `merge_path(a, b)` is called, which will detect the cycle and compress it into one 2-edge-connected component.

src/graph/bridge-searching.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Informally, the problem is formulated as follows: given a map of cities connecte
88

99
The algorithm described here is based on [depth first search](./graph/depth-first-search.html) and has $O(N+M)$ complexity, where $N$ is the number of vertices and $M$ is the number of edges in the graph.
1010

11-
Note that there also exists an [online algorithm for finding bridges (ru)](http://e-maxx.ru/algo/bridge_searching_online) - unlike the offline algorithm described here, the online algorithm is able to maintain the list of all bridges in a changing graph (assuming that the only type of change is addition of new edges).
11+
Note that there also is also the article [Finding Bridges Online](./graph/bridge-searching-online.html) - unlike the offline algorithm described here, the online algorithm is able to maintain the list of all bridges in a changing graph (assuming that the only type of change is addition of new edges).
1212

1313
## Algorithm
1414

src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ especially popular in field of competitive programming.*
8383
- [Kirchhoff Theorem](./graph/kirchhoff-theorem.html)
8484
- [Topological Sorting](./graph/topological-sort.html)
8585
- [Finding Bridges in O(N+M)](./graph/bridge-searching.html)
86+
- [Finding Bridges Online](./graph/bridge-searching-online.html)
8687
- [Finding Articulation Points in O(N+M)](./graph/cutpoints.html)
8788
- [Checking a graph for acyclicity and finding a cycle in O(M)](./graph/finding-cycle.html)
8889
- [Finding a Negative Cycle in the Graph](./graph/finding-negative-cycle-in-graph.html)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy