Pseudocode Analysis. COMP3600/6466 Algorithms Lecture 5. Recursive Algorithms. Asymptotic Bounds for Recursions

Similar documents
the recursion-tree method

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

CS473 - Algorithms I

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

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

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

From Last Time: Remove (Delete) Operation

Section IV.1: Recursive Algorithms and Recursion Trees

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

Data Structures and Algorithms Written Examination

CS473 - Algorithms I

5.2 The Master Theorem

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,

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

Binary Search Trees CMPSC 122

Full and Complete Binary Trees

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

Loop Invariants and Binary Search

Data Structures. Algorithm Performance and Big O Analysis

SECTION 10-2 Mathematical Induction

Algorithms and Data Structures

Math 115 Spring 2011 Written Homework 5 Solutions

Find-The-Number. 1 Find-The-Number With Comps

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

Lecture 1: Course overview, circuits, and formulas

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

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

GRAPH THEORY LECTURE 4: TREES

Sequential Data Structures

Grade Level Year Total Points Core Points % At Standard %

Dynamic Programming Problem Set Partial Solution CMPSC 465

Solutions of Equations in One Variable. Fixed-Point Iteration II

Closest Pair Problem

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Analysis of Algorithms I: Binary Search Trees

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Research Tools & Techniques

1/1 7/4 2/2 12/7 10/30 12/25

The Running Time of Programs

The LCA Problem Revisited


Analysis of Algorithms, I

3. Mathematical Induction

The Goldberg Rao Algorithm for the Maximum Flow Problem

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

Mathematical Induction. Lecture 10-11

Small Maximal Independent Sets and Faster Exact Graph Coloring

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

csci 210: Data Structures Recursion

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA === Homework submission instructions ===

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

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

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

Approximation Algorithms

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

3.1. RATIONAL EXPRESSIONS

CS/COE

6.3 Conditional Probability and Independence

Algorithms. Margaret M. Fleck. 18 October 2010

FACTORING LARGE NUMBERS, A GREAT WAY TO SPEND A BIRTHDAY

COMP3420: Advanced Databases and Data Mining. Classification and prediction: Introduction and Decision Tree Induction

Algorithms Chapter 12 Binary Search Trees

Randomized algorithms

Circles in Triangles. This problem gives you the chance to: use algebra to explore a geometric situation

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

9.2 Summation Notation

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Geometric Series and Annuities

Optimal Binary Search Trees Meet Object Oriented Programming

Lecture 5 - CPA security, Pseudorandom functions

Lecture Notes on Binary Search Trees

SYSTEMS OF PYTHAGOREAN TRIPLES. Acknowledgements. I would like to thank Professor Laura Schueller for advising and guiding me

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Lecture Notes on Polynomials

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

CS510 Software Engineering

6.042/18.062J Mathematics for Computer Science. Expected Value I

Lecture Notes on Binary Search Trees

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

Basic Proof Techniques

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

Cpt S 223. School of EECS, WSU

Investigation of Chebyshev Polynomials

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

6.852: Distributed Algorithms Fall, Class 2

Virtual Enterprise Transactions: A Cost Model

3.3 Addition and Subtraction of Rational Numbers

Union-Find Algorithms. network connectivity quick find quick union improvements applications

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

Multiplication and Division with Rational Numbers

Probability and Statistics Prof. Dr. Somesh Kumar Department of Mathematics Indian Institute of Technology, Kharagpur

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Answer: (a) Since we cannot repeat men on the committee, and the order we select them in does not matter, ( )

A 2-factor in which each cycle has long length in claw-free graphs

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

Find all of the real numbers x that satisfy the algebraic equation:

1 Present and Future Value

Indiana State Core Curriculum Standards updated 2009 Algebra I

Transcription:

