CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

Similar documents
CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

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

Divide and Conquer. Recurrence Relations

Analysis of Algorithms - Using Asymptotic Bounds -

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Algorithms Chapter 4 Recurrences

Data Structures and Algorithms Chapter 3

Divide and Conquer. Andreas Klappenecker

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

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

CS483 Design and Analysis of Algorithms

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

Asymptotic Analysis and Recurrences

Recurrence Relations

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

1 Substitution method

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

Methods for solving recurrences

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

Recurrence Relations

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

Chapter 4. Recurrences

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

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

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Design and Analysis of Algorithms Recurrence. Prof. Chuhua Xian School of Computer Science and Engineering

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

Data Structures and Algorithms Chapter 3

Notes for Recitation 14

Algorithm Design and Analysis

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

The Divide-and-Conquer Design Paradigm

Divide and Conquer Algorithms

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

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

Data Structures and Algorithms CMPSC 465

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

COMP 382: Reasoning about algorithms

CS483 Design and Analysis of Algorithms

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

Divide and Conquer Algorithms

Chapter 4 Divide-and-Conquer

Introduction to Algorithms 6.046J/18.401J/SMA5503

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Mergesort and Recurrences (CLRS 2.3, 4.4)

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

Practical Session #3 - Recursions

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

CS 4349 Lecture August 30th, 2017

Asymptotic Algorithm Analysis & Sorting

CPS 616 DIVIDE-AND-CONQUER 6-1

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

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

CS 231: Algorithmic Problem Solving

Data Structures and Algorithms

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

Introduction to Divide and Conquer

Pre-lecture R: Solving Recurrences

Design and Analysis of Algorithms

Reductions, Recursion and Divide and Conquer

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

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

Data Structures and Algorithms

CS 161 Summer 2009 Homework #2 Sample Solutions

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

Binomial Coefficient Identities/Complements

Advanced Counting Techniques. Chapter 8

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

6.046 Recitation 2: Recurrences Bill Thies, Fall 2004 Outline

Lecture 3. Big-O notation, more recurrences!!

Appendix: Solving Recurrences

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

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

Analysis of Multithreaded Algorithms

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

Notes on Recurrence Relations

Algorithms Design & Analysis. Analysis of Algorithm

CSCI 3110 Assignment 6 Solutions

CS Analysis of Recursive Algorithms and Brute Force

Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

Objective. - mathematical induction, recursive definitions - arithmetic manipulations, series, products

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

5. DIVIDE AND CONQUER I

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

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

CS 240A : Examples with Cilk++

Advanced Counting Techniques

MIDTERM I CMPS Winter 2013 Warmuth

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

Divide-and-Conquer. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici.

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

Design and Analysis of Algorithms

Appendix II: Solving Recurrences [Fa 10] Wil Wheaton: Embrace the dark side! Sheldon: That s not even from your franchise!

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

COMP 9024, Class notes, 11s2, Class 1

CS325: Analysis of Algorithms, Fall Midterm

Transcription:

CS 5321: Advanced Algorithms Analysis Using Recurrence Ali Ebnenasir Department of Computer Science Michigan Technological University Acknowledgement Eric Torng Moon Jung Chung Charles Ofria Outline Motivating example: Tower of Hanoi Recursive algorithms vs. recurrence relations Specifying recurrence relations Solution techniques Substitution method Recursion-tree Master theorem Characteristic equations

Example: Towers of Hanoi Problem Definition: Three pegs: A,B,C n disks Move n disks from A to B using C as a working peg Constraints: One disk at a time A larger disk cannot be on top of a smaller disk The Towers of Hanoi A C B What if we knew we could solve part of the problem? A C B Assume we can move k (in this case, 4) different rings

Can we do one better? A C B Solved for one more! A C B Recurrence Relation T(n): minimum number of moves needed for n disks T(n-1): minimum number of moves needed for n-1disks What is the relation between these two? Try to calculate the number of moves from small values of n

Where do recurrence relations come from? Analysis of a divide and conquer algorithm Towers of Hanoi, Merge Sort, Binary Search Analysis of a combinatorial object up-down permutations This is the key analysis step you should master Use small cases to check correctness of your recurrence relation Can recurrence relations be solved? No general procedure for solving recurrence relations is known, which is why it is an art. However, linear, finite history, constant coefficient recurrences always can be solved Example: a n = 2a n-1 + 2a n-2 +1 ; a 1 = 1 ; a 2 = 1 degree = 1 history = 2 coefficients = 2, 2, and 1 In the end, what is the best way to solve this? Software like Mathematica or Maple, but you should be able to solve some on your own without such aid Recurrence Formulas T(n) = a T(n/b) + f(n) Divide the problem into a subproblems The size of each subproblem is n/b The function f(n) often represents the cost of dividing the problem to subproblems and then composing the solutions of subproblems; f(n) = D(n) + C(n)

