Divide-and-conquer paradigm

Divide-and-conquer. 5. DIVIDE AND CONQUER I Divide up problem into several subproblems (of the same kind). Solve (conquer) each subproblem recursively. ‣ mergesort Combine solutions to subproblems into overall solution. ‣ counting inversions Most common usage. ‣ randomized Divide problem of size n into two subproblems of size n / 2. O(n) time ‣ and selection Solve (conquer) two subproblems recursively. ‣ closest pair of points Combine two solutions into overall solution. O(n) time

Consequence. Brute force: Θ(n2).

Lecture slides by Kevin Wayne Divide-and-conquer: O(n log n). Copyright © 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos

Last updated on 3/12/18 9:21 AM attributed to Julius Caesar 2

Sorting problem

Problem. Given a list L of n elements from a totally ordered universe, 5. DIVIDE AND CONQUER rearrange them in ascending order.

‣ mergesort ‣ counting inversions ‣ randomized quicksort ‣ median and selection ‣ closest pair of points

SECTIONS 5.1–5.2

4 Sorting applications Mergesort

Obvious applications. Recursively left half. Organize an MP3 library. Recursively sort right half. Display Google PageRank results. Merge two halves to make sorted whole. List RSS news items in reverse chronological order.

Some problems become easier once elements are sorted. input Identify statistical outliers. A L G O R I T H M S Binary search in a database. Remove duplicates in a mailing list. sort left half A G L O R I T H M S Non-obvious applications. sort right half Convex hull. Closest pair of points. A G L O R H I M S T Interval scheduling / interval partitioning. merge results Scheduling to minimize maximum lateness. A G H I L M O R S T Minimum spanning trees (Kruskal’s ). ... 5 6

Merging Mergesort implementation

Goal. Combine two sorted lists A and B into a sorted whole C. Input. List L of n elements from a totally ordered universe. Scan A and B from left to right. Output. The n elements in ascending order. Compare ai and bj. If ai ≤ bj, append ai to C (no larger than any remaining element in B). If ai > bj, append bj to C (smaller than every remaining element in A). MERGE-SORT(L)

______IF (list L has one element)

RETURN L.

sorted list A sorted list B Divide the list into two halves A and B. 3 7 10 ai 18 2 11 bj 20 23 A ← MERGE-SORT(A). T(n / 2) B ← MERGE-SORT(B). T(n / 2)

ERGE Θ(n) merge to form sorted list C L ← M (A, B). RETURN L.

2 3 7 10 11 ______

7 8 A useful recurrence relation Divide-and-conquer recurrence: recursion tree

Def. T (n) = max number of compares to mergesort a list of length n. Proposition. If T (n) satisfies the following recurrence, then T (n) = n log2 n.

0 n =1 assuming n Recurrence. T (n)= is a power of 2 2 T (n/2) + n n>1 0 n =1 AAACo3icbVFNa9tAEF0pbZq4H3HSYy5DnYaUFkcyhaSElEAvPfSQFrsJWMKsViNnyWoldkfBrvAP7S0/pStHhdrpwC6PNzNvZt8mpZKWguC35288ebr5bGu78/zFy1c73d29n7aojMCRKFRhrhNuUUmNI5Kk8Lo0yPNE4VVy+6XJX92hsbLQQ5qXGOd8qmUmBSdHTbq/hkf6HURncN5cnSjBqdS1cIp20YnOAjiEiHBGNcgMDrQrCw9gAVE0DvonmMeuZgDRB3Ayx4NG6L2T0etdn/92dSLUaSs/6faCfrAMeAzCFvRYG5eTXW8vSgtR5ahJKG7tOAxKimtuSAqFbt/KYsnFLZ/i2EHNc7RxvTRpAW8dk0JWGHc0wZL9t6PmubXzPHGVOacbu55ryP/lxhVlp3EtdVkRavEwKKsUUAGN45BKg4LU3AEujHS7grjhhgty/7IyZaldolh5ST2rtBRFimusohkZ3rgYrnv2GIwG/U/98PvH3sVpa+cW22dv2BEL2Qm7YF/ZJRsxwe69TW/H6/qH/jf/hz98KPW9tuc1Wwk//gPFucbd T (n) T ( n/2 )+T ( n/2 )+n n>1 AAACy3icbVFdb9MwFHUyPkr56sYjL1d0oCKkLJmQ2mkCTeKFJzSklk2qo8pxbjprjhPZDmoJfeSP8K/4NzhZJtGO++Kjc67P/UpKKYwNwz+ev3fv/oOHvUf9x0+ePns+2D/4ZopKc5zxQhb6MmEGpVA4s8JKvCw1sjyReJFcf2r0i++ojSjU1K5LjHO2VCITnFlHLQa/pyP1FugpUInN06cJLoWqufM0mz49DeENUIsrW4PI4FDBB4gOYQOUzsNgjHnscqYjKjNZFBrU0TFQ3eLG9Z1zbESOQnZaA28ltev98da7T1GlXROLwTAMwjbgLog6MCRdnC/2vQOaFrzKUVkumTHzKCxtXDNtBZfopqoMloxfsyXOHVQsRxPX7TI38NoxKWRumKxQFlr23x81y41Z54nLzJm9MrtaQ/5Pm1c2m8S1UGVlUfGbQlklwRbQXAZSoZFbuXaAcS1cr8CvmGbcuvttVWm9S+Rbk9SrSglepLjDSruymjVbjHZ3dhfMjoOTIPr6fng26dbZIy/JKzIiERmTM/KZnJMZ4V7PC7yxN/G/+Nb/4f+8SfW97s8LshX+r7/FTNWb

between ⎣n / 2⎦ and n − 1 compares T (n) n = n

Solution. T (n) is O(n log2 n). T (n / 2) T (n / 2) 2 (n/2) = n

T (n / 4) T (n / 4) T (n / 4) T (n / 4) 4 (n/4) = n Assorted proofs. We describe several ways to solve this recurrence. log 2 n Initially we assume n is a power of 2 and replace ≤ with = in the recurrence.

T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) 8 (n/8) = n ⋮ ⋮ T(n) = n log2 n 9 10

Proof by induction Divide-and-conquer: quiz 1

Proposition. If T (n) satisfies the following recurrence, then T (n) = n log2 n. Which is the exact solution of the following recurrence?

0 n =1 assuming n 0 n =1 T (n)= is a power of 2 T (n)= 2 T (n/2) + n n>1 T ( n/2 )+T ( n/2 )+n 1 n>1

AAACo3icbVFNa9tAEF0pbZq4H3HSYy5DnYaUFkcyhaSElEAvPfSQFrsJWMKsViNnyWoldkfBrvAP7S0/pStHhdrpwC6PNzNvZt8mpZKWguC35288ebr5bGu78/zFy1c73d29n7aojMCRKFRhrhNuUUmNI5Kk8Lo0yPNE4VVy+6XJX92hsbLQQ5qXGOd8qmUmBSdHTbq/hkf6HURncN5cnSjBqdS1cIp20YnOAjiEiHBGNcgMDrQrCw9gAVE0DvonmMeuZgDRB3Ayx4NG6L2T0etdn/92dSLUaSs/6faCfrAMeAzCFvRYG5eTXW8vSgtR5ahJKG7tOAxKimtuSAqFbt/KYsnFLZ/i2EHNc7RxvTRpAW8dk0JWGHc0wZL9t6PmubXzPHGVOacbu55ryP/lxhVlp3EtdVkRavEwKKsUUAGN45BKg4LU3AEujHS7grjhhgty/7IyZaldolh5ST2rtBRFimusohkZ3rgYrnv2GIwG/U/98PvH3sVpa+cW22dv2BEL2Qm7YF/ZJRsxwe69TW/H6/qH/jf/hz98KPW9tuc1Wwk//gPFucbd AAACxXicbVFda9swFJW9ry77SrvHvVyWbqSMZXYYtCNsFPbSxw6StRCZIMvXqagsGUkuCSbsj+yP7d9Mdl1Y0t2nwzn389y0lMK6KPoThA8ePnr8ZO9p79nzFy9f9fcPflpdGY4zrqU2lymzKIXCmRNO4mVpkBWpxIv0+nujX9ygsUKrqVuXmBRsqUQuOHOeWvR/T4fqCL5Cj6a4FKrmvpfd9OgkgvdAHa5cDSKHQ+Vz4kPYAKXzaHSMReJzpkMqc6m1AfVpDNS0+Ajo5AOdQCNyFLLTGngnqY/xbvdvd917FFXWrbHoD6JR1AbcB3EHBqSL88V+cEAzzasCleOSWTuPo9IlNTNOcIn+rspiyfg1W+LcQ8UKtEnd2riBd57JIPfn5Fo5aNl/K2pWWLsuUp9ZMHdld7WG/J82r1x+ktRClZVDxW8H5ZUEp6H5CWTCIHdy7QHjRvhdgV8xw7jzn9ua0vYukW9dUq8qJbjOcIeVbuUMa1yMdz27D2bj0ZdR/OPz4PSks3OPvCFvyZDE5JickjNyTmaEB2EwDOJgHJ6FKnThzW1qGHQ1r8lWhL/+Aje6018= no longer assuming n is a power of 2 Pf. [ by induction on n ] Base case: when n = 1, T(1) = 0 = n log2 n. A. T(n) = n ⎣log2 n⎦ Inductive hypothesis: assume T(n) = n log2 n. B. T(n) = n ⎡log2 n⎤ Goal: show that T(2n) = 2n log2 (2n).

