Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

CSC310 02 Algorithm Correctness

This document provides lecture notes on algorithm correctness, focusing on the principles of mathematical induction and its application in proving algorithm correctness. It explains the importance of ensuring algorithms yield correct results and discusses both recursive and iterative algorithms, including the use of loop invariants. The notes include examples and exercises to illustrate the concepts of induction and correctness proofs.

Uploaded by

masudusman.390
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CSC310 02 Algorithm Correctness

This document provides lecture notes on algorithm correctness, focusing on the principles of mathematical induction and its application in proving algorithm correctness. It explains the importance of ensuring algorithms yield correct results and discusses both recursive and iterative algorithms, including the use of loop invariants. The notes include examples and exercises to illustrate the concepts of induction and correctness proofs.

Uploaded by

masudusman.390
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Algorithms and Complexity Analysis

Lecture Notes

UNIT 2: ALGORITHM CORRECTNESS

Dr Fatimah Adamu-Fika
Department of Cyber Security
Faculty of Computing
Air Force Institute of Technology
Kaduna, Nigeria

April 2022
© Since 2020. All rights reserved. No part of this document may be reproduced or
transmitted in any form or by any means, electronic or mechanical, including
photocopy, recording, or any information storage and retrieval system, without
permission in writing from the author.
CONTENT

Overview................................................................................................................................... 1

Review of Mathematical Induction ...................................................................................... 1

Principle of Mathematical induction ................................................................................ 2

Generalisation of Principle of Mathematical Induction ................................................. 6

Strong Induction .............................................................................................................10

Practice: Induction Proofs .............................................................................................12

Proving Correctness of an Algorithm ..................................................................................13

Correctness of Recursive Algorithms ...............................................................................13

Correctness of Iterative Algorithms .................................................................................15

Loop Invariants ...............................................................................................................15

Unit Goals................................................................................................................................20

Exercises ..................................................................................................................................21

Bibliography and Resources ................................................................................................25

Textbooks ............................................................................................................................25

Websites ..............................................................................................................................25

Algorithms and Unit 2 Dr F. Adamu-Fika


ii Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
OVERVIEW

Supposing time and space are not major constraints for algorithms, we still need to
study algorithms. Because we would like to ascertain that given legitimate input, our
algorithm terminates with the correct solution within a finite amount of time. However,
we can accept an algorithm that is often correct, i.e., even if fails to produce the
correct result always, there is still control on how often it produces wrong solutions. For
example, Rabin-Miller Primality Test (used in the RSA algorithm) doesn’t always
produce correct results. 1 out of 250 times it gives an incorrect result. Correctness can
be tricky with another class of algorithms, which do not find exact solutions but rather
find near-optimal ones. By “near-optimal,” we typically mean that the quantitative
measure of the solution found by the algorithm is within some known factor of the
optimal solution’s quantitative measure. This desired factor is specified beforehand,
and we can say that a correct solution from an approximation algorithm is any
solution that is within that factor of the optimal solution. That is, for an approximation
algorithm we typically would like to show that the error produced by the algorithm
does not exceed a predefined limit. These types of algorithms are called
approximation algorithms and they are used in optimisation problems.

Consequently, we need to prove our algorithm is correct. Logically, we can test the
correctness of sample input instances. However, testing may not find obscure
(hidden) bugs, as such, it can be dangerous. An alternative method that can be used
alongside testing is a mathematical method called correctness proof. This involves
proving the correctness of the algorithms via mathematical methods. The most
common one used in verifying algorithms is called mathematical induction.

REVIEW OF MATHEMATICAL INDUCTION

Mathematical induction is a technique which is used to prove a statement, a formula


or a theorem asserted about every natural number. By “every” or “all” natural
number, we mean anyone that we specify.

For example,

1 + 3 + 5 + … + (2n – 1) = n2

This asserts that the sum of consecutive odd numbers from 1 to (2n – 1) is given by the
formula on the right. We want to prove that this is true for n = 1, n = 2, n = 3, and so
on. Now we can check the formula for any specific value of n, for example, n = 5:

1 + 3 + 5 + 7 + 9 = 25 = 52

This is true. It is also true for n = 6:

1 + 3 + 5 + 7 + 9 + 11 = 36 = 62

However, it would be impossible to check for every possible value of n. So, how do
we prove this rule for every value of n? We show that the statement is true for n = 1.
We then show that if the statement is true for any specific number k, (e.g., 21), then it
will be true for its successor (the next number), k + 1, (e.g., 22). Since the statement is
true when n = 1, it then follows that the statement is true for n = 1 + 1 = 2. Therefore,
it will be true for n = 2 + 1 =3, for n = 3 + 1 = 4, for n = 4 + 1 = 5, etc. Continuing in this

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 1
© Since 2020 Algorithm Correctness Lecture Notes
way shows it will be true for any number we specify. This method is called the principle
of mathematical induction.

Principle of Mathematical induction


The principle of mathematical induction can be summarised as follows.

If

1. a statement is true for n = 1 (without assuming any knowledge of other cases);

and

2. when a statement is true for n = k, then it will also be true for its successor, n =
k + 1;

then the statement will be true for every natural number n.

This can be informally defined as follows:

Let Sn be a statement concerning a positive integer n. Suppose that:

1. S1 is true;
2. for any positive integer k, n ≥ k, if Sk is true, then Sk+1 is also true;

Then Sn is true for every positive integer value of n.

To prove a statement by mathematical induction, we must prove parts 1 and 2 above.


In Step 1 we prove “the statement is true for n = 1”. This is called the Base Step or Basis,
it is also called the Anchor Step or Initial Step. Step 2, is called the Inductive Step. The
hypothesis of Step 2, “the statement is true for Sk” is called the induction (or inductive)
assumption or the induction (or inductive) hypothesis. The induction hypothesis is what
we assume when we prove a theorem by induction. Using the induction hypothesis as
a fact, we prove “the statement is true for Sk+1”.

Let S be the set of integers n for which a propositional function Pn is true. The basis of
mathematical induction verifies that 1 ∈ S. The inductive step shows that k ∈ S implies
k + 1 ∈ S. Thus, therefore the principle of mathematical induction proves S = ℕ. It
follows that Pn is true for all integers n ≥ 1.

The basis and inductive step, together, prove that

P1 ⟹ P2 ⟹ P3 ⟹ P4 ⟹ …

Therefore, Pn is true for all integers n ≥ 1.

We can compare induction to climbing an infinite ladder, when we climb the bottom
(first) rung, it makes it possible for us to climb the second rung. Climbing the second
rung in turn makes it possible to climb the third. Eventually, we can climb up as high
as we want. However, this will not happen unless these conditions are met:

• We must be able to climb unto the first rung. This is the basis.

