Algorithms and Data Structures Lecture IV

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

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

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

Data Structures and Algorithm. Xiaoqing Zheng

Fundamental Algorithms

Model of Computation and Runtime Analysis

Model of Computation and Runtime Analysis

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

Recurrence Relations

Algorithms Design & Analysis. Divide & Conquer

CS 332: Algorithms. Quicksort

Design and Analysis of Algorithms

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

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

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

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

4.3 Growth Rates of Solutions to Recurrences

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

Lecture 7: Solving Recurrences

Lecture 3: Asymptotic Analysis + Recurrences

Analysis of Algorithms -Quicksort-

CSI 5163 (95.573) ALGORITHM ANALYSIS AND DESIGN

Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

Data Structures Lecture 9

Test One (Answer Key)

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

CS / MCS 401 Homework 3 grader solutions

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

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

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

5. Solving recurrences

Recurrences: Methods and Examples

Data Structures and Algorithms

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

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

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

Chapter 6. Advanced Counting Techniques

Sequences, Mathematical Induction, and Recursion. CSE 2353 Discrete Computational Structures Spring 2018

Mathematical Foundation. CSE 6331 Algorithms Steve Lai

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

CSE 5311 Notes 1: Mathematical Preliminaries

Analysis of Algorithms. Introduction. Contents

Theorem: Let A n n. In this case that A does reduce to I, we search for A 1 as the solution matrix X to the matrix equation A X = I i.e.

Introduction to Algorithms

Math 25 Solutions to practice problems

1 Generating functions for balls in boxes

Introduction to Algorithms

Find a formula for the exponential function whose graph is given , 1 2,16 1, 6

The Binomial Theorem

Complex Numbers Solutions

APPENDIX F Complex Numbers

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

The Growth of Functions. Theoretical Supplement

P.3 Polynomials and Special products

Sect 5.3 Proportions

CSE 332. Data Structures and Parallelism

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

CSE 1400 Applied Discrete Mathematics Number Theory and Proofs

RADICAL EXPRESSION. If a and x are real numbers and n is a positive integer, then x is an. n th root theorems: Example 1 Simplify

Math 21, Winter 2018 Schaeffer/Solis Stanford University Solutions for 20 series from Lecture 16 notes (Schaeffer)

An Introduction to Randomized Algorithms

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

Lecture 4 Recursive Algorithm Analysis. Merge Sort Solving Recurrences The Master Theorem

September 2012 C1 Note. C1 Notes (Edexcel) Copyright - For AS, A2 notes and IGCSE / GCSE worksheets 1

What Is Required? You need to determine the hydronium ion concentration in an aqueous solution. K w = [H 3 O + ][OH ] =

Data Structures and Algorithms Chapter 3

Calculus 2 - D. Yuen Final Exam Review (Version 11/22/2017. Please report any possible typos.)

( 1) n (4x + 1) n. n=0

Sums, products and sequences

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

Advanced Course of Algorithm Design and Analysis

Lecture 4 Recursive Algorithm Analysis. Merge Sort Solving Recurrences The Master Theorem

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

Mathematics review for CSCI 303 Spring Department of Computer Science College of William & Mary Robert Michael Lewis

Practice Problems: Taylor and Maclaurin Series

MAT 271 Project: Partial Fractions for certain rational functions

A B = φ No conclusion. 2. (5) List the values of the sets below. Let A = {n 2 : n P n 5} = {1,4,9,16,25} and B = {n 4 : n P n 5} = {1,16,81,256,625}

Seunghee Ye Ma 8: Week 5 Oct 28

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

NICK DUFRESNE. 1 1 p(x). To determine some formulas for the generating function of the Schröder numbers, r(x) = a(x) =

Real Variables II Homework Set #5

COMP285 Midterm Exam Department of Mathematics

TEACHER CERTIFICATION STUDY GUIDE

Math 113 Exam 3 Practice

Series: Infinite Sums

Appendix F: Complex Numbers

Ma 530 Introduction to Power Series

ANSWERS SOLUTIONS iiii i. and 1. Thus, we have. i i i. i, A.

Math 475, Problem Set #12: Answers

Practice Test Problems for Test IV, with Solutions

ECE-S352 Introduction to Digital Signal Processing Lecture 3A Direct Solution of Difference Equations

Sorting Algorithms. Algorithms Kyuseok Shim SoEECS, SNU.

Polynomials. Computer Programming for Engineers (2014 Spring)

Assignment 5: Solutions

LESSON 2: SIMPLIFYING RADICALS

Chapter 2. Asymptotic Notation

SNAP Centre Workshop. Basic Algebraic Manipulation

Properties and Tests of Zeros of Polynomial Functions

Induction: Solutions

Transcription:

Algorithms ad Data Structures Lecture IV Simoas Šalteis Aalborg Uiversity simas@cs.auc.dk September 5, 00 1

This Lecture Aalyzig the ruig time of recursive algorithms (such as divide-ad-coquer) Writig ad solvig recurreces September 5, 00