⎣log n⎦ C. T(n) = n ⎣log2 n⎦ + 2 2 − 1 recurrence n ⎡log n⎤ D. T(n) = n ⎡log2 n⎤ − 2 2 + 1 = log k 2

T(2n) = 2 T(n) + 2n AAACWXicbVDLSgMxFE3HV62vVpdugkVwUcqMCCoiCG5cVrC20KlDJr2toZlkSO6IZehv+DVu9R8EP8b0sbDVC0lOzrk3N/fEqRQWff+r4K2srq1vFDdLW9s7u3vlyv6j1Znh0ORaatOOmQUpFDRRoIR2aoAlsYRWPLyd6K0XMFZo9YCjFLoJGyjRF5yho6KyH17Ra+q20GZJlA+vg/GTomEtrNFQchDSHXoQndIhDc3kHpWrft2fBv0Lgjmoknk0okphP+xpniWgkEtmbSfwU+zmzKDgEsalMLOQMj5kA+g4qFgCtptPRxvTY8f0aF8btxTSKfu7ImeJtaMkdpkJw2e7rE3I/7ROhv2Lbi5UmiEoPmvUzyRFTSc+0Z4wwFGOHGDcCPdXyp+ZYRydmwtdpm+nwBcmyV8zJbjuwRIr8RUNGzsXg2XP/oLmaf2y7t+fVW8u5nYWySE5IickIOfkhtyRBmkSTt7IO/kgn4Vvz/OKXmmW6hXmNQdkIbyDH4oEtJc= k=1 E. Not even Knuth knows. inductive hypothesis = 2 n log2 n + 2n

= 2 n (log2 (2n) – 1) + 2n

= 2 n log2 (2n). ▪

11 12 Analysis of mergesort recurrence Digression: sorting lower bound

Proposition. If T(n) satisfies the following recurrence, then T(n) ≤ n ⎡log2 n⎤. Challenge. How to prove a lower bound for all conceivable ?

0 n =1 Model of computation. Comparison trees. T (n) T ( n/2 )+T ( n/2 )+n n>1 Can access the elements only through pairwise comparisons. AAACynicbVFdb9MwFHUyYKN8deORlys6UBGiJBOiQxNoEi+8IA2pZZPqqHKcm86aY0e2M7WK+sYf4Wfxb3CyTKId98VH51yf+5WWUlgXRX+CcOfe/Qe7ew97jx4/efqsv3/w0+rKcJxyLbW5SJlFKRROnXASL0qDrEglnqdXXxv9/BqNFVpN3KrEpGALJXLBmfPUvP97MlRvgJ4Aldg8PZriQqiae0+77tGTCF4Ddbh0NYgcDhV8hvgQ1kDpbDTGIvEpkyGVudTagHp/BNS0uDF96w0bkaOQndbAW0ltW3+5te5RVFnXw7w/iEZRG3AXxB0YkC7O5vvBAc00rwpUjktm7SyOSpfUzDjBJfqhKosl41dsgTMPFSvQJnW7yzW88kwGuR8m18pBy/77o2aFtasi9ZkFc5d2W2vI/2mzyuXHSS1UWTlU/KZQXklwGprDQCYMcidXHjBuhO8V+CUzjDt/vo0qrXeJfGOSelkpwXWGW6x0S2dYs8V4e2d3wfRo9GkU//gwOD3u1rlHXpCXZEhiMian5Bs5I1PCg93gXfAxGIffQxuuwvomNQy6P8/JRoS//gIlc9Vh no longer assuming n All other operations (control, data movement, etc.) are free. is a power of 2 Pf. [ by strong induction on n ] Base case: n = 1. Cost model. Number of compares. Define n1 = ⎣n / 2⎦ and n2 = ⎡n / 2⎤ and note that n = n1 + n2. Induction step: assume true for 1, 2, ... , n – 1. Q. Realistic model? A1. Yes. Java, Python, C++, … T(n) ≤ T(n1) + T(n2) + n n2 = n/2 A2. Yes. Mergesort, , quicksort, , … n2 = dn/2e inductive hypothesis ≤ n1 log2 n1 + n2 log2 n2 + n d e ⎡ ⎤ ⎡ ⎤ log2 n A3. No. , radix sorts, … n2 =2dlogn/2ne / 2  2dd 2 ee / 2  log n ≤ n1 ⎡log2 n2⎤ + n2 ⎡log2 n2⎤ + n l log2 n 2 m =2ldlog22dne / 2e m/ 2 =2d 2 e / 2

= n ⎡log2 n2⎤ + n log n2 logl logn 2 n 1 m log2 n2 =2logd2 n e /12 Comparable[] a = ...; d 2 2ed 2 e ... ≤ n (⎡log2 n⎤ – 1) + n log2 n2 log2 n 1 can access elements only d e Arrays.sort(a); via calls to compareTo() log2 n2 log2 n 1 = n ⎡log2 n⎤. ▪ d ed e an integer 14

Comparison tree (for 3 distinct keys a, b, and c) Sorting lower bound

Theorem. Any deterministic compare-based must make

a < b Ω(n log n) compares in the worst-case. height of pruned tree = yes no worst-case number of compares code between compares Pf. [ information theoretic ] (e.g., sequence of exchanges) Assume array consists of n distinct values a1 through an. Worst-case number of compares = height h of pruned comparison tree. b < c a < c Binary tree of height h has ≤ 2h leaves. yes no yes no n! different orderings ⇒ n! reachable leaves.

a b c a < c b a c b < c

yes no yes no

h

a c b c a b b c a c b a

n! leaves ≤ 2h leaves each reachable leaf corresponds to one (and only one) ordering;

exactly one reachable leaf for each possible ordering 15 16 Sorting lower bound SHUFFLING A LINKED LIST

Theorem. Any deterministic compare-based sorting algorithm must make Ω(n log n) compares in the worst-case. Problem. Given a singly linked list, rearrange its nodes uniformly at random. Assumption. Access to a perfect random-number generator. all n! permutations Pf. [ information theoretic ] equally likely Assume array consists of n distinct values a1 through an. Performance. O(n log n) time, O(log n) extra space. Worst-case number of compares = height h of pruned comparison tree. Binary tree of height h has ≤ 2h leaves. n! different orderings ⇒ n! reachable leaves.

L 2h ≥ # leaves ≥ n ! input 2♣ 3♣ 4♣ 5♣ 6♣ 7♣ ⇒ h ≥ log2 (n!) null

≥ n log2 n − n / ln 2 ▪ L shufed Stirling’s formula 5♣ 6♣ 2♣ 7♣ 3♣ 4♣ null

Note. Lower bound can be extended to include randomized algorithms. 17 18

Counting inversions

Music site tries to match your song preferences with others. 5. DIVIDE AND CONQUER You rank n songs. Music site consults database to find people with similar tastes. ‣ mergesort ‣ counting inversions Similarity metric: number of inversions between two rankings. My rank: 1, 2, …, n. ‣ randomized quicksort Your rank: a1, a2, …, an. ‣ median and selection Songs i and j are inverted if i < j, but ai > aj. ‣ closest pair of points

A B C D E

me 1 2 3 4 5 SECTION 5.3 you 1 3 4 2 5

2 inversions: 3-2, 4-2

Brute force: check all Θ(n2) pairs.

20 Counting inversions: applications Counting inversions: divide-and-conquer

Voting theory. Divide: separate list into two halves A and B. Collaborative filtering. Conquer: recursively count inversions in each list. Measuring the “sortedness” of an array. Combine: count inversions (a, b) with a ∈ A and b ∈ B. Sensitivity analysis of Google’s ranking function. Return sum of three counts. Rank aggregation for meta-searching on the Web. Nonparametric statistics (e.g., Kendall’s tau distance). input 1 5 4 8 10 2 6 9 3 7 Rank Aggregation Methods for the Web