COMP600/6466 Algorithms Lecture 5 Pseudocode Analysis Iterative Recursive S 0 Dr. Hassan Hijazi Prof. Weifa Liang Recursive Algorithms Asymptotic Bounds for Recursions Total running time = Sum of times in each node of the recursion tree. Substitution method: Guess a bound and use mathematical induction to prove that the guess is correct. Can also be written as a recursion! For example, the running time T (n) can be expressed: ( (1) if n is small, T (n) = T (b n c)+ (n) otherwise. Recursion-tree method: Convert the recurrence into a tree, Use this tree to rewrite the function as a sum, Use techniques of bounding summations to solve the recurrence. We usually write: T (n) =T (b n c)+ (n) assuming that T (n) is constant for small values of n. 4

Substitution method The substitution method consists of two steps: Step 1. Guess the form of the solution. Step. Use mathematical induction to show that the guess is correct. It can be used to obtain either upper or lower bounds on a recurrence. A good guess is vital when applying this method. If the initial guess is wrong, it needs to be adjusted later. Mathematical Induction To prove that T (n) =O(g(n)) (Same principle for and ): Base case: Prove that there exists positive constants c, n 0 and n b such that T (n ) apple c g(n ) 8n s.t. n 0 apple n apple n b. Induction: Assume that T (n ) apple c g(n ) 8n s.t. n b apple n apple n, then show that T (n + 1) apple c g(n + 1). In the base case check for conditions on c and n 0. In the induction step, after substituting T (n + 1) using the recursion, make sure that the resulting number is n 0 and apple n. 5 COMP600/6466 - Lecture 4-0 6 Exercise 5.1 Using the substitution method, prove that T (n) =T + n = O(n log n) Complete the proof by hand Wrong Proofs T (n) =T + n T (n) =O(n)? 1. Base case: Let n b = 4, T (n )=c apple c n, n 0 apple n apple n b if c c, n 0 1.. Inductive step and substitution: assume that T (n ) apple c n, 8n s.t. n b apple n apple n, n +1 T (n + 1) = T apple c n +1 apple c(n + 1) apple (c + 1)(n + 1) = O(n) since n 0 apple n +1 Not same c! apple n, 8n n b 7 8

Wrong Proofs T (n) =T + n T (n) =O(n lg n)? Right Proof T (n) =T + n T (n) =O(n lg n)? 1. Base case: Let n b =, T (n )=c apple c n lg n, n 0 apple n apple n b if c c, n 0.. Inductive step and substitution: assume that T (n ) apple c n lg n, 8n s.t. n b apple n apple n, n +1 T (n + 1) = T n +1 apple c lg n +1 since n 0 apple n +1 apple n, 8n n b apple c(n + 1) lg(n + 1) c(lg )(n + 1) since lg a b =lga lg b apple c(n + 1) lg(n + 1) (n + 1)(c 1) (since lg = 1) apple c(n + 1) lg(n + 1) if c 1. Not True! Take n = nb 1. Base case: Let n b = 4, T (n )=c apple c n lg n, n 0 apple n apple n b if c c, n 0.. Inductive step and substitution: assume that T (n ) apple c n lg n, 8n s.t. n b apple n apple n, n +1 T (n + 1) = T n +1 apple c lg n +1 since n 0 apple n +1 apple n, 8n n b apple c(n + 1) lg(n + 1) c(lg )(n + 1) since lg a b =lga lg b apple c(n + 1) lg(n + 1) (n + 1)(c 1) (since lg = 1) apple c(n + 1) lg(n + 1) if c 1. 9 10 Exercise 5. Exercise 5. T (n) =T Show that l n m + T = O(n) Give an asymptotic upper bound for l n m T (n) =T + T +5 Complete the proof by hand Complete the proof by hand 11 1

Asymptotic Bounds for Recursions Also called iteration method How to make a good guess? Can be used to guess or find the solution Experience When guessing, we can make simplifying assumptions (e.g., ignore floor and ceiling) The goal is to expand the recurrence and express it as a summation 1 14 Consider the recurrence T (n) =T + cn 4 Simplification: we assume that n is a power of 4. 15

At depth d the subproblem size is n 4 d. We stop building the tree when we reach subproblem size 1, so when n = 1. 4 d This gives i = log 4 n.thus,thedepthof the tree is log 4 n. The number of levels is log 4 n + 1. d The cost at depth d is cn, except for the bottom level, whose cost is its number of nodes times T (1), that is, log 4 n T (1) = n log 4 T (1) = (n log 4 ). T (n) = Note that log 4 n 1 X i=0 log 4 n 1 X i=0 This leads to: i cn + (n log 4 )=cn log 4 n 1 X i=0 i + (n log 4 ). i = (largest term) = (1) (decreasing geometric sum). Finally, observe that n grows faster than n log 4 as its exponent is larger. Thus, T (n) = (n ) 17 18 n T (n) =T + T n + cn Why is the depth of this tree log n? 19 0

Do all paths from the root to tree leaves have the same length? If all paths were equal to the longest path, what would the cost of the last level be? 1 Exercise 5.4 n T (n) =T + T n + cn Prove that T (n) =O(n lg n) by induction.