Recurreces Ruig times of algorithms with Recursive calls ca be described usig recurreces A recurrece is a equatio or iequality that describes a fuctio i terms of its value o smaller iputs. For divide-ad-coquer algorithms: solvig_trivial_problem if = 1 T ( ) = um_pieces T ( / subproblem_size_factor) + dividig + combiig if > 1 Example: Merge Sort T ( ) Θ (1) if = 1 = T ( /) +Θ ( ) if > 1 September 5, 00 3

Solvig Recurreces Repeated (backward) substitutio method Expadig the recurrece by substitutio ad oticig a patter Substitutio method guessig the solutios verifyig the solutio by the mathematical iductio Recursio-trees Master method templates for differet classes of recurreces September 5, 00 4

Repeated Substitutio Let s fid the ruig time of the merge sort (let s assume that = b, for some b). 1 if = 1 T( ) = T( /) + if > 1 ( ) T( / 4 ) / expad T ( ) = T/ + substitute ( ) = + + = + T ( / 4) substitute = + + ( T ( /8) / 4) expad 3 T ( /8) 3 observe the patter = + T ( ) = i i T ( / ) + i = lg T ( / ) + lg = + lg September 5, 00 5

Repeated Substitutio Method The procedure is straightforward: Substitute Expad Substitute Expad Observe a patter ad write how your expressio looks after the i-th substitutio Fid out what the value of i (e.g., lg) should be to get the base case of the recurrece (say T(1)) Isert the value of T(1) ad the expressio of i ito your expressio September 5, 00 6

Repeated Substitutio (Ex. ) Let s fid a more exact ruig time of merge sort (let s assume that = b, for some b). T( ) if = 1 = T( /) + + 3 if > 1 ( ) T( ) ( ) T( ) T ( ) = T/ + + 3 substitute ( ) = / 4 + + 3 + + 3 expad = + + + T / 4 4 3 3 substitute ( ) = + + + + + / 8 / 3 4 3 3 expad ( ) ( ) = + + 3 3 1 0 T / 3 + + 3 observe the patter i 1 i i j T ( ) = T ( / ) + i+ 3 j= 0 = + + = + lg lg T ( / ) lg 3( 1) 5 lg 3 September 5, 00 7

Repeated Substitutio (Ex. 3) Let s fid the ruig time of the tromio tilig algorithm for a x board. T( ) 1 if = 1 = 4 T( 1) + 1 if > 1 T ( ) 44 ( T( ) 1) 1 expad 4 T( ) 4 1 substitute 4 ( 4T( 3) 1) 4 1 expad T ( ) = 4 1 + 1 substitute = + + = + + = + + + ( ) ( ) = + 3 1 0 4 T 3 4 +4 +4 observe the patter i 1 i T ( ) = 4 T ( i) + 4 j= 0 1 4 1 4 1 1 = 4 T (1) + = 4 1 3 j September 5, 00 8

Substitutio method Solve T ( ) = 4 T ( / ) + 1) Guess that T ( ) = O ( ), i.e., that Tof the form c 3 ) Assume Tk ( ) ck for k / ad 3 3) Prove T ( ) c by iductio 3 3 T ( ) = 4 T ( / ) + (recurrece) T 3 4c(/) (id. hypoth.) + 3 Thus ( ) ( )! c 3 = + (simplify) 3 c 3 = c (rearrage) 3 c if c ad 1 (satisfy) = O Subtlety: Must also check the base case (ot ecessarily T (1)) September 5, 00 9

Substitutio Method Achievig tighter bouds T Try to show ( ) ( ) Assume Tk ( ) ck T ( ) = 4 T ( /) + + 4( c/) = + c = O > c for o choice of c 0. September 5, 00 10

Substitutio Method () The problem? We could ot rewrite the equality as: T = ( ) c+ (somethig positive) T ( ) c i order to show the iequality we wated Sometimes to prove iductive step, try to stregthe your hypothesis T() (aswer you wat) - (somethig > 0) September 5, 00 11

Substitutio Method (3) Corrected proof: the idea is to stregthe the iductive hypothesis by subtractig lower-order terms! Assume Tk ( ) ck 1 ck for k< T ( ) = 4 T ( /) + 4( c ( / ) c ( / )) + = c c+ = c c ( c ) c cif c 1 1 1 1 1 September 5, 00 1

Recursio Tree A recursio tree is a coveiet way to visualize what happes whe a recurrece is iterated Good for guessig asymtotic solutios to recurreces T ( ) = T ( /4) + T ( /) + September 5, 00 13

Recursio Tree () September 5, 00 14

Master Method The idea is to solve a class of recurreces that have the form T ( ) = atb ( / ) + f ( ) a 1ad b > 1, ad f is asymptotically positive. Abstractly speakig, T() is the rutime for a algorithm ad we kow that a subproblems of size /b are solved recursively, each i time T(/b) f() is the cost of dividig the problem ad combiig the results. I merge-sort T ( ) = T ( /) +Θ( ) September 5, 00 15

Master Method () Split problem ito a parts at log b logb logba levels. There are a = leaves September 5, 00 16

