Solving Recurrences. Lecture 13 CS2110 Summer 2009

Similar documents
Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Data Structures. Algorithm Performance and Big O Analysis

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

the recursion-tree method

Randomized algorithms

CS473 - Algorithms I

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

The Running Time of Programs

Collatz Sequence. Fibbonacci Sequence. n is even; Recurrence Relation: a n+1 = a n + a n 1.

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Zabin Visram Room CS115 CS126 Searching. Binary Search

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Algorithms. Margaret M. Fleck. 18 October 2010

csci 210: Data Structures Recursion

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

mod 10 = mod 10 = 49 mod 10 = 9.

Section IV.1: Recursive Algorithms and Recursion Trees

The Goldberg Rao Algorithm for the Maximum Flow Problem

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

Loop Invariants and Binary Search

Lecture 3: Finding integer solutions to systems of linear equations

Math 55: Discrete Mathematics

Algorithm Design and Recursion

1 Review of Newton Polynomials

Faster deterministic integer factorisation

Binary Multiplication

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

5.2 The Master Theorem

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Mathematical Induction. Lecture 10-11

Lecture 1: Course overview, circuits, and formulas

Approximation Algorithms

General Framework for an Iterative Solution of Ax b. Jacobi s Method

Lectures 5-6: Taylor Series

Assignment 5 - Due Friday March 6

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Analysis of Algorithms I: Optimal Binary Search Trees

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

Binary search algorithm

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Analysis of Binary Search algorithm and Selection Sort algorithm

Near Optimal Solutions

Chapter 3. if 2 a i then location: = i. Page 40

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

The Mixed Binary Euclid Algorithm

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Heap Algorithms

PUTNAM TRAINING POLYNOMIALS. Exercises 1. Find a polynomial with integral coefficients whose zeros include

From Last Time: Remove (Delete) Operation

Sequential Data Structures

1.2. Successive Differences

Lesson 4 Annuities: The Mathematics of Regular Payments

recursion, O(n), linked lists 6/14

Math 55: Discrete Mathematics

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

Lecture Notes on Polynomials

6. Standard Algorithms

Lecture Notes on Binary Search Trees

LS.6 Solution Matrices

The Fibonacci Sequence and the Golden Ratio

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

9.2 Summation Notation

CS473 - Algorithms I

Arkansas Tech University MATH 4033: Elementary Modern Algebra Dr. Marcel B. Finan

CS 103X: Discrete Structures Homework Assignment 3 Solutions

FACTORING LARGE NUMBERS, A GREAT WAY TO SPEND A BIRTHDAY

Homework until Test #2

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Factoring & Primality

Notes from Week 1: Algorithms for sequential prediction

Factoring Algorithms

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

(Refer Slide Time: 01:11-01:27)

Biostatistics 615/815

How To Create A Tree From A Tree In Runtime (For A Tree)

Distributed Computing over Communication Networks: Maximal Independent Set

Follow the Perturbed Leader

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

CS/COE

Fibonacci Numbers and Greatest Common Divisors. The Finonacci numbers are the numbers in the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

Course Notes for Math 162: Mathematical Statistics Approximation Methods in Statistics

MATH 4330/5330, Fourier Analysis Section 11, The Discrete Fourier Transform

Dynamic Programming. Lecture Overview Introduction

2x 2x 2 8x. Now, let s work backwards to FACTOR. We begin by placing the terms of the polynomial inside the cells of the box. 2x 2

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

GREATEST COMMON DIVISOR

Elliott Wave Rules and Guidelines Overview of AGET Time and Price Squares Tool page 1

A Branch and Bound Algorithm for Solving the Binary Bi-level Linear Programming Problem

Lecture 11: Tail Recursion; Continuations

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Section 1.4. Difference Equations

Transcription:

Solving Recurrences Lecture 13 CS2110 Summer 2009

In the past Complexity: Big-O definition Non-recursive programs 2

Today Complexity of recursive programs Introduce recurrences Methods to solve recurrences Substitution Recursion tree Master 3

Analysis of Merge-Sort public static Comparable[] mergesort(comparable[] A, int low, int high) { if (low < 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+1, high); cost = T(n/2) + f return merge(a1,a2); cost = gn + h } Comparable[] temp = new Comparable[1]; cost = i (base case) temp[0] = A[low]; return temp; } Recurrence: T(n) = c + d + e + f + 2T(n/2) + gn + h T(1) = i recurrence base case Recurrences are important when designing divide & conquer algorithms How do we solve this recurrence? 4

