Design and Analysis of Algorithms

Similar documents
CS 332: Algorithms. Linear-Time Sorting. Order statistics. Slide credit: David Luebke (Virginia)

Analysis of Algorithms -Quicksort-

This Lecture. Divide and Conquer. Merge Sort: Algorithm. Merge Sort Algorithm. MergeSort (Example) - 1. MergeSort (Example) - 2

Sorting Algorithms. Algorithms Kyuseok Shim SoEECS, SNU.

Algorithms and Data Structures Lecture IV

CS 332: Algorithms. Quicksort

Divide & Conquer. Divide-and-conquer algorithms. Conventional product of polynomials. Conventional product of polynomials.

CS583 Lecture 02. Jana Kosecka. some materials here are based on E. Demaine, D. Luebke slides

CS 270 Algorithms. Oliver Kullmann. Growth of Functions. Divide-and- Conquer Min-Max- Problem. Tutorial. Reading from CLRS for week 2

Data Structures Lecture 9

A Probabilistic Analysis of Quicksort

Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and conquer Binary search Powering a number Fibonacci numbers Matrix multiplication

Average-Case Analysis of QuickSort

Divide and Conquer. 1 Overview. 2 Multiplying Bit Strings. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

CS / MCS 401 Homework 3 grader solutions

Analysis of Algorithms. Introduction. Contents

Data Structures and Algorithm. Xiaoqing Zheng

CSE 4095/5095 Topics in Big Data Analytics Spring 2017; Homework 1 Solutions

Divide and Conquer. 1 Overview. 2 Insertion Sort. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

COMP285 Midterm Exam Department of Mathematics

Model of Computation and Runtime Analysis

Fundamental Algorithms

Model of Computation and Runtime Analysis

4.3 Growth Rates of Solutions to Recurrences

Algorithms Design & Analysis. Divide & Conquer

Divide and Conquer II

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, February 4/Tuesday, February 5

CSE 332. Data Structures and Parallelism

Matriculation number: You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

A recurrence equation is just a recursive function definition. It defines a function at one input in terms of its value on smaller inputs.

) n. ALG 1.3 Deterministic Selection and Sorting: Problem P size n. Examples: 1st lecture's mult M(n) = 3 M ( È

Algorithm Analysis. Algorithms that are equally correct can vary in their utilization of computational resources

Department of Informatics Prof. Dr. Michael Böhlen Binzmühlestrasse Zurich Phone:

CSI 5163 (95.573) ALGORITHM ANALYSIS AND DESIGN

CSE 5311 Notes 1: Mathematical Preliminaries

Classification of problem & problem solving strategies. classification of time complexities (linear, logarithmic etc)

Mathematical Foundation. CSE 6331 Algorithms Steve Lai

CSE 202 Homework 1 Matthias Springer, A Yes, there does always exist a perfect matching without a strong instability.

Lecture 3: Asymptotic Analysis + Recurrences

Advanced Course of Algorithm Design and Analysis

COMP26120: More on the Complexity of Recursive Programs (2018/19) Lucas Cordeiro

Design and Analysis of ALGORITHM (Topic 2)

Similar idea to multiplication in N, C. Divide and conquer approach provides unexpected improvements. Naïve matrix multiplication

Examples: data compression, path-finding, game-playing, scheduling, bin packing

Sums, products and sequences

Chapter 22 Developing Efficient Algorithms

Recursive Algorithm for Generating Partitions of an Integer. 1 Preliminary

Spectral Partitioning in the Planted Partition Model

Lecture 7: Solving Recurrences

Plan. Analysis of Multithreaded Algorithms. Plan. Matrix multiplication. University of Western Ontario, London, Ontario (Canada) Marc Moreno Maza

Trial division, Pollard s p 1, Pollard s ρ, and Fermat s method. Christopher Koch 1. April 8, 2014

Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

Recurrences: Methods and Examples

Algorithms. Elementary Sorting. Dong Kyue Kim Hanyang University

CSED233: Data Structures (2018F) Lecture13: Sorting and Selection

CS161: Algorithm Design and Analysis Handout #10 Stanford University Wednesday, 10 February 2016

Merge Sort. Outline and Reading. Divide-and-Conquer. Divide-and-conquer paradigm ( 4.1.1) Merge-sort ( 4.1.1)

Review Of Topics. Review: Induction

Test One (Answer Key)

OPTIMAL ALGORITHMS -- SUPPLEMENTAL NOTES

Data Structures and Algorithms

CS161 Design and Analysis of Algorithms. Administrative

Merge and Quick Sort

Hand Out: Analysis of Algorithms. September 8, Bud Mishra. In general, there can be several algorithms to solve a problem; and one is faced

