Algorithm Search⌘K

Proofs: Interval tree search

These are the human proofs of Interval tree search, at full length.

Interval tree search reports exactly the matches and expands at most (k + 1) h nodes full

This proof shows: correctness, termination.

We use the pseudocode of the page. TT is a binary search tree of intervals ⟨l,r⟩\langle l, r \rangle whose in-order sequence is sorted by left end, so every interval in the left subtree of a node xx has l≤x.ll \le x.l and every interval in its right subtree has l≥x.ll \ge x.l. Each node has x.maxx.\mathit{max} equal to the largest right end in its subtree, and nil.max=−∞\mathrm{nil}.\mathit{max} = -\infty. The test reach\mathrm{reach} is upward closed, so reach(r)\mathrm{reach}(r) and r≤r′r \le r' give reach(r′)\mathrm{reach}(r'); the test start\mathrm{start} is downward closed, so start(l)\mathrm{start}(l) and l′≤ll' \le l give start(l′)\mathrm{start}(l'). An interval matches when reach(r)\mathrm{reach}(r) and start(l)\mathrm{start}(l). The height hh is the number of nodes on a longest root-to-leaf path.

Lemma 1 (pruning by max). If reach(x.max)\mathrm{reach}(x.\mathit{max}) fails, no interval below xx matches.

Proof. Each right end rr below xx has r≤x.maxr \le x.\mathit{max}. If reach(r)\mathrm{reach}(r) held, upward closure would give reach(x.max)\mathrm{reach}(x.\mathit{max}). □\square

Lemma 2 (pruning by order). If start(x.l)\mathrm{start}(x.l) fails, then xx and every interval of its right subtree fail start\mathrm{start}.

Proof. Each such interval has l≥x.ll \ge x.l. If start(l)\mathrm{start}(l) held, downward closure would give start(x.l)\mathrm{start}(x.l). □\square

Theorem 1 (REPORT). REPORT(x,reach,start,out)(x, \mathrm{reach}, \mathrm{start}, out) appends exactly the matching intervals below xx, each once, in in-order, and terminates.

Proof. By induction on the number of nodes below xx. Each call either returns at line L1 or makes calls only on the two children, which have fewer nodes, so the recursion terminates. If x=nilx = \mathrm{nil} nothing is appended, which is right. If line L1 returns because reach(x.max)\mathrm{reach}(x.\mathit{max}) fails, nothing matches below xx by Lemma 1. Otherwise line L2 appends the matches of the left subtree, by induction. If line L3 returns, then xx and its right subtree have no match by Lemma 2. Otherwise start(x.l)\mathrm{start}(x.l) holds, so xx matches if and only if reach(x.r)\mathrm{reach}(x.r), the test of line L4, and line L5 appends the matches of the right subtree by induction. The left subtree, xx and the right subtree are disjoint and appended in in-order. □\square

Theorem 2 (SEARCH). SEARCH returns a matching interval if one exists and nil otherwise, after at most hh passes of the loop.

Proof. Invariant: if some interval of TT matches, then one lies in the subtree of xx, where the subtree of nil is empty. It holds with xx the root. Suppose it holds and the loop body runs, so xx does not match.

Line L8 goes left. Then reach(x.left.max)\mathrm{reach}(x.\mathit{left}.\mathit{max}) holds, so the left subtree has an interval ⟨l′,r′⟩\langle l', r' \rangle with r′=x.left.maxr' = x.\mathit{left}.\mathit{max} and reach(r′)\mathrm{reach}(r'). If no interval of the left subtree matches, this one fails start(l′)\mathrm{start}(l'). Then xx and the intervals of the right subtree have l≥l′l \ge l' and fail start\mathrm{start} by downward closure, so nothing below xx matches; by the invariant nothing in TT matches, and the invariant holds for the left child. If some interval of the left subtree matches, it holds as well.

Line L8 goes right. Then the left subtree is empty or reach(x.left.max)\mathrm{reach}(x.\mathit{left}.\mathit{max}) fails, and by Lemma 1 nothing in it matches; xx does not match. So any match below xx is in the right subtree.

Each pass moves one level down, so there are at most hh passes. If the loop stops at a node, the test of line L7 says that it matches. If it stops at nil, the invariant says that nothing matches. □\square

Theorem 3 (cost of REPORT). Suppose REPORT(T.root)(T.\mathit{root}) outputs kk intervals. Call a node expanded if a call on it passes line L1. Then at most (k+1)h(k + 1) h nodes are expanded, and at most min⁡(2(k+1)h,2n)+1\min(2 (k + 1) h, 2 n) + 1 calls are made.

Proof. Let ff be the first node in in-order whose interval fails start\mathrm{start}, if there is one. Since start\mathrm{start} is downward closed and the in-order sequence is sorted by left end, the nodes that pass start\mathrm{start} are exactly the nodes before ff in in-order, a prefix.

Let vv be expanded. Then reach(v.max)\mathrm{reach}(v.\mathit{max}) holds, so some node ww below vv, possibly vv itself, has reach(w.r)\mathrm{reach}(w.r).

Case 1: start(w.l)\mathrm{start}(w.l) holds. Then ww matches. By Theorem 1 applied to the call on vv, ww is output, so vv is an ancestor of an output, or the output itself.

Case 2: start(w.l)\mathrm{start}(w.l) fails. Then ff exists, and ff is ww or comes before ww in in-order. We show that ff lies below vv. The nodes below vv form a contiguous block of the in-order sequence, and ww is in it. If vv is on the leftmost path from the root, the block is a prefix of the in-order sequence, and it contains ff, which is at or before ww. Otherwise let uu be the lowest ancestor of vv such that vv is in the right subtree of uu. Then uu is the node just before the block in in-order. The call on the right child of uu was made at line L5, so line L3 passed at uu, and start(u.l)\mathrm{start}(u.l) holds. So uu is before ff, and ff, which is at or before ww, is after uu: it lies in the block. In both cases vv is an ancestor of ff or ff itself.

Each node has at most hh ancestors including itself. So at most khk h nodes fall in Case 1 and at most hh in Case 2, which gives at most (k+1)h(k + 1) h expanded nodes. No node is called twice, so at most nn are expanded. Each call is the root call or is made by an expanded node, which makes at most two calls, so there are at most 2min⁡((k+1)h,n)+12 \min((k + 1) h, n) + 1 calls, and each call does O(1)O(1) work besides its calls. ■\blacksquare

Back to Interval tree search