Kartsuba s Algorithm and Linear Time Selection

Similar documents
Reductions, Recursion and Divide and Conquer

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

CS483 Design and Analysis of Algorithms

5. DIVIDE AND CONQUER I

Linear Time Selection

1 Divide and Conquer (September 3)

5. DIVIDE AND CONQUER I

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

Introduction to Divide and Conquer

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

CS483 Design and Analysis of Algorithms

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

5. DIVIDE AND CONQUER I

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Divide-and-conquer. Curs 2015

Divide-and-Conquer and Recurrence Relations: Notes on Chapter 2, Dasgupta et.al. Howard A. Blair

Divide and conquer. Philip II of Macedon

CPS 616 DIVIDE-AND-CONQUER 6-1

CSCI 3110 Assignment 6 Solutions

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

Practical Session #3 - Recursions

Algorithms And Programming I. Lecture 5 Quicksort

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Extended Algorithms Courses COMP3821/9801

CSE 4502/5717: Big Data Analytics

Lecture 4. Quicksort

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Divide and Conquer. Arash Rafiey. 27 October, 2016

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

Discrete Mathematics U. Waterloo ECE 103, Spring 2010 Ashwin Nayak May 17, 2010 Recursion

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Data Structures and Algorithms CSE 465

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem

Week 5: Quicksort, Lower bound, Greedy

CSE 591 Homework 3 Sample Solutions. Problem 1

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Data Structures and Algorithms

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Design and Analysis of Algorithms

Selection and Adversary Arguments. COMP 215 Lecture 19

Divide and Conquer Strategy

Divide and Conquer Algorithms

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Chapter 5 Divide and Conquer

Divide and Conquer Algorithms

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Partition and Select

COMP 382: Reasoning about algorithms

CS Analysis of Recursive Algorithms and Brute Force

Divide and Conquer. Recurrence Relations

data structures and algorithms lecture 2

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

How to Multiply. 5.5 Integer Multiplication. Complex Multiplication. Integer Arithmetic. Complex multiplication. (a + bi) (c + di) = x + yi.

Chapter 4 Divide-and-Conquer

Copyright 2000, Kevin Wayne 1

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

The divide-and-conquer strategy solves a problem by: Chapter 2. Divide-and-conquer algorithms. Multiplication

DIVIDE AND CONQUER II

Introduction to Algorithms

Midterm 1 for CS 170

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

Data selection. Lower complexity bound for sorting

Divide and Conquer CPE 349. Theresa Migler-VonDollen

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Algorithm Design and Analysis

Fast Sorting and Selection. A Lower Bound for Worst Case

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Data Structures and Algorithms Chapter 3

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences

CSE 421 Algorithms: Divide and Conquer

Divide and Conquer. Chapter Overview. 1.2 Divide and Conquer

CS 161 Summer 2009 Homework #2 Sample Solutions

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Divide-and-Conquer Algorithms Part Two

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

Review Of Topics. Review: Induction

On Partial Sorting. Conrado Martínez. Univ. Politècnica de Catalunya, Spain

shelat 16f-4800 sep Matrix Mult, Median, FFT

Asymptotic Analysis and Recurrences

Quick Sort Notes , Spring 2010

Advanced Analysis of Algorithms - Midterm (Solutions)

Objec&ves. Review. Divide and conquer algorithms

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

CSE 421, Spring 2017, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Divide and Conquer. Andreas Klappenecker

CS 231: Algorithmic Problem Solving

Matrix Multiplication

Range Medians. 1 Introduction. Sariel Har-Peled 1 and S. Muthukrishnan 2

1 Terminology and setup

Divide and Conquer algorithms

Transcription:

CS 374: Algorithms & Models of Computation, Fall 2015 Kartsuba s Algorithm and Linear Time Selection Lecture 09 September 22, 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 32

Part I Fast Multiplication Chandra & Manoj (UIUC) CS374 2 Fall 2015 2 / 32

Multiplying Numbers Problem Given two n-digit numbers x and y, compute their product. Grade School Multiplication Compute partial product by multiplying each digit of y with x and adding the partial products. 3141 2718 25128 3141 21987 6282 8537238 Chandra & Manoj (UIUC) CS374 3 Fall 2015 3 / 32

Time Analysis of Grade School Multiplication 1 Each partial product: Θ(n) 2 Number of partial products: Θ(n) 3 Addition of partial products: Θ(n 2 ) 4 Total time: Θ(n 2 ) Chandra & Manoj (UIUC) CS374 4 Fall 2015 4 / 32