Cynthia Dwork Ravi Kumar Moni Naor D. Sivakumar Rank Aggregation Methods for the Web count inversions in left half A count inversions in right half B

Cynthia Dwork Ravi Kumar Moni Naor D. Sivakumar 1 5 4 8 10 2 6 9 3 7

ABSTRACT 5-4 6-3 9-3 9-7

ABSTRACT count inversions (a, b) with a ∈ A and b ∈ B

1 5 4 8 10 2 6 9 3 7

1.1 Motivation 4-2 4-3 5-2 5-3 8-2 8-3 8-6 8-7 10-2 10-3 10-6 10-7 10-9

1. INTRODUCTION 21 output 1 + 3 + 13 = 17 22 1.1 Motivation

1. INTRODUCTION

Counting inversions: how to combine two subproblems? Counting inversions: how to combine two subproblems?

Q. How to count inversions (a, b) with a ∈ A and b ∈ B? Count inversions (a, b) with a ∈ A and b ∈ B, assuming A and B are sorted. A. Easy if A and B are sorted! Scan A and B from left to right. Compare ai and bj. Warmup algorithm. If ai < bj, then ai is not inverted with any element left in B. Copyright is held by the author/owner. Sort A and B. WWW10, May 1-5, 2001, Hong Kong. If ai > bj, then bj is inverted with every element left in A. ACM 1-58113-348-0/01/0005.

For each element b ∈ B, 613 Append smaller element to sorted list C. Copyright is held by the author/owner. WWW10, May 1-5, 2001, Hong Kong. - binaryACM search 1-58113-348-0/01/0005. in A to find how elements in A are greater than b.

613

list A list B

7 10 18 3 14 20 23 2 11 16 count inversions (a, b) with a ∈ A and b ∈ B

sort A sort B 3 7 10 ai 18 2 11 bj 20 23

5 2 3 7 10 14 18 2 11 16 20 23

binary search to count inversions (a, b) with a ∈ A and b ∈ B merge to form sorted list C

3 7 10 14 18 2 11 16 20 23 2 3 7 10 11

5 2 1 0 0 23 24 Counting inversions: divide-and-conquer algorithm implementation Counting inversions: divide-and-conquer algorithm analysis

Input. List L. Proposition. The sort-and-count algorithm counts the number of inversions Output. Number of inversions in L and L in sorted order. in a permutation of size n in O(n log n) time.

Pf. The worst-case running time T(n) satisfies the recurrence:

SORT-AND-COUNT(L)

______IF (list L has one element) (1) n =1 T (n)= RETURN (0, L). T ( n/2 )+T ( n/2 )+(n) n>1 AAAC2HicbVHLattAFB0pfaTuy0npqpuhTotDwZFCIQmmJdAWukzBbgIaYUbjK3vIaCRmroqN8KKr0m1/pN/Tv+lIUaC2exfD4Zw799xHUihpMQj+eP7Onbv37u8+6Dx89PjJ0+7e/lebl0bAWOQqN1cJt6CkhjFKVHBVGOBZouAyuf5Q65ffwFiZ6xEuC4gzPtMylYKjoybd36O+PqRsSN/VT4clMJO6Eq6iXXXYkI3mgLwfHtLXlCEssKIypQfapYcHdEUZiwYnkMUuddRnKlV5bqg+OqbMNLgu/cYVrkUBUrVaDW+l1kJvWby/tegw0NO2p0m3FwyCJug2CFvQI21cTPa8fTbNRZmBRqG4tVEYFBhX3KAUCtyQpYWCi2s+g8hBzTOwcdVsdkVfOWZKUzdUmmukDfvvj4pn1i6zxGVmHOd2U6vJ/2lRielpXEldlAha3BilpaKY0/pMdCoNCFRLB7gw0vVKxZwbLtAdc82lqV2AWJukWpRainwKG6zCBRpebzHc3Nk2GB8Pzgbhl7e989N2nbvkBXlJ+iQkJ+ScfCYXZEyE99wbeh+9T37kf/d/+D9vUn2v/fOMrIX/6y+lidpE

Divide the list into two halves A and B.

(rA, A) ← SORT-AND-COUNT(A). T(n / 2)

(rB, B) ← SORT-AND-COUNT(B). T(n / 2)

(rAB, L) ← MERGE-AND-COUNT(A, B). Θ(n)

RETURN (rA + rB + rAB, L).

______

25 26

3-WAY PARTITIONING

5. DIVIDE AND CONQUER Goal. Given an array A and pivot element p, partition array so that: Smaller elements in left subarray L. ‣ mergesort Equal elements in middle subarray M. ‣ counting inversions Larger elements in right subarray R. ‣ randomized quicksort Challenge. O(n) time and O(1) space. ‣ median and selection ‣ closest pair of points the array A

7 6 12 3 11 8 9 1 4 10 2

SECTION 7.1–7.3 p

the partitioned array A

3 1 4 2 6 7 12 11 8 9 10

L M R

28 Randomized quicksort Randomized quicksort

Pick a random pivot element p ∈ A. Pick a random pivot element p ∈ A. 3-way partition the array into L, M, and R. 3-way partition the array into L, M, and R. Recursively sort both L and R. Recursively sort both L and R.

the array A 7 6 12 3 11 8 9 1 4 10 2

p

RANDOMIZED-QUICKSORT(A)

partition A 3 1 4 2 6 7 12 11 8 9 10 ______IF (array A has zero or one element)

RETURN. sort L 1 2 3 4 6 7 12 11 8 9 10 Pick pivot p ∈ A uniformly at random. (L, M, R) ← PARTITION-3-WAY(A, p). Θ(n)

RANDOMIZED-QUICKSORT(L). T(i) sort R 1 2 3 4 6 7 8 9 10 11 12 new analysis required (i is a random variable—depends on p) RANDOMIZED-QUICKSORT(R). T(n − i − 1)

______

the sorted array A 1 2 3 4 6 7 8 9 10 11 12

29 30

Analysis of randomized quicksort Analysis of randomized quicksort

Proposition. The expected number of compares to quicksort an array Proposition. The expected number of compares to quicksort an array of n distinct elements a1 < a2 < < an is O(n log n). of n distinct elements a1 < a2 < < an is O(n log n).

Pf. Consider BST representation of pivot elements. Pf. Consider BST representation of pivot elements. ai and aj are compared once iff one is an ancestor of the other. the original array of elements A

a7 a6 a12 a3 a11 a8 a9 a1 a4 a10 a2 a13 a5

a3 and a6 are compared first pivot first pivot (when a3 is pivot) (chosen uniformly at random) (chosen uniformly at random) first pivot in first pivot in left subarray left subarray a9 a9

a3 a11 a3 a11

a1 a8 a10 a13 a1 a8 a10 a13

a2 a4 a12 a2 a4 a12

a6 a6

a5 a7 a5 a7 31 32 Analysis of randomized quicksort Divide-and-conquer: quiz 2

Proposition. The expected number of compares to quicksort an array Given an array of n ≥ 8 distinct elements a1 < a2 < < an, what is the of n distinct elements a1 < a2 < < an is O(n log n). probability that a7 and a8 are compared during randomized quicksort?

Pf. Consider BST representation of pivot elements. A. 0 ai and aj are compared once iff one is an ancestor of the other. B. 1 / n

C. 2 / n

D. 1 if two elements are adjacent, one must be an ancestor of the other a2 and a8 are not compared first pivot (because a3 partitions them) (chosen uniformly at random) first pivot in left subarray a9 a9

a3 a11 a3 a11

a1 a8 a10 a13 a1 a8 a10 a13

a2 a4 a12 a2 a4 a12

a6 a6

a5 a7 a5 a7 33 34

Divide-and-conquer: quiz 3 Analysis of randomized quicksort

Given an array of n ≥ 2 distinct elements a1 < a2 < < an, what is the Proposition. The expected number of compares to quicksort an array of n probability that a1 and an are compared during randomized quicksort? distinct elements a1 < a2 < < an is O(n log n).

Pf. Consider BST representation of pivot elements. A. 0 ai and aj are compared once iff one is an ancestor of the other. B. 1 / n Pr [ ai and aj are compared ] = 2 / ( j - i + 1), where i < j. C. 2 / n a1 and an are compared iff first pivot is either a1 or an D. 1

Pr[a2 and a8 compared] = 2/7

compared if either a2 or a8 is chosen

as pivot before any of { a3, a4, a5, a6, a7}

a9 a9

a3 a11 a3 a11