Recurrence Relations

An Introduction to Randomized Algorithms

Lecture 3: Divide and Conquer: Fast Fourier Transform

XT - MATHS Grade 12. Date: 2010/06/29. Subject: Series and Sequences 1: Arithmetic Total Marks: 84 = 2 = 2 1. FALSE 10.

IP Reference guide for integer programming formulations.

Approximate Sorting. Institute for Theoretical Computer Science, ETH Zürich, CH-8092 Zürich

COMP26120: Introducing Complexity Analysis (2018/19) Lucas Cordeiro

ITEC 360 Data Structures and Analysis of Algorithms Spring for n 1

CSE 1400 Applied Discrete Mathematics Number Theory and Proofs

Data Structures and Algorithms CSE 465

Math 475, Problem Set #12: Answers

Hashing and Amortization

Polynomials. Computer Programming for Engineers (2014 Spring)

Polynomials with Rational Roots that Differ by a Non-zero Constant. Generalities

Algorithm Analysis. Chapter 3

SECTION 1.5 : SUMMATION NOTATION + WORK WITH SEQUENCES

CSE Introduction to Parallel Processing. Chapter 3. Parallel Algorithm Complexity

Lecture 2: April 3, 2013

Algorithms And Programming I. Lecture 5 Quicksort

Disjoint set (Union-Find)

Fundamenta Informaticae XXI (2001) IOS Press

6.003 Homework #3 Solutions

2. ALGORITHM ANALYSIS

CS 5150/6150: Assignment 1 Due: Sep 23, 2010

Practice Problems: Taylor and Maclaurin Series

Matrix Multiplication. Data Structures and Algorithms Andrei Bulatov

Design and Analysis of Algorithms

CS:3330 (Prof. Pemmaraju ): Assignment #1 Solutions. (b) For n = 3, we will have 3 men and 3 women with preferences as follows: m 1 : w 3 > w 1 > w 2

WRITTEN ASSIGNMENT 1 ANSWER KEY

ACCESS TO SCIENCE, ENGINEERING AND AGRICULTURE: MATHEMATICS 1 MATH00030 SEMESTER / Statistics

For combinatorial problems we might need to generate all permutations, combinations, or subsets of a set.

The Binomial Theorem

Lecture 3: August 31

6.046 Recitation 5: Binary Search Trees Bill Thies, Fall 2004 Outline

THE ANALYSIS OF RANGE QUICKSELECT AND RELATED PROBLEMS

MAT1026 Calculus II Basic Convergence Tests for Series

Transcription:

Desig ad Aalysis of Algorithms CSE 53 Lecture 9 Media ad Order Statistics Juzhou Huag, Ph.D. Departmet of Computer Sciece ad Egieerig Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms

Medias ad Order Statistics The ith order statistic of elemets S{a, a,, a } : ith smallest elemets Also called selectio problem Miimum ad maximum Media, lower media, upper media Selectio i expected/average liear time Selectio i worst-case liear time Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms

Order Statistics The ith order statistic i a set of elemets is the ith smallest elemet The miimum is thus the st order statistic The maximum is (duh) the th order statistic The media is the / order statistic If is eve, there are medias How ca we calculate order statistics? What is the ruig time? Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 3

Order Statistics How may comparisos are eeded to fid the miimum elemet i a set? The maximum? Ca we fid the miimum ad maximum with less tha twice the cost? Yes: Walk through elemets by pairs Compare each elemet i pair to the other Compare the largest to maximum, smallest to miimum Total cost: 3 comparisos per elemets O(3/) Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 4

O(lg ) Algorithm Suppose elemets are sorted by a O(lg ) algorithm, e.g., MERGE-SORT Miimum: the first elemet Maximum: the last elemet The ith order statistic: the ith elemet. Media: If is odd, the ((+)/)th elemet. If is eve, the ( (+)/ )th elemet, lower media the ( (+)/ )th elemet, upper media All selectios ca be doe i O(), so total: O(lg ). Ca we do better? Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 5

Selectio i Expected Liear Time O() Select ith elemet A divide-ad-coquer algorithm RANDOMIZED- SELECT Similar to quicksort, partitio the iput array recursively Ulike quicksort, which works o both sides of the partitio, just work o oe side of the partitio. Called prue-ad-search, prue oe side, just search the other side). Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 6

Fidig Order Statistics: The Selectio Problem A more iterestig problem is selectio: fidig the ith smallest elemet of a set We will show: A practical radomized algorithm with O() expected ruig time A cool algorithm of theoretical iterest oly with O() worst-case ruig time Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 7