The distance between adjacent rungs must be set up correctly. Otherwise, the next
rung may be too high for us to reach. Maintaining the right inter-rung distance ensures
that Pk ⇒ Pk +1 for each rung (integer) k ≥ 1. This is the induction step. To prove the
implication Pk ⇒ Pk + 1, we first assume Pk is true, then using it we prove Pk + 1 is also

Algorithms and Unit 2 Dr F. Adamu-Fika


2 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
true. This means, that if we can reach a particular rung of the ladder, then we can
reach the next rung.

Another analogy we can use to understand induction is the domino effect. Suppose
that we have infinitely many dominoes arranged in a line. When we make the first
domino falls, it will in turn make the second fall, the second will knock the third and so
on. Eventually, all our dominoes will fall. However, this will not happen unless these
conditions are met:

• The first domino must fall. This is the basis.


• The distance between adjacent dominoes must be set up correctly. Otherwise,
the next domino may be too far to be reached. Maintaining the right inter-
domino distance ensures that Pk ⟹ Pk + 1 for each domino (integer)k ≥ 1. This
is the induction step. To prove the implication Pk ⇒ Pk + 1, we first assume Pk is
true, then using it we prove Pk + 1 is also true. This means that if we can make
any particular domino falls, then we can make the next domino fall.

When we use mathematical induction, we must take care to be precise with our
assumptions.

• We say the assumption “holds for some integer k ≥ 1.” We DO NOT assume it
“holds for all integers k ≥ 1.” If we already know the statement holds true for all
k ≥ 1, then there is no need to prove it.
• Be sure to specify the requirement k ≥ 1. In our ladder-climbing metaphor
above, this ensures that we start climbing from the lowest rung.
• We are assuming that the statement is true when “n equals k”, and using it to
show that the statement holds true when “n equals k + 1”, thus we DO NOT say
“let n = k” or “let n = k + 1.” Saying “let” denotes assignment, it means we are
assigning the value of k and k + 1 to n.

Example 1. Prove that the sum of the first positive odd numbers is given by the
formula:

1 + 3 + 5 + … + (2n – 1) = n2

Proof. We will do Steps 1 and 2 above.

Let’s assume Sn = the sum of the first n odd numbers greater than 0.

Step 1:

First, we will show that the formula is true for the first odd number, n = 1.

Base Case (n = 1):

LHS = 2 ⋅ 1 – 1 = 1

RHS = 12 = 1

LHS = RHS

S1 holds true.

Step 2:

Next, we assume that the formula is true for n = k.

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 3
© Since 2020 Algorithm Correctness Lecture Notes
Induction hypothesis: we will assume that Sk = k2.

Sk: 1 + 3 + 5 + … (2k – 1) = k2

Assuming this induction hypothesis, we must then prove that the formula is true for the
successor of k, k + 1, that is for n = k + 1.

RTP: [RTP is a mathematical abbreviation that means Required to Prove]

This means we must show that Sk+1 = (k +1)2,

Sk+1: 1 + 3 +5 + … + (2(k + 1) – 1) = (k + 1)2

To do that we simply add the next term after k (successor of k) to both sides of the
induction hypothesis. By definition of Sn, the next term following k is the next odd
number in the sequence, which will be (2(k + 1) – 1) = (2k + 1).

Proof: So, we now have: Sk+1 = Sk + (2k + 1)

1 + 3 + 5 + … + (2k – 1) + (2k + 1) = k2 + (2k + 1)

= k2 + 2k + 1

= k2 + k + k + 1

= k(k + 1) + 1(k +1)

= (k + 1)(k + 1)

= (k + 1)2

This shows that Sk+1 = (k + 1)2 holds true. We have now fulfilled both conditions of the
principle of mathematical induction. So, we can conclude that since S1 = 1 and that
Sk ⟹ Sk+1 then Sn is equal to the sum of the first n odd numbers for all n > 0. That is, the
formula is true for every natural number.

We can use the summation notation (also called the sigma notation) to abbreviate a
sum. For example, the sum in Example 1 above can be written as:
𝑛

∑(2𝑖 – 1) = 𝑛2
𝑖=1

The letter i is the index of summation. By putting i = 1 under ∑ and n above, we declare
that the summation starts with i = 1, and ranges through i = 2, i = 3, i = 4 and so on,
until we reach i = n. The quantity that follows ∑ describes the pattern of terms that
we are adding in the summation.

Accordingly,
𝑛

∑(2𝑖 – 1) = (2 × 1 – 1) + (2 × 2 – 1) + (2 × 3 – 1)
𝑖=1

In general, the sum of the first n terms in a sequence, {ai, a2, a3, …} is denoted by
∑𝑛𝑖=1 𝑎𝑖 . Observe that in an induction proof, the link between Sk+1 and Sk is provided
by:

Algorithms and Unit 2 Dr F. Adamu-Fika


4 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
𝑘+1 𝑘

∑ 𝑎𝑖 = (∑ 𝑎𝑖 ) + 𝑎𝑘+1
𝑖=1 𝑖=1

Example 2. Use mathematical induction to show that, for all integers n ≥ 1,


𝑛
𝑛(𝑛 + 1)(2𝑛 + 1)
∑ 𝑖 2 = 12 + 22 + 32 + ⋯ + 𝑛2 =
6
𝑖=1

Let Pn represent the claim.

Basis: We prove it holds when .

1(1 + 1)(2 ⋅ 1 + 1)
12 =
6
LHS = 1
1⋅2⋅3 6
𝑅𝐻𝑆 = = =1
6 6
LHS = RHS

The identity holds for n = 1.

Inductive Step: We now show that if the proposition holds for k, then the proposition
holds for k + 1.

Inductive hypothesis: We assume it holds when for some integer .


𝑘
𝑘 (𝑘 + 1)(2𝑘 + 1)
𝑃𝑘 : ∑ 𝑖 2 = 12 + 22 + 32 + ⋯ + 𝑘 2 =
6
𝑖=1

RTP: We want to prove it still holds when n = k + 1.


𝑘+1
(𝑘 + 1)[(𝑘 + 1) + 1] [2(𝑘 + 1) + 1] (𝑘 + 1) (𝑘 + 2) (2𝑘 + 1)
𝑃𝑘+1 : ∑ 𝑖 2 = =
6 6
𝑖=1

Proof: From the inductive hypothesis, we find:

Pk+1 = Pk + (k + 1)2
𝑘+1 𝑘

∑ 𝑖 2 = (∑ 𝑖 2 ) + (𝑘 + 1)2
𝑖=1 𝑖=1

