Recursion and Induction

Similar documents
Recursion and Induction

Recursion and Induction

Binomial Coefficient Identities/Complements

Recursion: Introduction and Correctness

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Advanced Counting Techniques. Chapter 8

1 Recursive Algorithms

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

1 Sequences and Summation

Practical Session #3 - Recursions

5. Sequences & Recursion

Linear Recurrence Relations

Lecture 12 : Recurrences DRAFT

Part III, Sequences and Series CS131 Mathematics for Computer Scientists II Note 16 RECURRENCES

Learning Objectives

Advanced Counting Techniques

CS 2110: INDUCTION DISCUSSION TOPICS

SOLVING LINEAR RECURRENCE RELATIONS

CS1800: Mathematical Induction. Professor Kevin Gold

CMSC250 Homework 9 Due: Wednesday, December 3, Question: Total Points: Score:

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

10.4 The Kruskal Katona theorem

Mathematical Induction. How does discrete math help us. How does discrete math help (CS160)? How does discrete math help (CS161)?

Math.3336: Discrete Mathematics. Advanced Counting Techniques

Divide and Conquer. Recurrence Relations

Fall 2017 Test II review problems

CDM. Recurrences and Fibonacci

Mathematical Fundamentals

Administrivia. COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion. Lecture 6 recap. Lecture 6 recap

CDM. Recurrences and Fibonacci. 20-fibonacci 2017/12/15 23:16. Terminology 4. Recurrence Equations 3. Solution and Asymptotics 6.

Alternative Combinatorial Gray Codes

CS Analysis of Recursive Algorithms and Brute Force

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Asymptotic Algorithm Analysis & Sorting

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

Chapter 2 - Basics Structures MATH 213. Chapter 2: Basic Structures. Dr. Eric Bancroft. Fall Dr. Eric Bancroft MATH 213 Fall / 60

Another Proof By Contradiction: 2 is Irrational

Chapter 2 - Basics Structures

Chapter 8: Recursion. March 10, 2008

1 Review of Vertex Cover

Models of Computation. by Costas Busch, LSU

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Induction and Recursion

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Intermediate Math Circles March 11, 2009 Sequences and Series

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

Shortest paths with negative lengths

1 Terminology and setup

Problem Solving in Math (Math 43900) Fall 2013

Topics in Algorithms. 1 Generation of Basic Combinatorial Objects. Exercises. 1.1 Generation of Subsets. 1. Consider the sum:

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

CSE 21 Practice Exam for Midterm 2 Fall 2017

Classical Complexity and Fixed-Parameter Tractability of Simultaneous Consecutive Ones Submatrix & Editing Problems

1 Examples of Weak Induction

Tight Bounds on the Diameter of Gaussian Cubes

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

MATH 324 Summer 2011 Elementary Number Theory. Notes on Mathematical Induction. Recall the following axiom for the set of integers.

What you learned in Math 28. Rosa C. Orellana

Why do we need math in a data structures course?

Constant Space and Non-Constant Time in Distributed Computing

PATH BUNDLES ON n-cubes

Algorithm efficiency analysis

CS483 Design and Analysis of Algorithms

Matrix Multiplication

Cpt S 223. School of EECS, WSU

Data Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University

Data Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University

CSC236H Lecture 2. Ilir Dema. September 19, 2018

CS Non-recursive and Recursive Algorithm Analysis

Edge-counting vectors, Fibonacci cubes, and Fibonacci triangle

CHAPTER 8 Advanced Counting Techniques

Introduction to Techniques for Counting

A New Variation of Hat Guessing Games

Analysis of Algorithms

The well-known Fibonacci sequence is defined by a recurrence. Specifically,letF i denote the ith number. Then, we have:

Recurrence Relations

Lecture 7: Dynamic Programming I: Optimal BSTs

Reductions, Recursion and Divide and Conquer

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

General Comments Proofs by Mathematical Induction

CSC236 Week 3. Larry Zhang

Every subset of {1, 2,...,n 1} can be extended to a subset of {1, 2, 3,...,n} by either adding or not adding the element n.

Electrical Engineering and Computer Science Department The University of Toledo Toledo, OH 43606

CS1802 Optional Recitation Week 11-12: Series, Induction, Recurrences

3.1 Induction: An informal introduction

7.11 A proof involving composition Variation in terminology... 88

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

Methods for solving recurrences

Induction and recursion. Chapter 5

MATH 243E Test #3 Solutions

CS1800 Discrete Structures Final Version A

EMBEDDED PATHS AND CYCLES IN FAULTY HYPERCUBES

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

What can you prove by induction?

6 CARDINALITY OF SETS

CS261: Problem Set #3

Introduction to Divide and Conquer

CS361 Homework #3 Solutions

Transcription:

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) move(from, to) hanoi(n-1, using, to, from)

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

Correctness Base case: 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

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. Guess the result and prove your guess correct using induction. n M(n) 1 1 2 3 3 7 4 15 5 31

Proof by Induction Base case: M(1) = 1 = 2 1-1 Inductive Hypothesis (IH) Assume M(n) = 2 n -1 Show IH M(n+1) = 2 n+1-1 M(n+1) = 2M(n) + 1 = 2(2 n -1) + 1 [by IH] = 2 2 n -1= 2 n+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 = 2 2 * M(n-2) + 1+2 = 2 2 * [2*M(n-3)+1] + 1 + 2 = 2 3 * M(n-3) + 1+2 + 2 2

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

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

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

Geometric Series Show 1 + 2 + + 2 n-1 = 2 i = 2 n 1 n 1 i=0 Base case: 0 i= 0 i 2 = 1 = 2 1 Inductive Hypothesis n 1 i= 0 i 2 = 2 n 1 Show n i= 0 n-1 2 i n i n n n = 2 + 2 = 2 + ( 2 1) = 2 2 1 = 2 i= 0 n+ 1 1 Generalize by replacing 2 by x. nn ii=0 xx ii = xxnn+1 1 xx 1

Worst Case Is it possible to solve Hanoi in fewer than 2 n -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) 2 n -1

Iterative Solution to Hanoi It is possible to derive an iterative solution to the tower of Hanoi problem, but it is much more complicated than the recursive solution The solution is related to the binary reflected Gray code An iterative method for computing the binary reflected Gray code will be derived and this will lead to an iterative solution of Tower of Hanoi

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) 010 001 011 000 100 111 101 110 Consecutive angles can have an abrupt change in the number of strips (bits) leading to potential detection errors.

Shaft Encoder (Gray Code) 011 001 010 000 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 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

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.

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) [000,001,011,010,110,111,101,100]

Tower of Hanoi (0,0,1) [000,001,011,010,110,111,101,100]

Tower of Hanoi (0,1,1) [000,001,011,010,110,111,101,100]

Tower of Hanoi (0,1,0) [000,001,011,010,110,111,101,100]

Tower of Hanoi (1,1,0) [000,001,011,010,110,111,101,100]

Tower of Hanoi (1,1,1) [000,001,011,010,110,111,101,100]

Tower of Hanoi (1,0,1) [000,001,011,010,110,111,101,100]

Tower of Hanoi (1,0,0) [000,001,011,010,110,111,101,100]

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 110 111 10 11 010 011 0 1 100 101 00 01 000 001

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

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

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

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

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

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

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

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

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