a1 a8 a10 a13 a1 a8 a10 a13

a2 a4 a12 a2 a4 a12

a6 a6

a5 a7 a5 a7 35 36 number). 9.9 X 10 45 is used to represent infinity. Imaginary conunent I and J are output variables, and A is the array (with Analysis of randomized quicksort valuesTony of x may Hoarenot be negative and reM values of x may not be subscript bounds M:N) which is operated upon by this procedure. smaller than 1. Partition takes the value X of a random element of the array A, Values of Qd~'(x) may be calculated easily by hypergeometric and rearranges the values of the elements of the array in such a series if x is not too small nor (n - m) too large. Q~m(x) can be way that there exist integers I and J with the following properties : computed from an appropriate set of values of Pnm(X) if X is near M _-< J < I =< NprovidedM < N 1.0 or ix is near 0. Loss of significant digits occurs for x as small as A[R] =< XforM =< R _-< J Proposition. The expected number of compares to quicksort an array of n 1.1 if n is largerInvented than 10. Loss of significant quicksort digits is a major diffi-to translateA[R] = RussianXforJ < R < I into English. culty in using finite polynomiM representations also if n is larger A[R] ~ Xfor I =< R ~ N than m. However, QLEG has been tested in regions of x and n The procedure uses an integer procedure random (M,N) which distinct elements a1 < a2 < < an is O(n log n). both large [and but small; couldn’t explain hischooses algorithm equiprobably a random integeror Fimplement between M and N, and it! ] procedure QLEG(m, nmax, x, ri, R, Q); value In, nmax, x, ri; also a procedure exchange, which exchanges the values of its two real In, mnax, x, ri; real array R, Q; parameters ; begin real t, i, n, q0, s; begin real X; integer F; n :=Learned 20; Algol 60 (and recursion).F := random (M,N); X := A[F]; if nmax > 13 then I:=M; J:=N; n := nmax +7; up: for I : = I step 1 until N do Pf. Consider BST representation of pivot elements. if Implementedri = 0then quicksort. if X < A [I] then go to down; begin ifm = 0then I:=N; Q[0] := 0.5 X 10g((x + 1)/(x - 1)) down: forJ := J step --1 until M do ai and aj are compared once iff one is an ancestor of the other. else if A[J]= 0 then n n n n ii+1 q0 := Q[0]; I:=I+l 3:begin n n 2 n n i+1 1 Q[0] := s end end; end e:= aXe;f := bXd;goto8 if x = 1 then else if F < J tllen begin exchange (A[F], A[J]) ; end 3 ; Expected number of compares = n n 2 =2n n i+1 1 Q[0] := 9.9 I" 45; J:=J-1 e:=bXc; jj ii2+11 =2 jj1 R[n + 1] := x - sqrt(x X x - 1); end ; ifd ~ 0then ii=1 jj=ii+1 j i 1 =2ii=1 jj=2 j for i := n step --1 until 1 do end partition 4: begin i=1 j=i+1 i=1 j=2 R[i] := (i + m)/((i + i + 1) X x f:=bXd; goto8 +(m-i- 1) X R[i+l]); end 4; j i 1 n j ALGORITHM 61 i=1 j=i+1 i=1 j=2 go to the end; f:=aXd; goto8 n 1 ALGORITHMPROCEDURES 64FOR RANGE ARITHMETIC if m = 0 then 5: end 2; 2n n 1 begin if x < 0.5 tben QUICKSORTALLAN GIBB* ifb > 0 then 2n Q[0] := arctan(x) - 1.5707963 else C.University A. R. HOARE of Alberta, Calgary, Alberta, Canada 6: begin all pairs i and j jj1 Q[0] := - aretan(1/x)end else jj=1 Elliottbegin Brothers Ltd., Borehamwood, Hertfordshire, Eng. if d > 0 then 2n j begin t := 1/sqrt(x X x + 1); begin j=1 procedure RANGESUM (a, b, c, d, e, f); q0 := 0; procedure quicksort (A,M,N); value M,N; e := MIN(a X d, b X c); n j real a,b,c,d,e,f; q[0] := t; array A; integer M,N; f:= MAX(a X c,b X d); go to8 2n (lnj=1nn +1 1) comment The term "range number" was used by P. S. Dwyer, AAADBHicjZFNb9NAEIbXLh8lfKXlyGVEBCoqRLZViVZRpSIuHItEaKVsiNabcbrteu3urlEjy2d+DSfElf+B+DOs7YCSFCFGsvzuMzPv2jNxLoWxQfDD8zdu3Lx1e/NO5+69+w8edre2P5is0ByHPJOZPo2ZQSkUDq2wEk9zjSyNJZ7EF2/q/Mkn1EZk6r2d5zhO2UyJRHBmHZp0f1JTpJNSHIbVx1JVQF9AS87hEMSuo6phiWa8jKry/GUN4RnQAR24kuYVAfzLJqpZ2/fHKnRW7kg7v62oxFZApJZ6l/z+p28AO1QqULAL4fNJtxf0gybguggXokcWcTzZ8rbpNONFispyyYwZhUFuxyXTVnCJVYcWBnPGL9gMR04qlqIZl80WKnjqyBSSTLtHWWjockfJUmPmaewqU2bPzHquhn/LjQqb7I9LofLCouLtRUkhwWZQrxSmQiO3cu4E41q4bwV+xty0rFv8yi2Nd4585U/Kq0IJnk1xjUp7ZTWr3BTD9ZldF8Oof9AP3+31jvYX49wkj8kTskNC8oockbfkmAwJ9157My/3Lv3P/hf/q/+tLfW9Rc8jshL+91/5zen5 for i : = 2 step 1 until m do comment Quicksort is a very fast and convenient method of end 6; 1 Linear Computations (Wiley, 1951). Machine procedures for 2n dx begin s := (x + x) X (i -- 1) X t X Q[0I sorting an array in the random-access store of a computer. The e:= bX c; f := aX c; go to8 range arithmetic were developed about 1958 by Ramon Moore, 2n dx +(3i+iX i -- 2) × q0; entire contents of the store may be sorted, since no extra space is end 5; 2n (ln n +x 1) "Automatic Error Analysis in Digital Computation," LMSD AAADBHicjZFdb9MwFIad8DXKx7pxyc0RFWhotIojJDZVk4a44XJIlE2qS+W4TufNcYLtTKusXO/X7Apxy/9A/BmctIi2Q4gjRXn9nHNeJ+ckhRTGRtGPILx1+87dexv3Ww8ePnq82d7a/mTyUjM+YLnM9UlCDZdC8YEVVvKTQnOaJZIfJ+fv6vzxBddG5OqjnRV8lNGpEqlg1Ho0bv8kpszGThzg6rNTFZBXMCdncABi11PVsFRT5uLKnXVFF1fwAkif9H1J84oB/mUT16xbm/2xwt7KH0nrtxWRfC4gVku9S37/09eHHSIVKNgF/HLc7kS9qAm4KfBCdNAijsZbwTaZ5KzMuLJMUmOGOCrsyFFtBZO8apHS8IKyczrlQy8VzbgZuWYLFTz3ZAJprv2jLDR0ucPRzJhZlvjKjNpTs56r4d9yw9KmeyMnVFFartj8orSUYHOoVwoToTmzcuYFZVr4bwV2Sv20rF/8yi2Nd8HZyp+4y1IJlk/4GpX20mpa+Sni9ZndFIO4t9/DH153DvcW49xAT9EztIMweoMO0Xt0hAaIBW+DaVAEX8Kr8Dr8Gn6bl4bBoucJWonw+y/+pen7 x=1 ▪ qO := Q[0]; required. The average number of comparisons made is 2(M--N) In f:=aXc; x Report 48421, 28 Jan. 1959, Lockheed Missiles and Space Divi- x=1 Q[0] := s end end; (N--M), and the average nmnber of exchanges is one sixth this if d _-< O then sion, Palo Alto, California, 59 pp. If a _< x -< b and c ~ y ~ d, R[n + 1] := x - sqrt(x × x + 1); amount. Suitable refinements of this method will be desirable for 7: begin =2n ln n then RANGESUM yields an interval [e, f] such that e =< (x + y) for i := n step - 1 until 1 do its implementation on any actual computer; e:=bXd; goto8 =2n ln n f. Because of machine operation (truncation or rounding) the R[i] := (i + m)/((i -- m + 1) × R[i + 1] begin integer 1,J ; end 7 ; machine sums a -4- c and b -4- d may not provide safe end-points --(i+i+ 1) X x); if M < N then begin partition (A,M,N,I,J); e:=aXd; of the output interval. Thus RANGESUM requires a non-local harmonic sum for i : = 1 step 2 until nmax do quicksort (A,M,J) ; 8: e := ADJUSTPROD (e, -1); real procedure ADJUSTSUM which will compensate for the Rill := -- Rill; quicksort (A, I, N) f := ADJUSTPROD (f, 1) machine arithmetic. The body of ADJUSTSUM will be de- the: for i : = 1 step 1 until nnmx do end end RANGEMPY; pendent upon the type of machine for which it is written and so Q[i] := Q[i - 1] X R[i] end quieksort procedure RANGEDVD (a, b, c, d, e, f) ; is not given here. (An example, however, appears below.) It end QLEG; real a, b, c, d, e, f; is assumed that ADJUSTSUM has as parameters real v and w, comment If the range divisor includes zero the program * This procedure was developed in part under the sponsorship and integer i, and is accompanied by a non-local real procedure exists to a non-local label "zerodvsr". RANGEDVD assumes a of the Air Force Cambridge Research Center. CORRECTION which gives an upper bound to the magnitude CommunicationsALGORITHM 65 of the ACM (July 1961) non-local real procedure ADJUSTQUOT which is analogous Remark. Number of compares only decreases if equal elements. of the error involved in the machine representation of a number. FIND (possibly identical) to ADJUSTPROD; 37 The output ADJUSTSUM provides the left end-point of the 38 begin C.output A. R. interval HOARE of RANGESUM when ADJUSTSUM is called ALGORITHM 63 if c =< 0 A d ~ 0 then go to zer0dvsr; Elliottwith i Brothers= --1, and Ltd., the right Borehamwood, end-point when Hertfordshire, called with i Eng.= 1 if c < 0 then PARTITION The procedures RANGESUB, RANGEMPY, and RANGEDVD procedure find (A,M,N,K); value M,N,K; C. A. R. HOARE provide for the remaining fundamental operations in range 1: begin array A; integer M,N,K; ifb > 0 then Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng. arithmetic. RANGESQR gives an interval within which the comment Find will assign to A [K] the value which it would 2: begin square of a range nmnber must lie. RNGSUMC, RNGSUBC, procedure partition (A,M,N,I,J); value M,N; have if the array A [M:N] had been sorted. The array A will be e := b/d; go to3 RNGMPYC and RNGDVDC provide for range arithmetic with array A; integer M,N,1,J; partly sorted, and subsequent entries will be faster than the first; end 2; complex range arguments, i.e. the real and imaginary parts are range numbers~ e := b/c; begin Communications of the ACM 321 3: ifa -->_ 0then NUTS AND BOLTS 4: begin e := ADJUSTSUM (a, c, -1); f := a/c; go to 8 f := ADJUSTSUM (b, d, 1) end RANGESUM; end 4; f := a/d; go to 8 procedure RANGESUB (a, b, c, d, e, f) ; real a, b,c, d,e, f; end 1 ; if a < 0 then comment RANGESUM is a non-local procedure; begin 5: begin RANGESUM (a, b, -d, --c, e, f) e := a/c; go to6 end RANGESUB5. ; DIVIDE AND CendONQUER 5 ; Problem. A disorganized carpenter has a mixed pile of n nuts and n bolts. procedure RANGEMPY (a, b, c, d, e, f); e := a/d; real a, b, c, d, e, f; 6: ifb > 0then comment ADJUSTPROD, which appears at the end of this 7: begin f := b/c; go to8 The goal is to find the corresponding pairs of nuts and bolts. procedure, is analogous to ADJUSTSUM above and is a non- local real procedure. MAX and MIN find the maximum and end 7 ; minimum of a set of real numbers and are non-local; f := b/d; begin ‣ mergesort 8: e := ADJUSTQUOT (e, -1); f := ADJUSTQUOT (f,1) Each nut fits exactly one bolt and each bolt fits exactly one nut. RANGEDVD ; real v, w; end if a < 0A c => 0then procedure RANGESQR (a, b, e, f); 1: begin real a, b, e, f; By fitting a nut and a bolt together, the carpenter can see which one is v:=c; ‣c:=a;counting a:=v; w:=d; d:=b; inversions b:=w comment ADJUSTPROD is a non-10cal procedure; end 1; begin bigger (but cannot directly compare either two nuts or two bolts). if a => O then if a < 0 then ‣ randomized quicksort Communications of the &CM 319