Master Method (3) Number of leaves: a log Iteratig the recurrece, expadig the tree yields T ( ) = f ( ) + atb ( / ) The first term is a divisio/recombiatio cost (totaled across all levels of the tree) log The secod term is the cost of doig all b a subproblems of size 1 (total of all work pushed to leaves) b = log = f + af b + a T b ( ) ( / ) ( / ) = f + af b + a T b + ( ) ( / ) ( / )... logb 1 logb 1 logb + a f( / b ) + a T(1) Thus, logb 1 j j logb a ( ) = ( / ) +Θ( ) T afb j= 0 b a September 5, 00 17

MM Ituitio Three commo cases: Ruig time domiated by cost at leaves Ruig time evely distributed throughout the tree Ruig time domiated by cost at the root Cosequetly, to solve the recurrece, we eed oly to characterize the domiat term logb I each case compare f( ) with O ( a ) September 5, 00 18

MM Case 1 logb a ε f( ) = O( ) for some costat ε > 0 f() grows polyomially (by factor ) slower log tha b a The work at the leaf level domiates logb a Summatio of recursio-tree levels O ( ) logb a Cost of all the leaves Θ( ) logb a Thus, the overall cost Θ( ) ε September 5, 00 19

MM Case logb f( ) =Θ( a ) log ( ) ad b a are asymptotically the same f The work is distributed equally logb a throughout the tree T ( ) =Θ( lg ) (level cost) (umber of levels) September 5, 00 0

MM Case 3 logb a+ε f( ) =Ω( ) for some costat Iverse of Case 1 f() grows polyomially faster tha Also eed a regularity coditio log b a c < 1 ad > 0 such that af ( / b) cf ( ) > 0 0 The work at the root domiates T ( ) =Θ( f ( )) ε > 0 September 5, 00 1

Master Theorem Summarized Give a recurrece of the form 1. f( ) = O T ( ) =Θ. f( ) =Θ logb a ε ( ) logb a ( ) logb a ( ) logb a ( ) logb a+ε ( ) T ( ) =Θ lg ( f) T ( ) = atb ( / ) + f ( ) 3. f( ) =Ω ad af( / b) cf( ), for some c< 1, > T ( ) =Θ ( ) The master method caot solve every recurrece of this form; there is a gap betwee cases 1 ad, as well as cases ad 3 September 5, 00 0

Strategy Extract a, b, ad f() from a give recurrece Determie log b a Compare f() ad asymptotically Determie appropriate MT case, ad apply Example merge sort T ( ) = T ( /) +Θ( ) log b a a= b= = = =Θ Also f( ) =Θ( ) log b a log, ; ( ) ( log ) b a ( ) Case : T ( ) =Θ lg =Θ lg September 5, 00 3

Examples of MM T ( ) = T ( / ) + 1 a = b= = log1 1, ; 1 also f( ) = 1, f( ) =Θ(1) Case : T ( ) =Θ(lg ) Biary-search(A, p, p, r, r, s): s): q (p+r)/ if if A[q]=s the the retur q else else if if A[q]>s the the Biary-search(A, p, p, q-1, q-1, s) s) else else Biary-search(A, q+1, q+1, r, r, s) s) T ( ) = 9 T ( /3) + a = 9, b= 3; f = f = O ε= log3 9 ε ( ), ( ) ( ) with 1 Case 1: T ( ) =Θ ( ) September 5, 00 4

Examples () T ( ) = 3 T ( /4) + lg = 3, = 4; = 4 a b log 3 0.793 log4 3+ε ( ) lg, ( ) ( ) with 0. f = f =Ω ε Case 3: Regularity coditio af ( / b) = 3( /4)lg( / 4) (3/ 4) lg = cf ( ) for c = 3/ 4 T ( ) =Θ( lg ) T ( ) = T ( /) + lg =, = ; = a b log 1 1+ε f ( ) = lg, f( ) =Ω( ) with ε? also l 1 g / lg = either Case 3 or Case! September 5, 00 5

Examples: Multiplicatio (I) Multiplyig two -digit (or -bit) umbers costs digit multiplicatios usig a classical procedure. Observatio: 3*14 = ( 10 1 +3)*(1 10 1 +4) = (*1)10 + (3*1 + *4)10 1 + (3*4) To save oe multiplicatio we use a trick: (3*1 + *4) = (+3)*(1+4) - (*1) - (3*4) September 5, 00 6

Examples: Multiplicatio (II) To multiply a ad b, which are -digit umbers, we use a divide ad coquer algorithm. We split them i half: a = a 1 10 / + a 0 ad b = b 1 10 / + b 0 The: a *b = (a 1 *b 1 )10 + (a 1 *b 0 + a 0 *b 1 )10 / + (a 0 *b 0 ) Use a trick to save a multiplicatio: (a 1 *b 0 + a 0 *b 1 ) =(a 1 +a 0 )*(b 1 +b 0 ) - (a 1 *b 1 ) - (a 0 *b 0 ) September 5, 00 7

Examples: Multiplicatio (III) The umber of sigle-digit multiplicatios performed by the algorithm ca be described by a recurrece: T( ) Solutio: 1 if = 1 = 3 T( /) if > 1 T( ) = = log 3 1.585 September 5, 00 8

Next Week Sortig QuickSort HeapSort September 5, 00 9