Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Recursion and Induction
Week 6 - Wednesday CS322.
Lecture 2 Based on Chapter 1, Weiss. Mathematical Foundation Series and summation: ……. N = N(N+1)/2 (arithmetic series) 1 + r+ r 2 + r 3 +………r.
MAT 4 – Kompleks Funktionsteori MATEMATIK 4 INDUKTION OG REKURSION MM 1.4 MM 1.4: Induktion og Rekursion Topics: Mathematical induction Example of Towers.
Chapter 6 Advanced Counting 6.1 Recurrence Relations.
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Recursive Definitions and Structural Induction
Recursion Lecture 18: Nov 18.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
Recursion.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Lecture 17: Nov 11. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do.
Induction and recursion
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recursive Algorithms: Selected Exercises
Analysis of Recursive Algorithms October 29, 2014
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Advanced Counting Techniques
Week 6 - Monday CS322.
Induction and recursion
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Analysis of Algorithms
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Chapter 4: Induction and Recursion
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Chapter 7 Recursion 1CSCI 3333 Data Structures. 2 Recurrent Sequence A recursively defined sequence – First, certain initial values are specified  c.f.,
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
Section 2.6 Representation of numbers. Decimal representation (base 10) Given a positive integer X, the decimal representation of X is a string of digits.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Mathematical Induction I Lecture 4: Sep 16. This Lecture Last time we have discussed different proof techniques. This time we will focus on probably the.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
1 Topics Recursion sections 8.1 – Recursion A recursively defined sequence –First, certain initial values are specified –Later terms of the sequence.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Examples of Recursion Instructor: Mainak Chaudhuri
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
RECURRENCE Sequence Recursively defined sequence
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
(Proof By) Induction Recursion
Chapter 4: Induction and Recursion
MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Copyright © Cengage Learning. All rights reserved.
Algorithm design and Analysis
Recursion and Induction
Recursion and Induction
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
Presentation transcript:

Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined recursively) Examples –Tower of Hanoi –Gray Codes –Hypercube

Recursion & Recurrence Relations Very handy for defining functions and data types simply: –Consider the nth Fibonacci number, F n : = 1, if n = 1 or n=2 = F n-1 + F n-2, for all n>2 Very handy when a large problem can be broken in similar (but smaller) problems –We’ll look at the Towers of Hanoi in a moment

Who needs Induction? This chapter will provide us with some tools, to reason precisely about algorithms, and programs –Check for correctness Does the program end? Does it do its job? –Check performance How does the runtime of a particular algorithm grow vs. the inputs (number and/or size)?

Induction & Recursion Very similar notions. They have exactly the same roots Inductive proofs apply in a very natural way to recursive algorithms, and recurrence relations This chapter will present tools we’ll use for the rest of the course Also gives us the flavor of how we’ll approach the rest of the material

Tower of Hanoi There are three towers 64 gold disks, with decreasing sizes, placed on the first tower You need to move the stack of disks from one tower to another, one disk at a time Larger disks can not be placed on top of smaller disks The third tower can be used to temporarily hold disks

Tower of Hanoi Assume one disk can be moved in 1 second How long would it take to move 64 disks? N disks? To create an algorithm to solve this problem, it is convenient to generalize the problem to the “N-disk” problem, where in our case N = 64.

Recursive Solution

Tower of Hanoi