𝑘 (𝑘 + 1)(2𝑘 + 1)
= + (𝑘 + 1)2
6
𝑘 (𝑘 + 1)(2𝑘 + 1) + 6(𝑘 + 1)2
=
6
𝑘 (2𝑘 + 1) + 6(𝑘 + 1)
= (𝑘 + 1) [ ]
6

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 5
© Since 2020 Algorithm Correctness Lecture Notes
2𝑘 2 + 𝑘 + 6𝑘 + 6
= (𝑘 + 1) [ ]
6

2𝑘 2 + 7𝑘 + 6
= (𝑘 + 1) [ ]
6

2𝑘 2 + 4𝑘 + 3𝑘 + 6
= (𝑘 + 1) [ ]
6

2𝑘(𝑘 + 2) + 3(𝑘 + 2)
= (𝑘 + 1) [ ]
6
(𝑘 + 2)(2𝑘 + 3)
= (𝑘 + 1) [ ]
6
(𝑘 + 1)(𝑘 + 2)(𝑘 + 3)
=
6

Conclusion: Therefore, the identity also holds when n = k + 1. This completes the
induction.

Even though the basis often starts with n = 1, this is not necessarily true for all cases, it
may begin with n = 0 and possibly with any fixed natural number n = N, establishing
the truth of the statement for all natural numbers n ≥ N. Some statements Pn are not
true for the first few values of n, but are true for all values of n greater than or equal to
some fixed integer n0. This means that in the base step, we can start with n being any
integer n0, n = n0.

Generalisation of Principle of Mathematical Induction


To prove that a proposition function Pn is true for all integers n ≥ n0, follow the following
steps:

• Verify that 𝑃𝑛0 is true.


• Assume that Pk is true for some integer k ≥ n0.
• Show that Pk+1 is also true.

The major difference is in the basis step: we need to verify that 𝑃𝑛0 is true. Also, in the
inductive hypothesis, we need to emphasise that k ≥ n0. Thus, the principle can be
formally defined as:

Let n0 be any integer, and let Pn be a proposition (about integers) such that 𝑃𝑛0 is true,
and for each n ≥ n0, if Pk holds for some integer k such that k ≥ n0 then so does Pk+1 ;
then Pn holds for all integers n ≥ n0.

Example 3. Use mathematical induction to show that, for all integers n ≥ 0,


𝑛
1 𝑛+1
∑ 4𝑖 = 1 + 4 + 42 + ⋯ + 4𝑛 = (4 – 1)
3
𝑖=0

Basis: We prove the proposition Pk holds when n = 0.


1
40 = [(40+1 ) − 1]
3

Algorithms and Unit 2 Dr F. Adamu-Fika


6 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
LHS = 1
1 1 1
𝑅𝐻𝑆 = [ 4 − 1] = [3] = 1
3 3
LHS = RHS

The identity holds for n = 0.

Inductive Step: We will show that if the proposition holds for n = k, then the proposition
holds for n = k + 1.

Inductive hypothesis: We assume it holds when n = k for some integer n ≥ 0.


𝑘
1 𝑘+1
𝑃𝑘 : ∑ 4𝑖 = 1 + 4 + 42 + ⋯ + 4𝑛 = (4 – 1)
3
𝑖=0

RTP: We want to prove it still holds when n = k + 1.


𝑘+1
1 (𝑘+1)+1 1
𝑃𝑘+1 : ∑ 4𝑖 = (4 – 1) = (4𝑘+2 – 1)
3 3
𝑖=0

Proof: From the inductive hypothesis, we find:

Pk + 1 = Pk + 1 + 4k + 1
𝑘+1 𝑘

∑ 4𝑖 = (∑ 4𝑖 ) + 4𝑘+1
𝑖=0 𝑖=0

1
= (4𝑘+1 – 1) + 4𝑘+1
3

1
= [(4𝑘+1 – 1) + 3 ⋅ 4𝑘+1 ]
3

1
= [(4𝑘+1 – 1) + (4 − 1) ⋅ 4𝑘+1 ]  [“3” is the same thing as “4 – 1”]
3

1
= [4𝑘+1 – 1 + 4 ⋅ 4𝑘+1 – 4𝑘+1 ]
3

1
= [4 ⋅ 4𝑘+1 + 4𝑘+1 – 4𝑘+1 – 1]  [rearranging the equation gives us]
3

1
= [41 ⋅ 4𝑘+1 – 1]  [“4” is the same thing as “41”]
3

1
= [4(𝑘+1)+1 – 1]
3

1 𝑘+2
= [4 – 1]
3
Conclusion: Therefore, the identity also holds when n = k + 1. This completes the
induction.

Besides identities, there are two other broad proposition structures that can be proved
by induction, divisibility and inequality propositions.

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 7
© Since 2020 Algorithm Correctness Lecture Notes
Example 4. Prove that 5n – 2n is divisible by 3 for all integers n ≥ 1.

Base case (n = 1):

P1: 51 – 21 = 5 – 2 = 3, is divisible by 3.

The claim holds for n = 1.

Inductive Step: We will show that if the proposition holds for n = k, then the proposition
holds for n = k + 1.

Induction hypothesis: We assume the claim holds when n = k for some integer n ≥ 1.

Pk: 5k – 2k is divisible by 3.

By definition, it is clear we are assuming is a multiple of 3.

RTP: Again, by definition, we want to show that

Pk + 1: 5k + 1 – 2k + 1 is divisible of 3.

The induction hypothesis can be rewritten as:

5k – 2k = 3q, for some integer q, where q ∈ ℤ.

and can be written as:

5k + 1 – 2k + 1 = 3Q, for some integer Q, where Q ∈ ℤ.

Since we have two distinct multiples of 3, we needed to write them as above to


distinguish between the two. By using the lowercase and uppercase of the same
letter, we indicate that they are different values, yet because they come from the
same letter, they both share some common attribute, in this case, being the quotients
when the respective values are divided by 3.

To show that the claim holds for Pk+1 holds, we need to make use of 5k – 2k = 3q from
the induction hypothesis. This means we need to connect the expression 5k + 1 – 2k + 1
to the expression 5k – 2k. Now, we proceed with our proof.

Proof: Using the inductive hypothesis, we find:

5k + 1 – 2k + 1 = 5 . 5k – 2 . 2k  [laws of exponents]

= 3 . 5k + 2 . 5k – 2. 2k  (5a = 3a + 2a)

= 3 . 5k + 2 . (5k – 2k)

= 3 . 5k + 2 . (3q)  [by definition]

= 3 . (5k + 2q)

= 3Q [(5k + 2q) is clearly an integer]

This completes the induction.

Using induction to prove inequalities requires more work.

Example 5. Use mathematical induction to show that nn ≥ 2n, for all integers n ≥ 2.

Algorithms and Unit 2 Dr F. Adamu-Fika


8 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
Base case (n = 2): We show that inequality holds for the base case.