Radomized Selectio Key idea: use partitio() from quicksort But, oly eed to examie oe subarray This savigs shows up i ruig time: O() We will agai use a slightly differet partitio tha the book: q RadomizedPartitio(A, p, r) A[q] A[q] p q r Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 8

Radomized Selectio RadomizedSelect(A, p, r, i) if (p r) the retur A[p]; q RadomizedPartitio(A, p, r) k q - p + ; if (i k) the retur A[q]; if (i < k) the retur RadomizedSelect(A, p, q-, i); else retur RadomizedSelect(A, q+, r, i-k); k A[q] A[q] p q r Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 9

Radomized Selectio Aalyzig RadomizedSelect() Worst case: partitio always 0:- T() T(-) + O()??? O( ) (arithmetic series) No better tha sortig! Best case: suppose a 9: partitio T() T(9/0) + O()??? O() (Master Theorem, case 3) Better tha sortig! What if this had bee a 99: split? Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 0

Radomized Selectio Average case For upper boud, assume ith elemet always falls i larger side of partitio: T ( ) T ( max( k, k) ) + Θ( ) k 0 T ( k) + Θ( ) k / Let s show that T() O() by substitutio What happeed here? Max(k-, -k)k- if k> / Max(k-, -k)-k if k< / Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms

CSE53 Desig ad Aalysis of Algorithms Dept. CSE, UT Arligto What happeed he Split the recurrece What happeed he What happeed here? What happeed here? Radomized Selectio Assume T() c for sufficietly large c: ( ) ( ) ( ) ( ) ( ) ( ) ( ) c c c k k c ck k T T k k k k + Θ + Θ + Θ + Θ + Θ ) ( ) ( / / The recurrece we started with Substitute T() c for T(k) Expad arithmetic series Multiply it out

Radomized Selectio Assume T() c for sufficietly large c: T ( ) c c c c c c + + Θ( ) 4 c c c + Θ( ) 4 c c c + Θ( ) 4 c (if c is big eough) ( ) + Θ( ) The recurrece so far What Multiply happeed it out here? What Subtract happeed c/ here? What Rearrage happeed the arithmetic here? What we set out to What happeed here? prove Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 3

Worst-Case Liear-Time Selectio Radomized algorithm works well i practice What follows is a worst-case liear time algorithm, really of theoretical iterest oly Basic idea: Geerate a good partitioig elemet Call this elemet x Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 4

Worst-Case Liear-Time Selectio The algorithm i words:. Divide elemets ito groups of 5. Fid media of each group (How? How log?) 3. Use Select() recursively to fid media x of the /5 medias 4. Partitio the elemets aroud x. Let k rak(x) 5. if (i k) the retur x if (i < k) the use Select() recursively to fid ith smallest elemet i first partitio else (i > k) use Select() recursively to fid (i-k)th smallest elemet i last partitio Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 5

Worst-Case Liear-Time Selectio (Sketch situatio o the board) How may of the 5-elemet medias are x? At least / of the medias /5 / /0 How may elemets are x? At least 3 /0 elemets For large, 3 /0 /4 (How large?) So at least /4 elemets x Similarly: at least /4 elemets x Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 6

Worst-Case Liear-Time Selectio Thus after partitioig aroud x, step 5 will call Select() o at most 3/4 elemets The recurrece is therefore: T ( ) Dept. CSE, UT Arligto T T ( 5 ) + T ( 3 4) + Θ( ) ( 5) + T ( 3 4) + Θ( ) c 5 + 3c 4 + Θ( ) 9c 0 + Θ( ) c ( c 0 Θ( ) ) c if c is big eough??? /5 /5??? Substitute T() c??? Combie fractios??? Express i desired form??? What we set out to prove CSE53 Desig ad Aalysis of Algorithms 7

Worst-Case Liear-Time Selectio Ituitively: Work at each level is a costat fractio (9/0) smaller Geometric progressio! Thus the O() work at the root domiates Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 8

Liear-Time Media Selectio Give a black box O() media algorithm, what ca we do? ith order statistic: Fid media x Partitio iput aroud x if (i (+)/) recursively fid ith elemet of first half else fid (i - (+)/)th elemet i secod half T() T(/) + O() O() Ca you thik of a applicatio to sortig? Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 9

Liear-Time Media Selectio Worst-case O( lg ) quicksort Fid media x ad partitio aroud it Recursively quicksort two halves T() T(/) + O() O( lg ) Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms 0

Summary The ith order statistic of elemets S{a, a,, a } : ith smallest elemets: Miimum ad maximum. Media, lower media, upper media Selectio i expected/average liear time Worst case ruig time Prue-ad-search Selectio i worst-case liear time: Dept. CSE, UT Arligto CSE53 Desig ad Aalysis of Algorithms