Solving Recurrences. Lecture 23 CS2110 Fall 2011

Similar documents
Asymptotic Running Time of Algorithms

CS 2110: INDUCTION DISCUSSION TOPICS

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

Topic 17. Analysis of Algorithms

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

CPS 616 DIVIDE-AND-CONQUER 6-1

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

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

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy FIBONACCI NUMBERS GOLDEN RATIO, RECURRENCES

Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy FIBONACCI NUMBERS AND RECURRENCES

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

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

Asymptotic Analysis and Recurrences

Analysis of Algorithms - Using Asymptotic Bounds -

Review Of Topics. Review: Induction

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

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

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

Algorithm efficiency analysis

CS173 Running Time and Big-O. Tandy Warnow

CS483 Design and Analysis of Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Introduction to Algorithms 6.046J/18.401J/SMA5503

Design and Analysis of Algorithms

7. The Recursion Theorem

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

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

Recurrences COMP 215

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

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Lecture 1 - Preliminaries

CS Data Structures and Algorithm Analysis

7. The Recursion Theorem

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Analysis of Algorithms

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

The Divide-and-Conquer Design Paradigm

1 Divide and Conquer (September 3)

7. The Recursion Theorem

CSC2100B Data Structures Analysis

CSCI Honor seminar in algorithms Homework 2 Solution

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

Lecture 29: Tractable and Intractable Problems

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

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

Practical Session #3 - Recursions

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

CS Non-recursive and Recursive Algorithm Analysis

Lecture 22: Multithreaded Algorithms CSCI Algorithms I. Andrew Rosenberg

Algorithms CMSC The Method of Reverse Inequalities: Evaluation of Recurrent Inequalities

Problem Set 1 Solutions

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Divide and Conquer. Recurrence Relations

Data Structures and Algorithms

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Asymptotic Algorithm Analysis & Sorting

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS Analysis of Recursive Algorithms and Brute Force

CS483 Design and Analysis of Algorithms

Algorithm Design and Analysis

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

Quiz 3 Reminder and Midterm Results

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

Example: Fib(N) = Fib(N-1) + Fib(N-2), Fib(1) = 0, Fib(2) = 1

Week 7 Solution. The two implementations are 1. Approach 1. int fib(int n) { if (n <= 1) return n; return fib(n 1) + fib(n 2); } 2.

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Divide-and-Conquer Algorithms Part Two

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

COMPUTER ALGORITHMS. Athasit Surarerks.

MA/CSSE 473 Day 05. Factors and Primes Recursive division algorithm

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Chapter 4 Divide-and-Conquer

CS 231: Algorithmic Problem Solving

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

CSC236 Intro. to the Theory of Computation Lecture 7: Master Theorem; more D&C; correctness

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

Divide and Conquer Strategy

Data Structures and Algorithms Chapter 3

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Copyright 2000, Kevin Wayne 1

CSC236 Week 4. Larry Zhang

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

Divide and Conquer Algorithms

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

data structures and algorithms lecture 2

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

Fall 2016 Test 1 with Solutions

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

CS 161 Summer 2009 Homework #2 Sample Solutions

Transcription:

Solving Recurrences Lecture 23 CS2110 Fall 2011 1

Announcements Makeup Prelim 2 Monday 11/21 7:30-9pm Upson 5130 Please do not discuss the prelim with your classmates! Quiz 4 next Tuesday in class Topics: graphs, threads, recurrences, Franklin Olin s middle name 2