median and selection ‣ ‣ closest pair of points

SECTION 9.3

Brute-force solution. Compare each bolt to each nut—Θ(n 2) compares. Challenge. Design an algorithm that makes O(n log n) compares.

39 Median and selection problems Randomized

Selection. Given n elements from a totally ordered universe, find kth smallest. Pick a random pivot element p ∈ A. Minimum: k = 1; maximum: k = n. 3-way partition the array into L, M, and R. Median: k = ⎣(n + 1) / 2⎦. Recur in one subarray—the one containing the kth smallest element. O(n) compares for min or max. O(n log n) compares by sorting. O(n log k) compares with a binary . max heap with k smallest

Applications. Order statistics; find the “top k”; bottleneck paths, …

QUICK-SELECT(A, k)

Q. Can we do it with O(n) compares? ______A. Yes! Selection is easier than sorting. Pick pivot p ∈ A uniformly at random. (L, M, R) ← PARTITION-3-WAY(A, p). Θ(n)

IF (k ≤ | L |) RETURN QUICK-SELECT(L, k). T(i)

ELSE IF (k > | L | + | M |) RETURN QUICK-SELECT(R, k – | L | – | M |) T(n − i − 1)

ELSE IF (k = | L |) RETURN p.

______

43 44

Randomized quickselect analysis Selection in worst-case linear time

Intuition. Split candy bar uniformly ⇒ expected size of larger piece is ¾. Goal. Find pivot element p that divides list of n elements into two pieces so that each piece is guaranteed to have ≤ 7/10 n elements. T(n) ≤ T(3 n / 4) + n ⇒ T(n) ≤ 4 n

not rigorous: can’t assume E[T(i)] ≤ T(E[i]) Q. How to find approximate median in linear time? Def. T(n, k) = expected # compares to select kth smallest in array of length ≤ n. A. Recursively compute median of sample of ≤ 2/10 n elements.

Def. T(n) = maxk T(n, k).

Θ(1) if n = 1 Proposition. T(n) ≤ 4 n. T(n) = T (7/10 n) + T (2/10 n) + Θ(n) otherwise Pf. [ by strong induction on n ] can assume we always recur of Assume true for 1, 2, …, n – 1. larger of two subarrays since T(n) is monotone non-decreasing T(n) satisfies the following recurrence: two subproblems of different sizes! T(n) ≤ n + 1 / n [ 2T(n / 2) + … + 2T(n – 3) + 2T(n – 2) + 2T(n – 1) ]

≤ n + 1 / n [ 8(n / 2) + … + 8(n – 3) + 8(n – 2) + 8(n – 1) ] ⇒ T(n) = Θ(n) ≤ n + 1 / n (3n2) inductive hypothesis we’ll need to show this = 4 n. ▪ tiny cheat: sum should start at T(⎣n/2⎦)

45 46 Choosing the pivot element Choosing the pivot element

Divide n elements into ⎣n / 5⎦ groups of 5 elements each (plus extra). Divide n elements into ⎣n / 5⎦ groups of 5 elements each (plus extra). Find median of each group (except extra).

29 10 38 37 2 55 18 24 34 35 36 29 10 38 37 2 55 18 24 34 35 36

22 44 52 11 53 12 13 43 20 4 27 22 44 52 11 53 12 13 43 20 4 27

28 23 6 26 40 19 1 46 31 49 8 28 23 6 26 40 19 1 46 31 49 8

14 9 5 3 54 30 48 47 32 51 21 14 9 5 3 54 30 48 47 32 51 21

45 39 50 15 25 16 41 17 22 7 45 39 50 15 25 16 41 17 22 7

n = 54 47 n = 54 48

Choosing the pivot element Median-of-medians

Divide n elements into ⎣n / 5⎦ groups of 5 elements each (plus extra). Find median of each group (except extra). MOM-SELECT(A, k) Find median of ⎣n / 5⎦ medians recursively. ______Use median-of-medians as pivot element. n ← | A |.

medians IF (n < 50) RETURN kth smallest of element of A via mergesort. 29 10 38 37 2 55 18 24 34 35 36 Group A into ⎣n / 5⎦ groups of 5 elements each (ignore leftovers). B ← median of each group of 5. 22 44 52 11 53 12 13 43 20 4 27 p ← MOM-SELECT(B, ⎣n / 10⎦) median of medians