A Trick of Gauss Carl Friedrich Gauss: 1777 1855 Prince of Mathematicians Observation: Multiply two complex numbers: (a + bi) and (c + di) (a + bi)(c + di) = ac bd + (ad + bc)i Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 32

A Trick of Gauss Carl Friedrich Gauss: 1777 1855 Prince of Mathematicians Observation: Multiply two complex numbers: (a + bi) and (c + di) (a + bi)(c + di) = ac bd + (ad + bc)i How many multiplications do we need? Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 32

A Trick of Gauss Carl Friedrich Gauss: 1777 1855 Prince of Mathematicians Observation: Multiply two complex numbers: (a + bi) and (c + di) (a + bi)(c + di) = ac bd + (ad + bc)i How many multiplications do we need? Only 3! If we do extra additions and subtractions. Compute ac, bd, (a + b)(c + d). Then (ad + bc) = (a + b)(c + d) ac bd Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 32

Divide and Conquer Assume n is a power of 2 for simplicity and numbers are in decimal. Split each number into two numbers with equal number of digits 1 x = x n 1 x n 2... x 0 and y = y n 1 y n 2... y 0 2 x = x n 1... x n/2 0... 0 + x n/2 1... x 0 3 x L = 10 n/2 x L where x L = x n 1... x n/2 and x R = x n/2 1... x 0 4 Similarly y = 10 n/2 y L + y R where y L = y n 1... y n/2 and y R = y n/2 1... y 0 Chandra & Manoj (UIUC) CS374 6 Fall 2015 6 / 32

Example 1234 5678 = (100 12 + 34) (100 56 + 78) = 10000 12 56 +100 (12 78 + 34 56) +34 78 Chandra & Manoj (UIUC) CS374 7 Fall 2015 7 / 32

Divide and Conquer Assume n is a power of 2 for simplicity and numbers are in decimal. 1 x = x n 1 x n 2... x 0 and y = y n 1 y n 2... y 0 2 x = 10 n/2 x L + x R where x L = x n 1... x n/2 and x R = x n/2 1... x 0 3 y = 10 n/2 y L + y R where y L = y n 1... y n/2 and Therefore y R = y n/2 1... y 0 xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R Chandra & Manoj (UIUC) CS374 8 Fall 2015 8 / 32

Time Analysis xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0 s to the right) Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 32

Time Analysis xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0 s to the right) T(n) = 4T(n/2) + O(n) T(1) = O(1) Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 32

Time Analysis xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0 s to the right) T(n) = 4T(n/2) + O(n) T(1) = O(1) T(n) = Θ(n 2 ). No better than grade school multiplication! Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 32

Time Analysis xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0 s to the right) T(n) = 4T(n/2) + O(n) T(1) = O(1) T(n) = Θ(n 2 ). No better than grade school multiplication! Can we invoke Gauss s trick here? Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 32

Improving the Running Time xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R Gauss trick: x L y R + x R y L = (x L + x R )(y L + y R ) x L y L x R y R Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 32

Improving the Running Time xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R Gauss trick: x L y R + x R y L = (x L + x R )(y L + y R ) x L y L x R y R Recursively compute only x L y L, x R y R, (x L + x R )(y L + y R ). Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 32

Improving the Running Time xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R Gauss trick: x L y R + x R y L = (x L + x R )(y L + y R ) x L y L x R y R Recursively compute only x L y L, x R y R, (x L + x R )(y L + y R ). Time Analysis Running time is given by T(n) = 3T(n/2) + O(n) T(1) = O(1) which means Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 32

Improving the Running Time xy = (10 n/2 x L + x R )(10 n/2 y L + y R ) = 10 n x L y L + 10 n/2 (x L y R + x R y L ) + x R y R Gauss trick: x L y R + x R y L = (x L + x R )(y L + y R ) x L y L x R y R Recursively compute only x L y L, x R y R, (x L + x R )(y L + y R ). Time Analysis Running time is given by T(n) = 3T(n/2) + O(n) T(1) = O(1) which means T(n) = O(n log 2 3 ) = O(n 1.585 ) Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 32

State of the Art Schönhage-Strassen 1971: O(n log n log log n) time using Fast-Fourier-Transform (FFT) Martin Fürer 2007: O(n log n2 O(log n) ) time Conjecture There is an O(n log n) time algorithm. Chandra & Manoj (UIUC) CS374 11 Fall 2015 11 / 32