LHS = 22 = 2 × 2 = 4

RHS = 22 = 2 × 2 = 4

LHS = RHS

This obviously holds.

Inductive Step: We will show that if the proposition holds for n = k, then the proposition
holds for n = k + 1.

Induction hypothesis: We assume it holds when n = k for some integer n ≥ 2.

Pk: kk ≥ 2k

RTP: We want to show that it still holds when n = k + 1,

Pk+1: (k + 1)k+1 ≥ 2k+1.

Proof:

Since, k ≥ 2, it follows from induction hypothesis,

(k + 1)k+1 ≥ kk+1 [Since k ≥ 2, ∴k + 1 ≥ 3 > 2]

= k ⋅ kk [kk + 1 = k ⋅ kk, by laws of exponent]

≥ 2 ⋅ 2k [k ≥ 2, by induction hypothesis kk ≥ 2k]

≥ 2k+1

Thus,

(k + 1)k+1 ≥ kk+1 ≥ 2 ⋅ 2k ≥ 2k+1

∴ (k + 1)k+1 ≥ 2k+1

Conclusion: Therefore, the inequality still holds when n = k + 1. This completes the
induction.

Example 6. Use mathematical induction to show that n! ≥ 2n for all integers n ≥ 4.

Base case : We show that inequality holds for the base case.

LHS = 4! = 4 × 3 × 2 × 1 = 24

RHS = 24 = 2 × 2 × 2 × 2 = 16

LHS > RHS

This holds for n = 4.

Inductive Step: We will show that if the proposition holds for n = k , then the proposition
holds for n = k + 1.

Induction hypothesis: We assume it holds when n = k for some integer n ≥ 4.

Pk: k! ≥ 2k.

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 9
© Since 2020 Algorithm Correctness Lecture Notes
RTP: We want to show that it still holds when n = k + 1,

Pk+1: (k + 1)! ≥ 2k+1.

Proof:

(k + 1)! = k! ⋅ (k + 1) [∵ (k + 1)! = (k + 1) ⋅ k!]

≥ 2k ⋅ (k + 1) [by inductive hypothesis]

≥ 2 ⋅ 2k [since k ≥ 4, ∴ k +1 ≥ 5 >2]

= 2k+1 [by laws of exponents]

Thus

(k + 1)! ≥ 2k ⋅ (k + 1) ≥ 2 ⋅ 2k

∴ (k + 1)! ≥ 2k+1

Conclusion: Therefore, the inequality still holds when n = k + 1. This completes the
induction.

Sometimes in an induction proof, it is hard to show that Pk implies Pk+1. It may be easier
to show some “lower” Pm (with m = k) implies Pk+1. For such situations, there is a slight
variant of induction called strong induction, in which we assume that the statement
holds for all values preceding k. This provides us with more information to use when
trying to prove the statement.

Strong Induction
Strong induction is quite similar to regular induction. However, there is a difference in
the induction hypothesis. Normally, in standard induction, we assume that Pk is true to
prove that Pk+1. In strong induction, we assume that all of P1, P2, P3, …, Pk are true to
prove Pk+1. Returning to our domino effect analogy above. Suppose that we still have
infinitely many dominoes arranged in a line. However, this time, the weight of the kth
domino is not enough to knock down the (k + 1)th domino. Knocking down the (k +
1)th domino required the weight of all the dominoes before it. Even with this additional
constraint, we can prove that all the dominoes will fall if we are able to knock down
the first domino. Thus, in standard induction the induction hypothesis is Pk , and the
inductive step consists in proving that Pk ⟹ Pk+1. Whereas in strong induction the
induction hypothesis is 𝑃𝑛0 ∧ 𝑃𝑛0 +1 ∧ 𝑃𝑛0+2 ∧ … ∧ Pk, and the induction step consists in
proving that 𝑃𝑛0 ∧ 𝑃𝑛0 +1 ∧ 𝑃𝑛0 +2 ∧ … ∧ Pk ⟹ Pk+1. This variant is called strong induction
because we use more statements in the induction hypothesis.

To prove that a proposition function Pn is true for all integers n ≥ n0, follow the following
steps:

1. Verify that 𝑃𝑛0 is true.


2. Assume that all of 𝑃𝑛0 , 𝑃𝑛0 +1 , 𝑃𝑛0 +2 , …, Pk are true for some integer k ≥ n0.
3. Show that Pk+1 is also true.

Thus, the principle of strong induction can be formally defined as:

Let n0 be any integer, and let Pn be a proposition (about integers) such that 𝑃𝑛0 is true
for each n ≥ n0, if Pk holds for all integers k such that n0 ≤ k ≤ n then so does Pk+1; then
Pn holds for all integers n ≥ n0.

Algorithms and Unit 2 Dr F. Adamu-Fika


10 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
Example 7. Let an be the sequence defined by a1 = 1, a2 = 8, an = an–1 + 2an–2 for n ≥
3. Prove that an = 3 ⋅ 2n–1 + 2(–1)n for all n ∈ ℤ+.

We work by strong induction on n.

Base case: We show the proposition holds for the base case(s).

When n = 1:

a1 = 3 ⋅ 21–1 + 2(–1)1

LHS = a1 = 1

RHS = 3 ⋅ 21–1 + 2(–1)1 = 3 ⋅ 20 + 2(–1)1 = 3 ⋅ 1 + 2(–1) = 3 – 2 = 1

∴ LHS = RHS

The proposition holds for n = 1.

When n = 2:

a2= 3 ⋅ 22–1 + 2(–1)2

LHS = a2 = 8

RHS = 3 ⋅ 22–1 + 2(–1)2 = 3 ⋅ 21 + 2(–1)2 = 3 ⋅ 2 + 2(1) = 6 + 2 = 8

∴ LHS = RHS

The proposition holds for n = 2 as well.

Inductive Step: Let k ∈ ℤ+ with k ≥ 3 given and assume the proposition is true for n = 1,
2, 3, …, k, then ak+1 = ak + 2ak–1.

Induction hypothesis: For the strong inductive hypothesis, suppose that for some k ≥ 3
we assume that am = 3 ⋅ 2m–1 + 2(–1)m for all 1 ≤ m ≤ k.

RTP: We prove that:

ak+1 = 3 ⋅ 2k + 2(–1)k+1

Proof:

ak+1 = ak + 2ak–1

[by recurrence relation for an]

= (3 ⋅ 2𝑘–1 + 2(– 1)𝑘 ) + (2 (3 ⋅ 2𝑘–2 + 2(– 1)𝑘–1 ) )

[by induction hypothesis]

= 3 ⋅ 2𝑘–1 + 2 ⋅ 3 ⋅ 2𝑘–2 + 2(– 1)𝑘 + 2 ⋅ 2(– 1)𝑘–1