Solving Recurrences Unfortunately, solving recurrences is like solving differential equations No general technique works for all recurrences Some techniques: Substitution: Make a guess, then prove the guess correct by induction Can sometimes change variables to get a simpler recurrence Recursion-tree: Build a recursion tree and use it to determine solution Can use the Master Method A cookbook scheme that handles many common recurrences 5

Substitution Method 1. Guess the form of the solution 2. Use induction to show that the solution works. 6

Recurrence: Ex1: Analysis of Merge-Sort T(n) = c + d + e + f + 2T(n/2) + gn + h T(1) = i First, simplify the recurrence: T(n) 2T(n/2) + kn T(1) = i 7

Analysis of Merge-Sort Recurrence for MergeSort T(n) 2T(n/2) + kn T(2) = 2i + 2k Solution is T(n) = O(n log n) Proof: strong induction on n Show that T(2) 2i + 2k T(n) 2T(n/2) + kn imply T(n) pn log n (where p i + k) Basis T(2) = 2(i + k) 2p = p 2 log 2 Induction step T(n) 2T(n/2) + kn 2(pn/2 log n/2) + kn (IH) = pn (log n 1) + kn = pn log n + (k p)n pn log n 8

Ex2: 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, static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); } Fibonacci (Leonardo Pisano) 1170 1240? Statue in Pisa, Italy Giovanni Paganucci 1863 9

Recursive Execution static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; 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) 10

The Fibonacci Recurrence static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; 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 )... 11

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... 12

Fibonacci Recurrence is O( n ) want to show T(n) c n have 2 = + 1 multiplying by c n, c n+2 = c n+1 + c n Basis: T(0) = c = c 0 T(1) = c c 1 Induction step: T(n+2) = T(n+1) + T(n) c n+1 + c n = c n+2 13

Detour: 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? Less than n Number of basic steps per loop? Constant Complexity of iterative algorithm = O(n) Much, much, much, much, much, better than O( n )! 14

Detour:...But We Can Do Even Better! Let f n denote the n th Fibonacci number f 0 = 0 f 1 = 1 f n+2 = f n+1 + f n, n 0 Note that 0 1 1 1 f n f n+1 = f n+1 f n+2, thus 0 1 1 1 n f 0 f 1 = f n f n+1 Can compute the nth power of a matrix by repeated squaring in O(log n) time Gives complexity O(log n) Just a little cleverness got us from exponential to logarithmic! 15

Detour: Recall Problem-Size Examples Suppose we have a computing device that can execute 1000 operations per second; how large a problem can we solve? 1 second 1 minute 1 hour 1 century log n 2 1000 2 60,000 2 3,600,000 2 3.2 1012 n 1000 60,000 3,600,000 3.2 10 12 n log n 140 4893 200,000 8.7 10 10 n 2 31 244 1897 1,776,446 3n 2 18 144 1096 1,025,631 n 3 10 39 153 1,318 2 n 9 15 21 41 16

Hints on Guessing Use recursion trees (next) Use similar, known recurrences E.g., T(n) = 2T(n/2 + 17) + n => O(n log n) Loose bounds for first guess, then adjust gradually E.g., T(n) = 2T(n/2) + n First lower bound: O(n) First upper bound: O(n 2 ) 17

Prove the exact form Hints on Proving E.g., T(n) = 2T(n/2) + n, guess T(n) = O(n) T(n) 2(cn/2) + n cn + n = O(n) WRONG Change variables E.g., T(n) = 2T(n.5 ) + log n Let m = log n => T(2 m ) = 2T(2 m/2 ) + m Let S(m) = T(2 m ) => S(m) = 2S(m/2) + m => S(m) = O(m log m) => T(n) = T(2 m ) = S(m) = O(m log m) = O(log n * log log n) 18

Recursion Tree Method Node = cost of a single subproblem Sum nodes in same level to get per-level costs Sum all per-level costs to get total costs 19

Ex: Merge-Sort T(n) = 2T(n/2) + cn cn cn c(n/2) c(n/2) cn c(n/4) c(n/4) c(n/4) c(n/4) cn c c c c c c c c c c cn Total: (log n)*cn

Master Method To solve recurrences of the form T(n) = at(n/b) + f(n), with constants a 1, b > 1, compare f(n) with n log b a if f(n) grows more rapidly, and if a*f(n/b) c*f(n) for some c 1, n sufficiently large Solution is T(n) = O(f(n)) if n log b a grows more rapidly Solution is T(n) = O(n log b a ) if both grow at same rate Solution is T(n) = O(f(n) log n) 21

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 ) T(n) = 4 T(n/2) + n T(n) = O(n 2 ) 22

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