Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions · 3. Measuring cost: doubling ratios, prefix sums, monotonic queues and cell lists

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 3. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Verify Tarjan’s exact potential telescoping identity total_pushes - total_pops == final_deque_len >= 1 and total_pushes + total_pops <= 2*N - 1 on invariants_complexity.sliding_window_max_monotonic_deque across strictly increasing, strictly decreasing, sawtooth, and impulse signals (N=3000,k=200N = 3000, k = 200).


Exercise 1: Tarjan potential function verification across adversarial signals
=============================================================================
Signal Waveform            |  Pushes |    Pops | Final Phi_N |  Pushes+Pops | Bound 2N-1
--------------------------------------------------------------------------------------
Strictly increasing ramp   |    3000 |    2999 |           1 |         5999 |       5999
Strictly decreasing ramp   |    3000 |    2800 |         200 |         5800 |       5999
High-frequency sawtooth    |    3000 |    2998 |           2 |         5998 |       5999
Sparse impulses            |    3000 |    2998 |           2 |         5998 |       5999

Explanation. Pushing an element stores +1 unit of potential Φ\Phi in the deque, and every future pop pays for itself by releasing -1 unit of stored potential. Because Φ0=0\Phi_0 = 0 and ΦN≥1\Phi_N \ge 1 at loop end, ∑(pushes+pops)=2N−ΦN\sum (\text{pushes} + \text{pops}) = 2N - \Phi_N holds as an exact algebraic identity regardless of whether pops happen 1-by-1 (increasing ramp, ΦN=1\Phi_N = 1) or in sudden bursts of kk at once (sawtooth).


Exercise 2

Problem. Quantify (a) exact element-shift operations in list.pop(0) vs. deque.popleft() across N∈[500,1000,2000,4000]N \in [500, 1000, 2000, 4000], and (b) silent false-negative neighbor misses when a 3×33 \times 3 cell-list search uses an undersized cell width h=0.5 rch = 0.5\,r_c.


Exercise 2: Audit of hidden O(N^2) list.pop(0) and undersized cell width h < r_c
================================================================================
(a) FIFO drain scaling: list.pop(0) shifts -> O(N^2) (p=2.00) vs deque.popleft() -> O(N)
(b) True pairs (r_c=1.5): 1943 | h = 1.0*r_c found: 1943 | h = 0.5*r_c found: 1413 (missed 27.3% of true neighbors)

Explanation. Python’s list is a contiguous array of pointers, so pop(0) calls memmove on all remaining elements (∑m=1N(m−1)=N(N−1)/2\sum_{m=1}^N (m-1) = N(N-1)/2). With cells of width h=0.5 rch = 0.5\,r_c, two points 0.6 rc0.6\,r_c apart can land in cells (cx,cy)(c_x, c_y) and (cx+2,cy)(c_x + 2, c_y), outside the 3×33\times 3 stencil (|dc| <= 1); here 27.3 % of the true pairs are missed, and in a molecular dynamics run the corresponding short-range repulsive forces would be lost without any error message.


Exercise 3

Problem. Implement largest_zero_defect_rectangle_histogram(heights) using an increasing monotonic stack in strictly O(N)\mathcal{O}(N) time (pushes == N + 1 including sentinel 0, pops <= N), returning max_area, best_span (left_idx, right_idx_exclusive, height), pushes, and pops. Verify exact equality against an O(N2)\mathcal{O}(N^2) brute-force oracle on 40 random and structured histograms.


Exercise 3: Implement it yourself — O(N) monotonic stack largest histogram rectangle
====================================================================================
Classic [2, 1, 5, 6, 2, 3] -> max_area = 10.0, rect (l, r, h) = (2, 4, 5.0)
Verified 100% optimality across 40 structured + random histograms with exact pushes == pops == N

Summary. Whenever a bar at top_idx is popped by a shorter bar at i, i is its first shorter bar on the right, and the new stack top stack[-1] (by the increasing-stack invariant) is its first shorter-or-equal bar on the left. One O(N)\mathcal{O}(N) pass thus discovers the maximal horizontal span of every bar simultaneously.