Recursive Definitions. Recursive Definition A definition is called recursive if the object is defined in terms of itself.

Similar documents
Recursive Definitions. Recursive Definition A definition is called recursive if the object is defined in terms of itself.

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Turtle Programming. Ron Goldman Department of Computer Science Rice University

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

Divisibility in the Fibonacci Numbers. Stefan Erickson Colorado College January 27, 2006

Mathematical Fundamentals

Fall 2017 Test II review problems

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

2. THE EUCLIDEAN ALGORITHM More ring essentials

1 Examples of Weak Induction

Algebra for error control codes

Finite Fields. Mike Reiter

PUTNAM TRAINING NUMBER THEORY. Exercises 1. Show that the sum of two consecutive primes is never twice a prime.

Logic and Discrete Mathematics. Section 6.7 Recurrence Relations and Their Solution

Lecture 12 : Recurrences DRAFT

Mathematical Olympiad Training Polynomials

Outline. Some Review: Divisors. Common Divisors. Primes and Factors. b divides a (or b is a divisor of a) if a = mb for some m

Outline. AIT 682: Network and Systems Security. GCD and Euclid s Algorithm Modulo Arithmetic Modular Exponentiation Discrete Logarithms

Applications. More Counting Problems. Complexity of Algorithms

Algebra II Learning Targets

Notes 6: Polynomials in One Variable

CSC 474 Network Security. Outline. GCD and Euclid s Algorithm. GCD and Euclid s Algorithm Modulo Arithmetic Modular Exponentiation Discrete Logarithms

CPSC 467: Cryptography and Computer Security

5. Sequences & Recursion

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

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

The Fundamental Theorem of Arithmetic

CS 173: Discrete Structures, Fall 2009 Homework 4 Solutions

Algorithms CMSC Basic algorithms in Number Theory: Euclid s algorithm and multiplicative inverse

Chapter 5.1: Induction

Polynomials: Add and Subtract

Fibonacci Numbers. Justin Stevens. Lecture 5. Justin Stevens Fibonacci Numbers (Lecture 5) 1 / 10

ICS141: Discrete Mathematics for Computer Science I

Subsets of Euclidean domains possessing a unique division algorithm

Recursion: Introduction and Correctness

4 Powers of an Element; Cyclic Groups

Topic Contents. Factoring Methods. Unit 3: Factoring Methods. Finding the square root of a number

Induction and Recursion

NOTES ON SIMPLE NUMBER THEORY

M381 Number Theory 2004 Page 1

MAT 243 Test 2 SOLUTIONS, FORM A

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

3 Finite continued fractions

Intermediate Math Circles February 26, 2014 Diophantine Equations I

Mathematical Induction Assignments

STUDY GUIDE FOR THE WRECKONING. 1. Combinatorics. (1) How many (positive integer) divisors does 2940 have? What about 3150?

Basic Algorithms in Number Theory

Polynomials. In many problems, it is useful to write polynomials as products. For example, when solving equations: Example:

CSC2100B Data Structures Analysis

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

Intermediate Math Circles February 29, 2012 Linear Diophantine Equations I

Discrete Mathematics and Probability Theory Summer 2014 James Cook Note 5

Mat Week 8. Week 8. gcd() Mat Bases. Integers & Computers. Linear Combos. Week 8. Induction Proofs. Fall 2013

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 3

Student Responsibilities Week 8. Mat Section 3.6 Integers and Algorithms. Algorithm to Find gcd()

MA 323 Geometric Modelling Course Notes: Day 12 de Casteljau s Algorithm and Subdivision

Recurrences COMP 215

CS1800: Strong Induction. Professor Kevin Gold

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

586 Index. vertex, 369 disjoint, 236 pairwise, 272, 395 disjoint sets, 236 disjunction, 33, 36 distributive laws

Why do we need math in a data structures course?

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

Algorithmic number theory. Questions/Complaints About Homework? The division algorithm. Division

NUMBER THEORY AND CODES. Álvaro Pelayo WUSTL

Chinese Remainder Theorem

CS 4424 GCD, XGCD

1 Recursive Algorithms

Inverses. Today: finding inverses quickly. Euclid s Algorithm. Runtime. Euclid s Extended Algorithm.

POLYNOMIALS. Maths 4 th ESO José Jaime Noguera

Chapter 9 Basic Number Theory for Public Key Cryptography. WANG YANG

