Algorithm Search⌘K

Proofs: Euler tour tree

These are the human proofs of Euler tour tree, at full length.

The operations of an Euler tour tree keep the Euler tour invariant full

This proof shows: correctness.

We use the invariant (E) of the page. For a tree TT, an Euler sequence of TT is a cyclic sequence of loop nodes and arc nodes with the three properties of (E). We first prove two facts about Euler sequences, then show that each operation maps Euler sequences of the old forest to Euler sequences of the new forest, and that each query reads the right value.

Lemma 1 (ends of a rotation). Let SS be an Euler sequence of a tree TT with at least two vertices, rotated so that it starts with the loop node (w,w)(w, w). Then the first arc of SS leaves ww and the last node of SS is an arc into ww.

Proof. By (E3), the loop node (w,w)(w, w) lies cyclically between an arc into ww and an arc out of ww. In the rotation that starts with (w,w)(w, w), the node cyclically before it is the last node of SS, and the next arc after it is the first arc of SS. A loop node is never between two arcs of different vertices, so the node before (w,w)(w,w) is an arc into ww (and not another loop node, since by (E3) each loop node sits between arcs and TT has an arc at ww). □\square

Lemma 2 (the two arcs of an edge). Let SS be an Euler sequence of a tree TT, let {u,v}\{u, v\} be an edge of TT, and let TuT_u and TvT_v be the two trees of TT without this edge, with u∈Tuu \in T_u and v∈Tvv \in T_v. Write S=X a Y b ZS = X\,a\,Y\,b\,Z, where {a,b}={(u,v),(v,u)}\{a, b\} = \{(u, v), (v, u)\} and aa comes first. Let ww be the head of aa. Then YY is an Euler sequence of the side Tw∈{Tu,Tv}T_w \in \{T_u, T_v\} that contains ww, and the cyclic sequence Z XZ\,X (equal, as a cyclic sequence, to XX followed by ZZ in the treap order) is an Euler sequence of the other side.

Proof. Every arc of TT other than aa and bb has both ends in TuT_u or both ends in TvT_v, because {u,v}\{u, v\} is the only edge between the two sides. The walk of (E1) is at ww just after aa. Each later arc of YY starts where the previous one ends, and none of them is aa or bb, so by induction along YY every arc of YY has both ends in TwT_w. The arc bb leaves the tail of aa's head, that is, bb starts at ww; so the last arc of YY ends at ww (or YY has no arc). Hence the arcs of YY form a closed walk at ww inside TwT_w. In the same way, the arcs of Z XZ\,X form a closed walk inside the other side, since they start after bb at the head of bb and end before aa at the tail of aa, which is the same vertex. Each arc of TwT_w occurs once in SS by (E2), and it is not in Z XZ\,X, so it is in YY; the same holds for the other side. So (E1) and (E2) hold for both parts.

For (E3), take a loop node (x,x)(x, x). If TT has at least two vertices, by (E3) for SS it lies between an arc into xx and an arc out of xx. If neither neighbor arc is aa or bb, both are in the same part as (x,x)(x, x), and xx is in the side of those arcs. If the arc before (x,x)(x, x) is aa, then x=wx = w and (x,x)(x, x) is in YY; if the arc after it is bb, then x=wx = w again, since bb starts at ww, and (x,x)(x, x) is in YY. If the arc after (x,x)(x,x) is aa, then xx is the tail of aa, which is in the other side, and (x,x)(x, x) is at the end of XX; if the arc before it is bb, then xx is the head of bb, in the other side, and (x,x)(x, x) is at the start of ZZ. So each part holds exactly the loop nodes of its side. A loop node next to aa or bb now has, in its part, the neighbor arcs that the closed walk of its part gives it, and if its side is a single vertex, the part is that loop node alone. □\square

Theorem. After each operation, the treaps of the structure hold one Euler sequence for each tree of the current forest, and each query returns the value of its definition.

Proof. By induction on the number of operations. At the start the forest has no edges, and each vertex vv has the sequence (v,v)(v, v), which satisfies (E). Assume that (E) holds before an operation.

Reroot (lines L1 and L2) replaces S=A BS = A\,B by B AB\,A, which is the same cyclic sequence, so (E) still holds.

Link(u,v)(u, v) (lines L4 to L6), where uu and vv are in different trees T1T_1 and T2T_2. After line L4, SuS_u starts with (u,u)(u, u) and SvS_v with (v,v)(v, v). Consider S=Su (u,v) Sv (v,u)S = S_u\,(u, v)\,S_v\,(v, u). If T1T_1 has an edge, by Lemma 1 SuS_u ends with an arc into uu, so the next arc (u,v)(u, v) continues the walk; if T1T_1 is the single vertex uu, SuS_u has no arc and the arc before (u,v)(u, v) cyclically is (v,u)(v, u), which ends at uu. The same argument at vv shows that (u,v)(u, v) is followed by the walk of SvS_v from vv back to vv, and then by (v,u)(v, u), which returns to uu, where the walk of SuS_u starts. So the arcs of SS form a closed walk (E1). It uses each arc of T1T_1 and T2T_2 once and the two new arcs once (E2). Each old loop node keeps its old neighbors except that (u,u)(u, u) now has (v,u)(v, u) before it and the first arc of SuS_u (or (u,v)(u, v)) after it, and (v,v)(v, v) has (u,v)(u, v) before it and the first arc of SvS_v (or (v,u)(v, u)) after it, all with the right ends (E3).

Cut(u,v)(u, v) (lines L7 to L10). Line L8 orders the two arcs by position, and line L9 writes the sequence as X a Y b ZX\,a\,Y\,b\,Z in treap order. A cyclic Euler sequence read from position 0 is also read cyclically, so Lemma 2 applies: YY is an Euler sequence of one side and XX followed by ZZ is one of the other. Line L10 stores these two sequences, and the two arc nodes are deleted.

Set value changes one loop value and recomputes the sums on the path to the treap root, so every stored sum is again the sum of its subtree.

Queries. By (E3), the loop nodes of a sequence are exactly the vertices of its tree, so line L3 answers connectivity, and the loop count and sum at the treap root (line L11) are the size and the value sum of the tree. For subtree_sum(v,p)\mathrm{subtree\_sum}(v, p), apply Lemma 2 to the rotation of the sequence that starts at position 0, with aa the first of (p,v)(p, v) and (v,p)(v, p). If (p,v)(p, v) comes first (i<ji < j), then w=vw = v, and the side of vv is the segment strictly between positions ii and jj (line L13). If (v,p)(v, p) comes first (j<ij < i), then w=pw = p, the segment strictly between jj and ii is the side of pp, and the side of vv is the rest; since arc nodes have value 0, its sum is the total minus the sum of positions j+1j + 1 to i−1i - 1 (line L14). ■\blacksquare

Back to Euler tour tree