This document discusses recursive definitions, algorithms, and program correctness. It provides examples of recursively defining functions and sets using a basis step and recursive step. Examples of recursive algorithms are given for computing factorials and exponentials. The difference between recursion and iteration is explained. Program verification and correctness are defined, and examples are worked through to show initial and final assertions for program segments. The document concludes with an announcement about an upcoming exam.
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).
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 }
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.
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.
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.
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.
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).
Describe the code!
Due to unavailability of Extra Computer Labs and Schedule,