Binomial coefficients and k-regular sequences

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing

Introduction to Abstract Mathematics

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

Questionnaire for CSET Mathematics subset 1

Fractals. Justin Stevens. Lecture 12. Justin Stevens Fractals (Lecture 12) 1 / 14

Introduction to Number Theory

1) Synthetic Division: The Process. (Ruffini's rule) 2) Remainder Theorem 3) Factor Theorem

Number Theory Basics Z = {..., 2, 1, 0, 1, 2,...} For, b Z, we say that divides b if z = b for some. Notation: b Fact: for all, b, c Z:

+ 1 3 x2 2x x3 + 3x 2 + 0x x x2 2x + 3 4

CSE 1400 Applied Discrete Mathematics Proofs

Computing the rank of configurations on Complete Graphs

CPSC 467b: Cryptography and Computer Security

EDULABZ INTERNATIONAL NUMBER SYSTEM

1. Algebra 1.5. Polynomial Rings

Euclidean Domains. Kevin James

Chapter 5: Exponents and Polynomials

(Inv) Computing Invariant Factors Math 683L (Summer 2003)

Binomial Coefficient Identities/Complements

2301 Assignment 1 Due Friday 19th March, 2 pm

Induction and recursion. Chapter 5

MAT 243 Test 2 SOLUTIONS, FORM A

1. Given the public RSA encryption key (e, n) = (5, 35), find the corresponding decryption key (d, n).

Topics Covered in Math 115

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Department of Computer Sciences Graphics Fall 2005 (Lecture 8) Fractals

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

Discrete Mathematics and Probability Theory Fall 2018 Alistair Sinclair and Yun Song Note 6

Proofs. Methods of Proof Divisibility Floor and Ceiling Contradiction & Contrapositive Euclidean Algorithm. Reading (Epp s textbook)

Remainders. We learned how to multiply and divide in elementary

Transcription:

Recursion

Recursive Definitions Recursive Definition A definition is called recursive if the object is defined in terms of itself. Base Case Recursive definitions require a base case at which to either initiate or terminate the definition. Otherwise recursive definitions would be circular.

Recursive Programs Recursive Programs A program is called recursive if the program calls itself. Base Case Recursive programs require a base case at which to either initiate or terminate the program. Otherwise recursive programs would never terminate.

Fractals

Fractals Recursion Made Visible Recursive Turtle Programs Sierpinski Triangle Koch Snowflake Fractal Trees

Turtles What the Turtle Knows CURRENT_POSITION = (x, y) location -- where she is CURRENT_HEADING = (u,v) vector -- where she is going (Slightly more than the average professor.) Turtle Commands (Rice LOGO) FORWARD(D) -- change position, and draw a straight line of length D MOVE(D) -- same as FORWARD without drawing a line TURN(A) -- change heading by A, but do not change position RESIZE(S) -- change step size by a factor of S, but do not change position or direction Usual collection of control commands -- loops, conditionals, Turtle Programs Finite sequence of FORWARD, TURN, AND RESIZE commands Draws a piecewise linear curve

More Examples of Recursion 1. Sums and Differences 2. IQ-Tests 3. Binomial Coefficients -- Pascal s Triangle 3. Fibonacci Numbers -- Cat Problem 4. Tower of Hanoi -- Animation 5. Natural Numbers 6. Polynomials 7. Rooted Binary Trees -- Single Vertex -- Root connected to two roots

IQ Tests 6, 6, 6, 6, 6, 6,? 4, 7, 10, 13, 16, 19,? 3, 13, 29, 51, 79, 113,? 2, 3, 12, 35, 78, 147,?

IQ Test -- Solution Methods Neville s Algorithm -- Slow Forward Differencing -- Fast

IQ Tests -- Revisited F 3, 13, 29, 51, 79, 113,? ΔF 10 16 22 28 34? Δ 2 F 6 6 6 6? F 2, 3, 12, 35, 78, 147,? ΔF 1 9 23 43 69? Δ 2 F 8 14 20 26? Δ 3 F 6 6 6?

IQ Tests -- Revisited F 3, 13, 29, 51, 79, 113, 153? ΔF 10 16 22 28 34 40? Δ 2 F 6 6 6 6 6? F 2, 3, 12, 35, 78, 147, 248? ΔF 1 9 23 43 69 101? Δ 2 F 8 14 20 26 32? Δ 3 F 6 6 6 6?

Forward Differencing Forward Differencing (ΔF)(k) = F(k +1) F(k) (Δ n+1 F)(k) = Δ ( Δ n F )(k) Properties of First Difference ( Δ(F + G) )(k) = ΔF(k) + ΔG(k) Δ(c F)(k) = c ( ΔF(k) )

Forward Differencing Monomials Theorem 1 (Δx p )(k) = (k +1) p k p = pk p 1 + lower order terms Proof: Binomial Theorem. Theorem 2 (Δ n x p )(k) = p ( p n +1)k p n + lower order terms n p Proof: Induction on n. (Δ n+1 x p )(k) = Δ ( Δ n x p )(k) ( ) = Δ p ( p n +1)k p n + lower order terms

Forward Differencing Polynomials Corollaries (Δ p x p )(k) = p! (constant) (Δ n x p )(k) = 0 n > p Δ p (a p x p + + a 1 x + a 0 )(k) = p! a p

IQ Tests -- Revisited F 3, 13, 29, 51, 79, 113,? ΔF 10 16 22 28 34? Δ 2 F 6 6 6 6? F 2, 3, 12, 35, 78, 147,? ΔF 1 9 23 43 69? Δ 2 F 8 14 20 26? Δ 3 F 6 6 6?

IQ Tests -- Revisited F 3, 13, 29, 51, 79, 113, 153? ΔF 10 16 22 28 34 40? Δ 2 F 6 6 6 6 6? F 2, 3, 12, 35, 78, 147, 248? ΔF 1 9 23 43 69 101? Δ 2 F 8 14 20 26 32? Δ 3 F 6 6 6 6?

Exponential Sequences F 1, 2, 4, 8, 16, 32,? ΔF 1 2 4 8 16?

Exponential Sequences -- Revisited F 1, 2, 4, 8, 16, 32,? ΔF 1 2 4 8 16? Δ 2 F 1 2 4 8? Δ 3 F 1 2 4? Δ 4 F 1 2? Δ 5 F 1?

Exponential Sequences -- Revisited F 1, 2, 4, 8, 16, 32, 63 ΔF 1 2 4 8 16 31 Δ 2 F 1 2 4 8 15 Δ 3 F 1 2 4 7 Δ 4 F 1 2 3 Δ 5 F 1 1

Exponential Sequences F 1, 2, 4, 8, 16, 32, 64 ΔF 1 2 3 8 16 32 ΔF = F n+1 F n = 2 n+1 2 n = 2 n (2 1) = 2 n Theorem There is no polynomial p(x) for which 2 n = p(n) for all n.

Differences and Derivatives Difference Derivative (Δx p )(k) = p k p 1 + lower order terms d dx x p = px p 1 (Δ p x p )(k) = p! d p d p x x p = p! (Δ2 p )(k) = 2 k d dx ex = e x

Pounce the Cat My cat, Pounce, can walk up steps either one or two at a time. There are 21 steps from the first to the second floor of Duncan Hall. In how many different ways can Pounce chase a mouse up the steps from the first to the second floor of Duncan Hall?

Fibonacci Sequences Fibonacci Recurrence f 1 = f 2 = 1 (Base Cases) f n+1 = f n + f n 1 (Recursion) Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Fibonacci Sequences Fibonacci Sequence f 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, Δf 0 1 1 2 3 5 8 13 21 34 Δf = f n+1 f n = ( f n + f n 1 ) f n = f n 1 Theorem There is no polynomial p(x) for which f n = p(n) for all n.

Recursive Computation of Fibonacci Numbers f n f n 1 f n 2 f 3 f 3 f 1 f 2 f 1 f 2 Number of Additions = Exponential

Recursive Computation of Fibonacci Numbers f 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, # adds 0, 0, 1, 2, 4, 7, 12, 20, 33, 54, 88, Fibonacci Recurrence Adds Recurrence f 1 = f 2 = 1 a 1 = a 2 = 0 a 2 = 1 f n+1 = f n + f n 1 a n+1 = a n + a n 1 +1 Theorem Proof a n = f n 1 Induction

Iterative Computation of Fibonacci Numbers 1 2 5 f 2n 3 f 2n 1 1 3 8 f 2n 2 f 2n Number of Additions = 2(n 2)

Iterative Computation of Binomial Coefficients 1 1 1 ( n 1 ) 0 1 2 1 ( n 1 ) ( k 1 ) k ( n 1 ) n 1 ( n ) ( 0 1 n ) ( k n ) ( ( n n ) n 1 n ) Number of Additions n +1 k = k =1 (n +1)(n + 2) 2

Neville s Algorithm Linear Interpolation (Base Case) I D 2 (x) = x 2 x x 2 x 1 y 1 + x x 1 x 2 x 1 y 2 Recursion I D n (x) = x n x x n x 1 I D n (x) + x x 1 x n x 1 I D n + (x) D n = (x 1, y 1 ),,(x n, y n ) D n = (x 1, y 1 ),,(x n 1, y n 1 ) D n + = (x 2, y 2 ),,(x n, y n )

Polynomial Interpolation and Neville s Algorithm Questions i. How Fast is Neville s Algorithm? ii. What is the Best Way to Program Neville s Algorithm?

Neville s Algorithm -- Recursive Implementation I 1,,n (x) I 1,,n 1 (x) I 2,,n (x) I 1,2 (x) I n 1, n (x) y 1 y 2 y n 1 y n n 1 Levels 2 n 2 multiplications

Neville s Algorithm -- Iterative Implementation I 1, 2, 3, 4 I 1, 2, 3 I 2, 3, 4 I 1,2 I 2, 3 I 3, 4 y 1 y 2 y 3 y 4 n 1 n 1 Levels 2 k = (n 1)n multiplications k =1

Tower of Hanoi http://www.cut-the-knot.org/recurrence/hanoi.shtml http://www.dynamicdrive.com/dynamicindex12/towerhanoi.htm http://www.mathsisfun.com/games/towerofhanoi.html

Tower of Hanoi Recursive Solution 1 Ring -- Trivial N +1 Rings -- Solve N Ring Problem Twice Number of Moves M (1) = 1 M (N +1) = 2M (N) +1 -- M (N) = 2 N 1

Program Correctness Loop Invariant A property or number that is the same before and after each iteration of a loop. Used to prove program correctness. Proofs proceed by induction on the number of iterations.

Integer Division Input: m, n = integers Example Output: q, r = integers (quotient and remainder) -- n = mq + r with 0 r < m Algorithm q = 0 r = n While r m q = q +1 Loop Invariant n = mq + r r = r m

Proofs Proof of Loop Invariance By induction on the number of iteration of the loop. Base Case: q = 0 and r = n n = mq + r. Induction: Suppose that n = mq + r after k iterations of the loop. Proof of Program Correctness Must show that n = mq + r after k +1 iterations of the loop. But after k +1 iterations of the loop: (mq + r) k +1 = mq k +1 + r k+1 = m (q k +1) + r k m = m q k + r k (inductive hypothesis) = n. 1. The loop will terminate with r < m. 2. When the loop terminates n = m q + r. (Loop Invariance)

Example GCD -- Greatest Common Divisor Input: m, n = integers Output: GCD(m,n) Euclidean Algorithm x = m y = n While y 0 Loop Invariant r = remainder of x divided by y GCD(x, y) = GCD(m, n) (use division algorithm) x = y y = r Output: x is GCD(m, n)

Proof of Loop Invariance By induction on the number of iteration of the loop. Base Case: x = m and y = n GCD(x, y) = GCD(m, n). Induction: Suppose that GCD(x, y) = GCD(m, n) after k iterations of the loop. Must show that GCD(x, y) = GCD(m, n) after k +1 iterations. After the first line of the (k +1) st iteration of the loop: x = yq + r r = x yq GCD(y, r) = GCD(x, y) and by the inductive hypothesis GCD(x, y) = GCD(m, n) so GCD(y, r) = GCD(m, n). But after the (k +1) st iteration of the loop: x = y and y = r so GCD(x, y) = GCD(y, r) = GCD(m, n).

Loop While y 0 Loop Invariant r = remainder of x divided by y GCD(x, y) = GCD(m, n) (use division algorithm) x = y y = r Proof of Program Correctness 1. The loop will terminate when y = r = 0. 2. Therefore before the last iteration of the Loop, y divides x. Hence just before the start of the final iteration a. y = GCD(x, y) b. y = GCD(m, n) (Loop Invariance) 3. But after the final iteration: x = y so after the final iteration: x = GCD(m, n).