Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.

Slides:



Advertisements
Similar presentations
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Advertisements

Induction and recursion
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
© Love Ekenberg The Algorithm Concept, Big O Notation, and Program Verification Love Ekenberg.
CSE115/ENGR160 Discrete Mathematics 03/22/12 Ming-Hsuan Yang UC Merced 1.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Chapter 10 Sequences, Induction, and Probability Copyright © 2014, 2010, 2007 Pearson Education, Inc Mathematical Induction.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
Solving ODEs UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
analysis, plug ‘n’ chug, & induction
Mathematical Induction
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
What it is? Why is it a legitimate proof method? How to use it?
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Solving Linear Equations UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
ICS 253 Presents Mathematical Induction Sultan Almuhammadi muhamadi
Mathematical Maxims and Minims, 1988
Chapter 6 Mathematical Induction
Discrete Mathematics, 1st Edition Kevin Ferland
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
1 Introduction to Abstract Mathematics Chapter 4: Sequences and Mathematical Induction Instructor: Hayk Melikya 4.1- Sequences. 4.2,
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
9.4 Mathematical Induction
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Module #13: Inductive Proofs Rosen 5 th ed., § inference of a generalized conclusion from particular instances 2. mathematical demonstration of the.
Sorting UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
CompSci 102 Discrete Math for Computer Science March 1, 2012 Prof. Rodger Slides modified from Rosen.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Math Review 1.
Inductive Proofs and Inductive Definitions Jim Skon.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Mathematical Induction
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Chapter 5. 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 we can reach.
Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
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.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
3.3 Mathematical Induction 1 Follow me for a walk through...
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
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.
Direct Proof and Counterexample IV: Division into Cases and the Quotient-Remainder Theorem For each of the following values of n and d, find integers q.
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Induction and recursion
Chapter 5 Induction and Recursion
Proofs, Recursion and Analysis of Algorithms
Induction and recursion
Lesson 11 – Proof by induction
Applied Discrete Mathematics Week 9: Integer Properties
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
This Lecture Substitution model
Copyright © Cengage Learning. All rights reserved.
Foundations of Discrete Mathematics
Mathematical Induction
Mathematical Induction
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Recursion.
Presentation transcript:

Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Intro to Recursion and Induction Consider problems/tasks that are parametrized by an integer –Computing the factorial of a number n –Computing the n th row of Pascal’s triangle –Computing the eigenvalues of an n-by-n matrix –Computing the determinant of an n-by-n matrix Assume that –For any k, the solution for the case n=k+1 can be written very clearly in terms of the solution for the n=k case. –Solution for n=0 case is “obvious” Then, a recursive strategy can be used to solve the problem for any value of n. A program is recursive if it calls itself. A program is recursive if it calls itself. Hmmm …

A recursive program for factorial function A = rfac(N) if N==0 % The N=0 case is “obvious” A = 1; else % The solution for N is easily % written in terms of the solution % for the N-1 case. A = N*rfac(N-1); end rfac calls itself

What happens when a function calls itself? First consider the simpler question: What happens when a function calls another function?” Consider two really simple functions, times2 and t2p5. >> >> D = 3; >> C = t2p5(D); function B = times2(A) B = 2*A; function B = t2p5(A) tmp = times2(A) B = tmp + 5;

Function calling function >> >> D=3; >> C=t2p5(D); >> D (value=3) C (value=11) function B=t2p5(A) tmp = times2(A); B = tmp + 5; A (value=3) tmp (value=6) B (value = 11) “function instance #1” workspace for t2p5 function B=times2(A) B = 2*A; A (value=3) B (value = 6) “function instance #1” workspace for times2

OK, what happens when a function calls itself? Essentially the same thing –A 2 nd function instance (with its own unique workspace) associated with the same code instructions is created –the code instructions associated with the function are executed in the new workspace. So, at certain times, there are –Two (or even more) function instances of the same function Different workspaces (separate, different sets of variables) Identical copies of code instructions Let’s see it work on >> C = 3; >> D = rfac(C);

Function Instances in Recursive Call func A=rfac(N) if N==0 A=1; else A=N*rfac(N-1); end N (value=2) A (value=2) func A=rfac(N) if N==0 A=1; else A=N*rfac(N-1); end N (value=1) A (value=1) func A=rfac(N) if N==0 A=1; else A=N*rfac(N-1); end N (value=0) A (value=1) >> >> C=2; >> D=rfac(C); >> C (value=2) D (value=2) Function instance #1 Function instance #2 Function instance #3

Let’s make the progression of variables more interesting function A = fac3(N) if N==0 A = 1; else A = N*fac3(N-3); End So fac3(12) = 12*9*6*3*1 = 1944

Function Instances in Recursive Call func A=fac3(N) if N==0 A=1; else A=N*fac3(N-3); end N (value=6) A (value=18) func A=fac3(N) if N==0 A=1; else A=N*fac3(N-3); end N (value=3) A (value=3) func A=fac3(N) if N==0 A=1; else A=N*fac3(N-3); end N (value=0) A (value=1) >> >> C=6; >> D=fac3(C); >> C (value=6) D (value=18) Function instance #1 Function instance #2 Function instance #3

A wrong recursive program for factorial function A = rfac(N) % The solution for N is easily % written in terms of the solution % for the N-1 case. A = N*rfac(N-1); What’s wrong?

Recursion A problem that can be solved recursively can also be solved without recursion. Often, the code will “look” more complicated. Recursion can be an elegant way to solve a problem (we’ll see more interesting examples soon) Recursive solutions generally use more memory, because at the deepest level, several function workspaces are in existence at the same time. (Nearly) all programming languages allow recursion. Matlab has a built-in recursion limit. This limits the number of simultaneous function instances of a given function. Use >> get(0,’RecursionLimit’)

Mathematical Induction Suppose that P is a function that maps an integer k into a statement P(k). If the following assumptions hold: A1: P(0) is true, and A2: for any k, P(k) true implies that P(k+1) is true Then P(k) is true for all integers k≥0. Proof: Let К be the set of integers for which P is false. All of the integers in К are ≥0, so if К is nonempty, let k min be the smallest element of К. So, P(k min ) is false, but P(k min -1) is true. But this violates the assumption A2. Therefore, it must be that К is empty, which means P(k) is true for all integers k≥0. #

Induction and Recursion are closely related Induction (establishing the truth of a sequence of statements) If the following assumptions hold: A1: P(0) is true, and A2: for any k, P(k) true implies that P(k+1) is true Then P(k) is true for all integers k≥0. Recursion (solving all instances of a sequence of problems) Assume that For any k, the solution for n=k+1 can be written very clearly in terms of the solution for the n=k. Solution for n=0 case is “obvious” Then, a recursive program can be used to solve the problem for any value of n.

Example of Induction Here is an example of a statement P(k) Is P(0) true? In other words, is it true that Can we derive Statement P(k) is that these two expressions are indeed equal. Yes. from P(k)?

See if P(k+1) is true, exploiting that P(k) is true. This follows from P(k) is true.

Using Induction So, using a “proof by induction”, we have no established that the identity holds for all integers k≥0.