Recursive Algorithm (see void Hanoi( int n, string a, string b, string c) { if (n == 1) /* base case */ Move( a, b ); else { /* recursion */ Hanoi( n-1, a, c, b ); Move( a, b ); Hanoi( n-1, c, b, a ); }

Induction To prove a statement S(n) for positive integers n –Need a base case (typically n=0 or n=1). Prove that S(0) or S(1) is true –Assume S(n) is true [inductive hypothesis] –Prove that S(n+1) is true. You’ll need to use the hypothesis in this step, or you did something wrong See for an example

Induction Examples Prove: … + (2n-1) = n 2 4n < 2 n,  n ≥ 5 7 | 8 n -1 2 n < n!,  n ≥ 4

Correctness Use induction to prove that the recursive algorithm solves the Tower of Hanoi problem. (see ctures/1/hanoiCorrect.swf )

Cost Show that the number of moves M(n) required by the algorithm to solve the n-disk problem satisfies the recurrence relation –M(n) = 2M(n-1) + 1 –M(1) = 1 This can be done inductively, and it would be very similar to the last proof.

Cost We’d like to find a closed form, a formula that is not a recurrence relation We can do this a couple ways: –We can guess at the answer (and then prove it, of course) –We can unwind the recurrence (still need to prove it)

Guess and Prove Calculate M(n) for small n and look for a pattern. Guess the result and prove your guess correct using induction. nM(n) See

Substitution Method Unwind recurrence, by repeatedly replacing M(n) by the r.h.s. of the recurrence until the base case is encountered. M(n) = 2M(n-1) + 1 = 2*[2*M(n-2)+1] + 1 = 2 2 * M(n-2) = 2 2 * [2*M(n-3)+1] = 2 3 * M(n-3) For other examples of unwinding recurrences, see or

Geometric Series After k steps: M(n) = 2 k * M(n-k) … + 2 k-1 Base case, M(1), encountered when n-k=1 => k = n-1 Substituting in, to get rid of all of the k’s: M(n) = 2 n-1 * M(1) … + 2 n-2 = … + 2 n-1 = = 2 n - 1 Use induction to reprove result for M(n) using this sum. Generalize by replacing 2 by x. To see this done, see

Gray Code An n-bit Gray code is a 1-1 onto mapping from [0..2 n -1] such that the binary representation of consecutive numbers differ by exactly one bit. Invented by Frank Gray for a shaft encoder - a wheel with concentric strips and a conducting brush which can read the number of strips at a given angle. The idea is to encode 2 n different angles, each with a different number of strips, corresponding to the n-bit binary numbers.

Shaft Encoder (Counting Order) Consecutive angles can have an abrupt change in the number of strips (bits) leading to potential detection errors.

Shaft Encoder (Gray Code) Since a Gray code is used, consecutive angles have only one change in the number of strips (bits).

Binary-Reflected Gray Code G 1 = [0,1] G n = [0G n-1,1G n-1 ], G  reverse order  complement leading bit G 2 = [0G 1,1G 1 ] = [00,01,11,10] G 3 = [0G 2,1G 2 ] = [000,001,011,010,110,111,101,100] Use induction to prove that this is a Gray code (See )

Iterative Formula Let G n (i) be a function from [0,…,2 n -1] G n (i) = i ^ (i >> 1) [exclusive or of i and i/2] –G 2 (0) = 0, G 2 (1) = 1, G 2 (2) = 3, G 2 (3) = 2 Use induction to prove that the sequence G n (i), i=0,…,2 n -1 is a binary-reflected Gray code. (See )

Gray Code & Tower of Hanoi Introduce coordinates (d 0,…,d n-1 ), where d i  {0,1} Associate d i with the ith disk Initialize to (0,…,0) and flip the ith coordinate when the i-th disk is moved The sequence of coordinate vectors obtained from the Tower of Hanoi solution is a Gray code (why?)

Tower of Hanoi (0,0,0)

Tower of Hanoi (0,0,1)

Tower of Hanoi (0,1,1)

Tower of Hanoi (0,1,0)

Tower of Hanoi (1,1,0)

Tower of Hanoi (1,1,1)

Tower of Hanoi (1,0,1)

Tower of Hanoi (1,0,0)

Hypercube Graph (recursively defined) n-dimensional cube has 2 n nodes with each node connected to n vertices Binary labels of adjacent nodes differ in one bit

Hypercube, Gray Code and Tower of Hanoi A Hamiltonian path is a sequence of edges that visit each node exactly once. A Hamiltonian path on a hypercube provides a Gray code (why?)

Hypercube and Gray Code