SlideShare une entreprise Scribd logo
1  sur  64
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)
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!”

Contenu connexe

Tendances (20)

Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Bucket sort
Bucket sortBucket sort
Bucket sort
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Master method
Master method Master method
Master method
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
 
Automata theory - NFA to DFA Conversion
Automata theory - NFA to DFA ConversionAutomata theory - NFA to DFA Conversion
Automata theory - NFA to DFA Conversion
 
Data structure tries
Data structure triesData structure tries
Data structure tries
 
Ring
RingRing
Ring
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
K-Means manual work
K-Means manual workK-Means manual work
K-Means manual work
 

Similaire à Recursive Definitions, Algorithms, and Program Correctness

Recursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptxRecursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptxgbikorno
 
5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functions5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functionssmiller5
 
Algebra 2 Section 4-4
Algebra 2 Section 4-4Algebra 2 Section 4-4
Algebra 2 Section 4-4Jimbo Lamb
 
Mathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptxMathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptxgbikorno
 
Mathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptxMathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptxgbikorno
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
02-Basic Structures .ppt
02-Basic Structures .ppt02-Basic Structures .ppt
02-Basic Structures .pptAcct4
 
Applications of Differentiation
Applications of DifferentiationApplications of Differentiation
Applications of DifferentiationJoey Valdriz
 
Sequences and series
Sequences and seriesSequences and series
Sequences and seriesmstf mstf
 
The Fundamental theorem of calculus
The Fundamental theorem of calculus The Fundamental theorem of calculus
The Fundamental theorem of calculus AhsanIrshad8
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms IiSri Prasanna
 

Similaire à Recursive Definitions, Algorithms, and Program Correctness (20)

Recursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptxRecursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptx
 
Recursion DM
Recursion DMRecursion DM
Recursion DM
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Recursion
RecursionRecursion
Recursion
 
5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functions5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functions
 
L16
L16L16
L16
 
6e-ch4.ppt
6e-ch4.ppt6e-ch4.ppt
6e-ch4.ppt
 
Algebra 2 Section 4-4
Algebra 2 Section 4-4Algebra 2 Section 4-4
Algebra 2 Section 4-4
 
Mathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptxMathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptx
 
Mathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptxMathematical Reasoning in Discrete Mathmatics.pptx
Mathematical Reasoning in Discrete Mathmatics.pptx
 
Task 4
Task 4Task 4
Task 4
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Twinkle
TwinkleTwinkle
Twinkle
 
file_5.pptx
file_5.pptxfile_5.pptx
file_5.pptx
 
02-Basic Structures .ppt
02-Basic Structures .ppt02-Basic Structures .ppt
02-Basic Structures .ppt
 
Applications of Differentiation
Applications of DifferentiationApplications of Differentiation
Applications of Differentiation
 
Sequences and series
Sequences and seriesSequences and series
Sequences and series
 
Per4 induction
Per4 inductionPer4 induction
Per4 induction
 
The Fundamental theorem of calculus
The Fundamental theorem of calculus The Fundamental theorem of calculus
The Fundamental theorem of calculus
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
 

Plus de allyn joy calcaben

CMSC 56 | Lecture 17: Matrices
CMSC 56 | Lecture 17: MatricesCMSC 56 | Lecture 17: Matrices
CMSC 56 | Lecture 17: Matricesallyn joy calcaben
 
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial OrderingCMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Orderingallyn joy calcaben
 
CMSC 56 | Lecture 15: Closures of Relations
CMSC 56 | Lecture 15: Closures of RelationsCMSC 56 | Lecture 15: Closures of Relations
CMSC 56 | Lecture 15: Closures of Relationsallyn joy calcaben
 
CMSC 56 | Lecture 14: Representing Relations
CMSC 56 | Lecture 14: Representing RelationsCMSC 56 | Lecture 14: Representing Relations
CMSC 56 | Lecture 14: Representing Relationsallyn joy calcaben
 
CMSC 56 | Lecture 13: Relations and their Properties
CMSC 56 | Lecture 13: Relations and their PropertiesCMSC 56 | Lecture 13: Relations and their Properties
CMSC 56 | Lecture 13: Relations and their Propertiesallyn joy calcaben
 
CMSC 56 | Lecture 11: Mathematical Induction
CMSC 56 | Lecture 11: Mathematical InductionCMSC 56 | Lecture 11: Mathematical Induction
CMSC 56 | Lecture 11: Mathematical Inductionallyn joy calcaben
 
