SlideShare a Scribd company logo
Recursive Definition & Algorithms,
and Program Correctness
Lecture 12, CMSC 56
Allyn Joy D. Calcaben
Recursive Definition
Recursion
A method where the solution to a problem depends on solutions
to smaller instances of the same problem.
We use 2 steps to define a function with the set of nonnegative
integers as its domain:
BASIS STEP: Specify the value of the function at zero
We use 2 steps to define a function with the set of nonnegative
integers as its domain:
BASIS STEP: Specify the value of the function at zero
RECURSIVE STEP: Give a rule for finding its value at an integer
from its values at smaller integers.
Example
Give the first four terms of the following sequence:
f(1) = 5
f(n + 1) = f(n) + 3
That is, we find f(1), f(2), f(3), and f(4).
Example
Give the first four terms of the following sequence:
BASIS STEP: f(1) = 5
f(n + 1) = f(n) + 3
That is, we find f(1), f(2), f(3), and f(4).
Example
Give the first four terms of the following sequence:
BASIS STEP: f(1) = 5
RECURSIVE STEP: f(n + 1) = f(n) + 3
That is, we find f(1), f(2), f(3), and f(4).
Solution
BASIS STEP: f(1) = 5
RECURSIVE STEP: f(n + 1) = f(n) + 3
f(1) = 5
Solution
BASIS STEP: f(1) = 5
RECURSIVE STEP: f(n + 1) = f(n) + 3
f(1) = 5
f(2) = f(1) + 3 = 5 + 3 = 8
Solution
BASIS STEP: f(1) = 5
RECURSIVE STEP: f(n + 1) = f(n) + 3
f(1) = 5
f(2) = f(1) + 3 = 5 + 3 = 8
f(3) = f(2) + 3 = 8 + 3 = 11
Solution
BASIS STEP: f(1) = 5
RECURSIVE STEP: f(n + 1) = f(n) + 3
f(1) = 5
f(2) = f(1) + 3 = 5 + 3 = 8
f(3) = f(2) + 3 = 8 + 3 = 11
f(4) = f(3) + 3 = 11 + 3 = 14
Example
Suppose that f is defined recursively by:
f(1) = 12
f(2) = 4
f(n + 1) = f(n) + 2 ⋅ f(n - 1)
Find f(1), f(2), f(3), and f(4).
Example
Suppose that f is defined recursively by:
BASIS STEP: f(1) = 12
f(2) = 4
f(n + 1) = f(n) + 2 ⋅ f(n - 1)
Find f(1), f(2), f(3), and f(4).
Example
Suppose that f is defined recursively by:
BASIS STEP: f(1) = 12
f(2) = 4
RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1)
Find f(1), f(2), f(3), and f(4).
Solution
BASIS STEP: f(1) = 12
f(2) = 4
RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1)
f(1) = 12
Solution
BASIS STEP: f(1) = 12
f(2) = 4
RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1)
f(1) = 12
f(2) = 4
Solution
BASIS STEP: f(1) = 12
f(2) = 4
RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1)
f(1) = 12
f(2) = 4
f(3) = f(2) + 2 ⋅ f(1) = 4 + 2 ⋅ 12 = 28
Solution
BASIS STEP: f(1) = 12
f(2) = 4
RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1)
f(1) = 12
f(2) = 4
f(3) = f(2) + 2 ⋅ f(1) = 4 + 2 ⋅ 12 = 28
f(4) = f(3) + 2 ⋅ f(2) = 28 + 2 ⋅ 4 = 36
Example
Suppose that f is defined recursively by:
f(0) = 3
f(n + 1) = 2 ⋅ f(n) + 3
Find f(1), f(2), f(3), and f(4).
Example
Suppose that f is defined recursively by:
BASIS STEP: f(0) = 3
f(n + 1) = 2 ⋅ f(n) + 3
Find f(1), f(2), f(3), and f(4).
Example
Suppose that f is defined recursively by:
BASIS STEP: f(0) = 3
RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3
Find f(1), f(2), f(3), and f(4).
Solution
BASIS STEP: f(0) = 3
RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3
f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9
Solution
BASIS STEP: f(0) = 3
RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3
f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9
f(2) = 2 ⋅ f(1) + 3 = 2 ⋅ 9 + 3 = 21
Solution
BASIS STEP: f(0) = 3
RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3
f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9
f(2) = 2 ⋅ f(1) + 3 = 2 ⋅ 9 + 3 = 21
f(3) = 2 ⋅ f(2) + 3 = 2 ⋅ 21 + 3 = 45
Solution
BASIS STEP: f(0) = 3
RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3
f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9
f(2) = 2 ⋅ f(1) + 3 = 2 ⋅ 9 + 3 = 21
f(3) = 2 ⋅ f(2) + 3 = 2 ⋅ 21 + 3 = 45
f(4) = 2 ⋅ f(3) + 3 = 2 ⋅ 45 + 3 = 93
Example
Find a recursive definition for the following sequence with n as a
positive integer:
20, 17, 14, 11, 8, . . . .
Solution
Find a recursive definition for the following sequence with n as a
positive integer:
20, 17, 14, 11, 8, . . . .
BASIS STEP: f(1) = 20
Solution
Find a recursive definition for the following sequence with n as a
positive integer:
20, 17, 14, 11, 8, . . . .
BASIS STEP: f(1) = 20
RECURSIVE STEP: f(n + 1) = f(n) - 3
Example
Give a recursive definition of an, where a is a nonzero real number
and n is a nonnegative integer.
Solution
Give a recursive definition of an, where a is a nonzero real number
and n is a nonnegative integer.
BASIS STEP: f(0) = 1
Solution
Give a recursive definition of an, where a is a nonzero real number
and n is a nonnegative integer.
BASIS STEP: f(0) = 1
RECURSIVE STEP: f(n) = a ⋅ f(n - 1)
Recursively Defined Sets
Just as in the recursive definition of functions, recursive definitions
of sets have two parts:
Recursively Defined Sets
Just as in the recursive definition of functions, recursive definitions
of sets have two parts:
BASIS STEP: An initial collection of elements is specified.
Recursively Defined Sets
Just as in the recursive definition of functions, recursive definitions
of sets have two parts:
BASIS STEP: An initial collection of elements is specified.
RECURSIVE STEP: Rules for forming new elements in the set from
those already known to be in the set are provided.
Recursively Defined Sets
Just as in the recursive definition of functions, recursive definitions
of sets have two parts:
BASIS STEP: An initial collection of elements is specified.
RECURSIVE STEP: Rules for forming new elements in the set from
those already known to be in the set are provided.
(Optional)
EXCLUSION RULE: Specifies that a recursively defined set contains
nothing other than those elements specified in the
basis step or generated by applications of the
recursive step.
Example
Consider the subset S of the set of integers recursively defined by:
BASIS STEP: 3 ϵ S
RECURSIVE STEP: If x ϵ S and y ϵ S, then x + y ϵ S
What is set S?
Solution
Consider the subset S of the set of integers recursively defined by:
BASIS STEP: 3 ϵ S
RECURSIVE STEP: If x ϵ S and y ϵ S, then x + y ϵ S
What is set S?
S is the set of all positive multiples of 3.
Example
Give a recursive definition of the set T = {2n – 2 | n is a natural
number greater than 0}
Solution
Give a recursive definition of the set T = {2n – 2 | n is a natural
number greater than 0}
Elements: T = { 0, 2, 6, 14, 30, . . . . }
Solution
Give a recursive definition of the set T = {2n – 2 | n is a natural
number greater than 0}
Elements: T = { 0, 2, 6, 14, 30, . . . . }
BASIS STEP: 0 ϵ T
Solution
Give a recursive definition of the set T = {2n – 2 | n is a natural
number greater than 0}
Elements: T = { 0, 2, 6, 14, 30, . . . . }
BASIS STEP: 0 ϵ T
RECURSIVE STEP: if x ϵ T, then 2x + 2 ϵ T
Find f(2), f(3), f(4), and f(5) if f is defined recursively by:
f(0) = − 1, f(1) = 2, and for n = 1, 2, …
1. f (n + 1) = f (n) + 3f (n − 1)
2. f (n + 1) = f (n − 1)/f (n)
Challenge (1/4 Sheet Paper)
Recursive Algorithms
Recursive Algorithm
Definition 1
An algorithm is called recursive
if it solves a problem by reducing it to
an instance of the same problem with
smaller input.
1 #include <stdio.h>
2 void recurse() {
3 . . .
4 recurse();
5 . . .
6 }
7 int main() {
8 . . .
9 recurse();
10 . . .
11 }
Example
Give a recursive algorithm for computing n!, where n is a
nonnegative integer.
Solution
Give a recursive algorithm for computing n!, where n is a
nonnegative integer.
Example
Give a recursive algorithm for computing an, where a is a nonzero
real number and n is a nonnegative integer.
Solution
Give a recursive algorithm for computing an, where a is a nonzero
real number and n is a nonnegative integer.
Recursion and Iteration
1 #include <stdio.h>
2 void recurse() {
3 . . .
4 recurse();
5 . . .
6 }
7 int main() {
8 . . .
9 recurse();
10 . . .
11 }
1 #include <stdio.h>
2 int main() {
3 . . .
4 for ( initialization ; condition ; action) {
5 . . .
6 . . .
7 }
8 }
Example (Recursive Algorithm)
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
Example (Iterative Algorithm)
Program Correctness
Program Verification
A program is said to be correct if it produces the correct output for
every possible input.
Program Verification
A program is said to be correct if it produces the correct output for
every possible input.
Consists of 2 parts:
1. shows that the correct answer is obtained if the program
terminates. This establishes the partial correctness.
2. Shows that the program always terminates.
Program Verification
To specify what it means for a program to produce the correct output, two
propositions are used:
1. Initial Assertion - gives the properties that the input values must
have.
2. Final Assertion - gives the properties that the output of the
program should have, if the program did what was
intended.
Program Verification
Definition 1
Example
Show that the program segment
y ≔ 2
z ≔ x + y
is correct with respect to the initial assertion p : x = 1 and the final assertion
q : z = 3.
Solution
Show that the program segment
y ≔ 2
z ≔ x + y
is correct with respect to the initial assertion p : x = 1 and the final assertion
q : z = 3.
Suppose that p is true, so that x = 1 as the program begins. Then y is
assigned the value 2, and z is assigned the sum of the values of x and y,
which is 3. Hence, S is correct with respect to the initial assertion p and the
final assertion q. Thus, p{S}q is true.
Example
Verify that the program segment
if x > y then
y ≔ x
is correct with respect to the initial assertion T and the final assertion y >= x.
Solution
Verify that the program segment
if x > y then
y ≔ x
is correct with respect to the initial assertion T and the final assertion y >= x.
When the initial assertion is true and x > y, the assignment y := x is carried
out. Hence, the final assertion, which asserts that y >= x, is true in this case.
Moreover, when the initial assertion is true and x > y is false, so that x <= y,
the final assertion is again true. Hence, using the rule of inference for
program segments of this type, this program is correct with respect to the
given initial and final assertions.
Any Question?
Announcement
2ND LONG EXAMINATION
OCTOBER 25, 2018
5:00PM – 7:00PM, CL3 & B6
COVERS LECTURE 7 – 12
“STUDY HARD!”