Solution Techniques: Substitution Method Substitution Method 1. Guess a solution and prove by induction. Try back-substituting until you know what is going on Draw a recursion tree 2. Use induction to verify your guess Recursion and Mathematical Induction In both, we have general and boundary conditions: The general conditions break the problem into smaller and smaller pieces. The initial or boundary condition(s) terminate the recursion. Both take a Divide and Conquer approach to solving mathematical problems.

First Step Using the base case and the recursive case, calculate small values Use these values to help guess a solution Use induction to verify the correctness of your solution Guessing a Solution We can use mathematical induction to prove that a general function solves for a recursive one. T n = 2T n-1 + 1 ; T 0 = 0 n = 0 1 2 3 4 5 6 7 8 T n = Guess what the solution is? Prove by Induction Prove: T n = 2 n - 1 by induction: 1. Base Case: n=0: T 0 = 2 0-1 = 0 2. Inductive hypothesis: assume T n = 2 n 1 for n 0 3. Inductive Step: Show T n+1 = 2 n+1 1 for n 0 T n+1 = 2T n + 1 = 2 ( 2 n - 1 ) + 1 = 2 n+1-1

Substitution Method Drawbacks Sometimes it is difficult to guess a solution We need systematic approaches for coming up with a guess Back-substitution Recursion tree Back-Substitution Example: T(n) = 3T( n/4 ) + n, T(1) = 1 = 3(3T( n/16 )+n/4) + n = 9T( n/16 ) + 3n/4 + n = 9(3T( n/64 ) +n/16) + 3n/4 + n = 27T( n/64 )+9n/16 + 3n/4 + n? = n = i 0 i ( 3 1 ) n = 4n 4 1 3/ 4 Recursion Trees T(n) = 2 T(n/2) + n 2, T(1) = 1

Example Problem Use induction to prove that MergeSort is an O(n log n) algorithm. Mergesort(array) n = size(array) if ( n == 1) return array array1 = Mergesort(array[1.. n/2]) array2 = Mergesort(array[n/2.. n]) return Merge(array1, array2) Induction Proof Example: Prove that T(n) = 2T( n/2 ) + n, T(1) = 1 is O(n log n). We need to prove that T(n) c n log n, for all n greater than some value. 1. Base cases: T(2) = 4 c 2 and T(3) = 5 c 3 log 2 3 c 2 suffices 2. Inductive hypothesis: Assume T( n/2 ) c ( n/2 ) log ( n/2 ) What is happening to the quantifications? Induction Step Given : T( n/2 ) c ( n/2 ) log ( n/2 ) T(n) = 2T( n/2 ) + n 2( c( n/2 ) log ( n/2 ) ) + n 2( c(n/2) log (n/2) ) + n (dropping floors makes it bigger!) = c n log(n/2) + n = c n ( log(n) - log(2) ) + n = c n log(n) - c n + n (log 2 2 = 1) = c n log(n) - (c - 1) n < c n log(n) (c > 1)

Example Problem 2 T(n) = T(n/3) + T(2n/3) + n T(1) = 1 Show T(n) is Ω(n log n) by appealing to the recursion tree Recursion Tree What is the length of the longest path from root to a leaf? Solution Techniques: Master Theorem

Master Theorem T(n) = a T(n/b) + f(n) Ignore floors and ceilings for n/b constants a 1 and b > 1 f(n) any function Case 1: If f(n) = O(n log_b a-ε ) for constant ε>0, T(n) = Θ(n log_b a ) Case 2: If f(n) = Θ(n log_b a ), T(n) = Θ(n log_b a lg n) Case 3: If f(n) = Ω(n log_b a+ε ) for some constant ε >0, and if af(n/b) c f(n) for some constant c < 1 and all sufficiently large n, T(n) = Θ(f(n)). Key idea: Compare n log_b a with f(n) Examples Revisited T n = 2T n-1 + 1 ; T 0 = 0 Example: T(n) = 3T( n/4 ) + n, T(1) = 1 Example: Prove that T(n) = 2T( n/2 ) + n, T(1) = 1 is O(n log n). Solution Techniques: Characteristics Equations

