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

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

Data Structures and Algorithms CSE 465

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Lecture 4. Quicksort

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

Week 5: Quicksort, Lower bound, Greedy

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

Algorithms And Programming I. Lecture 5 Quicksort

Analysis of Algorithms. Randomizing Quicksort

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Review Of Topics. Review: Induction

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

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

Kartsuba s Algorithm and Linear Time Selection

Analysis of Algorithms CMPSC 565

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

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

Data selection. Lower complexity bound for sorting

Quick Sort Notes , Spring 2010

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

Problem Set 2 Solutions

Data Structures and Algorithms

COMP Analysis of Algorithms & Data Structures

CS361 Homework #3 Solutions

CS 161 Summer 2009 Homework #2 Sample Solutions

Linear Time Selection

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

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

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

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

Asymptotic Algorithm Analysis & Sorting

Divide-and-conquer. Curs 2015

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

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

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

CSE 312, Winter 2011, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Divide and Conquer Strategy

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Midterm 1 for CS 170

Computational Complexity

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

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

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

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

CS473 - Algorithms I

Advanced Analysis of Algorithms - Midterm (Solutions)

Partition and Select

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

Data Structures and Algorithms Chapter 3

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

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

Chapter 4 Divide-and-Conquer

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

Quicksort algorithm Average case analysis

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

CSCI 3110 Assignment 6 Solutions

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

Problem Set 1. CSE 373 Spring Out: February 9, 2016

CSE 591 Homework 3 Sample Solutions. Problem 1

COMP 382: Reasoning about algorithms

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

Divide and conquer. Philip II of Macedon

CS Data Structures and Algorithm Analysis

Algorithm Design and Analysis

Design and Analysis of Algorithms

5. DIVIDE AND CONQUER I

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Randomized algorithms. Inge Li Gørtz

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

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

Ch01. Analysis of Algorithms

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

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

Data Structures and Algorithms Running time and growth functions January 18, 2018

1 Substitution method

CSE 4502/5717: Big Data Analytics

Lecture 5: Loop Invariants and Insertion-sort

1 Divide and Conquer (September 3)

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

5. DIVIDE AND CONQUER I

Asymptotic Analysis and Recurrences

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

Reductions, Recursion and Divide and Conquer

CS483 Design and Analysis of Algorithms

CSE 613: Parallel Programming. Lectures ( Analyzing Divide-and-Conquer Algorithms )

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

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

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

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

COMP 9024, Class notes, 11s2, Class 1

CS 361, Lecture 14. Outline

Ch 01. Analysis of Algorithms

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

Transcription:

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, 2018 Problem 3-1. (Quicksort Median-of-3 Partition) One way to improve the randomized quicksort procedure is to partition around a pivot that is chosen more carefully than by simply picking a random element from the subarray. In the median-of-3 method, the pivot is chosen as the median of a set of 3 elements randomly selected from the subarray. (a) Assume the elements in the input array A[1..n] are distinct and n 3. We denote the sorted output array by A [1..n]. Using the median-of-3 method to choose the pivot element x, define p i = Pr{x = A [i]}. Give an exact formula for p i as a function of n and i for i = 2,3,...,n 1. p i = 6(i 1)(n i) n(n 1)(n 2) The three randomly selected elements must include one smaller than A [i], one larger than A [i], and A [i] itself. These elements can be chosen in any order. (b) By what amount have we increased the likelihood of choosing the pivot as the median of A[1..n] compared to the ordinary implementation where the pivot is chosen uniformly randomly? Assume n, and give the limiting ratio of these probabilities. lim n 6(i 1)(n i) n(n 1)(n 2) /1 n = lim 6( 2 n 1)(n n 2 ) n (n 1)(n 2) = lim n 3 2 (n 2 2n) (n 2 3n + 2) = 3 2 (c) Argue intuitively that in the Ω(n lg n) running time of quicksort, the median-of-3 method affects only the constant factor. Even if the best pivot (the median of the subarray) is chosen every time, the lower bound on the runtime is still only Ω(nlgn). So, a technique that increases the probability of a good pivot being chosen such as median-of-3 cannot improve the asymptotic runtime beyond Ω(nlgn).