Analyzing the Recurrences 1 Basic divide and conquer: T(n) = 4T(n/2) + O(n), T(1) = 1. Claim: T(n) = Θ(n 2 ). 2 Saving a multiplication: T(n) = 3T(n/2) + O(n), T(1) = 1. Claim: T(n) = Θ(n 1+log 1.5 ) Chandra & Manoj (UIUC) CS374 12 Fall 2015 12 / 32

Analyzing the Recurrences 1 Basic divide and conquer: T(n) = 4T(n/2) + O(n), T(1) = 1. Claim: T(n) = Θ(n 2 ). 2 Saving a multiplication: T(n) = 3T(n/2) + O(n), T(1) = 1. Claim: T(n) = Θ(n 1+log 1.5 ) Use recursion tree method: 1 In both cases, depth of recursion L = log n. 2 Work at depth i is 4 i n/2 i and 3 i n/2 i respectively: number of children at depth i times the work at each child 3 Total work is therefore n L i=0 2i and n L i=0 (3/2)i respectively. Chandra & Manoj (UIUC) CS374 12 Fall 2015 12 / 32

Recursion tree analysis Chandra & Manoj (UIUC) CS374 13 Fall 2015 13 / 32

Part II Selecting in Unsorted Lists Chandra & Manoj (UIUC) CS374 14 Fall 2015 14 / 32

Rank of element in an array A: an unsorted array of n integers Definition For 1 j n, element of rank j is the j th smallest element in A. Unsorted array 16 14 34 20 12 5 3 19 11 Ranks 6 5 9 8 4 2 1 7 3 Sort of array 3 5 11 12 14 16 19 20 34 Chandra & Manoj (UIUC) CS374 15 Fall 2015 15 / 32

Problem - Selection Input Unsorted array A of n integers and integer j Goal Find the jth smallest number in A (rank j number) Median: j = (n + 1)/2 Chandra & Manoj (UIUC) CS374 16 Fall 2015 16 / 32

Problem - Selection Input Unsorted array A of n integers and integer j Goal Find the jth smallest number in A (rank j number) Median: j = (n + 1)/2 Simplifying assumption for sake of notation: elements of A are distinct Chandra & Manoj (UIUC) CS374 16 Fall 2015 16 / 32

Algorithm I 1 Sort the elements in A 2 Pick jth element in sorted order Time taken = O(n log n) Chandra & Manoj (UIUC) CS374 17 Fall 2015 17 / 32

Algorithm I 1 Sort the elements in A 2 Pick jth element in sorted order Time taken = O(n log n) Do we need to sort? Is there an O(n) time algorithm? Chandra & Manoj (UIUC) CS374 17 Fall 2015 17 / 32

Algorithm II If j is small or n j is small then 1 Find j smallest/largest elements in A in O(jn) time. (How?) 2 Time to find median is O(n 2 ). Chandra & Manoj (UIUC) CS374 18 Fall 2015 18 / 32

Divide and Conquer Approach 1 Pick a pivot element a from A 2 Partition A based on a. A less = {x A x a} and A greater = {x A x > a} 3 A less = j: return a 4 A less > j: recursively find jth smallest element in A less 5 A less < j: recursively find kth smallest element in A greater where k = j A less. Chandra & Manoj (UIUC) CS374 19 Fall 2015 19 / 32

Example 16 14 34 20 12 5 3 19 11 Chandra & Manoj (UIUC) CS374 20 Fall 2015 20 / 32

Time Analysis 1 Partitioning step: O(n) time to scan A 2 How do we choose pivot? Recursive running time? Chandra & Manoj (UIUC) CS374 21 Fall 2015 21 / 32

Time Analysis 1 Partitioning step: O(n) time to scan A 2 How do we choose pivot? Recursive running time? Suppose we always choose pivot to be A[1]. Chandra & Manoj (UIUC) CS374 21 Fall 2015 21 / 32

Time Analysis 1 Partitioning step: O(n) time to scan A 2 How do we choose pivot? Recursive running time? Suppose we always choose pivot to be A[1]. Say A is sorted in increasing order and j = n. Exercise: show that algorithm takes Ω(n 2 ) time Chandra & Manoj (UIUC) CS374 21 Fall 2015 21 / 32

A Better Pivot Suppose pivot is the lth smallest element where n/4 l 3n/4. That is pivot is approximately in the middle of A Then n/4 A less 3n/4 and n/4 A greater 3n/4. If we apply recursion, Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 32