Analysis of MergeSort public static Comparable[] mergesort(comparable[] A, int low, int high) { if (low + 2 <= high) { //at least 2 elements? cost = c int mid = (low + high)/2; cost = d Comparable[] A1 = mergesort(a, low, mid); cost = T(n/2) + e Comparable[] A2 = mergesort(a, mid, high); cost = T(n/2) + f return merge(a1,a2); cost = gn + h } cost = i... Recurrence: T(n) = c + d + e + f + 2T(n/2) + gn + h recursive case T(1) = i base case How do we solve this recurrence? 3

Recurrence: T(n) = c + d + e + f + 2T(n/2) + gn + h, n > 1 T(1) = i First, simplify by dropping lower-order terms Simplified recurrence: T(n) = 2T(n/2) + cn + d, n > 1 T(1) = e Analysis of MergeSort How do we find the solution? 4

Solving Recurrences Unfortunately, solving recurrences is like solving differential equations No general technique works for all recurrences Luckily, can get by with a few common patterns You will learn some more techniques in CS 2800 5

Analysis of MergeSort Recurrence for MergeSort: T(n) = 2T(n/2) + cn + d, n > 1 T(1) = e Solution is T(n) = O(n log n) 6

Analysis of MergeSort Recurrence for MergeSort: T(n) = 2T(n/2) + cn + d, n > 1 T(1) = e Solution is T(n) = O(n log n) Tricks: Take larger constants to simplify Ignore small inputs Use inequalities instead of equalities 7

Analysis of MergeSort Proof: Strong induction on n. Show that If T(2) c and T(3) c and T(n) 2T(n/2) + cn, n > 3 then for all n 2, T(n) cn log n Basis: T(2) c c 2 log 2 T(3) c c 3 log 3 Induction step: For n > 3, T(n) 2T(n/2) + cn 2 c(n/2) log (n/2) + cn (IH) = cn (log n 1) + cn = cn log n 8

Solving Recurrences Recurrences are important when using divide & conquer to design an algorithm Solution techniques: Can sometimes change variables to get a simpler recurrence Make a guess, then prove the guess correct by induction Build a recursion tree and use it to determine solution Can use the Master Method w A cookbook scheme that handles many common recurrences To solve T(n) = at(n/b) + f(n) compare f(n) with n log b a Solution is T(n) = O(f(n)) if f(n) grows more rapidly Solution is T(n) = O(n log b a ) if n log b a grows more rapidly Solution is T(n) = O(f(n) log n) if both grow at same rate Not an exact statement of the theorem f(n) must be wellbehaved 9

Recurrence Examples T(n) = T(n 1) + 1 T(n) = O(n) Linear Search T(n) = T(n 1) + n T(n) = O(n 2 ) QuickSort worst-case T(n) = T(n/2) + 1 T(n) = O(log n) Binary Search T(n) = T(n/2) + n T(n) = O(n) T(n) = 2 T(n/2) + n T(n) = O(n log n) MergeSort T(n) = 2 T(n 1) T(n) = O(2 n ) 10

10 50 100 300 1000 n n n! 2 n n 3 n 2 nlogn 5n 50 250 500 1500 5000 33 282 665 2469 9966 100 2500 10,000 90,000 1,000,000 1000 125,000 1,000,000 27 million 1 billion 1024 3.6 million 10 billion a 16-digit number a 65-digit number an 85-digit number a 31-digit number a 161-digit number a 201-digit number a 91-digit number a 623-digit number a 744-digit number a 302-digit number unimaginably large unimaginably large protons in the known universe ~ 126 digits µsec since the big bang ~ 24 digits - Source: D. Harel, Algorithmics 11

How long would it take @ 1 instruction / µsec? 10 20 50 100 300 n n 2 n n 5 n 2 1/10,000 sec 1/2500 sec 1/400 sec 1/100 sec 9/100 sec 1/10 sec 3.2 sec 5.2 min 2.8 hr 28.1 days 1/1000 sec 1 sec 35.7 yr 2.8 hr 3.3 trillion years a 70-digit number of centuries 400 trillion centuries a 185-digit number of centuries a 75-digit number of centuries a 728-digit number of centuries The big bang was 15 billion years ago (5 10 17 secs) - Source: D. Harel, Algorithmics 12

The Fibonacci Function Mathematical definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n 1) + fib(n 2), n 2 Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, Fibonacci (Leonardo Pisano) 1170 1240? int fib(int n) { if (n == 0 n == 1) return n; else return fib(n-1) + fib(n-2); } Statue in Pisa, Italy Giovanni Paganucci 1863 13

Recursive Execution int fib(int n) { if (n == 0 n == 1) return n; else return fib(n-1) + fib(n-2); } Execution of fib(4): fib(4) fib(3) fib(2) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 14

The Fibonacci Recurrence int fib(int n) { if (n == 0 n == 1) return n; else return fib(n-1) + fib(n-2); } T(0) = c T(1) = c T(n) = T(n 1) + T(n 2) + c Solution is exponential in n But not quite O(2 n )... 15

The Golden Ratio ϕ = (a+b)/b = b/a ϕ 2 = ϕ + 1 a b ratio of sum of sides (a+b) to longer side (b) = ratio of longer side (b) to shorter side (a) ϕ = 1 + 5 2 = 1.618... 16

Fibonacci Recurrence is O(ϕ n ) Want to show that for all n, T(n) cϕ n Have ϕ 2 = ϕ + 1 Multiplying by cϕ n-2, cϕ n = cϕ n-1 + cϕ n-2 Basis: T(0) = c = cϕ 0 T(1) = c cϕ 1 Induction step: T(n) = T(n-1) + T(n-2) cϕ n-1 + cϕ n-2 = cϕ n 17

Can We Do Better? if (n <= 1) return n; int parent = 0; int current = 1; for (int i = 2; i n; i++) { int next = current + parent; parent = current; current = next; } return (current); Number of times loop is executed? n 1 Number of basic steps per loop? constant Complexity of iterative algorithm = O(n) Much, much, much better than O(ϕ n )! 18

...But We Can Do Even Better! 0 1 1 1 f n f = n+1 f n+1 f n+2 0 1 1 1 n f 0 f 1 = f n f n+1 Repeated squaring of the matrix gives O(log n) 19

But We Are Not Done Yet... Would you believe constant time? fn = φ n φ n 5 1 + 5 where φ = φ = 2 1 5 2 20

Matrix Multiplication in Less Than O(n 3 ) (Strassen's Algorithm) Idea: Naive 2 x 2 matrix multiplication takes 8 scalar multiplications, but we can do it in 7: a c b d e g f h = s 1 + s 2 - s 4 + s 6 s 4 + s 5 s 6 + s 7 s 2 - s 3 + s 5 - s 7 where s 1 = (b - d)(g + h) s 5 = a(f - h) s 2 = (a + d)(e + h) s 6 = d(g - e) s 3 = (a - c)(e + f) s 7 = e(c + d) s 4 = h(a + b) 21

Now Apply This Recursively Divide and Conquer! A C B D E G F H = S 1 + S 2 - S 4 + S 6 S 4 + S 5 S 6 + S 7 S 2 - S 3 + S 5 - S 7 22

Now Apply This Recursively Divide and Conquer! Gives recurrence M(n) = 7 M(n/2) + cn 2 for the number of multiplications Solution is M(n) = O(n log 7 ) = O(n 2.81... ) Number of additions is O(n 2 ), bound separately 23

Is That the Best You Can Do? How about 3 x 3 for a base case? best known is 23 multiplications not good enough to beat Strassen In 1978, Victor Pan discovered how to multiply 70 x 70 matrices with 143640 multiplications, giving O(n 2.795... ) Best bound to date (obtained by entirely different methods) is O(n 2.376... ) (Coppersmith & Winograd 1987) Best know lower bound is still Ω(n 2 ) 24

Moral: Complexity Matters! But you are acquiring the best tools to deal with it! 25