More Related Content

What's hot (20)

Asymptotes and holes 97
Asymptotes and holes 97
swartzje
 
Mathematical Induction
Mathematical Induction
Edelyn Cagas
 
Power series
Power series
Dr. Nirav Vyas
 
26 the logarithm functions x
26 the logarithm functions x
math260
 
Pigeonhole Principle
Pigeonhole Principle
nielsoli
 
Linear Algebra and Matrix
Linear Algebra and Matrix
itutor
 
False Point Method / Regula falsi method
False Point Method / Regula falsi method
Nasima Akhtar
 
5.4 mathematical induction
5.4 mathematical induction
math260
 
The natural numbers
The natural numbers
Judi Maee
 
lattice
lattice
Daffodil International University
 
Mathematical induction
Mathematical induction
rey castro
 
Regula Falsi (False position) Method
Regula Falsi (False position) Method
Isaac Yowetu
 
Set
Set
H K
 
Lesson 26: The Fundamental Theorem of Calculus (slides)
Lesson 26: The Fundamental Theorem of Calculus (slides)
Matthew Leingang
 
recurence solutions
recurence solutions
soumya8396
 
Linear transformation.ppt
Linear transformation.ppt
Raj Parekh
 
Newton Raphson
Newton Raphson
Nasima Akhtar
 