A Better Pivot Suppose pivot is the lth smallest element where n/4 l 3n/4. That is pivot is approximately in the middle of A Then n/4 A less 3n/4 and n/4 A greater 3n/4. If we apply recursion, T(n) T(3n/4) + O(n) Implies T(n) = O(n)! Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 32

A Better Pivot Suppose pivot is the lth smallest element where n/4 l 3n/4. That is pivot is approximately in the middle of A Then n/4 A less 3n/4 and n/4 A greater 3n/4. If we apply recursion, T(n) T(3n/4) + O(n) Implies T(n) = O(n)! How do we find such a pivot? Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 32

A Better Pivot Suppose pivot is the lth smallest element where n/4 l 3n/4. That is pivot is approximately in the middle of A Then n/4 A less 3n/4 and n/4 A greater 3n/4. If we apply recursion, T(n) T(3n/4) + O(n) Implies T(n) = O(n)! How do we find such a pivot? Randomly? Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 32

A Better Pivot Suppose pivot is the lth smallest element where n/4 l 3n/4. That is pivot is approximately in the middle of A Then n/4 A less 3n/4 and n/4 A greater 3n/4. If we apply recursion, T(n) T(3n/4) + O(n) Implies T(n) = O(n)! How do we find such a pivot? Randomly? In fact works! Analysis a little bit later. Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 32

A Better Pivot Suppose pivot is the lth smallest element where n/4 l 3n/4. That is pivot is approximately in the middle of A Then n/4 A less 3n/4 and n/4 A greater 3n/4. If we apply recursion, T(n) T(3n/4) + O(n) Implies T(n) = O(n)! How do we find such a pivot? Randomly? In fact works! Analysis a little bit later. Can we choose pivot deterministically? Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 32

Divide and Conquer Approach A game of medians Idea 1 Break input A into many subarrays: L 1,... L k. 2 Find median m i in each subarray L i. 3 Find the median x of the medians m 1,..., m k. 4 Intuition: The median x should be close to being a good median of all the numbers in A. 5 Use x as pivot in previous algorithm. Chandra & Manoj (UIUC) CS374 23 Fall 2015 23 / 32

Example 11 7 3 42 174 310 1 92 87 12 19 15 Chandra & Manoj (UIUC) CS374 24 Fall 2015 24 / 32

Choosing the pivot A clash of medians 1 Partition array A into n/5 lists of 5 items each. L 1 = {A[1], A[2],..., A[5]}, L 2 = {A[6],..., A[10]},..., L i = {A[5i + 1],..., A[5i 4]},..., L n/5 = {A[5 n/5 4,..., A[n]}. 2 For each i find median b i of L i using brute-force in O(1) time. Total O(n) time 3 Let B = {b 1, b 2,..., b n/5 } 4 Find median b of B Chandra & Manoj (UIUC) CS374 25 Fall 2015 25 / 32

Choosing the pivot A clash of medians 1 Partition array A into n/5 lists of 5 items each. L 1 = {A[1], A[2],..., A[5]}, L 2 = {A[6],..., A[10]},..., L i = {A[5i + 1],..., A[5i 4]},..., L n/5 = {A[5 n/5 4,..., A[n]}. 2 For each i find median b i of L i using brute-force in O(1) time. Total O(n) time 3 Let B = {b 1, b 2,..., b n/5 } 4 Find median b of B Lemma Median of B is an approximate median of A. That is, if b is used a pivot to partition A, then A less 7n/10 + 6 and A greater 7n/10 + 6. Chandra & Manoj (UIUC) CS374 25 Fall 2015 25 / 32

Algorithm for Selection A storm of medians select(a, j): Form lists L 1, L 2,..., L n/5 where L i = {A[5i 4],..., A[5i]} Find median b i of each L i using brute-force Find median b of B = {b 1, b 2,..., b n/5 } Partition A into A less and A greater using b as pivot if ( A less ) = j return b else if ( A less ) > j) return select(a less, j) else return select(a greater, j A less ) Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 32

Algorithm for Selection A storm of medians select(a, j): Form lists L 1, L 2,..., L n/5 where L i = {A[5i 4],..., A[5i]} Find median b i of each L i using brute-force Find median b of B = {b 1, b 2,..., b n/5 } Partition A into A less and A greater using b as pivot if ( A less ) = j return b else if ( A less ) > j) return select(a less, j) else return select(a greater, j A less ) How do we find median of B? Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 32