= [3 ⋅ 2𝑘–1 + 2 ⋅ 3 ⋅ 2𝑘–2 ] + [2(– 1)𝑘 + 2 ⋅ 2(– 1)𝑘–1 ]

= 3 (2𝑘–1 + 2 ⋅ 2𝑘–2 ) + 2 ((– 1)𝑘 + 2(– 1)𝑘–1 )

= 3 (2𝑘–1 + 2𝑘–2+1 ) + 2 ((– 1)(– 1)𝑘−1 + 2(– 1)𝑘–1 )

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 11
© Since 2020 Algorithm Correctness Lecture Notes
[by laws of exponents ∵ (–1)k = ((– 1)(– 1)𝑘−1 )]

= 3 (2𝑘–1 + 2𝑘–2+1 ) + 2 ((– 1)(– 1)𝑘−1 + 2(– 1)𝑘–1 )

= 3 (2𝑘–1 + 2𝑘–1 ) + 2(– 1)𝑘−1 ((– 1) + 2)

= 3 (2 ⋅ 2𝑘–1 ) + 2(– 1)𝑘−1 (– 1 + 2)

[2k–1 + 2k–1 = 2(2k–1 ) ∵ a + a = 2a]

= 3 (2𝑘–1+1 ) + 2(– 1)𝑘−1 (1)

= 3 (2𝑘 ) + 2(– 1)𝑘−1 ((−1) ⋅ (−1))

[∵ (1) = (–1) × (–1)]

= 3 (2𝑘 ) + 2 ((– 1)𝑘−1 (−1) (−1))

= 3 ⋅ 2𝑘 + 2 (– 1)𝑘+1

[by laws of exponents ∵ ((– 1)(– 1)𝑘−1 ) = (−1)𝑘 and


((– 1)(– 1)𝑘 ) = (−1)𝑘+1 ]

Therefore, the proposition holds for k + 1 as well.

Conclusion:

Hence, by the Principle of Strong Induction, we have an = 3 ⋅ 2n–1 + 2(–1)n for all n ∈
ℤ +.

Strong induction is sometimes called the second principle of mathematical induction


or complete induction.

A variation of mathematical induction, called structural induction, is used in computer


science. This has been extended to prove statements about more general well-
founded structures, such as graphs. It is an inference rule used in formal proofs, and
to an extent, it is the foundation of all correctness proofs for computer programs.

Now, let’s practice some proofs!

Practice: Induction Proofs


Prove these identities are true for every natural number:

1. ∑𝑛𝑖=1 2𝑖 = 𝑛(𝑛 + 1)
1
2. ∑𝑛𝑖=1(2𝑖) 2 = 3 2𝑛(𝑛 + 1)(2𝑛 + 1)
3. The sum of the square of the first 2n positive integers for all n ≥ 1 is given by:
𝑛(2𝑛+1)(4𝑛+1)
∑2𝑛 2
𝑖=1 𝑖 = .
3

Prove the following statements using mathematical induction:

4. For any positive integer n, 9n – 5n is divisible by 4.


5. Let x be a positive real number. Then for any n ∈ ℕ, we have (1 + x)n ≥ 1 + nx.
6. For all n ≥ 5, 4n < 2n.

Prove the following statements using strong induction:

Algorithms and Unit 2 Dr F. Adamu-Fika


12 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
7. Given n ∈ ℕ, define an recursively as follows:
a0 = 1, a1 = 3, an = 2an–1 – an–2 for n ≥ 2.
Prove that for all n ≥ 0, an = 2n + 1.
8. Given n ∈ ℕ, define gn recursively as follows:
g1 = 1, g2 = 2, gn = (n3 – 3n2 + 2n)gn–3 for n ≥ 4.
Prove that for all n ≥ 1, gn = n!.
9. For all n ≥ 0, 10 | (n2 – n).
10. Prove that for every positive integer n ≥ 4, it can be formed only using 2 and 5.
11. Prove that for every positive integer n ≥ 8, it can be formed only using 3 and 5.

PROVING CORRECTNESS OF AN ALGORITHM

We will be doing a lot of strong induction proofs to check our algorithms that are
recursive in nature. For those that are iterative in nature, we are going to prove their
correctness by induction on their loop invariants.

Correctness of Recursive Algorithms


To prove the correctness of a recursive algorithm:

• Prove it by induction on the “size” of the problem being solved (e.g., size (or
length) of an array, number of bits in an integer, etc).
• The base of recursion is the base of induction.
• Inductive step: Assume that the recursive calls work correctly and use this
assumption to prove that the current call works correctly.
• Need to prove that recursive calls are given sub-problems, i.e., no infinite
recursion (often trivial).

Now, let's prove the correctness of our recursive algorithms from our algorithm
examples in Unit 1.

Recursive POW2(n)
Claim:

For all n ≥ 0, POW2(n) returns 2n.

Basis:

For n = 0, POW2(0) returns as claimed.

For n = 1, POW2(1) returns as claimed.

For n = 2, POW2(2) returns as claimed.

Induction hypothesis:

Assume that k ≥ 2 and for all 0 ≤ m ≤ k, POW2(m) returns 2m.

RTP:

POW2(k + 1) returns 2k+1.

Proof:

What does POW2(k + 1) actually return?

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 13
© Since 2020 Algorithm Correctness Lecture Notes
2 × POW2(k)

= 2 × 2k [by induction hypothesis]

= 2k+1

Conclusion:

Therefore, for all n ≥ 0, POW2(n) returns 2n.

Recursive EVENSUM(n)
Claim:

For all n ≥ 1, EVENSUM(n) returns ∑𝑛𝑖=1(2𝑖 – 2) .

Basis:

For n = 1, ( ) returns 0 as claimed.

For n = 2, ( ) returns 2 as claimed.

Induction hypothesis:

Assume that k ≥ 2 and for all 1 ≤ m ≤ k, EVENSUM(m) returns ∑𝑚


𝑖=1(2𝑖 – 2).

RTP:

EVENSUM(k + 1) returns ∑𝑘+1


𝑖=1 (2𝑖 – 2).

What does EVENSUM(k + 1) actually return?

EVENSUM(k) + (2 (k + 1) – 2)

= ∑𝑘𝑖=1(2𝑖 – 2) + (2 (𝑘 + 1) + 2) [by induction hypothesis]

= ∑𝑘+1
𝑖=1 (2𝑖 – 2)

Conclusion:

Therefore, for all n ≥ 1, EVENSUM(n) returns ∑𝑛𝑖=1(2𝑖 – 2).

Recursive FIB(n)
Claim:

For all n ≥ 0, FIB(n) returns Fn.

Basis:

For n = 0, FIB(0) returns 0 as claimed.

For n = 1, FIB(1) returns 1 as claimed.

Induction hypothesis:

Assume that k ≥ 2 and for all 0 ≤ m ≤ k, FIB(m) returns Fm.

RTP:

FIB(k + 1) returns Fk+1.

Algorithms and Unit 2 Dr F. Adamu-Fika


14 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
What does FIB(k + 1) actually return?

FIB(k) + FIB(k – 1)

= Fk + Fk–1 [by induction hypothesis]

= Fk+1

Conclusion:

Therefore, for all n ≥ 0, FIB(n) returns Fn.

Correctness of Iterative Algorithms


To prove the correctness of an iterative algorithm, we perform these steps:

1. Analyse the algorithm one loop at a time, starting at the innermost loop in case
of nested loops.
2. For each loop devise a loop invariant that remains true through each iteration
of the loop and captures the “progress” made by the loop.
3. Prove that the loop invariants hold.
4. Use the loop invariants to prove that the algorithm terminates.
5. Use the loop invariants to prove that the algorithm computes the correct result.

Loop Invariants
An invariant is a statement about program vari-ables that is true every time the
execution of the program reaches the invariant. A loop invariant is a statement about
program variables that is true before and after each iteration of a loop. Knowing the
invariant(s) of the loop is essential in understanding its effect.

A good loop invariant should satisfy three properties:

1. Initialisation: The loop invariant must be true before the first execution of
the loop.

2. Maintenance: If the loop invariant is true before an iteration of the loop, it


should remain true before the next iteration.

3. Termination: The loop terminates. And when the loop is terminated, the loop
invariant, along with the reason that the loop terminated, gives us a useful
property. That is, the loop invariant should tell us something useful that shows
the algorithm is correct, i.e., something that helps us understand the algorithm.

A loop invariant is usually placed at the beginning of the loop. A well-chosen loop
invariant is useful both when designing, testing, and modifying code. It also serves as
documentation and can be the foundation of correctness proof.

For example, let’s consider our iterative EVENSUM(n) algorithm in Unit 1, we can
place a loop invariant at the start of the loop, before any other statements. This
invariant will assert the value of the variable at every execution of the loop:

1. while( i ≤ n )
2. //Invariant: even = 0 + 2 + 4 + … + (2i – 2)
3. even = even + (2i – 2)
4. i=i+1

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 15
© Since 2020 Algorithm Correctness Lecture Notes
1. Initially, before the codes of the loop get executed, even is empty and its value
will be zero because even = 0 and i = 1.

2. Supposing the invariant hold true before the ith iteration, it will also hold true
after this iteration since the loop adds 2i – 2 to even and increment by one.

3. Just before the loop terminates, the invariant states that:

even = 0 + 2 + 4 + … + (2i – 2), this is exactly what’s required for the algorithm
to be correct.

You will notice the three steps above, constitute an induction proof, which shows
when the program leaves the loop even = 0 + 2 + 4 + … + (2i – 2).

Notation
To explain how induction on loop variants works, we will look at our simple one-loop
iterative algorithms in the examples above. The value in identifier j immediately after
the kth iteration of the loop is denoted jk (k = 0 means immediately before entering
the loop for the first time). For example, j8 denotes the value of the identifier j after 8th
time around the loop.

Iterative POW2(n)
Claim:

For all natural numbers, POW2(n) returns 2n.

Let’s find a pattern as the programme variables in POW2(n) change from one
iteration of the loop to the next.

i0 = 1, i1 = 1, i2 = 2, i3 = 3 …

Observing the progression of i through the loop iteration we can see a


pattern where the value of i at the end of the kth iteration is k.

Thus, ik = k

power0 = 1 = 20, power1 = 2 = 21, power2 = 4 = 22, power3 = 8 = 23 …

Thus, powerk = 2k

The Loop Invariants of POW2(n)

For all natural numbers k ≥ 0, ik = k, and powerk = 2k.

The proof is by induction on k.

Basis:

When n = 0

k = 0, since i0 = 0 [From the loop invariants we know that ik = k].

So, power0 = 20 = 1.

Induction hypothesis:

Now suppose that k ≥ 0, ik = k, and powerk = 2k.

Algorithms and Unit 2 Dr F. Adamu-Fika


16 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
RTP:

ik+1 = ik + 1, and powerk+1 = 2k+1.

Proof:

ik+1 = ik + 1

=k+1 [by induction hypothesis]

powerk+1 = 2 × powerk

= 2 × 2k [by induction hypothesis]

= 2k+1

Conclusion:

The loop invariant holds.

Correctness Proof for POW2(n) terminating with the correct result.

Claim:

POW2(n) terminates with power containing 2n.

Termination:

Since ik+1 = ik + 1, eventually i will be equal n and the loop will terminate.
Suppose this happens after t iterations. Since it = n and it = t [∵ ik = k ], we can
conclude that t = n [∵ it = n = t].

Results:

By the loop invariant,

powert = 2t = 2n [∵ t = n].

Note that the proof by induction is the initialisation and maintenance phase of the
loop invariant.

Iterative EVENSUM(n)
Claim:

EVENSUM(n) returns ∑𝑛𝑖=1(2𝑖 – 2).

To determine the loop invariants let’s for EVENSUM(n), let’s find a pattern of
its programme variables within the loop through each execution of the
loop.

i0 = 1 = 0 + 1, i1 = 2 = 1 + 1, i2 = 3 = 2 + 1 …

Thus, ik = k + 1

even0 = 0

even1 = 0 = even0 + (2i0 – 2) = even0 + [2(1)- 2]

even2 = 2 = even1 + (2i1 – 2) = even1 + [2(2)- 2]

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 17
© Since 2020 Algorithm Correctness Lecture Notes
even3 = 2 = even2 + (2i2 – 2) = even2 + [2(3)- 2]

Thus, evenk = evenk-1 + (2ik–1 – 2) = evenk–1 + [2(k)- 2]

= ∑𝑘𝑖=1(2𝑖 – 2)

The Loop Invariant of EVENSUM(n)

For all natural numbers , ik = k + 1, and evenk = ∑𝑘𝑖=1(2𝑖 – 2).

The proof is by induction on .

Basis:

When n = 1

k = 0 is trivial, since i0 = 1 [From the loop invariants we know that ik = k + 1].

So, even0 = ∑0𝑖=1(2𝑖 – 2) = 0.

[Trivial is a solution that is simple and of little interest. Often, solutions or examples
involving 0 are considered trivial. Non-zero solutions or examples are
considered non-trivial. For example,3x + y = 0 has the trivial solution x = 0, y =
0]

Induction hypothesis:

Now, assume that k ≥ 0, ik = k + 1, and evenk = ∑𝑘𝑖=1(2𝑖 – 2).