CMSC 56 | Lecture 10: Integer Representations & Algorithms
CMSC 56 | Lecture 10: Integer Representations & AlgorithmsCMSC 56 | Lecture 10: Integer Representations & Algorithms
CMSC 56 | Lecture 10: Integer Representations & Algorithmsallyn joy calcaben
 
CMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions RepresentationsCMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions Representationsallyn joy calcaben
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functionsallyn joy calcaben
 
CMSC 56 | Lecture 4: Rules of Inference
CMSC 56 | Lecture 4: Rules of InferenceCMSC 56 | Lecture 4: Rules of Inference
CMSC 56 | Lecture 4: Rules of Inferenceallyn joy calcaben
 
CMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set OperationsCMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set Operationsallyn joy calcaben
 
CMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and StrategyCMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and Strategyallyn joy calcaben
 
CMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & QuantifiersCMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & Quantifiersallyn joy calcaben
 
CMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional EquivalencesCMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional Equivalencesallyn joy calcaben
 
CMSC 56 | Lecture 1: Propositional Logic
CMSC 56 | Lecture 1: Propositional LogicCMSC 56 | Lecture 1: Propositional Logic
CMSC 56 | Lecture 1: Propositional Logicallyn joy calcaben
 
La Solidaridad and the Propaganda Movement
La Solidaridad and the Propaganda MovementLa Solidaridad and the Propaganda Movement
La Solidaridad and the Propaganda Movementallyn joy calcaben
 
Computer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC AlgorithmComputer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC Algorithmallyn joy calcaben
 
Chapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements iiChapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements iiallyn joy calcaben
 

Plus de allyn joy calcaben (19)

CMSC 56 | Lecture 17: Matrices
CMSC 56 | Lecture 17: MatricesCMSC 56 | Lecture 17: Matrices
CMSC 56 | Lecture 17: Matrices
 
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial OrderingCMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
 
CMSC 56 | Lecture 15: Closures of Relations
CMSC 56 | Lecture 15: Closures of RelationsCMSC 56 | Lecture 15: Closures of Relations
CMSC 56 | Lecture 15: Closures of Relations
 
CMSC 56 | Lecture 14: Representing Relations
CMSC 56 | Lecture 14: Representing RelationsCMSC 56 | Lecture 14: Representing Relations
CMSC 56 | Lecture 14: Representing Relations
 
CMSC 56 | Lecture 13: Relations and their Properties
CMSC 56 | Lecture 13: Relations and their PropertiesCMSC 56 | Lecture 13: Relations and their Properties
CMSC 56 | Lecture 13: Relations and their Properties
 
CMSC 56 | Lecture 11: Mathematical Induction
CMSC 56 | Lecture 11: Mathematical InductionCMSC 56 | Lecture 11: Mathematical Induction
CMSC 56 | Lecture 11: Mathematical Induction
 
CMSC 56 | Lecture 10: Integer Representations & Algorithms
CMSC 56 | Lecture 10: Integer Representations & AlgorithmsCMSC 56 | Lecture 10: Integer Representations & Algorithms
CMSC 56 | Lecture 10: Integer Representations & Algorithms
 
CMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions RepresentationsCMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions Representations
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 
CMSC 56 | Lecture 4: Rules of Inference
CMSC 56 | Lecture 4: Rules of InferenceCMSC 56 | Lecture 4: Rules of Inference
CMSC 56 | Lecture 4: Rules of Inference
 
CMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set OperationsCMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set Operations
 
CMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and StrategyCMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and Strategy
 
CMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & QuantifiersCMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & Quantifiers
 
CMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional EquivalencesCMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional Equivalences
 
CMSC 56 | Lecture 1: Propositional Logic
CMSC 56 | Lecture 1: Propositional LogicCMSC 56 | Lecture 1: Propositional Logic
CMSC 56 | Lecture 1: Propositional Logic
 
La Solidaridad and the Propaganda Movement
La Solidaridad and the Propaganda MovementLa Solidaridad and the Propaganda Movement
La Solidaridad and the Propaganda Movement
 
Computer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC AlgorithmComputer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC Algorithm
 
Chapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements iiChapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements ii
 
#11 osteoporosis
#11 osteoporosis#11 osteoporosis
#11 osteoporosis
 

Dernier

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Dernier (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Recursive Definitions, 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 }
  • 52.
  • 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!”

Notes de l'éditeur

  1. 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.
  2. 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.
  3. 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.
  4. 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).
  5. Describe the code!
  6. Due to unavailability of Extra Computer Labs and Schedule,