Find Min and Max. Find them independantly: 2n 2. Can easily modify to get 2n 3. Should be able to do better(?) Try divide and conquer.

Similar documents
15.1 Introduction to Lower Bounds Proofs

Selection and Adversary Arguments. COMP 215 Lecture 19

Quick Sort Notes , Spring 2010

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

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

Finding the Median. 1 The problem of finding the median. 2 A good strategy? lecture notes September 2, Lecturer: Michel Goemans

Partition and Select

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

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

1 Terminology and setup

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

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

Divide-and-conquer. Curs 2015

CSE 591 Homework 3 Sample Solutions. Problem 1

Design and Analysis of Algorithms

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

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Week 5: Quicksort, Lower bound, Greedy

Searching. Sorting. Lambdas

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

Data Structures and Algorithms

ITEC2620 Introduction to Data Structures

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

Asymptotic Analysis and Recurrences

N/4 + N/2 + N = 2N 2.

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

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

Introduction to Algorithms

Notes for Recitation 14

CSCI 3110 Assignment 6 Solutions

Analysis of Algorithms - Using Asymptotic Bounds -

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

CS361 Homework #3 Solutions

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CS 2110: INDUCTION DISCUSSION TOPICS

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Concrete models and tight upper/lower bounds

CPS 616 DIVIDE-AND-CONQUER 6-1

Kartsuba s Algorithm and Linear Time Selection

Algorithms Design & Analysis. Analysis of Algorithm

Algorithms And Programming I. Lecture 5 Quicksort

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

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Divide and Conquer Algorithms

Divide and Conquer. Arash Rafiey. 27 October, 2016

COMP 355 Advanced Algorithms

CS483 Design and Analysis of Algorithms

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

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

COMP 9024, Class notes, 11s2, Class 1

Weighted Activity Selection

Review Of Topics. Review: Induction

Divide and Conquer Algorithms

Divide and Conquer. Andreas Klappenecker

Design and Analysis of Algorithms

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Chapter 4. Recurrences

Advanced Analysis of Algorithms - Midterm (Solutions)

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

Reductions, Recursion and Divide and Conquer

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

CS483 Design and Analysis of Algorithms

Lecture 4. Quicksort

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Data Structures and Algorithms CSE 465

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

4.4 Recurrences and Big-O

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Divide and Conquer Problem Solving Method

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

Advanced Analysis of Algorithms - Homework I (Solutions)

Gaussian integrals. Calvin W. Johnson. September 9, The basic Gaussian and its normalization

Advanced Counting Techniques. Chapter 8

Omega notation. Transitivity etc.

CS 161 Summer 2009 Homework #2 Sample Solutions

1 Divide and Conquer (September 3)

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

CS173 Running Time and Big-O. Tandy Warnow

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Asymptotic Algorithm Analysis & Sorting

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

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Analysis of Algorithms. Randomizing Quicksort

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Chapter 5 Divide and Conquer

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue

Divide and conquer. Philip II of Macedon

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

Fall 2017 November 10, Written Homework 5

Analysis of Algorithms CMPSC 565

CHAPTER 8 Advanced Counting Techniques

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

Divide-and-Conquer Algorithms Part Two

Quicksort algorithm Average case analysis

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

5. DIVIDE AND CONQUER I

Transcription:

