Algorithm Search⌘K

Proofs: Muthukrishnan's document listing algorithm

These are the human proofs of Muthukrishnan's document listing algorithm, at full length.

Muthukrishnan's listing procedure reports each document of the range once, with at most 2d + 1 range-minimum queries full

This proof shows: correctness, complexity.

We use the pseudocode and the notation of the page. Fix a query with range [lo,hi)[lo, hi), and let R={r:lo≤r<hi}R = \{ r : lo \le r < hi \}. For a document jj with a row in RR, let fjf_j be the smallest row of RR with D[fj]=jD[f_j] = j.

Lemma 1 (first rows). For r∈Rr \in R, C[r]<loC[r] < lo if and only if r=fD[r]r = f_{D[r]}.

Proof. By definition C[r]C[r] is the largest row r′<rr' < r of the same document, or −1-1. If r=fD[r]r = f_{D[r]}, no row of RR before rr has document D[r]D[r], so C[r]<loC[r] < lo. If r≠fD[r]r \ne f_{D[r]}, then fD[r]f_{D[r]} is a row of RR before rr with the same document, so C[r]≥fD[r]≥loC[r] \ge f_{D[r]} \ge lo. □\square

So the documents of the answer are in bijection with the set F={r∈R:C[r]<lo}F = \{ r \in R : C[r] < lo \}. By the choice of separators, a suffix starts with pp exactly when it is an occurrence of pp inside its document, so the documents with a row in RR are exactly the documents that contain pp.

Invariant of lines L5 to L8. The intervals on the stack are pairwise disjoint subintervals of RR; every row of FF is either in an interval of the stack or already reported, as D[r]D[r]; and no row outside FF has been reported.

At the start the stack holds RR and nothing is reported. Pop an interval [a,b][a, b]. If it is empty, it holds no row. Otherwise line L6 returns a row rr with C[r]=min⁡C[a…b]C[r] = \min C[a \ldots b]. If C[r]≥loC[r] \ge lo, then every row of [a,b][a, b] has C≥loC \ge lo, so [a,b]∩F[a, b] \cap F is empty and dropping it keeps the invariant. If C[r]<loC[r] < lo, then r∈Fr \in F, and line L8 reports D[r]D[r] and pushes [a,r−1][a, r-1] and [r+1,b][r+1, b], which are disjoint, cover [a,b][a, b] except rr, and are disjoint from the other intervals of the stack. So the invariant holds.

When the stack is empty, every row of FF has been reported, and only rows of FF, each once since each row leaves the stack at most once (the intervals are disjoint and rr is not pushed again). By Lemma 1 and the bijection, LIST reports each document that contains pp exactly once.

Number of queries. Consider the tree of the nonempty popped intervals, where an interval that reports has the at most two nonempty intervals it pushes as children. Each reporting interval is an internal node with at most two children, and each failing interval is a leaf. A tree with dd internal nodes of degree at most 2 has at most d+1d + 1 leaves, so there are at most 2d+12d + 1 nonempty intervals, and line L6 runs at most 2d+12d + 1 times. If d=0d = 0, the one query on RR fails, or RR is empty and no query runs. With an O(1)O(1) range-minimum structure, the listing time is O(d+1)O(d + 1), and with the range found in O(m)O(m) by a suffix tree, the query time is O(m+d)O(m + d). ■\blacksquare

Back to Muthukrishnan's document listing algorithm