RTP:

ik+1 = (k + 1) + 1 = k + 2, and evenk+1 = ∑𝑘+1


𝑖=1 (2𝑖 – 2).

Proof:

ik+1 = ik + 1

=k+1+1 [by induction hypothesis, ik = k + 1]

=k+2

evenk+1 = evenk + (2ik – 2) [EVENSUM[k + 1] returns evenk + (2ik – 2)]

= ∑𝑘𝑖=1(2𝑖 – 2) + (2 (𝑘 + 1) – 2) [by induction hypothesis:

evenk = ∑𝑘𝑖=1(2𝑖 – 2) and ik = k + 1]


𝑖𝑘+1
= ∑𝑖=1 (2𝑖 – 2)

Conclusion:

The loop invariant holds.

Correctness Proof for EVENSUM(n) terminating with the correct result.

Claim:

EVENSUM(n) terminates with containing ∑𝑛𝑖=1(2𝑖 – 2).

Algorithms and Unit 2 Dr F. Adamu-Fika


18 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
Termination:

Since ik+1 = ik + 1, eventually i will equal n + 1 and the loop will terminate.
Assume this happens after iterations. Since it = n + 1 and it = t + 1 [∵ ik = k +
1], we can conclude that t = n [∵ it = n + 1 = t + 1].

Results:

By the loop invariant,

event = ∑𝑡𝑖=1(2𝑖 – 2) = ∑𝑛𝑖=1(2𝑖 – 2) [∵ t = n].

Note that the proof by induction is the initialisation and maintenance phase of the
loop invariant.

Iterative FIB(n)
Claim:

FIB(n) returns Fn.

Facts about FIB(n) after observing the progression of the programme


variables through the execution of the loops.

i0 = 2 = 0 + 2, ii = 3 = 1 + 2,

i2 = 4 = 2+ 2, i3 = 5 = 3 + 2, …

Thus, ik = k + 2

a0 = 0 = F0, a1 = b0 = 1 = F1,

a2 = b1 = 1 = F2, a3 = b2 = 2 = F3, ….

Thus, ak = Fk

b0 = 1 = F1, b1 = fib1 = 1 = F2,

b2 = fib2 = 2 = F3, b3 = fib2 = 3 = F4, …

Thus, bk = Fk+1

The Loop Invariant of FIB(n)

For all natural numbers k ≥ 0, ik = k + 2, ak = Fk, and bk = Fk+1.

The proof is by induction on k.

Basis:

k = 0, is trivial, since i0 = 2, a0 = 0 = F0, and b0 = 1 = F1.

Induction hypothesis:

Now suppose that k ≥ 0, ik = k + 2, ak = Fk, and bk = Fk+1.

RTP:

ik+1 = k + 3, ak+1 = Fk+1, and bk+1 = Fk+2.

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 19
© Since 2020 Algorithm Correctness Lecture Notes
Proof:

ik+1 = ik + 1

= (k + 2) + 1 [by induction hypothesis]

=k+3

ak+1 = bk

= Fk+1 [by induction hypothesis]

bk+1 = fibk+1

= ak + bk

= Fk + Fk+1 [by induction hypothesis]

= Fk+2

Conclusion:

The loop invariant holds.

Correctness Proof for FIB(n) terminating with the correct result.

Claim:

FIB(n) terminates with b containing Fn.

The claim is obviously true if n = 0 or n = 1. If n = 1, then we enter the while-


loop.

Termination:

Since ik+1 = ik + 1, eventually i will equal n + 1 and the loop will terminate.
Suppose this happens after t iterations. Since it = n + 1 and it = t + 2, we can
conclude that t = n – 1.

Results:

By the loop invariant, bt = Ft+1 = Fn. [∵ t + 1 = n – 1 + 1 = n]

Note that the proof by induction is the initialisation and maintenance phase of the
loop invariant.

UNIT GOALS

At this point (after completing this unit) you should:

• Understand what it means for an algorithm to be correct.


• Be able to describe the behaviour of algorithms.
• Know how to specify good loop invariants.
• Know how to prove the correctness of both iterative and recursive algorithms
by induction.

Algorithms and Unit 2 Dr F. Adamu-Fika


20 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
EXERCISES

1. Using recursion solves these problems and proof they are correct.
a. Computing the nth factorial.
b. Finding the maximum number in a list of numbers.
c. Computing the greatest common divisor.

2. Solve these problems using non-recursive methods and proof their solutions are
correct.
a. Computing the nth factorial.
b. Finding the maximum number in a list of numbers.
c. Multiplying two square matrices of integers.

3. Write pseudocode for linear search, which scans through a list, looking for a
value. Using loop invariants, prove that your algorithm is correct. Make sure that
your loop invariant fulfils the three necessary properties.

4. The two algorithms below are both intended to calculate the factorial of any
positive integer n. For example, if n = 5, the algorithm should calculate the
factorial of 5 and return 120, from 1 × 2 × 3 × 4 × 5.
I.
Algorithm Factorial(n)
Input: n, a natural number
Output: factorial, the factorial of n (n!)
START
1. factorial = 1
2. i = 1
3. while( i ≤ n )
4. factorial = factorial – i
5. i=i +1
6. return factorial
END
II.
Algorithm Factorial(n)
Input: n, a natural number
Output: factorial, the factorial of n (n!)
START
1. factorial = 1
2. i = 1
3. while( i ≤ n )
4. factorial = factorial * i
5. i=i + 1
6. return factorial
END
Which statements best describe the behaviour of the algorithms?
a. Algorithm (I) calculates the correct factorial, but algorithm (II) does not.
b. Algorithm (II) calculates the correct factorial, but algorithm (I) does not.
c. Both algorithms calculate the correct factorial.
d. Neither algorithm calculates the correct factorial.

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 21
© Since 2020 Algorithm Correctness Lecture Notes
5. An algorithm will be used to calculate the difference between the smallest and
largest values in a list. For the list of[12, 1, 5, 9], it should calculate a difference
of 11.
There are two proposals for the algorithm:
(i) Set minVal to the first value in the list and maxVal to the last value in the
list. Iterate through each number in the list. If the number is greater than
maxVal, store it in maxVal. If the number is less than minVal, store it in
minVal. After the loop, set maxDiff to the difference between maxVal
and minVal.
(ii) Set minVal to 1000 and maxVal to 0. Iterate through each number in
the list. If the number is greater than maxVal, store it in maxVal. If the
number is less than minVal, store it in minVal. After the loop, set maxDiff
to the difference between maxVal and minVal.

Which of these statements are true about these algorithms?