Lesson 10: The Chain Rule
Lesson 10: The Chain Rule
Matthew Leingang
 
CMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and Strategy
allyn joy calcaben
 
mathematical induction
mathematical induction
ankush_kumar
 
Asymptotes and holes 97
Asymptotes and holes 97
swartzje
 
Mathematical Induction
Mathematical Induction
Edelyn Cagas
 
26 the logarithm functions x
26 the logarithm functions x
math260
 
Pigeonhole Principle
Pigeonhole Principle
nielsoli
 
Linear Algebra and Matrix
Linear Algebra and Matrix
itutor
 
False Point Method / Regula falsi method
False Point Method / Regula falsi method
Nasima Akhtar
 
5.4 mathematical induction
5.4 mathematical induction
math260
 
The natural numbers
The natural numbers
Judi Maee
 
Mathematical induction
Mathematical induction
rey castro
 
Regula Falsi (False position) Method
Regula Falsi (False position) Method
Isaac Yowetu
 
Set
Set
H K
 
Lesson 26: The Fundamental Theorem of Calculus (slides)
Lesson 26: The Fundamental Theorem of Calculus (slides)
Matthew Leingang
 
recurence solutions
recurence solutions
soumya8396
 
Linear transformation.ppt
Linear transformation.ppt
Raj Parekh
 
CMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and Strategy
allyn joy calcaben
 
mathematical induction
mathematical induction
ankush_kumar
 

Similar to CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness (20)

Recursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptx
gbikorno
 
Recursion
Recursion
Abdur Rehman
 
3. Recursion and Recurrences.ppt detail about recursive learning
3. Recursion and Recurrences.ppt detail about recursive learning
KashifNadeem52
 
Lec-6 Recursion of Data Structures & Algorithms
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
04-Induction and Recursion.ppt ppt bai tap
04-Induction and Recursion.ppt ppt bai tap
15LThThuHuyn
 
CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4
David Wood
 
Recursion
Recursion
Kasun Ranga Wijeweera
 
L16
L16
FALLEE31188
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
17recursion
17recursion
fyjordan9
 
17-recursive-definitions-and-structural-induction.pdf
17-recursive-definitions-and-structural-induction.pdf
Endelion
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
Study on a class of recursive functions
Study on a class of recursive functions
Luca Polidori
 
Recursion.pdf
Recursion.pdf
Flavia Tembo Kambale
 
Theoryofcomp science
Theoryofcomp science
Raghu nath
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
Lecture9 recursion
Lecture9 recursion
Muhammad Zubair
 
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
rokiah64
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
Recursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptx
gbikorno
 
3. Recursion and Recurrences.ppt detail about recursive learning
3. Recursion and Recurrences.ppt detail about recursive learning
KashifNadeem52
 
Lec-6 Recursion of Data Structures & Algorithms
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
04-Induction and Recursion.ppt ppt bai tap
04-Induction and Recursion.ppt ppt bai tap
15LThThuHuyn
 
CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4
David Wood
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
17-recursive-definitions-and-structural-induction.pdf
17-recursive-definitions-and-structural-induction.pdf
Endelion
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
Study on a class of recursive functions
Study on a class of recursive functions
Luca Polidori
 
Theoryofcomp science
Theoryofcomp science
Raghu nath
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
rokiah64
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
Ad

More from allyn joy calcaben (17)

CMSC 56 | Lecture 17: Matrices
CMSC 56 | Lecture 17: Matrices
allyn joy calcaben
 
CMSC 56 | Lecture 15: Closures of Relations
CMSC 56 | Lecture 15: Closures of Relations
allyn joy calcaben
 
