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 · 4. Divide and conquer: recursion trees, binary search on the answer and inversion counting

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 4. 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 the exact identity τ=1−4 IN(N−1)\tau = 1 - \frac{4\,I}{N(N-1)} using divide_spatial.mergesort_with_inversions against O(N2)\mathcal{O}(N^2) concordant/discordant pair counting across identity (τ=+1\tau = +1), reverse (τ=−1\tau = -1), and 30 random permutations of N=80N = 80 distinct sensors.


Exercise 1: Exact Kendall-tau rank correlation in O(N log N) via MergeSort inversions
=====================================================================================
Verified 30 random N=80 permutations to < 1e-15 Sample mean E[tau] = -0.0041 (theory = 0.0000)

Explanation. Permuting both arrays so X0<X1<⋯<XN−1X_0 < X_1 < \dots < X_{N-1} leaves every pair’s concordance unchanged while forcing Xi−Xj<0X_i - X_j < 0 for all i<ji < j. Consequently, pair (i,j)(i, j) is discordant (DD) iff Yi>YjY_i > Y_j (an inversion) and concordant (C=(N2)−DC = \binom{N}{2} - D) otherwise, giving (C−D)/(N2)=((N2)−2I)/(N2)=1−4IN(N−1)(C - D)/\binom{N}{2} = (\binom{N}{2} - 2I)/\binom{N}{2} = 1 - \frac{4I}{N(N-1)}.


Exercise 2

Problem. Demonstrate (a) how parametric_binary_search_min fails silently on a non-monotone rotor resonance predicate A(ω)≤2.0 mmA(\omega) \le 2.0\,\mathrm{mm} over ω∈[35,100] rad/s\omega \in [35, 100]\,\mathrm{rad/s} with resonance peaks near 35 and 78 rad/s (the search misses the unsafe band around 78 rad/s), and (b) how changing <= to < in MergeSort fabricates (N2)\binom{N}{2} fake inversions on a constant sensor vector.


Exercise 2: Audit of non-monotone resonance trap and strict `<` MergeSort tie corruption
========================================================================================
(a) Parametric binary search reported threshold = 41.58 rad/s,
    yet 198 supercritical speeds above 41.6 rad/s (around 78 rad/s) are unsafe

(b) Constant array [5.0]*8: True inversions = 0 vs Buggy `<` MergeSort = 28 (= 8*7/2 = 28)

Explanation. Binary-searching the answer relies entirely on the monotonicity implication P(x1)  ⟹  P(x2)P(x_1) \implies P(x_2) for all x2>x1x_2 > x_1; when multiple resonant modes create False -> True -> False -> True islands, mid = 67.5 evaluates True and discards [67.5, 100], hiding the second critical speed at 78 rad/s78\,\mathrm{rad/s}. In MergeSort, equal elements Ai=AjA_i = A_j (i<ji < j) are not inversions and must be emitted from left first (<=) to preserve stability.


Exercise 3

Problem. Implement min_relay_radius_connected(coords, max_hops) using BFS from gateway station 0 inside divide_spatial.parametric_binary_search_min on constants_benchmarks.make_sensor_station_coords(n_stations=50, seed=42). Verify monotonically decreasing Rmin⁡R_{\min} across max_hops in [1, 2, 3, 4, 6, 49], cross-check against exact discrete pairwise distance binary search, and plot the 4-hop radio relay tree.


Exercise 3: Implement it yourself — minimum multi-hop radio relay radius (BFS + parametric search)
==================================================================================================
max_hops =  1 -> R_min =  88.3286 km (exact pairwise edge =  88.3286 km)
max_hops =  2 -> R_min =  45.1535 km (exact pairwise edge =  45.1535 km)
max_hops =  3 -> R_min =  32.4729 km (exact pairwise edge =  32.4729 km)
max_hops =  4 -> R_min =  27.8464 km (exact pairwise edge =  27.8464 km)
max_hops =  6 -> R_min =  20.3714 km (exact pairwise edge =  20.3714 km)
max_hops = 49 -> R_min =  16.7034 km (exact pairwise edge =  16.7034 km)
<Figure size 1265x462 with 2 Axes>

Figure S4.1. Multi-hop relay radius. (a) The breadth-first tree that connects all 50 stations to the gateway within 4 hops at Rmin⁡=27.85 kmR_{\min} = 27.85\,\mathrm{km}. (b) Rmin⁡R_{\min} against the hop budget.

Summary. The threshold Rmin⁡R_{\min} is always one of the (N2)\binom{N}{2} pairwise distances. Continuous bisection with parametric_binary_search_min approaches it to within its tolerance, while bisection over the sorted distances with parametric_binary_search_int finds the critical link exactly in ⌈log⁡2(502)⌉=11\lceil \log_2 \binom{50}{2} \rceil = 11 BFS runs. Going from one hop to four lowers Rmin⁡R_{\min} from 88.3 km to 27.8 km; if the transmitter power scales as R2R^2 (an assumption, as in free space), that is a factor of about 10 in power.