Graph Theory37 sections · 1633 units
Open in Course

Sparse Table - Building

(Dynamic programming)

For k=0k = 0, set st[i][0] = tourDepth[i] (minimum over range of length 11). For larger kk, use the recurrence: st[i][k] = min(st[i][k-1], st[i + 2^(k-1)][k-1]). This works because a range of length 2k2^k splits into two ranges of length 2k12^{k-1}. The first half starts at ii, the second half starts at i+2k1i + 2^{k-1}.

You compute this bottom-up: first all ranges of length 11, then length 22, then length 44, and so on. Each entry depends only on smaller powers, so the DP order is correct. Time: O(nlogn)O(n \log n) because you fill a table of size n×lognn \times \log n, and each entry takes O(1)O(1) time to compute.