CMSC 56 | Lecture 14: Representing Relations
CMSC 56 | Lecture 14: Representing Relations
allyn joy calcaben
 
CMSC 56 | Lecture 13: Relations and their Properties
CMSC 56 | Lecture 13: Relations and their Properties
allyn joy calcaben
 
CMSC 56 | Lecture 11: Mathematical Induction
CMSC 56 | Lecture 11: Mathematical Induction
allyn joy calcaben
 
CMSC 56 | Lecture 10: Integer Representations & Algorithms
CMSC 56 | Lecture 10: Integer Representations & Algorithms
allyn joy calcaben
 
CMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions Representations
allyn joy calcaben
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
allyn joy calcaben
 
CMSC 56 | Lecture 4: Rules of Inference
CMSC 56 | Lecture 4: Rules of Inference
allyn joy calcaben
 
CMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set Operations
allyn joy calcaben
 
CMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & Quantifiers
allyn joy calcaben
 
CMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional Equivalences
allyn joy calcaben
 
CMSC 56 | Lecture 1: Propositional Logic
CMSC 56 | Lecture 1: Propositional Logic
allyn joy calcaben
 
La Solidaridad and the Propaganda Movement
La Solidaridad and the Propaganda Movement
allyn joy calcaben
 
Computer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC Algorithm
allyn joy calcaben
 
Chapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements ii
allyn joy calcaben
 
#11 osteoporosis
#11 osteoporosis
allyn joy calcaben
 
CMSC 56 | Lecture 17: Matrices
CMSC 56 | Lecture 17: Matrices
allyn joy calcaben
 
CMSC 56 | Lecture 15: Closures of Relations
CMSC 56 | Lecture 15: Closures of Relations
allyn joy calcaben
 
CMSC 56 | Lecture 14: Representing Relations
CMSC 56 | Lecture 14: Representing Relations
allyn joy calcaben
 
CMSC 56 | Lecture 13: Relations and their Properties
CMSC 56 | Lecture 13: Relations and their Properties
allyn joy calcaben
 
CMSC 56 | Lecture 11: Mathematical Induction
CMSC 56 | Lecture 11: Mathematical Induction
allyn joy calcaben
 
CMSC 56 | Lecture 10: Integer Representations & Algorithms
CMSC 56 | Lecture 10: Integer Representations & Algorithms
allyn joy calcaben
 
CMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions Representations
allyn joy calcaben
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
allyn joy calcaben
 
CMSC 56 | Lecture 4: Rules of Inference
CMSC 56 | Lecture 4: Rules of Inference
allyn joy calcaben
 
CMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set Operations
allyn joy calcaben
 
CMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & Quantifiers
allyn joy calcaben
 
CMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional Equivalences
allyn joy calcaben
 
CMSC 56 | Lecture 1: Propositional Logic
CMSC 56 | Lecture 1: Propositional Logic
allyn joy calcaben
 
La Solidaridad and the Propaganda Movement
La Solidaridad and the Propaganda Movement
allyn joy calcaben
 
Computer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC Algorithm
allyn joy calcaben
 
Chapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements ii
allyn joy calcaben
 
Ad

Recently uploaded (20)

How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 

CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness

  • 1. Recursive Definition & Algorithms, and Program Correctness Lecture 12, CMSC 56 Allyn Joy D. Calcaben
  • 3. Recursion A method where the solution to a problem depends on solutions to smaller instances of the same problem.
  • 4. We use 2 steps to define a function with the set of nonnegative integers as its domain: BASIS STEP: Specify the value of the function at zero
  • 5. We use 2 steps to define a function with the set of nonnegative integers as its domain: BASIS STEP: Specify the value of the function at zero RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integers.
  • 6. Example Give the first four terms of the following sequence: f(1) = 5 f(n + 1) = f(n) + 3 That is, we find f(1), f(2), f(3), and f(4).
  • 7. Example Give the first four terms of the following sequence: BASIS STEP: f(1) = 5 f(n + 1) = f(n) + 3 That is, we find f(1), f(2), f(3), and f(4).
  • 8. Example Give the first four terms of the following sequence: BASIS STEP: f(1) = 5 RECURSIVE STEP: f(n + 1) = f(n) + 3 That is, we find f(1), f(2), f(3), and f(4).
  • 9. Solution BASIS STEP: f(1) = 5 RECURSIVE STEP: f(n + 1) = f(n) + 3 f(1) = 5
  • 10. Solution BASIS STEP: f(1) = 5 RECURSIVE STEP: f(n + 1) = f(n) + 3 f(1) = 5 f(2) = f(1) + 3 = 5 + 3 = 8
  • 11. Solution BASIS STEP: f(1) = 5 RECURSIVE STEP: f(n + 1) = f(n) + 3 f(1) = 5 f(2) = f(1) + 3 = 5 + 3 = 8 f(3) = f(2) + 3 = 8 + 3 = 11
  • 12. Solution BASIS STEP: f(1) = 5 RECURSIVE STEP: f(n + 1) = f(n) + 3 f(1) = 5 f(2) = f(1) + 3 = 5 + 3 = 8 f(3) = f(2) + 3 = 8 + 3 = 11 f(4) = f(3) + 3 = 11 + 3 = 14
  • 13. Example Suppose that f is defined recursively by: f(1) = 12 f(2) = 4 f(n + 1) = f(n) + 2 ⋅ f(n - 1) Find f(1), f(2), f(3), and f(4).
  • 14. Example Suppose that f is defined recursively by: BASIS STEP: f(1) = 12 f(2) = 4 f(n + 1) = f(n) + 2 ⋅ f(n - 1) Find f(1), f(2), f(3), and f(4).
  • 15. Example Suppose that f is defined recursively by: BASIS STEP: f(1) = 12 f(2) = 4 RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1) Find f(1), f(2), f(3), and f(4).
  • 16. Solution BASIS STEP: f(1) = 12 f(2) = 4 RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1) f(1) = 12
  • 17. Solution BASIS STEP: f(1) = 12 f(2) = 4 RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1) f(1) = 12 f(2) = 4
  • 18. Solution BASIS STEP: f(1) = 12 f(2) = 4 RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1) f(1) = 12 f(2) = 4 f(3) = f(2) + 2 ⋅ f(1) = 4 + 2 ⋅ 12 = 28
  • 19. Solution BASIS STEP: f(1) = 12 f(2) = 4 RECURSIVE STEP: f(n + 1) = f(n) + 2 ⋅ f(n - 1) f(1) = 12 f(2) = 4 f(3) = f(2) + 2 ⋅ f(1) = 4 + 2 ⋅ 12 = 28 f(4) = f(3) + 2 ⋅ f(2) = 28 + 2 ⋅ 4 = 36
  • 20. Example Suppose that f is defined recursively by: f(0) = 3 f(n + 1) = 2 ⋅ f(n) + 3 Find f(1), f(2), f(3), and f(4).
  • 21. Example Suppose that f is defined recursively by: BASIS STEP: f(0) = 3 f(n + 1) = 2 ⋅ f(n) + 3 Find f(1), f(2), f(3), and f(4).
  • 22. Example Suppose that f is defined recursively by: BASIS STEP: f(0) = 3 RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3 Find f(1), f(2), f(3), and f(4).
  • 23. Solution BASIS STEP: f(0) = 3 RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3 f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9
  • 24. Solution BASIS STEP: f(0) = 3 RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3 f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9 f(2) = 2 ⋅ f(1) + 3 = 2 ⋅ 9 + 3 = 21
  • 25. Solution BASIS STEP: f(0) = 3 RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3 f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9 f(2) = 2 ⋅ f(1) + 3 = 2 ⋅ 9 + 3 = 21 f(3) = 2 ⋅ f(2) + 3 = 2 ⋅ 21 + 3 = 45
  • 26. Solution BASIS STEP: f(0) = 3 RECURSIVE STEP: f(n + 1) = 2 ⋅ f(n) + 3 f(1) = 2 ⋅ f(0) + 3 = 2 ⋅ 3 + 3 = 9 f(2) = 2 ⋅ f(1) + 3 = 2 ⋅ 9 + 3 = 21 f(3) = 2 ⋅ f(2) + 3 = 2 ⋅ 21 + 3 = 45 f(4) = 2 ⋅ f(3) + 3 = 2 ⋅ 45 + 3 = 93
  • 27. Example Find a recursive definition for the following sequence with n as a positive integer: 20, 17, 14, 11, 8, . . . .
  • 28. Solution Find a recursive definition for the following sequence with n as a positive integer: 20, 17, 14, 11, 8, . . . . BASIS STEP: f(1) = 20
  • 29. Solution Find a recursive definition for the following sequence with n as a positive integer: 20, 17, 14, 11, 8, . . . . BASIS STEP: f(1) = 20 RECURSIVE STEP: f(n + 1) = f(n) - 3
  • 30. Example Give a recursive definition of an, where a is a nonzero real number and n is a nonnegative integer.
  • 31. Solution Give a recursive definition of an, where a is a nonzero real number and n is a nonnegative integer. BASIS STEP: f(0) = 1
  • 32. Solution Give a recursive definition of an, where a is a nonzero real number and n is a nonnegative integer. BASIS STEP: f(0) = 1 RECURSIVE STEP: f(n) = a ⋅ f(n - 1)
  • 33. Recursively Defined Sets Just as in the recursive definition of functions, recursive definitions of sets have two parts:
  • 34. Recursively Defined Sets Just as in the recursive definition of functions, recursive definitions of sets have two parts: BASIS STEP: An initial collection of elements is specified.
  • 35. Recursively Defined Sets Just as in the recursive definition of functions, recursive definitions of sets have two parts: BASIS STEP: An initial collection of elements is specified. RECURSIVE STEP: Rules for forming new elements in the set from those already known to be in the set are provided.
  • 36. Recursively Defined Sets Just as in the recursive definition of functions, recursive definitions of sets have two parts: BASIS STEP: An initial collection of elements is specified. RECURSIVE STEP: Rules for forming new elements in the set from those already known to be in the set are provided. (Optional) EXCLUSION RULE: Specifies that a recursively defined set contains nothing other than those elements specified in the basis step or generated by applications of the recursive step.
  • 37. Example Consider the subset S of the set of integers recursively defined by: BASIS STEP: 3 ϵ S RECURSIVE STEP: If x ϵ S and y ϵ S, then x + y ϵ S What is set S?
  • 38. Solution Consider the subset S of the set of integers recursively defined by: BASIS STEP: 3 ϵ S RECURSIVE STEP: If x ϵ S and y ϵ S, then x + y ϵ S What is set S? S is the set of all positive multiples of 3.
  • 39. Example Give a recursive definition of the set T = {2n – 2 | n is a natural number greater than 0}
  • 40. Solution Give a recursive definition of the set T = {2n – 2 | n is a natural number greater than 0} Elements: T = { 0, 2, 6, 14, 30, . . . . }
  • 41. Solution Give a recursive definition of the set T = {2n – 2 | n is a natural number greater than 0} Elements: T = { 0, 2, 6, 14, 30, . . . . } BASIS STEP: 0 ϵ T
  • 42. Solution Give a recursive definition of the set T = {2n – 2 | n is a natural number greater than 0} Elements: T = { 0, 2, 6, 14, 30, . . . . } BASIS STEP: 0 ϵ T RECURSIVE STEP: if x ϵ T, then 2x + 2 ϵ T
  • 43. Find f(2), f(3), f(4), and f(5) if f is defined recursively by: f(0) = − 1, f(1) = 2, and for n = 1, 2, … 1. f (n + 1) = f (n) + 3f (n − 1) 2. f (n + 1) = f (n − 1)/f (n) Challenge (1/4 Sheet Paper)
  • 45. Recursive Algorithm Definition 1 An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. 1 #include <stdio.h> 2 void recurse() { 3 . . . 4 recurse(); 5 . . . 6 } 7 int main() { 8 . . . 9 recurse(); 10 . . . 11 }
  • 46. Example Give a recursive algorithm for computing n!, where n is a nonnegative integer.
  • 47. Solution Give a recursive algorithm for computing n!, where n is a nonnegative integer.
  • 48. Example Give a recursive algorithm for computing an, where a is a nonzero real number and n is a nonnegative integer.
  • 49. Solution Give a recursive algorithm for computing an, where a is a nonzero real number and n is a nonnegative integer.
  • 50. Recursion and Iteration 1 #include <stdio.h> 2 void recurse() { 3 . . . 4 recurse(); 5 . . . 6 } 7 int main() { 8 . . . 9 recurse(); 10 . . . 11 } 1 #include <stdio.h> 2 int main() { 3 . . . 4 for ( initialization ; condition ; action) { 5 . . . 6 . . . 7 } 8 }
  • 55. Program Verification A program is said to be correct if it produces the correct output for every possible input.
  • 56. Program Verification A program is said to be correct if it produces the correct output for every possible input. Consists of 2 parts: 1. shows that the correct answer is obtained if the program terminates. This establishes the partial correctness. 2. Shows that the program always terminates.
  • 57. Program Verification To specify what it means for a program to produce the correct output, two propositions are used: 1. Initial Assertion - gives the properties that the input values must have. 2. Final Assertion - gives the properties that the output of the program should have, if the program did what was intended.
  • 59. Example Show that the program segment y ≔ 2 z ≔ x + y is correct with respect to the initial assertion p : x = 1 and the final assertion q : z = 3.
  • 60. Solution Show that the program segment y ≔ 2 z ≔ x + y is correct with respect to the initial assertion p : x = 1 and the final assertion q : z = 3. Suppose that p is true, so that x = 1 as the program begins. Then y is assigned the value 2, and z is assigned the sum of the values of x and y, which is 3. Hence, S is correct with respect to the initial assertion p and the final assertion q. Thus, p{S}q is true.
  • 61. Example Verify that the program segment if x > y then y ≔ x is correct with respect to the initial assertion T and the final assertion y >= x.
  • 62. Solution Verify that the program segment if x > y then y ≔ x is correct with respect to the initial assertion T and the final assertion y >= x. When the initial assertion is true and x > y, the assignment y := x is carried out. Hence, the final assertion, which asserts that y >= x, is true in this case. Moreover, when the initial assertion is true and x > y is false, so that x <= y, the final assertion is again true. Hence, using the rule of inference for program segments of this type, this program is correct with respect to the given initial and final assertions.
  • 64. Announcement 2ND LONG EXAMINATION OCTOBER 25, 2018 5:00PM – 7:00PM, CL3 & B6 COVERS LECTURE 7 – 12 “STUDY HARD!”