I. Algorithm (i) does not work on lists where the smallest value is at the
start of the list or the largest value is at the end of the list.
II. Algorithm (ii) does not work on lists that contain all negative numbers
or all numbers over 1000.
a. I
b. II
c. Both I and II
d. Neither I nor II

6. A programmer develops an algorithm HasDoubleDigits([…]), which should


return true if it ever finds a double-digit somewhere, i.e., the same number in
two consecutive positions. For example, HasDoubleDigits ([1, 5, 3, 3]) should
return true , but HasDoubleDigits ([3, 1, 5, 3]) should return false.
Algorithm HasDoubleDigits(A)
Input: A, a list of numbers
Output: true if it has double digits otherwise false.
START
1. i = 0
2. while( i < n – 2 )
3. if( A[i] == A[i + 1] )
4. return true
5. i = i + 1
6. return false
END
The programmer tests it with HasDoubleDigits ([7, 4, 9, 9, 7]) and sees that it
does indeed return true.
Can the programmer conclude that the procedure works correctly for all
inputs?
a. Yes, that test case is sufficient proof that the procedure works as
intended for all possible lists of numbers.
b. No, since the expected return value is false for that test case.
c. No, but the programmer can conclude that it always works for lists that
contain a single instance of a double number.
d. No, they've only verified that it works for that test case. Additional
analysis and reasoning are necessary.

Algorithms and Unit 2 Dr F. Adamu-Fika


22 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
7. The goal of the algorithm ReportDisallowed is to iterate through a list of strings
and report how many of them contain disallowed characters ("!" and "&").

For example, for the given list [“hello”, “world!”, “black&white”], it should return
the number 2 since two of the three strings in the list contain disallowed
characters.
Algorithm ReportDisallowed(S)
Input: S, an array of strings
Output: haDisallowed, number of strings that have disallowed
characters.
START
1. haDisallowed = 0
2. for( i = 0; i < S.length; i++ )
3. if( S[i] contains “!” OR S[i] contains “&” )
4. haDisallowed = haDisallowed + 1
5. return haDisallowed
6. return haDisallowed
END
A programmer tests the procedure with various inputs and finds multiple cases
where it does not produce the expected output.
Which calls to ReportDisallowed([…]) will return an incorrect number?
a. ReportDisallowed([“gov&politics”, “h1st0ry”, “l1t3r@ture”])
b. ReportDisallowed([“b!ology”, “ch3m1stry”, “physics”])
c. ReportDisallowed([“@lgebra”, “log&exp”, “factorials!”])
d. ReportDisallowed([“algor!thm-analysis”, “arrays&lists”, “complex1ty”])

8. A programmer develops the procedure MaxPairSum([…]) to compute the sum


of subsequent pairs in a list of numbers and return the maximum sum.
For example, for a list of [7, 3, 6, 1] , the sums of subsequent pairs are 10(from
7 + 3), 9 (from 3 + 6), and 7 (from 6 + 1). The procedure should return the
maximum sum of 10.
Algorithm MaxPairSum(A)
Input: A, an array of numbers
Output: maxSum, maximum sum of subsequent pairs.
START
1. i = 0
2. maxSum = 0
3. while( i < A.length )
4. sum = A[i] + A[i + 1]
5. if( sum > maxSum )
6. maxSum = sum
7. i = i + 1
8. return maxSum
END
The programmer tests with MaxPairSum([7, 2, –2, 5, 6]) and sees a return value
of 11
Can the programmer conclude that the procedure works correctly for all
inputs?
a. Yes, that test case is sufficient proof that the procedure works as
intended for all possible lists of numbers.
b. No, since the expected return value is not 9 for that test case.

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 23
© Since 2020 Algorithm Correctness Lecture Notes
c. No, but the programmer can conclude that it works for all lists of
integers.
d. No, they've only verified that it works for that test case. Additional
analysis and reasoning are necessary.

9. The goal of the procedure longestWord is to return the longest word in a given
list of words. For example, if it's given the list [“superhero”, “captain”, “marvel”] ,
it should return “superhero” as the longest word.
Algorithm LongestWord(S)
Input: S, an array of strings
Output: longWord, the longest word in array S.
START
1. longWordLen = 0
2. longWord = “”
3. for( i = 0; i < S.length; i++ )
4. if( S[i].length > longWordLen )
5. longWord = S[i]
6. longWordLen = S[i].length
7. return longWord
8. return longWord
END
A programmer tests the algorithm with various inputs and finds that there are
multiple cases where it does not produce the expected output.
Which calls to LongestWord([…]) do not actually return the longest word?
a. LongestWord([“computer”, “phone”, “tablet”])
b. LongestWord([“triangle”, “circle”, “square”])
c. LongestWord([“pear”, “banana”, “pineapple”])
d. LongestWord([“blue”, “purple”, “red”])

10. The two algorithms below are both intended to calculate the product of
squares from 1 to n, where n is any positive integer. For example, if n is 3, the
algorithms should calculate a product of 36, from 12 × 22 × 33.
I.
Algorithm ProductSquare(n)
Input: n, a natural number
Output: product, the product of squares from 1 to n
START
1. product = 0
2. i = n
3. while( i > 0 )
4. product = product * ( i * i )
5. i = i – 1
6. return product
END
II.
Algorithm ProductSquare (n)
Input: n, a natural number
Output: product, the product of squares from 1 to n
START
1. product = 1
2. i = 0

Algorithms and Unit 2 Dr F. Adamu-Fika


24 Complexity Analysis Algorithm Analysis: Cyber Security Dept. AFIT
Lecture Notes Algorithm Correctness © Since 2020
3. while( i ≤ n )
4. product = product * ( i * i )
5. i = i + 1
6. return product
END
Which statements best describe the behaviour of the algorithms?
a. Algorithm (I) calculates the correct product, but algorithm (II) does not.
b. Algorithm (II) calculates the correct product, but algorithm (I) does not.
c. Both algorithms calculate the correct product.
d. Neither algorithm calculates the correct product.

BIBLIOGRAPHY AND RESOURCES

Textbooks

[1] A. Levitin, Introduction to the Design and Analysis of Algorithms, Boston: Pearson,
2012.

[2] T. H. Cormen, C. E. Leiserson, R. L. Ronald and C. Stein, Introduction to Algorithms,


Cambridge, MA: MIT Press, 2009.

[3] R. Sedgewick and K. Wayne, Algorithms, Boston: Pearson, 2011.

Websites

[1] “Khan Academy,” [Online]. Available:


https://www.khanacademy.org/computing/ap-computer-science-
principles/algorithms-101. [Accessed 30 April 2022].

Dr F. Adamu-Fika Unit 2 Algorithms and


Cyber Security Dept. AFIT Algorithm Analysis: Complexity Analysis 25
© Since 2020 Algorithm Correctness Lecture Notes

You might also like