Recursion and Induction

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursion and Induction
Week 6 - Wednesday CS322.
AE1APS Algorithmic Problem Solving John Drake
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.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
1 Recursion, Recurrences and Induction Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
The Tower of Hanoi
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
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.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
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.
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.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Chapter 7 Recursion 1CSCI 3333 Data Structures. 2 Recurrent Sequence A recursively defined sequence – First, certain initial values are specified  c.f.,
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
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.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
+ 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,
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.
Divide & Conquer Themes –Reasoning about code (correctness and cost) –iterative code, loop invariants, and sums –recursion, induction, and recurrence relations.
1 In this puzzle, the player begins with n disks of decreasing diameter placed one on top of the other on one of three pegs of the game board. The player.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
De Bruijn sequences 陳柏澍 Novembers Each of the segments is one of two types, denoted by 0 and 1. Any four consecutive segments uniquely determine.
(Proof By) Induction Recursion
Algorithm Analysis 1.
MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi
Recursion CENG 707.
Digital Logic and Design
Proofs, Recursion and Analysis of Algorithms
Random walks on undirected graphs and a little bit about Markov Chains
COMP108 Algorithmic Foundations Divide and Conquer
CS 3343: Analysis of Algorithms
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
SECTION 5.1 Sequences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Discrete Structures for Computer Science
Copyright © Cengage Learning. All rights reserved.
CS 3343: Analysis of Algorithms
Discrete Mathematics for Computer Science
Algorithms (2IL15) – Lecture 2
Lectures on Graph Algorithms: searching, testing and sorting
Copyright © Cengage Learning. All rights reserved.
CSE 421: Introduction to Algorithms
Copyright © Cengage Learning. All rights reserved.
Section 1.8 INVERSE FUNCTIONS.
Algorithms: the big picture
Recursion and Induction
Recursion as a Problem-Solving Technique
Solving recurrence equations
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright © Cengage Learning. All rights reserved.
Recursive Thinking.
Presentation transcript:

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

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

Recursive Solution

Recursive Solution

Recursive Solution

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Recursive Algorithm define hanoi (n, from, to, using) if n = 1 then move(from, to) else hanoi(n-1, from, using, to) hanoi(n-1, using, to, from) Note that the first argument to each recursive call is smaller. This is, essentially, a 3-line algorithm. Coming up w/an iterative sol’n would’ve taken a bit more effort That’s recursion. On to induction!

Correctness Use induction to prove that the recursive algorithm solves the Tower of Hanoi problem. Let H(n,a,b,c) = property that hanoi(n,a,b,c) moves n disks from tower a to b using tower c without placing larger disks on top of smaller disks We can get the number of moves several ways: We can set up a recurrence relation, and solve it. We can guess at the answer, and prove it. To set up the recurrence relation: To move a stack of n disks we have to move a stack of n-1 disks out of the way (whatever that costs, M(n-1)), move a single disk (1), and move the short stack back on top (M(n-1)).

Correctness Base case: Inductive Hypothesis (IH): H(1,a,b,c) works since hanoi(1, a, b, c) = move(a,b) Inductive Hypothesis (IH): Assume H(n,a,b,c) is correct for arbitrary a b c Show IH  H(n+1,a,b,c) The first recursive call correctly moves n disks from tower a to tower c [by IH] move moves the largest disk to the empty tower b (all other disks on tower c) The second recursive call correctly moves n disks from tower c to tower b on top of largest disk We can get the number of moves several ways: We can set up a recurrence relation, and solve it. We can guess at the answer, and prove it. To set up the recurrence relation: To move a stack of n disks we have to move a stack of n-1 disks out of the way (whatever that costs, M(n-1)), move a single disk (1), and move the short stack back on top (M(n-1)).

Cost 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

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. 1 2 3 7 4 15 5 31 Calculate M(n) for small n and look for a pattern. Guess the result and prove your guess correct using induction. Here we work out M(n) (by hand, or work our program to give us this information) for a couple small values of n. We then guess at a closed result for M(n), and prove it using induction.

Proof by Induction Base case: M(1) = 1 = 21-1 Inductive Hypothesis (IH) Assume M(n) = 2n-1 Show IH  M(n+1) = 2n+1-1 M(n+1) = 2M(n) + 1 = 2(2n-1) + 1 [by IH] = 2×2n -1= 2n+1-1

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 = 22 * M(n-2) + 1+2 = 22 * [2*M(n-3)+1] + 1 + 2 = 23 * M(n-3) + 1+2 + 22

Geometric Series After k steps: (prove by induction on k) M(n) = 2k * M(n-k) + 1+2 + 22 + … + 2k-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) = 2n-1 * M(1) + 1+2 + 22 + … + 2n-2 = 1 + 2 + … + 2n-1 = = 2n – 1 Use induction to reprove result for M(n) using this sum. Generalize by replacing 2 by x.

Induction Base case: Induction Hypothesis Show IH  After k steps: (prove by induction on k) M(n) = 2k * M(n-k) + 1+2 + 22 + … + 2k-1 (1 ≤ k < n) Base case: Using recurrence after 1 step M(n) = 2M(n-1) + 1 Induction Hypothesis M(n) = 2k * M(n-k) + 1+2 + 22 + … + 2k-1 Show IH  M(n) = 2k+1 * M(n-(k+1)) + 1+2 + 22 + … + 2k

Induction Show IH  M(n) = 2k+1 * M(n-(k+1)) + 1+2 + 22 + … + 2k M(n) = 2k * M(n-k) + 1+2 + 22 + … + 2k-1 [IH] = 2k * (2M(n-k-1)+1) + 1+2 + 22 + … + 2k-1 [recurrence] = 2k *2M(n-k-1)+ 2k + 1+2 + 22 + … + 2k-1 = 2k+1 *M(n-(k+1)) + 1+2 + 22 + … + 2k

Geometric Series Show 1 + 2 + … + 2n-1 = = 2n – 1 Base case: Inductive Hypothesis Show Generalize by replacing 2 by x. 𝑖=0 𝑛 𝑥 𝑖 = 𝑥 𝑛+1 −1 𝑥−1

Worst Case Is it possible to solve Hanoi in fewer than 2n-1 moves? Let M*(n) be the fewest moves required to solve H(n,a,b,c) Claim M*(n)  2M*(n-1) + 1 [why?] Use induction to show M*(n)  2n-1

Gray Code An n-bit Gray code is a 1-1 onto mapping from [0..2n-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 2n different angles, each with a different number of strips, corresponding to the n-bit binary numbers.

Shaft Encoder (Counting Order) 000 001 010 011 100 101 110 111 Consecutive angles can have an abrupt change in the number of strips (bits) leading to potential detection errors.

Shaft Encoder (Gray Code) 011 001 000 010 110 100 111 101 Since a Gray code is used, consecutive angles have only one change in the number of strips (bits).

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

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

Gray Code & Tower of Hanoi Introduce coordinates (d0,…,dn-1), where di {0,1} Associate di 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 2n nodes with each node connected to n vertices Binary labels of adjacent nodes differ in one bit 000 001 101 100 010 011 110 111 00 01 10 11 1

Hypercube, Gray Code and Tower of Hanoi A Hamiltonian cycle is a sequence of edges that visit each node exactly once and ends at a node with an edge to the starting node A Hamiltonian cycle on a hypercube provides a Gray code (why?) 110 111 010 011 100 101 000 001