This chapter covers recursive definition, including finding closed forms.

Similar documents
CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

The divide and conquer strategy has three basic parts. For a given problem of size n,

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

(x 2)(3x + 1) = 3x + 1

Solving Linear Recurrence Relations (8.2)

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Figure 4.1: The evolution of a rooted tree.

Reasoning and writing about algorithms: some tips

Recursively Defined Functions

Algorithmic Analysis. Go go Big O(h)!

UNIT 1 BASICS OF AN ALGORITHM

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

THE PRINCIPLE OF INDUCTION. MARK FLANAGAN School of Electrical, Electronic and Communications Engineering University College Dublin

Recursive Algorithms II

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

3.2 Recursions One-term recursion, searching for a first occurrence, two-term recursion. 2 sin.

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

CSE 20 DISCRETE MATH WINTER

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Divide and Conquer Algorithms

Recursion Chapter 3.5

#23: Sequences March 28, 2009

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Lecture 15 : Review DRAFT

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

Divide and Conquer Algorithms

What is an algorithm?

SECTION 5.1. Sequences

CS 261 Data Structures. Big-Oh Analysis: A Review

Inference rule for Induction

AXIOMS FOR THE INTEGERS

Framework for Design of Dynamic Programming Algorithms

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

Pseudo code of algorithms are to be read by.

(Refer Slide Time: 01.26)

Assignment 1 (concept): Solutions

Algorithmics. Some information. Programming details: Ruby desuka?

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

CS 173, Running Time Analysis, Counting, and Dynamic Programming. Tandy Warnow

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

n! = 1 * 2 * 3 * 4 * * (n-1) * n

I. Recursive Descriptions A phrase like to get the next term you add 2, which tells how to obtain

Computer Science 236 Fall Nov. 11, 2010

CSC236 Week 4. Larry Zhang

The Running Time of Programs

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Choice of C++ as Language

Selection (deterministic & randomized): finding the median in linear time

Solutions for Algorithm Design Exercises and Tests

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

H c. Show that n n (n!) 2 for all integers n n 1.

Algorithms and Data Structures

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Midterm CSE 21 Spring 2012

We will give examples for each of the following commonly used algorithm design techniques:

Fall Recursion and induction. Stephen Brookes. Lecture 4

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

Analysis of Algorithms. 5-Dec-16

Chapter 1. Math review. 1.1 Some sets

Recursive Definitions Structural Induction Recursive Algorithms

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Introduction to Data Structure

Analysis of algorithms

Hanoi, Counting and Sierpinski s triangle Infinite complexity in finite area

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016

Grey Codes in Black and White

CS5114: Theory of Algorithms CS5114: Theory of Algorithms lots lots Review of Mathematical Induction Mathematical Induction

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Analyzing Complexity of Lists

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Lecture 1: Overview

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Recursion and Induction

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

COMP 250 Fall Midterm examination

Solutions to the Second Midterm Exam

6.001 Notes: Section 4.1

Graph Algorithms. Chromatic Polynomials. Graph Algorithms

Week - 04 Lecture - 02 Merge Sort, Analysis

CS 303 Design and Analysis of Algorithms

A point is pictured by a dot. While a dot must have some size, the point it represents has no size. Points are named by capital letters..

Test 1 Review Questions with Solutions

ANALYSIS OF ALGORITHMS

Transcription:

Chapter 12 Recursive Definition This chapter covers recursive definition, including finding closed forms. 12.1 Recursive definitions Thus far, we have defined objects of variable length using semi-formal definitions involving... For example, we defined the summation n i=1 i by n i=1 i = 1+2+3+...+(n 1)+n This method is only ok when the reader can easily see what regular pattern the...istrying toexpress. Whenprecision isessential, e.g. when thepattern is less obvious, we need to switch to recursive definitions. Recursive function definitions in mathematics are basically similar to recursive procedures in programming languages. recursive definition defines an object in terms of smaller objects of the same type. ecause this process has to end at some point, we need to include explicit definitions for the smallest objects. So a recursive definition always has two parts: ase case or cases 137