Editor's Notes

  • #33: First a0 is specified, namely, a0 = 1. Then the rule for finding an+1 from an, namely, an+1 = a · an, for n = 0, 1, 2, 3, . . . , is given. These two equations uniquely define an for all nonnegative integers n.
  • #52: Recursion and iteration both repeatedly executes the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that is a recursion is a process, always applied to a function. The iteration is applied to the set of instructions which we want to get repeatedly executed.
  • #53: When we use a recursive procedure to find f(n), we first express f(n) as f(n-1) + f(n-2). Then we replace both of these Fibonacci numbers by the sum of two previous Fibonacci numbers, and so on. When f(1) or f(0) arises, it is replaced by its value. Note that at each stage of the recursion, until f(1) or f(0) is obtained, the number of Fibonacci numbers to be evaluated has doubled.
  • #54: For instance, when we find f(4) using this recursive algorithm, we must carry out all the computations illustrated in the tree diagram in Figure 1. This tree consists of a root labeled with f(4), and branches from the root to vertices labeled with the two Fibonacci numbers f(3) and f(2) that occur in the reduction of the computation of f(4). Each subsequent reduction produces two branches in the tree. This branching ends when f(0) and f(1) are reached. The reader can verify that this algorithm requires f(n+1)-1 additions to find f(n).
  • #55: Describe the code!
  • #65: Due to unavailability of Extra Computer Labs and Schedule,