28 23 6 26 40 19 1 46 31 49 8 (L, M, R) ← PARTITION-3-WAY(A, p).

IF (k ≤ | L |) RETURN MOM-SELECT(L, k). 14 9 5 3 54 30 48 47 32 51 21 ELSE IF (k > | L | + | M |) RETURN MOM-SELECT(R, k – | L | – | M |)

45 39 50 15 25 16 41 17 22 7 ELSE RETURN p.

______

n = 54 49 50 Analysis of median-of-medians selection algorithm Analysis of median-of-medians selection algorithm

At least half of 5-element medians ≤ p. At least half of 5-element medians ≤ p. At least ⎣⎣n / 5⎦ / 2⎦ = ⎣n / 10⎦ medians ≤ p.

medians medians ≤ p median of median of medians p 29 10 38 37 2 55 18 24 34 35 36 medians p 29 10 38 37 2 55 18 24 34 35 36

22 44 52 11 53 12 13 43 20 4 27 22 44 52 11 53 12 13 43 20 4 27

28 23 6 26 40 19 1 46 31 49 8 28 23 6 26 40 19 1 46 31 49 8

14 9 5 3 54 30 48 47 32 51 21 14 9 5 3 54 30 48 47 32 51 21

45 39 50 15 25 16 41 17 22 7 45 39 50 15 25 16 41 17 22 7

n = 54 51 n = 54 52

Analysis of median-of-medians selection algorithm Analysis of median-of-medians selection algorithm

At least half of 5-element medians ≤ p. At least half of 5-element medians ≥ p. At least ⎣⎣n / 5⎦ / 2⎦ = ⎣n / 10⎦ medians ≤ p. At least 3 ⎣n / 10⎦ elements ≤ p.

medians ≤ p medians median of median of medians p 29 10 38 37 2 55 18 24 34 35 36 medians p 29 10 38 37 2 55 18 24 34 35 36

22 44 52 11 53 12 13 43 20 4 27 22 44 52 11 53 12 13 43 20 4 27

28 23 6 26 40 19 1 46 31 49 8 28 23 6 26 40 19 1 46 31 49 8

14 9 5 3 54 30 48 47 32 51 21 14 9 5 3 54 30 48 47 32 51 21

45 39 50 15 25 16 41 17 22 7 45 39 50 15 25 16 41 17 22 7

n = 54 53 n = 54 54 Analysis of median-of-medians selection algorithm Analysis of median-of-medians selection algorithm

At least half of 5-element medians ≥ p. At least half of 5-element medians ≥ p. At least ⎣⎣n / 5⎦ / 2⎦ = ⎣n / 10⎦ medians ≥ p. At least ⎣⎣n / 5⎦ / 2⎦ = ⎣n / 10⎦ medians ≥ p. At least 3 ⎣n / 10⎦ elements ≥ p.

medians ≥ p medians ≥ p median of median of medians p 29 10 38 37 2 55 18 24 34 35 36 medians p 29 10 38 37 2 55 18 24 34 35 36

22 44 52 11 53 12 13 43 20 4 27 22 44 52 11 53 12 13 43 20 4 27

28 23 6 26 40 19 1 46 31 49 8 28 23 6 26 40 19 1 46 31 49 8

14 9 5 3 54 30 48 47 32 51 21 14 9 5 3 54 30 48 47 32 51 21

45 39 50 15 25 16 41 17 22 7 45 39 50 15 25 16 41 17 22 7

n = 54 55 n = 54 56

Median-of-medians selection algorithm recurrence Median-of-medians selection algorithm recurrence

Median-of-medians selection algorithm recurrence. Median-of-medians selection algorithm recurrence. Select called recursively with ⎣n / 5⎦ elements to compute MOM p. Select called recursively with ⎣n / 5⎦ elements to compute MOM p. At least 3 ⎣n / 10⎦ elements ≤ p. At least 3 ⎣n / 10⎦ elements ≤ p. At least 3 ⎣n / 10⎦ elements ≥ p. At least 3 ⎣n / 10⎦ elements ≥ p. Select called recursively with at most n – 3 ⎣n / 10⎦ elements. Select called recursively with at most n – 3 ⎣n / 10⎦ elements.

Def. C(n) = max # compares on any array of n elements. Def. C(n) = max # compares on any array of n elements.

C(n) C ( n/5 )+C (n 3 n/10 )+ 11 n C(n) C ( n/5 )+C (n 3 n/10 )+ 11 n 5 5 median of recursive computing median of 5 median of recursive computing median of 5 medians select (≤ 6 compares per group) medians select (≤ 6 compares per group) partitioning partitioning (≤ n compares) (≤ n compares)

Intuition. Now, let’s solve given recurrence. C(n) is going to be at least linear in n ⇒ C(n) is super-additive. Assume n is both a power of 5 and a power of 10 ? Ignoring floors, this implies that C(n) ≤ C(n / 5 + n − 3n / 10) + 11/5 n Prove that C(n) is monotone non-decreasing. = C(9n / 10) + 11/5 n C(n) ≤ 22n. ⇒ 57 58 Divide-and-conquer: quiz 4 Median-of-medians selection algorithm recurrence

Consider the following recurrence Analysis of selection algorithm recurrence. T(n) = max # compares on any array of ≤ n elements. 0 n 1 T(n) is monotone non-decreasing, but C(n) is not! C(n)= C( n/5 )+C(n 3 n/10 )+ 11 n n>1 AAAC0nicbVHLbtNAFB2bVwmPpmXJ5ooIlAoRbCCiKKJUyoZlkTCtlLGi8fg6HXU8tmbGKJGVBWLLj/BJ/A1j1xJNwl0dnXOf5yalFMYGwR/Pv3X7zt17e/d7Dx4+erzfPzj8ZopKc4x4IQt9kTCDUiiMrLASL0qNLE8knidX00Y//47aiEJ9tasS45wtlMgEZ9ZR8/7v6VAdAZ18pJMeTXAhVM1dO7Pu0UkAL4BaXFqoQWSwBgVUIoRA6SzEPHYp0yGVmSwKDer1GKhu8RG8BNcWXsFb+CeHwU2dZprxOgzX9bjpuzPopB3To6jSbqF5fxCMgjZgF4QdGJAuzuYH3iFNC17lqCyXzJhZGJQ2rpm2gkt0F1YGS8av2AJnDiqWo4nr1tM1PHdMCplbPSuUhZa9WVGz3JhVnrjMnNlLs6015P+0WWWz47gWqqwsKn49KKsk2AKaB0EqNHIrVw4wroXbFfglc25Z98aNKW3vEvnGJfWyUoIXKW6x0i6tZo2L4bZnuyB6M/owCr+8G5wed3bukafkGRmSkLwnp+QzOSMR4d6+N/ZOvE9+5Nf+D//ndarvdTVPyEb4v/4Ck0DZRw== 5 6n n<50 T (n) max T (n 1),T( n/5 )+T (n 3 n/10 )+ 11 n) n 50 Is C(n) monotone non-decreasing? AAAC9HicbVFNj9MwEHXC11K+usuRy4gK1Aq2xEBhUTmsxIXjIrV0pboqjjvpWus4ke1Aq6g/hRPiyh/hF/BvcNJKbFvm9PTeeObNc5wraV0U/QnCa9dv3Lx1cLtx5+69+w+ah0efbVYYgUORqcycx9yikhqHTjqF57lBnsYKR/Hlh0offUVjZaYHbpnjJOVzLRMpuPPUtPl70NYdYH2mkPUbLMa51KXwA+2qwfpvNDwF5nDhoASZwAo0vIdeBIyNKaYT3wIs5QtgpZ8BftYx7Tz3cNBmKlFZZkC/6AEzNe7As6oFjuEV/JNpdFVnieGipHRV9vyyyhmw1b4JNse1jQZDPdsYnjZbUTeqC/YB3YAW2dTZ9DA4YrNMFClqJxS3dkyj3E1KbpwUCn0ChcWci0s+x7GHmqdoJ2Wd+gqeeGYGiT8iybSDmr36ouSptcs09p0pdxd2V6vI/2njwiUnk1LqvHCoxXpRUihwGVRfCDNpUDi19IALI71XEBfc5+b8R29tqWfnKLYuKReFliKb4Q6r3MIZXqVIdzPbB8OX3Xdd+ul16/RkE+cBeUQekzah5C05JR/JGRkSEdBgFHwJePgt/B7+CH+uW8Ng8+Yh2arw118pF+RQ { 5 }

