Recursion and Induction

Slides:



Advertisements
Similar presentations
Mathematical Preliminaries
Advertisements

Analysis of Algorithms II
Algorithm Design Techniques
Magnetic Data Storage A computer hard drive stores your data magnetically Disk NS direction of disk motion Write Head __ Bits of information.
Computational Complexity
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.
The Towers of Hanoi or Apocalypse When?.
Week 6 - Wednesday CS322.
22C:19 Discrete Structures Advanced Counting Spring 2014 Sukumar Ghosh.
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible, i.e. satisfying the.
Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Binomial Heaps. Heap Under most circumstances you would use a “normal” binary heap Except some algorithms that may use heaps might require a “Union” operation.
Finite-state Recognizers
Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.
Synthesis For Finite State Machines. FSM (Finite State Machine) Optimization State tables State minimization State assignment Combinational logic optimization.
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.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
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.
Recursion.
Costas Busch - RPI1 Mathematical Preliminaries. Costas Busch - RPI2 Mathematical Preliminaries Sets Functions Relations Graphs Proof Techniques.
Courtesy Costas Busch - RPI1 Mathematical Preliminaries.
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 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.
MATH 310, FALL 2003 (Combinatorial Problem Solving) Lecture 6, Friday, September 12.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recursive Algorithms: Selected Exercises
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
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
1 © 2010 Pearson Education, Inc. All rights reserved 10.1 DEFINITION OF A SEQUENCE An infinite sequence is a function whose domain is the set of positive.
Week 6 - Monday CS322.
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.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
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.,
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
Mathematical Preliminaries. Sets Functions Relations Graphs Proof Techniques.
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.
Prof. Busch - LSU1 Mathematical Preliminaries. Prof. Busch - LSU2 Mathematical Preliminaries Sets Functions Relations Graphs Proof Techniques.
1 Topics Recursion sections 8.1 – Recursion A recursively defined sequence –First, certain initial values are specified –Later terms of the sequence.
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.
1 Mathematical Preliminaries. 2 Sets Functions Relations Graphs Proof Techniques.
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.
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.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
(Proof By) Induction Recursion
MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi
Digital Logic and Design
Copyright © Cengage Learning. All rights reserved.
Notes 9.5 – Mathematical Induction
CSCI-2400 Models of Computation.
Copyright © Cengage Learning. All rights reserved.
The Hanoi Tower Problem
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 Relations Recursive Definitions 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 all of the disks from the first tower to the last tower Larger disks can not be placed on top of smaller disks The third tower can be used to temporarily hold disks

Tower of Hanoi The disks must be moved within one week. Assume one disk can be moved in 1 second. Is this possible? 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 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); Hanoi(n-1,c,b,a); }

Induction To prove a statement S(n) for positive integers n Prove S(1) Prove that if S(n) is true [inductive hypothesis] then S(n+1) is true. This implies that S(n) is true for n=1,2,3,…

Correctness and Cost Use induction to prove that the recursive algorithm solves the Tower of Hanoi problem. 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

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 2 3 7 4 15 5 31

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 M(n) = 2k * M(n-k) + 1+2 + 22 + … + 2n-k-1 Base case encountered when k = n-1 M(n) = 2n-1 * M(1) + 1+2 + 22 + … + 2n-2 = 1 + 2 + … + 2n-1 = Use induction to reprove result for M(n) using this sum. Generalize by replacing 2 by x.

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 path is a sequence of edges that visit each node exactly once. A Hamiltonian path on a hypercube provides a Gray code (why?) 000 001 101 100 010 011 110 111

Hypercube and Gray Code 000 001 101 100 010 011 110 111