CHPTER 12. RECURSIVE DEFINITION 138 Recursive formula For example, the summation n i=1 i can be defined as: g(1) = 1 g(n) = g(n 1)+n, for all n 2 oth the base case and the recursive formula must be present to have a complete definition. However, it is traditional not to explicitly label these two pieces. You re just expected to figure out for yourself which parts are base case(s) and which is the recursive formula. The input values are normally assumed to be integers. The true power of recursive definition is revealed when the result for n depends on the results for more than one smaller value, as in the strong induction examples. For example, the famous Fibonacci numbers are defined: F 0 = 0 F 1 = 1 F i = F i 1 +F i 2, i 2 So F 2 = 1, F 3 = 2, F 4 = 3, F 5 = 5, F 6 = 8, F 7 = 13, F 8 = 21, F 9 = 34. It isn t at all obvious how to express this pattern non-recursively. 12.2 Finding closed forms Many recursive numerical formulas have a closed form, i.e. an equivalent expression that doesn t involve recursion (or summation or the like). Sometimes you can find the closed form by working out the first few values of the function and then guessing the pattern. More often, you need to use an organized technique. The simplest technique for finding closed forms is called unrolling. For example, suppose we have a function T : N Z defined by

CHPTER 12. RECURSIVE DEFINITION 139 T(1) = 1 T(n) = 2T(n 1)+3, n 2 The values of this function are T(1) = 1, T(2) = 5, T(3) = 13, T(4) = 29, T(5) = 61. It isn t so obvious what the pattern is. The idea behind unrolling is to substitute a recursive definition into itself, so as to re-express T(n) in terms of T(n 2) rather than T(n 1). We keep doing this, expressing T(n) in terms of the value of T for smaller and smaller inputs, until we can see the pattern required to express T(n) in terms of n and T(0). So, for our example function, we would compute: T(n) = 2T(n 1)+3 = 2(2T(n 2)+3)+3 = 2(2(2T(n 3)+3)+3)+3 = 2 3 T(n 3)+2 2 3+2 3+3 = 2 4 T(n 4)+2 3 3+2 2 3+2 3+3... = 2 k T(n k)+2 k 1 3+...+2 2 3+2 3+3 The first few lines of this are mechanical substitution. To get to the last line, you have to imagine what the pattern looks like after k substitutions. We can use summation notation to compactly represent the result of the kth unrolling step: T(n) = 2 k T(n k)+2 k 1 3+...+2 2 3+2 3+3 = 2 k T(n k)+3(2 k 1 +...+2 2 +2+1) k 1 = 2 k T(n k)+3 (2 i ) i=0

CHPTER 12. RECURSIVE DEFINITION 140 Now, we need to determine when the input to T will hit the base case. In our example, the input value is n k and the base case is for an input of 1. So we hit the base case when n k = 1. i.e. when k = n 1. Substituting this value for k back into our equation, and using the fact that T(1) = 1, we get k 1 T(n) = 2 k T(n k)+3 (2 i ) i=0 n 2 = 2 n 1 T(1)+3 (2 i ) i=0 n 2 = 2 n 1 +3 (2 k ) k=0 = 2 n 1 +3(2 n 1 1) = 4(2 n 1 ) 3 = 2 n+1 3 So the closed form for this function is T(n) = 2 n+1 3. The unrolling process isn t a formal proof that our closed form is correct. However, we ll see below how to write a formal proof using induction. 12.3 Divide and conquer Many important algorithms in computer science involve dividing a big problem of (integer) size n into a sub-problems, each of size n/b. This general method is called divide and conquer. nalyzing such algorithms involves recursive definitions that look like: S(1) = c S(n) = as( n/b )+f(n), n 2 The base case takes some constant amount of work c. The term f(n) is the work involved in dividing up the big problem and/or merging together

CHPTER 12. RECURSIVE DEFINITION 141 the solutions for the smaller problems. The call to the ceiling function is required to ensure that the input to S is always an integer. Handling such definitions in full generality is beyond the scope of this class. 1 So let s consider a particularly important special case: dividing our problem into two half-size problems, where the dividing/merging takes time proportional to the size of the problem. nd let s also restrict our input n to be a power of two, so that we don t need to use the ceiling function. We then get a recursive definition that looks like: S(1) = c S(n) = 2S(n/2)+n, n 2 (n a power of 2) Unrolling this, we get S(n) = 2S(n/2) + n = 2(2S(n/4)+n/2)+n = 4S(n/4)+n+n = 8S(n/8)+n+n+n... = 2 i S( n 2 i)+in We hit the base case when n 2 i = 1 i.e. when i = logn (i.e. log base 2, which is the normal convention for algorithms applications). Substituting in this value for i and the base case value S(1) = c, we get S(n) = 2 i S( n 2 i)+in = 2logn c+nlogn = cn+nlogn So the closed form for S(n) is cn+nlogn. In real applications, our input n might not be a power of 2, so our actual recurrence might look like: 1 See any algorithms text for more details.