Claim. T(n) ≤ 44 n. A. Yes, obviously. Pf. [ by strong induction ] B. Yes, but proof is tedious. Base case: T(n) ≤ 6 n for n < 50 (mergesort). C. Yes, but proof is hard. Inductive hypothesis: assume true for 1, 2, …, n – 1. Induction step: for n ≥ 50, we have either T(n) ≤ T(n – 1) ≤ 44 n or D. No. T(n) ≤ T(⎣n / 5⎦) + T(n – 3 ⎣n / 10⎦) + 11/5 n inductive ≤ 44 (⎣n / 5⎦) + 44 (n – 3 ⎣n / 10⎦) + 11/5 n C(19) = 165 hypothesis C(20) = 134.2 ≤ 44 (n / 5) + 44 n – 44 (n / 4) + 11/5 n for n ≥ 50, 3 ⎣n / 10⎦ ≥ n / 4 = 44 n. ▪ 59 60

Divide-and-conquer: quiz 5 Linear-time selection retrospective

Suppose that we divide n elements into ⎣n / r⎦ groups of r elements each, Proposition. [Blum–Floyd–Pratt–Rivest–Tarjan 1973] There exists a and use the median-of-medians of these ⎣n / r⎦ groups as the pivot. compare-based selection algorithm whose worst-case running time is O(n). JOURNAL OF COMPUTER AND SYSTEM SCIENCES 7, 448-461 (1973) For which r is the worst-case running time of select O(n) ?

Time Bounds for Selection*

MANUEL BLUM, ROBERT W. FLOYD, , A. r = 3 T(n) = T(n / 3) + T(n − 2n / 6) + Θ(n) ⇒ T(n) = Θ(n log n) RONALD L. RIVEST, AND ROBERT E. TARJAN Department of Computer Science, Stanford University, Stanford, California 94305 B. r = 7 T(n) = T(n / 7) + T(n − 4n / 14) + Θ(n) ⇒ T(n) = Θ(n) Received November 14, 1972

C. Both A and B. The number of comparisons required to select the i-th smallest of n numbers is shown to be at most a linear function of n by analysis of a new selection algorithm--PICK. Specifically, no more than 5.4305 n comparisons are ever required. This bound is D. Neither A nor B. improved for extreme values of i, and a new lower bound on the requisite number of comparisons is also proved.

1. INTRODUCTION

Theory. In this paper we present a new selection algorithm, PICK, and derive by an analysis of its efficiency the (surprising) result that the cost of selection is at most a linear function of the number of input items. In addition, we prove a new lower bound Optimized versionfor the costof of BFPRT:selection. ≤ 5.4305 n compares. The selection problem is perhaps best exemplified by the computation of medians. Upper bound: [Dor–ZwickIn general, we may wish 1995] to select the i-th≤ smallest2.95 ofn a compares.set of n distinct numbers, or the element ranking closest to a given percentile level. Lower bound: [Dor–ZwickInterest in this problem 1999] may be traced ≥ to(2 the + realm 2− 80 of )sports n compares.and the design of (traditionally, tennis) tournaments to select the first- and second-best players. In 1883, Lewis Carroll published an article [1] denouncing the unfair method by which the second-best player is usually determined in a "knockout tournament" -- the loser of the final match is often not the second-best! (Any of the players who lost only Practice. Constantsto toothe best largeplayer may tobe second-best.) be useful. Around 1930, Hugo Steinhaus brought the 61 problem into the realm of algorithmic complexity by asking for the minimum number 62 of matches required to (correctly) select both the first- and second-best players from a field of n contestants. In 1932, J. Schreier [8] showed that no more than n + [logg(n)]- 2 matches are required, and in 1964, S. S. Kislitsin [6] proved this number to be necessary as well. Schreier's method uses a knockout tournament to determine the winner, followed by a second knockout tournament among the

* This work was supported by the National Science Foundation under grant GJ-992. 448 Copyright © I973 by Academic Press, Inc. All rights of reproduction in any form reserved. Closest pair of points

Closest pair problem. Given n points in the plane, find a pair of points 5. DIVIDE AND CONQUER with the smallest Euclidean distance between them.

‣ mergesort Fundamental geometric primitive. ‣ counting inversions Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. ‣ randomized quicksort Special case of nearest neighbor, Euclidean MST, Voronoi. ‣ median and selection fast closest pair inspired fast algorithms for these problems ‣ closest pair of points

SECTION 5.4

64

Closest pair of points Closest pair of points: first attempt

Closest pair problem. Given n points in the plane, find a pair of points Sorting solution. with the smallest Euclidean distance between them. Sort by x-coordinate and consider nearby points. Sort by y-coordinate and consider nearby points. Brute force. Check all pairs with Θ(n2) distance calculations.

1D version. Easy O(n log n) algorithm if points are on a line.

Non-degeneracy assumption. No two points have the same x-coordinate.

65 66 Closest pair of points: first attempt Closest pair of points: second attempt

Sorting solution. Divide. Subdivide region into 4 quadrants. Sort by x-coordinate and consider nearby points. Sort by y-coordinate and consider nearby points.

8

67 68

Closest pair of points: second attempt Closest pair of points: divide-and-conquer algorithm

Divide. Subdivide region into 4 quadrants. Divide: draw vertical line L so that n / 2 points on each side. Obstacle. Impossible to ensure n / 4 points in each piece. Conquer: find closest pair in each side recursively. Combine: find closest pair with one point in each side. Return best of 3 solutions. seems like Θ(n2)

L

21 8

12

69 70 How to find closest pair with one point in each side? How to find closest pair with one point in each side?

Find closest pair with one point in each side, assuming that distance < δ. Find closest pair with one point in each side, assuming that distance < δ. Observation: suffices to consider only those points within δ of line L. Observation: suffices to consider only those points within δ of line L. Sort points in 2 δ-strip by their y-coordinate. Check distances of only those points within 7 positions in sorted list!

why?

L L

7

6

5 21 4 21

δ = min(12, 21) δ = min(12, 21) 12 12 3

2

1 δ 71 δ 72

How to find closest pair with one point in each side? Closest pair of points: divide-and-conquer algorithm

th Def. Let si be the point in the 2 δ-strip, with the i smallest y-coordinate.

CLOSEST-PAIR(p1, p2, …, pn) Claim. If ⎜j – i⎟ > 7, then the distance between

______s and s is at least δ. L i j Compute vertical line L such that half the points O(n) are on each side of the line. Pf. δ1 ← CLOSEST-PAIR(points in left half). T(n / 2) Consider the 2δ-by-δ rectangle R in strip sj δ2 ← CLOSEST-PAIR(points in right half). T(n / 2) whose min y-coordinate is y-coordinate of si. δ ← min { δ1 , δ2 }. Distance between si and any point sj R ½δ above R is ≥ δ. Delete all points further than δ from line L. O(n) diameter is δ / 2 < O(n log n) Subdivide R into 8 squares. AAACT3icdVDBTuMwEHXKsgsFllKOXKytWO0pJKWlRewBicseuxIFpKaqHGcKFo6TtSeoVdQ/4Gu4Ll/BjT/hhNbJFokieJKtp/dmPJ4XplIY9LxHp7L0afnzl5XV6tr6xtfN2lb9zCSZ5tDniUz0RcgMSKGgjwIlXKQaWBxKOA+vTwr//Aa0EYk6xWkKw5hdKjEWnKGVRrXvQQQSGQ2O6F5xBeaPxrw5K/jPUij9Ua3hue2D/U63RT3XP2zvN5sFabW7nRb1Xa9Eg8zRG2059SBKeBaDQi6ZMQPfS3GYM42CS5hVg8xAyvg1u4SBpYrFYIZ5udCM7lolouNE26OQlurrjpzFxkzj0FbGDK/MW68Q3/MGGY67w1yoNENQ/P+gcSYpJrRIh0ZCA0c5tYRxLexfKb9imnG0GS5MKd9OgS9skk8yJXgSwRtV4gQ1m9kUX6KiH5N+0z10/d+txnF3HucK2SHfyA/ikw45Jr9Ij/QJJ7fkjvwl986D8+Q8V+alFWdOtskCKqv/AOSCstc= ½δ Sort remaining points by y-coordinate. At most 1 point per square. Scan points in y-order and compare distance between si At most 7 other points can be in R. ▪ each point and next 7 neighbors. If any of these O(n) distances is less than δ, update δ.

constant can be improved with more RETURN δ.

______refined geometric packing argument ______

2δ 73 74 Divide-and-conquer: quiz 6 Refined version of closest-pair algorithm

