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

PPTX
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
PPTX
Knapsack Problem (DP & GREEDY)
PPT
Quick Sort
PDF
Chapter 2: Relations
PDF
Rooted &amp; binary tree
PPT
Leftist heap
PDF
Abstraction
PPT
Mathematical foundations of computer science
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Knapsack Problem (DP & GREEDY)
Quick Sort
Chapter 2: Relations
Rooted &amp; binary tree
Leftist heap
Abstraction
Mathematical foundations of computer science

What's hot (20)

PPTX
Graph in data structure
PDF
Complex analysis book by iit
PPTX
Relations in Discrete Math
PPTX
Matrix chain multiplication
PPT
Divide and Conquer
PPT
02 order of growth
PDF
Binary search algorithm
PPTX
CMSC 56 | Lecture 15: Closures of Relations
PPTX
Asymptotic notations(Big O, Omega, Theta )
PDF
Formal Logic - Lesson 5 - Logical Equivalence
PDF
Poset in Relations(Discrete Mathematics)
PPTX
linked list
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
PPTX
Asymptotic Notations
PPT
Discrete Math Lecture 01: Propositional Logic
PPTX
Forms of learning in ai
PPTX
Branching statements
PPT
how to calclute time complexity of algortihm
PPTX
sum of subset problem using Backtracking
Graph in data structure
Complex analysis book by iit
Relations in Discrete Math
Matrix chain multiplication
Divide and Conquer
02 order of growth
Binary search algorithm
CMSC 56 | Lecture 15: Closures of Relations
Asymptotic notations(Big O, Omega, Theta )
Formal Logic - Lesson 5 - Logical Equivalence
Poset in Relations(Discrete Mathematics)
linked list
CLASS OBJECT AND INHERITANCE IN PYTHON
Asymptotic Notations
Discrete Math Lecture 01: Propositional Logic
Forms of learning in ai
Branching statements
how to calclute time complexity of algortihm
sum of subset problem using Backtracking
Ad

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

PPTX
Recursive Definitions in Discrete Mathmatcs.pptx
PPTX
Recursion
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPT
Lec-6 Recursion of Data Structures & Algorithms
PPTX
Recursion DM
PPT
04-Induction and Recursion.ppt ppt bai tap
PDF
CPSC 125 Ch 2 Sec 4
PPTX
PPT
PDF
Copy of y16 02-2119divide-and-conquer
PPT
17recursion
PDF
17-recursive-definitions-and-structural-induction.pdf
PDF
01 Notes Introduction Analysis of Algorithms Notes
PDF
Study on a class of recursive functions
PDF
Recursion.pdf
DOCX
Theoryofcomp science
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPT
Lecture9 recursion
PDF
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Recursive Definitions in Discrete Mathmatcs.pptx
Recursion
3. Recursion and Recurrences.ppt detail about recursive learning
Lec-6 Recursion of Data Structures & Algorithms
Recursion DM
04-Induction and Recursion.ppt ppt bai tap
CPSC 125 Ch 2 Sec 4
Copy of y16 02-2119divide-and-conquer
17recursion
17-recursive-definitions-and-structural-induction.pdf
01 Notes Introduction Analysis of Algorithms Notes
Study on a class of recursive functions
Recursion.pdf
Theoryofcomp science
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture9 recursion
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Ad

More from allyn joy calcaben (18)

PPTX
CMSC 56 | Lecture 17: Matrices
PPTX
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
PPTX
CMSC 56 | Lecture 14: Representing Relations
PPTX
CMSC 56 | Lecture 13: Relations and their Properties
PPTX
CMSC 56 | Lecture 11: Mathematical Induction
PPTX
CMSC 56 | Lecture 10: Integer Representations & Algorithms
PPTX
CMSC 56 | Lecture 9: Functions Representations
PPTX
CMSC 56 | Lecture 8: Growth of Functions
PPTX
CMSC 56 | Lecture 4: Rules of Inference
PPTX
CMSC 56 | Lecture 6: Sets & Set Operations
PPTX
CMSC 56 | Lecture 5: Proofs Methods and Strategy
PPTX
CMSC 56 | Lecture 3: Predicates & Quantifiers
PPTX
CMSC 56 | Lecture 2: Propositional Equivalences
PPTX
CMSC 56 | Lecture 1: Propositional Logic
PDF
La Solidaridad and the Propaganda Movement
PDF
Computer Vision: Feature matching with RANSAC Algorithm
PDF
Chapter 7 expressions and assignment statements ii
PDF
#11 osteoporosis
CMSC 56 | Lecture 17: Matrices
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 14: Representing Relations
CMSC 56 | Lecture 13: Relations and their Properties
CMSC 56 | Lecture 11: Mathematical Induction
CMSC 56 | Lecture 10: Integer Representations & Algorithms
CMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 4: Rules of Inference
CMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 1: Propositional Logic
La Solidaridad and the Propaganda Movement
Computer Vision: Feature matching with RANSAC Algorithm
Chapter 7 expressions and assignment statements ii
#11 osteoporosis

Recently uploaded (20)

PDF
IP : I ; Unit I : Preformulation Studies
PDF
CRP102_SAGALASSOS_Final_Projects_2025.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Hazard Identification & Risk Assessment .pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Complications of Minimal Access-Surgery.pdf
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PPTX
Climate Change and Its Global Impact.pptx
PDF
HVAC Specification 2024 according to central public works department
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
semiconductor packaging in vlsi design fab
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
Education and Perspectives of Education.pptx
IP : I ; Unit I : Preformulation Studies
CRP102_SAGALASSOS_Final_Projects_2025.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
Hazard Identification & Risk Assessment .pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Complications of Minimal Access-Surgery.pdf
Literature_Review_methods_ BRACU_MKT426 course material
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
Climate Change and Its Global Impact.pptx
HVAC Specification 2024 according to central public works department
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
semiconductor packaging in vlsi design fab
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
My India Quiz Book_20210205121199924.pdf
Education and Perspectives of Education.pptx

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,