CHPTER 12. RECURSIVE DEFINITION 142 S(1) = c S(n) = 2S( n/2 )+n, n 2 We could extend the details of our analysis to handle the input values that aren t powers of 2. In many practical contexts, however, we are only interested in the overall shape of the function, e.g. is it roughly linear? cubic? exponential? So it is often sufficient to note that S is increasing, so values of S for inputs that aren t powers of 2 will lie between the values of S at the adjacent powers of 2. 12.4 Hypercubes Non-numerical objects can also be defined recursively. For example, the hypercube Q n is the graph of the corners and edges of an n-dimensional cube. It is defined recursively as follows (for any n N): Q 0 is a single node with no edges Q n consists of two copies of Q n 1 with edges joining corresponding nodes, for any n 1. That is, each node v i in one copy of Q n 1 is joined by an edge to its clone copy v i in the second copy of Q n 1. Q 0, Q 1, Q 2, and Q 3 look as follows. The node labels distinguish the two copies of Qn 1 The hypercube defines a binary coordinate system. To build this coordinate system, we label nodes with binary numbers, where each binary digit

CHPTER 12. RECURSIVE DEFINITION 143 corresponds to the value of one coordinate. The edges connect nodes that differ in exactly one coordinate. 111 101 011 001 110 100 010 000 Q n has2 n nodes. Tocomputethenumber ofedges, wesetupthefollowing recursive definition for the number of edges E(n) in the Q n : E(0) = 0 E(n) = 2E(n 1)+2 n 1, for all n 1 The 2 n 1 term is the number of nodes in each copy of Q n 1, i.e. the number of edges required to join corresponding nodes. We ll leave it as an exercise to find a closed form for this recursive definition. 12.5 Proofs with recursive definitions Recursive definitions are ideally suited to inductive proofs. The main outline of the proof often mirrors the structure of the recursive definition. For example, let s prove the following claim about the Fibonacci numbers: Claim 44 For any n 0, F 3n is even. Let s check some concrete values: F 0 = 0, F 3 = 2, F 6 = 8, F 9 = 34. ll are even. Claim looks good. So, let s build an inductive proof: Proof: by induction on n. ase: F 0 = 0, which is even.

CHPTER 12. RECURSIVE DEFINITION 144 Induction: Suppose that F 3n is even for n = 0,1,...,k. We need to show that F 3k is even. We need to show that that F 3(k+1) is even. F 3(k+1) = F 3k+3 = F 3k+2 +F 3k+1 utf 3k+2 = F 3k+1 +F 3k. So,substitutingintotheaboveequation, we get: F 3(k+1) = (F 3k+1 +F 3k )+F 3k+1 = 2F 3k+1 +F 3k y the inductive hypothesis F 3k is even. 2F 3k+1 is even because it s 2 times an integer. So their sum must be even. So F 3(k+1) is even, which is what we needed to show. Some people feel a bit uncertain if the base case is a special case like zero. It s ok to also include a second base case. For this proof, you would check the case for n = 1 i.e. verify that F 3 is even. The extra base case isn t necessary for a complete proof, but it doesn t cause any harm and may help the reader. 12.6 Inductive definition and strong induction Claims involving recursive definitions often require proofs using a strong inductive hypothesis. For example, suppose that the function f : N Z is defined by f(0) = 2 f(1) = 3 n 1, f(n+1) = 3f(n) 2f(n 1) I claim that: Claim 45 n N,f(n) = 2 n +1 We can prove this claim as follows:

CHPTER 12. RECURSIVE DEFINITION 145 Proof: by induction on n. ase: f(0) is defined to be 2. 2 0 +1 = 1+1 = 2. So f(n) = 2 n +1 when n = 0. f(1) is defined to be 3. 2 1 + 1 = 2 + 1 = 3. So f(n) = 2 n + 1 when n = 1. Induction: Suppose that f(n) = 2 n +1 for n = 0,1,...,k. f(k +1) = 3f(k) 2f(k 1) ytheinductionhypothesis, f(k) = 2 k +1andf(k 1) = 2 k 1 +1. Substituting these formulas into the previous equation, we get: f(k+1) = 3(2 k +1) 2(2 k 1 +1) = 3 2 k +3 2 k 2 = 2 2 k +1 = 2 k+1 +1 So f(k +1) = 2 k+1 +1, which is what we needed to show. We need to use a strong induction hypothesis, as well as two base cases, because the inductive step uses the fact that the formula holds for two previous values of n (k and k 1). 12.7 Variation in notation Recursive definitions are sometimes called inductive definitions or (especially for numerical functions) recurrence relations. Folks who call them recurrence relations typically use the term initial condition to refer to the base case.