What is the solution to the following recurrence? Q. How to improve to O(n log n) ? A. Don’t sort points in strip from scratch each time. Each recursive call returns two lists: all points sorted by x-coordinate, (1) n =1 and all points sorted by y-coordinate. T (n)= T ( n/2 )+T ( n/2 )+(n log n) n>1 Sort by merging two pre-sorted lists. AAAC2nicbVFNixNBEO0Zv9b4lV0PHrwUZpUEIc4swq6ElQVBPa6QuAvpIfR0apJme7qH7h5JGHLyJF79I/4c/4092VkwiXV6vFdVrz7SQgrrouhPEN66fefuvb37rQcPHz1+0t4/+Gp1aTiOuJbaXKbMohQKR044iZeFQZanEi/Sqw+1fvENjRVaDd2ywCRnMyUywZnz1KT9e9hVPTiFFk1xJlTFfS+7atEBHc7RsW7cg1dAHS5cBSKDQ+Vz40NYAaXjqH+MeeJzh10qM6m1AfXmCKhZ4x7QwWs6gFrkKGSj1fBGajwUUKlnoHas3t9YtSiqaTPbpN2J+tE6YBfEDeiQJs4n+8EBnWpe5qgcl8zacRwVLqmYcYJL9MuWFgvGr9gMxx4qlqNNqvVtV/DSM1PI/G6ZVg7W7L8VFcutXeapz8yZm9ttrSb/p41Ll50klVBF6VDxa6OslOA01I+CqTDInVx6wLgRflbgc2YYd/6dGy7r3gXyjU2qRakE11PcYqVbOMPqK8bbN9sFo6P+u3785W3n7KQ55x55Tl6QLonJMTkjn8k5GREePAtOg4/BpzAJv4c/wp/XqWHQ1DwlGxH++gt4httm

A. T(n) = Θ(n). Theorem. [Shamos 1975] The divide-and-conquer algorithm for finding a B. T(n) = Θ(n log n). closest pair of points in the plane can be implemented in O(n log n) time.

C. T(n) = Θ(n log2 n). (1) n =1 Pf. T (n)= D. T(n) = Θ(n2). T ( n/2 )+T ( n/2 )+(n) n>1 AAAC03icbVFNb9NAEF27fJTwlZYjlxEpKBFSaleIFkVAJC4ci5S0lbJWtN6Mk1XXa2t3XSUyuSCu/BH+Ef+GtetKJGFOT+/NzJuPOJfC2CD44/l79+4/eLj/qPX4ydNnz9sHhxcmKzTHMc9kpq9iZlAKhWMrrMSrXCNLY4mX8fWXSr+8QW1EpkZ2lWOUsrkSieDMOmra/j3qqh58hBaNcS5UyV0vs27RAR0t0LJu2IM3QC0ubQkigSPlcsMjWAOlk6B/imnkckddKhOZZRrU8QlQXeMe0MFbOoBK5Chko1XwTmo81I7HpzuPFkU1a4aatjtBP6gDdkHYgA5p4nx64B3SWcaLFJXlkhkzCYPcRiXTVnCJbsvCYM74NZvjxEHFUjRRWR91Da8dM4PELZVkykLN/ltRstSYVRq7zJTZhdnWKvJ/2qSwyVlUCpUXFhW/NUoKCTaD6kMwExq5lSsHGNfCzQp8wTTj1v1xw6XunSPf2KRcFkrwbIZbrLRLq1l1xXD7ZrtgfNL/0A+/vesMz5pz7pOX5BXpkpCckiH5Ss7JmHCv7b33PntD/8L/7v/wf96m+l5T84JshP/rL+PW2NQ=

75 76

Divide-and-conquer: quiz 7 Computational complexity of closest-pair problem

What is the complexity of the 2D closest pair problem? Theorem. [Ben-Or 1983, Yao 1989] In quadratic decision tree model, any algorithm for closest pair (even in 1D) requires Ω(n log n) quadratic tests.

A. Θ(n). 2 2 (x1 - x2) + (y1 - y2) B. Θ(n log* n). C. Θ(n log log n).

D. Θ(n log n).

E. Not even Tarjan knows. Theorem. [Rabin 1976] There exists an algorithm to find the closest pair of points in theVolume 8,plane number 1 whoseIhFCRMATION expected PROCESSING running LE,TTERS time isJanuary O( n1979) .

A NOTE ON RABIN’S NEAREST-NEIGHBOR ALGORITHM * not subject to Ω(n log n) lower bound because it uses the floor function Steve FORTUNE and John HOPCROFT Department of Computer Science, Cornell University,Ithaca, NY, U.S.A.

Received 20 July 1978, revised version received 21 August 1978

Probabiktic algorithms, nearest neighbor, hashing

77 78

9. introduction tion to the distance between nearest neighbors in the whole set. Using this approximation the distance be- One notion that has received some attention tween the nearest neighbor in the whole set can be recently is that of a probabilistic algorithm. An algo- found in expected linear time. The algorithm’s use of rithm is probabilistic if at certain steps it chooses a randomness in choosing the subset is crucial. number randomly to determine the next step and at Rabin’s algorithm also assumes constant time all other steps it is deterministic. The expected run- arithmetic operations. In partictilar, he assumes that ning time of such an algorithm on a given input is a special operation, described below, which is similar obtained by averaging over all possible random to hashing can be performed in constant expected choices of the algorithm on that input. The running time. It follows from [3] that the special operation time of the algorithm as a whole can be measured can indeed be implemented by a probabilistic algo- either by averaging the expected running time over rithm to run in constant expected time given that all inputs or by taking the worst case of the expected evaluating a hash function takes constant time. running time. In [l] the former analysis is used; in The fast algorithms in [ 11, [4], and [6] all have [4] and [6] the latter analysis is used. the property that for some sequence of random Rabin [4] has proposed a probabilistic algorithm choices an incorrect answer could result. Rabin’s algo- for finding the closest pair of a set of points in rithm is apparently the only known example other Euclidean space. The running time of his algorithm than hashing of an error-free probabilistic algorithm using the worst-case expected-time measure is linear. which runs faster than the deterministic equivalent. This compares with time O(n log II) for the best of In this paper we present an algorithm to find the the known determinis?ic algorithms [or this problem nearest neighbor which runs in time O(n loglog n). j2,5,7]. The algorithm does not make any random choices; Rabin’s algc-rithm works by randomly choosing a however it does assume that the special operation subset of the points and recursively using the algo- uses only constant time. The conclusion we reach is rithm to find the distance between the nearest neigh- that mclst of the speedup of Rabin’s algorithm is due bors of the subset. Ra.bin is able to show that, with to the hashing, and not to the probabilistic nature of very high probability, tht; distance between the near- the basic algorithm. It would be interesting to find est neighbors of the subset is a very good ;Ipproxima- examples of probabilistic algorithms that are faster * Research was wpported under grant number ONR N00014, than their deterministic counterparts and for which 76-C-lrO18. the speed does noi come from the capability to hash.

20 Digression: computational geometry Convex hull

Ingenious divide-and-conquer algorithms for core geometric problems. The convex hull of a set of n points is the smallest perimeter fence enclosing the points.

problem brute clever

closest pair O(n2) O(n log n)

farthest pair O(n2) O(n log n)

convex hull O(n2) O(n log n)

Delaunay/Voronoi O(n4) O(n log n)

Euclidean MST O(n2) O(n log n)

running time to solve a 2D problem with n points Equivalent definitions. Smallest area convex polygon enclosing the points. Note. 3D and higher dimensions test limits of our ingenuity. Intersection of all convex set containing all the points.

79 80

Farthest pair Delaunay triangulation

Given n points in the plane, find a pair of points with the largest Euclidean The Delaunay triangulation is a triangulation of n points in the plane distance between them. such that no point is inside the circumcircle of any triangle.

no point in the set is inside the circumcircle

point inside circumcircle of 3 points

Delaunay triangulation of 19 points

Some useful properties. Fact. Points in farthest pair are extreme points on convex hull. No edges cross. Among all triangulations, it maximizes the minimum angle. Contains an edge between each point and its nearest neighbor. 81 82 Euclidean MST Computational geometry applications

Given n points in the plane, find MST connecting them. Applications. [distances between point pairs are Euclidean distances] Robotics. VLSI design. Data mining. Medical imaging. Computer vision. Scientific computing. airflow around an aircraft wing Finite-element meshing. Astronomical simulation. Models of physical world. Geographic information systems. Computer graphics (movies, games, virtual reality).

http://www.ics.uci.edu/~eppstein/geom.html Fact. Euclidean MST is subgraph of Delaunay triangulation. Implication. Can compute Euclidean MST in O(n log n) time. Compute Delaunay triangulation. Compute MST of Delaunay triangulation. it’s planar (≤ 3n edges) 83 84