Characteristic Equation Approach t n = 3t n-1 + 4t n-2 for n > 1 t 0 = 0, t 1 = 5 Rewrite recurrence t n - 3t n-1-4t n-2 = 0 Properties Homogeneous: no terms not involving t n Linear: t n terms have no squares or worse constant coefficients: 1, -3, -4 Characteristic Equation t n - 3t n-1-4t n-2 = 0 Rewrite assuming solution of the form t n = x n x n 3x n-1 4x n-2 = 0 x n-2 (x 2 3x 4) = 0 Find roots of (x 2 3x 4) (x+1)(x-4) roots are -1 and 4 Solution is of form c 1 (-1) n + c 2 4 n Solving for Constants t n = c 1 (-1) n + c 2 4 n Use base cases to solve for constants t 0 = 0 = c 1 (-1) 0 + c 2 4 0 = c 1 + c 2 t 1 = 5 = c 1 (-1) 1 + c 2 4 1 = -c 1 + 4c 2 5c 2 = 5 c 2 = 1 c 1 = -1 t n = (-1) n+1 + 4 n Always test solution on small values!

Repeated Roots for Characteristic Equations t n - 5t n-1 + 8t n-2 4t n-3 = 0 boundary conditions: t n = n for n = 0, 1, 2 x 3-5x 2 + 8x 4 = 0 (x-1)(x-2) 2 roots are 1, 2, 2 Solution is of form c 1 (1) n + c 2 2 n + c 3 n2 n If root is repeated third time, then n 2 2 n term, and so on Solving for Constants t n = c 1 (1) n + c 2 2 n + c 3 n2 n Use base cases to solve for constants t 0 = 0 = c 1 (1) 0 + c 2 2 0 + c 3 0 2 0 = c 1 + c 2 t 1 = 1 = c 1 (1) 1 + c 2 2 1 + c 3 1 2 1 = c 1 + 2c 2 + 2c 3 t 2 = 2 = c 1 (1) 2 + c 2 2 2 + c 3 2 2 2 = c 1 + 4c 2 + 8c 3 c 1 = -2, c 2 = 2, c 3 = -1/2 t n = 2 n+1 n2 n-1 2 Test the solution on small values! Inhomogeneous Equation t n - 2t n-1 = 3 n base case value for t 0 only (x 2) (x-3) = 0 (x-2) term comes from homogeneous solution If rhs is of form b n poly(n) of degree d In this case, b = 3, poly (n) = 1 is of degree 0 Plug (x-b) d+1 into characteristic equation

Solving for constants (x 2) (x-3) = 0 t n = c 1 2 n + c 2 3 n Solve for c 1 and c 2 with only t 0 base case This is only 1 equation and 2 unknowns Use recurrence to generate extra equations t n - 2t n-1 = 3 n t 1 = 2t 0 + 3 Now we have two equations t 0 = c 1 2 0 + c 2 3 0 = c 1 + c 2 t 1 = 2t 0 + 3 = c 1 2 1 + c 2 3 1 = 2c 1 + 3c 2 c 1 = t 0 3 and c 2 = 3 t n = (t 0-3)2 n + 3 n+1 Changing variable t n 3t n/2 = n if n is a power of 2 t 1 = 1 Let n = 2 i and s i = t n s i 3s i-1 = 2 i for i >= 1 s 0 = 1 (x-3)(x-2) = 0 x-3 from characteristic equation x-2 b n poly(n) rhs Solving for constants (x-3)(x-2) = 0 s i = c 1 3 i + c 2 2 i Generating two equations t 1 = s 0 = 1 = c 1 3 0 + c 2 2 0 = c 1 + c 2 t 2 = s 1 = 3t 1 +2 = 5 = c 1 3 1 + c 2 2 1 = 3c 1 + 2c 2 c 1 = 3, c 2 = -2 s i = 3 i+1 2 i+1 for i >= 0 t n = 3n lg 3 2n for n a power of 2 >= 1

Example: Up-Down Permutations A permutation σ of n numbers a 1,..., a n selected from the set {1,, n} such that one of the following properties holds for σ If i is odd, then a i < a i+1, otherwise a i > a i+1, or If i is even, then a i < a i+1, otherwise a i > a i+1 Up-down permutations are also called zigzag permutation Example The up-down permutations for n = 3 are {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} Up-down Permutations - Continued n Let t n denote the number of up-down permutations What is the recurrence relation for t n? What is the solution? Up-down permutations 1 {1} 2 {1,2}, {2,1} 3 {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} Example: Towers of Hanoi Suppose we have two disks of each size. Let n be the number of sizes of disks What is recurrence relation? What is solution?

Example: Merge Sort Merge sort breaking array into 3 pieces What is recurrence relation? What is solution? How does this compare to breaking into 2 pieces?