Find Min and Max Find them independantly: 2n 2. Can easily modify to get 2n 3. Should be able to do better(?) Try divide and conquer. Find_Max_Min(ELEM *L, int lower, int upper) { if (upper == lower) return lower, lower; // n=1 if (upper == lower+1) // n=2 return max(l[upper], L[lower]), min(l[upper], L[lower]); // Only 1 compare mid = (lower + upper)/2; // n>2 max1, min1 = Find_Max_Min(L, lower, mid); max2, min2 = Find_Max_Min(L, mid+1, upper); return max(l[max1], L[max2]), min(l[min1], L[min2]); } Recurrence: f(n) = { 2f(n/2) + 2 n > 2 1 n = 2 104

Solving the Recurrence Assume n = 2 k. Let s expand the recurrence a bit. f(n) = 2f(n/2) + 2 = 2[2f(n/4) + 2] + 2 = 4f(n/4) + 4 + 2 = 4[2f(n/8) + 2] + 4 + 2 = 8f(n/8) + 8 + 4 + 2 = 2 i f(n/2 i ) + = 2 k 1 f(n/2 k 1 ) + = 2 k 1 f(2) + = 2 k 1 + = n/2 + 2 k 2 = 3n/2 2 i 2 j k 1 2 j k 1 2 j k 1 2 j 105

Looking Closer But its not always true that n = 2 k. The true cost recurrence is: f(n) = 0 n = 1 1 n = 2 f( n/2 ) + f( n/2 ) + 2 n > 2 Here is what really happens: n 2 3 4 5 6 7 8 9 10 11 f(n) 1 2 4 6 8 9 10 12 14 16 3n/2 2 1 2.5 4 5.5 7 8.5 10 11.5 13 14.5 The true cost for f(n) ranges between 3n/2 2 and 5n/3 2. For what sort of input does the algorithm work best? 106

Finding a Better Algorithm What is the cost with six values? What if we divide into a group of 4 and a group of 2? With divide and conquer, we seek to minimize the work, not necessarily balance the input sizes. When does the algorithm do its best? What about 12? 24? Lesson: For divide and conquer, pay attention to what happens for small n. 107

Algorithms from Recurrences What does this model? f(n) = 0 n = 1 1 n = 2 min 1 k n 1 {f(k) + f(n k)} + 2 n > 2 n 1 2 3 4 5 6 7 8 3 3 3 4 5 4 5 5 7 6 6 7 6 9 7 8 7 9 7 11 9 9 9 9 11 8 13 10 11 10 11 10 13 9 15 12 12 12 12 12 12 15 k = 2 looks promising. Cost: f(n) = 0 n = 1 1 n = 2 f(2) + f(n 2) + 2 n > 2 What is the corresponding algorithm? 108

Is 3n/2 2 optimal? The Lower Bound Consider all states that a successful algorithm must go through: The state space lower bound. At any given instant, track the following four categories: Novices: not tested. Winners: Won at least once, never lost. Losers: Lost at least once, never won. Moderates: Both won and lost at least once. Who can get ignored? What is the initial state? What is the final state? How is this relevant? 109

Lower Bound (cont.) Every algorithm must go from (n, 0, 0, 0) to (0, 1, 1, n 2). There are 10 types of comparison. Comparing with a moderate cannot be more efficient than other comparisons, so ignore them. If we are in state (i, j, k, l) and we have a comparison, then: N : N (i 2, j + 1, k + 1, l) W : W (i, j 1, k, l + 1) L : L (i, j, k 1, l + 1) L : N (i 1, j + 1, k, l) or (i 1, j, k, l + 1) W : N (i 1, j, k + 1, l) or (i 1, j, k, l + 1) W : L (i, j, k, l) or (i, j 1, k 1, l + 2) 110

Adversarial Argument What should an adversary do? Comparing a winner to a loser is of no value. Only the following five transitions are of interest: N : N (i 2, j + 1, k + 1, l) L : N (i 1, j + 1, k, l) W : N (i 1, j, k + 1, l) W : W (i, j 1, k, l + 1) L : L (i, j, k 1, l + 1) Only the last two types increase the number of moderates, so there must be n 2 of these. The number of novices must go to 0, and the first is the most efficient way to do this: n/2 are required. 111

Finding the ith Best We need to find the following poset: We don t care about the relative order within the upper and lower groups. Can we do better than sorting? (Θ(n log n)) Can we tighten the lower bound beyond n? What if we want to find the median element? 112

Splitting a List Given an arbitrary element, split the list into those elements less and those elements greater. int Split(ELEM *L, int lower, int upper, int piv_loc) { ELEM pivot = L[piv_loc]; swap(l[lower], L[piv_loc]); piv_loc = lower; for (i=lower+1; i<=upper; i++) if (pivot > L[i]) { piv_loc++; swap(l[i], L[piv_loc]); } swap(l[lower], L[piv_loc]); return piv_loc; } If the pivot is ith best, we are done. If not, solve the subproblem recursively. 113

Cost What is the worst case cost of this algorithm? Under what circumstances? What is the average case cost if we pick the pivots at random? Let f(n, i) be the average time to find the ith best of n elements. f(n, i) = n 1 + 1 n + 1 n Set j = n k + 1. n n i k=1 k=n i+2 f(n, i) = n 1 + 1 n + 1 n f(n k, i) + 1 n 0 f(k 1, i + k n 1). n j=i+1 i 1 f(j 1, i) f(n j, i j). Let f(n) be the cost averaged over all i. f(n) = 1 n n i=1 f(n, i). 114

Technique nf(n) = n i=1 f(n, i) = n 2 n + 1 n i 1 n n i=1 j=i+1. f(n j, i j) f(j 1, i)+ It turns out that the two double sums are the same (just going from different directions). nf(n) = n 2 n + 2 n = n 2 n + 2 n n 1 j i=1 n 1 jf(j) f(j, i) 115

Technique (cont.) Therefore, n 2 f(n) = n 3 n 2 + 2 n 1 jf(j). This is an example of a full history recurrence. 116

Solving the Recurrence If we subtract the appropriate form of f(n 1), most of the terms will cancel out. n 2 f(n) (n 1) 2 f(n 1) = n 3 n 2 + 2 n 1 jf(j) (n 1) 3 + (n 1) 2 2 n 2 jf(j) = 3n 2 5n + 2 + 2(n 1)f(n 1) n 2 f(n) = (n 2 1)f(n 1) + 3n 2 5n + 2. Estimate: n 2 f(n) = (n 2 1)f(n 1) + 3n 2 5n + 2 < n 2 f(n 1) + 3n 2 f(n) < f(n 1) + 3 f(n) < 3n Therefore, f(n) is in O(n). Does this mean that the worst case is linear? 117

Improving the Worst Case Want worst case linear algorithm. Goal: Pick a pivot that guarentees discarding a fixed proportion of the elements. Can t just choose a pivot at random. Median would be ideal too expensive. Choose a constant c, pick the median of a sample of size n/c elements. Will discard at least n/2c elements. 118

Selecting an Approximate Median Algorithm: Choose the n/5 medians for groups of 5 elements of L. Recursively, select the median of the n/5 elements. Use SPLIT to partition the list into large and small elements around the median. Now, the algorithm for finding the ith element uses the median finding algorithm to recursively reach the goal. 119

Constructive Induction Is the following recurrence linear? f(n) f( n/5 ) + f( (7n 5)/10 ) + 6 n/5 + n 1. To answer this, assume it is true for some constant r such that f(n) rn for all n greater than some bound. f(n) f( n 5 ) + f( 7n 5 10 ) + 6 n 5 + n 1 r( n 5 + 1) + r(7n + 1) + 6( n 5 10 5 + 1) + n 1 ( r 5 + 7r 10 + 11 3r )n + 5 2 + 5 9r + 22 3r + 10 n +. 10 2 This is true for r 23 and n 380. Thus, we can use induction to prove that, n 380, f(n) 23n. Actually, this algorithm is not practical. Better to rely on luck. 120