2 CS161: : Recitation Section 3 Problem 3-2. (Deterministic-Quicksort) (a) Recall from lecture the Randomized-Select algorithm to select a rank(i) element from an array. function rand_select(a, p, q, i): r = rand_partition(a, p, q) k = r - p + 1 // rank of A[r] if i == k: return A[r] else if i < k: return rand_select(a, p, r - 1, i) else: return rand_select(a, r + 1, q, i - k) What is the best, worst, and expected asymptotic runtime of this algorithm? The best and expected runtimes are both θ(n). The worst case is θ(n 2 ). (b) In lecture, we saw a worst-case linear-time algorithm for selection that involved recursively choosing the median of n 5 group medians to be pivot. Given this algorithm, how would you modify the Quicksort code below to bound the worstcase runtime on any input to θ(nlgn)? function rand_quicksort(a, p, q): if p < q: i = rand_int(p, q) // choose a pivot r = partition(a, p, q, i) rand_quicksort(a, p, r - 1) rand_quicksort(a, r + 1, q) Use the deterministic selection algorithm to find the median. Take the median as the pivot and partition around it. Now, recurse on both sides. The recurrence for Deterministic-Quicksort is T (n) = 2T (n/2) + θ(n). Apply Master Theorem case 2 to obtain T (n) = θ(nlgn). (c) Why is the above algorithm typically not used in practice? The worst-case linear-time selection algorithm runs slowly due to large constant factors.

CS161: : Recitation Section 3 3 Problem 3-3. (Deterministic-Select) In lecture, we covered Deterministic-Select for groups of size 5. In this problem we generalize the algorithm to groups of size k. Consider the pseudocode below: deterministic-select(a, k, i): 1. Divide A into groups of size k, and find group medians. 2. Recursively call deterministic-select to find the median, x, of the n/k group medians 3. Partition around x. Let r = rank(x). if r == i: return x else if i < r: Recurse on left. A[:r-1]. else: Recurse on right. A[r+1:]. (a) Give a recurrence for Deterministic-Select with groups of size 7. Half of the n/7 groups have at least 4 elements greater than the pivot. Omit the group containing the pivot and the group of n mod 7 elements. The number of elements greater than the pivot is at least: 4( 1 2 n/7 2) 2n 7 8 The same holds true for the number of elements less than the pivot. So, each time we recurse on at most n ( 2n 7 8) = 5n 7 + 8 elements. T (n) T ( n/7 ) + T (5n/7 + 8) + θ(n) (b) Argue that the algorithm with groups of size 7 runs in θ(n). T (n) = Ω(n) is trivial. Guess T (m) cm for m < n. T (n) c( n/7 ) + c(5n/7 + 8) + n c(n/7) + c + 5cn/7 + 8c + n = 6cn/7 + 9c + n cn = O(n) (1) The inequalities hold for large c, n. Try, c 20, n 100. (c) Give a recurrence for Deterministic-Select with groups of size 3. Argue that the algorithm is ω(n). Half of the n/3 groups have at least 2 elements greater than the pivot. Omit the group containing the pivot and the group of n mod 3 elements. The number of elements greater than the pivot is at least: 2( 1 2 n/3 2) n 3 4

4 CS161: : Recitation Section 3 The same holds true for the number of elements less than the pivot. So, each time we recurse on at most n ( n 2n 3 4) = 3 + 4 elements. T (n) T ( n/3 ) + T (2n/3 + 4) + θ(n)

CS161: : Recitation Section 3 5 Problem 3-4. (Super Slow Search...) You are given an array A[1...n] of distinct integers. We will now consider various search algorithms to find an element x. (a) Define Random-Search: function rand_search(a, n, x): while True i = rand_int(0, n) if A[i] == x: return i What is the best, expected, and worst case runtime? Best: O(1) Worst:... Expected: O(n), from geometric distribution if we know the x is in the array. Otherwise... (b) Define Linear-Search: function linear_search(a, n, x): /* A = shuffle(a) */ for i in 1...n: if A[i] == x: return i throw exception What is the best, expected, and worst case runtime? Best: O(1) Worst: O(n) Expected: On random inputs, 1 n n i=1 i = 1 n n(n+1) 2 = n+1 2 = θ(n) (c) Define Shuffle-Search. Uncomment the first line from Linear-Search in the previous part. What is the best, expected, and worst case runtime? Best: O(n), from shuffling Worst: O(n) Expected: O(n), same as before. (d) Which searching algorithm do you prefer? Linear-Search. (Shuffle-Search makes an extra pass through the array.)

6 CS161: : Recitation Section 3 Problem 3-5. (Iterative Randomized-Select) (a) What is the space complexity of Randomized-Select? Consider the number of necessary stack frames. Expected, O(lg n). Worst-case, O(n). (b) Can we do better? Give an iterative algorithm that runs with O(1) space. Yes. Use the same tail recursion technique from problem set 2. function rand_select(a, p, q, i): while True: r = rand_partition(a, p, q) k = r - p + 1 // rank of A[r] if i == k: return A[r] else if i < k: q = r - 1 else: p = r + 1 i = i - k