Algorithm for Selection A storm of medians select(a, j): Form lists L 1, L 2,..., L n/5 where L i = {A[5i 4],..., A[5i]} Find median b i of each L i using brute-force Find median b of B = {b 1, b 2,..., b n/5 } Partition A into A less and A greater using b as pivot if ( A less ) = j return b else if ( A less ) > j) return select(a less, j) else return select(a greater, j A less ) How do we find median of B? Recursively! Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 32

Algorithm for Selection A storm of medians select(a, j): Form lists L 1, L 2,..., L n/5 where L i = {A[5i 4],..., A[5i]} Find median b i of each L i using brute-force B = [b 1, b 2,..., b n/5 ] b = select(b, n/10 ) Partition A into A less and A greater using b as pivot if ( A less ) = j return b else if ( A less ) > j) return select(a less, j) else return select(a greater, j A less ) Chandra & Manoj (UIUC) CS374 27 Fall 2015 27 / 32

Running time of deterministic median selection A dance with recurrences T(n) = T( n/5 ) + max{t( A less ), T( A greater ) } + O(n) Chandra & Manoj (UIUC) CS374 28 Fall 2015 28 / 32

Running time of deterministic median selection A dance with recurrences T(n) = T( n/5 ) + max{t( A less ), T( A greater ) } + O(n) From Lemma, T(n) T( n/5 ) + T( 7n/10 + 6 ) + O(n) and T(n) = O(1) n < 10 Chandra & Manoj (UIUC) CS374 28 Fall 2015 28 / 32

Running time of deterministic median selection A dance with recurrences T(n) = T( n/5 ) + max{t( A less ), T( A greater ) } + O(n) From Lemma, T(n) T( n/5 ) + T( 7n/10 + 6 ) + O(n) and T(n) = O(1) n < 10 Exercise: show that T(n) = O(n) Chandra & Manoj (UIUC) CS374 28 Fall 2015 28 / 32

Median of Medians: Proof of Lemma Proposition There are at least 3n/10 6 elements greater than the median of medians b. Figure : Shaded elements are all greater than b Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 32

Median of Medians: Proof of Lemma Proposition There are at least 3n/10 6 elements greater than the median of medians b. Figure : Shaded elements are all greater than b Proof. At least half of the n/5 groups have at least 3 elements larger than b, except for last group and the group containing b. Hence number of elements greater than b is: 3( (1/2) n/5 2) 3n/10 6 Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 32

Median of Medians: Proof of Lemma Proposition There are at least 3n/10 6 elements greater than the median of medians b. Corollary A less 7n/10 + 6. Via symmetric argument, Corollary A greater 7n/10 + 6. Chandra & Manoj (UIUC) CS374 30 Fall 2015 30 / 32

Questions to ponder 1 Why did we choose lists of size 5? Will lists of size 3 work? 2 Write a recurrence to analyze the algorithm s running time if we choose a list of size k. Chandra & Manoj (UIUC) CS374 31 Fall 2015 31 / 32

Median of Medians Algorithm Due to: M. Blum, R. Floyd, D. Knuth, V. Pratt, R. Rivest, and R. Tarjan. Time bounds for selection. Journal of Computer System Sciences (JCSS), 1973. Chandra & Manoj (UIUC) CS374 32 Fall 2015 32 / 32

Median of Medians Algorithm Due to: M. Blum, R. Floyd, D. Knuth, V. Pratt, R. Rivest, and R. Tarjan. Time bounds for selection. Journal of Computer System Sciences (JCSS), 1973. How many Turing Award winners in the author list? Chandra & Manoj (UIUC) CS374 32 Fall 2015 32 / 32

Median of Medians Algorithm Due to: M. Blum, R. Floyd, D. Knuth, V. Pratt, R. Rivest, and R. Tarjan. Time bounds for selection. Journal of Computer System Sciences (JCSS), 1973. How many Turing Award winners in the author list? All except Vaughn Pratt! Chandra & Manoj (UIUC) CS374 32 Fall 2015 32 / 32

Takeaway Points 1 Recursion tree method and guess and verify are the most reliable methods to analyze recursions in algorithms. 2 Recursive algorithms naturally lead to recurrences. 3 Some times one can look for certain type of recursive algorithms (reverse engineering) by understanding recurrences and their behavior. Chandra & Manoj (UIUC) CS374 33 Fall 2015 33 / 32