0% found this document useful (0 votes)
6 views7 pages

Comp Sci Analysis Assignment3-Solutions

The document outlines a homework assignment for a course on the analysis of algorithms, detailing three algorithmic problems with accompanying solutions. Each problem involves proving correctness or termination of algorithms using loop invariants and induction. The assignment is due on February 7, 2025, and consists of various proofs related to while loops and recursive functions.

Uploaded by

aignue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Comp Sci Analysis Assignment3-Solutions

The document outlines a homework assignment for a course on the analysis of algorithms, detailing three algorithmic problems with accompanying solutions. Each problem involves proving correctness or termination of algorithms using loop invariants and induction. The assignment is due on February 7, 2025, and consists of various proofs related to while loops and recursive functions.

Uploaded by

aignue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Homework Assignment 3

due Friday, February 7, 2025, 11:59pm

SOLUTIONS

(3 marks) 1. For this question, I have provided a while loop and a loop invariant.
Let P(k) be the predicate: “LoopInv is true when the loop condition is checked for the k th time".
For an arbitrary k ≥ 2, I would like you to write a full proof of the statement P(k − 1) ⇒ P(k).

Algorithm 1
1: //LoopInv: a + b == 100
2: while a ≥ 0 do
3: a ← a+1
4: b ← b−1
5: end while

Solution:

(1) Notation: for any j ≥ 1, denote by a j and b j the values of variables a and b when the loop
condition on line 2 is checked for the j th time.

(2) Considering an arbitrary k ≥ 2, we prove P(k − 1) ⇒ P(k) using a Direct Proof: suppose that
P(k − 1) is true, i.e., LoopInv is true when the loop condition is checked for the (k − 1) th time,
and consider the k th time that the loop condition is checked.

(3) Since k ≥ 2 and the loop condition is being checked for a k th time, the loop condition must have
been checked for a (k − 1) th time.

(4) From (2), we know that LoopInv is true when the loop condition is checked for the (k − 1) th time,
so ak−1 + bk−1 = 100.

(5) During loop iteration k − 1, we see from the code that a increases by 1 and b decreases by 1, i.e.,
ak = ak−1 + 1 and bk = bk−1 − 1.

(6) So, when the loop condition is checked for the k th time, we see that
ak + bk = (ak−1 + 1) + (bk−1 − 1) from (5). Simplifying this gives ak + bk = ak−1 + bk−1 .

(7) From (4) and (6), we see that ak + bk = ak−1 + bk−1 and ak−1 + bk−1 = 100, which means that
ak + bk = 100. This proves that P(k) is true.

COMP 2080: Analysis of Algorithms (Winter 2025) Page 1 of 7


Pn
(5 marks) 2. Consider the following algorithm that attempts to compute the value k · i=1 i for any real number k and any
integer n > 0. Prove that sumAlg(k, n) is fully correct.

Algorithm 2 sumAlg(k, n)
1: //pre: (k ∈ R) ∧ (n ∈ Z) ∧ (n > 0)
2: if n == 1 then
3: answer ← k
4: else
5: answer ← (k · n) + sumAlg(k, n − 1)
6: end if
7: return answer Pn
8: //post: the return value is equal to k · i=1 i

Solution: Note: you may include k as a parameter of the predicate, but it is not necessary, since the value of
k isn’t modified in the recursive calls.

(1) Let P(n) be the predicate: Pn


sumAlg(k, n) eventually terminates and returns the value k · i=1 i

(2) We prove that the predicate P(n) is true for all k ∈ R and n ≥ 1, by induction on n. Consider an
arbitrary fixed k ∈ R.

(3) Base Case:

(4) Assume that pre is true at line 1.

(5) Consider n = 1. At line 2 of the code, the if condition evaluates to true, so line 3 is
executed next.

(6) At line 3, the value of answer is set to k, and line 7 is executed next.

(7) At line 7, the value


”P of answer
— is returned (and the function terminates), so k is returned.
1 Pn 
Note that k = k · 1 = k · i=1 i = k · i=1 i , which concludes the proof of P(1).

(8) Induction Hypothesis: Assume, for some n ≥ 2, that P(1) ∧ · · · ∧ P(n − 1) is true.

(9) Inductive Step: We prove that P(n) is true.

(10) Assume that pre is true at line 1.

(11) Since we are assuming that n ≥ 2, the if condition at line 2 evaluates to false, so line 5 is
executed next.

(12) By the Induction Hypothesis, P(n − 1) is true, so sumAlg(k, n − 1) eventually terminates


Pn−1
and returns the value k · i=1 i.

(13) It follows that, at line 5, after the call to sumAlg(k, n − 1) terminates, the variable answer
Pn−1
is assigned the value (k · n) + k · i=1 i.
Pn−1 Pn
(14) Factoring out k, we get that the value of answer is k · [n + i=1 i] = k · i=1 i
immediately after line 5.

(15) As the value ofP answer does not change after line 5, it follows that the value of answer
n
returned at line 7 is k · i=1 i (and the function terminates), which proves that P(n) is true.

COMP 2080: Analysis of Algorithms (Winter 2025) Page 2 of 7


3. Consider the following algorithm that attempts to compute the value y n for real number y ̸= 0 and any integer
n ≥ 0.

Algorithm 3
1: //pre: ( y ∈ R) ∧ ( y ̸= 0) ∧ (n ∈ Z) ∧ (n ≥ 0)
2: answer ← 1
3: i ← 1
4: //LoopInv:
5: while i ≤ n do
6: answer ← answer ∗ y
7: i ← i+1
8: end while
9: //post: answer == y n

(5 marks) (a) Prove that the loop in Algorithm 3 eventually terminates. In particular: give a formula E and prove that
it satisfies the two conditions in the definition of loop measure.

Solution: Notation: for an arbitrary m ≥ 1 and any variable v, we write vm to denote the value of v
when the loop condition is checked for the m th time.

(1) Let E = n − i + 1. We prove that E is a loop measure.

(2) Proof of Condition 1: between every two checks of the loop condition, the value of E
decreases by at least 1.

(3) Consider any k ≥ 2. By the definition of E, we know that Ek−1 = nk−1 − ik−1 + 1 and
Ek = nk − ik + 1.

(4) Consider the execution of the inner statements of the loop during the (k − 1) th loop
iteration. We see that n is not modified, so nk = nk−1 , and we see that i is incremented by 1,
so ik = ik−1 + 1.

(5) From (3) and (4), we see that


Ek = nk − ik + 1 = nk−1 − (ik−1 + 1) + 1 = nk−1 − ik−1 = Ek−1 − 1.
This proves Ek = Ek−1 − 1 (in other words, E decreases by at least 1).

(6) Proof of Condition 2: if E ≤ 0, then the loop condition evaluates to false.

(7) Suppose that E ≤ 0.

(8) Since E is defined as n − i + 1, it follows from (7) that n − i + 1 ≤ 0. Re-arranging


this inequality gives us i ≥ n + 1.

(9) From (8), we know that i ≥ n + 1. Since the loop condition is i ≤ n, we have shown
that the loop condition evaluates to false.

(10) Since we proved that E is a loop measure, we have proven that the while loop eventually
terminates.

COMP 2080: Analysis of Algorithms (Winter 2025) Page 3 of 7


(7 marks) (b) Define a loop invariant LoopInv that could appear at line 4. Prove that your loop invariant is true every
time that the loop condition is checked. You’ll need to do some planning ahead: read part (c) to help
guide your choice of loop invariant.

Solution: Define LoopInv to be (answer == y i−1 ) ∧ (i ≤ n + 1) ∧ (i ∈ Z).


We prove that LoopInv is true each time the loop condition at line 5 is checked.
Notation: for an arbitrary m ≥ 1 and any variable v, we write vm to denote the value of v when the
loop condition is checked for the m th time.

(1) Let P(k) be the predicate “LoopInv is true when the loop condition at line 5 is checked for
the k th time". We prove that P(k) is true for all k ≥ 1 by induction on k.

Base Case: Consider k = 1.

(2) Line 2 of the code sets answer to 1, and line 3 of the code sets i to 1. Therefore, the first time
that the loop condition is checked, we see that i = 1 ∈ Z and that answer = 1 = y 0 = y i−1 .

(3) From pre, we know that n ≥ 0 at line 1, and we see that the value of n is not modified in
lines 2-3, so n ≥ 0 the first time the loop condition is checked. Therefore, n + 1 ≥ 1 the first
time the loop condition is checked. From (2), we know that i = 1 immediately before line 5,
so we conclude that n + 1 ≥ i the first time the loop condition is checked.

(4) From (2) and (3), we see that (answer == y i−1 ) ∧ (i ≤ n + 1) ∧ (i ∈ Z) the first time that the
loop condition at line 5 is checked, which proves that P(1) is true.

Induction Hypothesis: Assume, for a k ≥ 2, that P(1) ∧ · · · ∧ P(k − 1) is true.

Inductive Step:

(5) By the Induction Hypothesis, P(k − 1) is true, so LoopInv is true when the loop condition
was checked for the (k − 1) th time. From LoopInv, this means that
(answerk−1 == y ik−1 −1 ) ∧ (ik−1 ≤ n + 1) ∧ (ik−1 ∈ Z).

(6) Consider the execution of the inner statements of the loop during the (k − 1) th loop iteration.
We see that answer is multiplied by y, and then i is incremented by 1. In other words,
answerk = answerk−1 ∗ y and ik = ik−1 + 1.
(7) From (5) and (6), we start with answerk = answerk−1 ∗ y and substitute in
answerk−1 == y ik−1 −1 to get answerk = y ik−1 . Since ik = ik−1 + 1, this proves that
answerk = y ik −1 . Also, since ik = ik−1 + 1 and ik−1 ∈ Z, we conclude that ik ∈ Z.
(8) The fact that we are checking the loop condition for a k th time means that the (k − 1) th check
of the loop condition evaluated to true. From the loop condition on line 5, it follows that
ik−1 ≤ n.

(9) From (6) and (8), since ik = ik−1 + 1 and ik−1 ≤ n, we conclude that ik ≤ n + 1.

(10) Putting together our conclusions from (7) and (9), we have shown that
(answer == y i−1 ) ∧ (i ≤ n + 1) ∧ (i ∈ Z) when the loop condition is checked for the k th time.
This proves P(k) is true, and the induction is complete.

COMP 2080: Analysis of Algorithms (Winter 2025) Page 4 of 7


(3 marks) (c) Suppose that LoopInv is true at each check of the loop condition, and suppose that line 9 is reached.
Prove that post is true when line 9 is reached.
This proof should not reference anything that happens inside the loop or before the loop: like in the provided lectures
and examples in UM Learn, your LoopInv should contain enough information about the program’s variables so that
you can prove post directly using only LoopInv and the fact that the loop condition evaluated to false. You may
need to go back to part (b) to modify your LoopInv to achieve this.

Solution:

(1) Suppose that LoopInv is true immediately before each check of the loop condition. Suppose
that line 9 is reached, which means that the loop condition has evaluated to false when line 9
is reached.

(2) Since the loop condition is i ≤ n, we know that i > n.

(3) LoopInv tells us that i ≤ n + 1 and i ∈ Z. From (2), we know that i > n, which, together
with the fact that i ∈ Z, means that i ≥ n + 1. So we have (i ≥ n + 1) ∧ (i ≤ n + 1), which
implies that i = n + 1.

(4) From LoopInv, we know that answer = y i−1 at the final loop check, and so answer = y i−1
when line 9 is reached as well.

(5) From (3) and (4), we have that (answer = y i−1 ) ∧ (i = n + 1) at line 9, which implies that
answer = y n at line 9. This means post is true when line 9 is reached.

COMP 2080: Analysis of Algorithms (Winter 2025) Page 5 of 7


(7 marks) 4. Prove that the loop in Algorithm 4 below eventually terminates. In particular: give a formula E and prove
that it satisfies the two conditions in the definition of loop measure. You should assume (without proof) that
the loop invariant is true each time the loop condition is checked.

Algorithm 4
1: //LoopInv: (x, y ∈ Z) ∧ ( y ≥ 0)
2: while (x ≥ 3) ∧ ( y ≤ 100) do
3: if x is odd then
4: y ← y +1
5: x ← x +1
6: else
7: x ← x −3
8: end if
9: end while

Solution: Note: there are lots of correct possibilities here for E. For example, anything of the form x −α y +β
where α ≥ 2 and β ≥ 198 should work, but I’m sure there are many more.
Notation: for an arbitrary m ≥ 1 and any variable v, we write vm to denote the value of v when the loop
condition is checked for the m th time.

(1) Let E = x − 2 y + 200. We prove that E is a loop measure.

(2) Proof of Condition 1: between every two checks of the loop condition, the value of E decreases by
at least 1.

(3) Consider any k ≥ 2. By the definition of E, we know that Ek−1 = x k−1 − 2 yk−1 + 200 and
Ek = x k − 2 yk + 200.

(4) Consider the execution of the inner statements of the loop during the (k − 1) th loop
iteration. There are two sub-cases to consider:
• Case (a): the if condition evaluates to true (i.e., x is odd). We see that both x and y are
incremented, so x k = x k−1 + 1 and yk = yk−1 + 1.
Substituting these values into our equation for Ek , we get
Ek = (x k−1 + 1) − 2( yk−1 + 1) + 200 = x k−1 + 1 − 2 yk−1 − 2 + 200 = Ek−1 − 1.
In other words, E decreases by 1.
• Case (b): the if condition evaluates to false (i.e., x is even). We see that x decreases by 3,
and y is not changed, so x k = x k−1 − 3 and yk = yk−1 .
Substituting these values into our equation for Ek , we get
Ek = (x k−1 − 3) − 2( yk−1 ) + 200 = x k−1 − 3 − 2 yk−1 + 200 = Ek−1 − 3.
In other words, E decreases by 3.
(5) Proof of Condition 2: if E ≤ 0, then the loop condition evaluates to false.

(6) Suppose that E ≤ 0.

(7) We consider two sub-cases:


• Case (a): x < 3. Then the loop condition evaluates to false, as it requires that x ≥ 3.
• Case (b): x ≥ 3. Then from our definition of E and the fact that E ≤ 0, we get that
x − 2 y + 200 ≤ 0. Re-arranging this gives 2 y ≥ 200 + x. As x ≥ 3, we see that 2 y ≥ 203, so
y > 101. The loop condition evaluates to false, as it requires y ≤ 100.
In all cases, we proved that the loop condition evaluates to false, as required.

COMP 2080: Analysis of Algorithms (Winter 2025) Page 6 of 7


Definitions and Facts
Definitions
• The symbol Z represents the set of integers {. . . , −2, −1, 0, 1, 2, . . .}.
• The symbol N represents the set of positive integers {1, 2, 3, . . .}.
• The symbol R represents the set of real numbers.
• An integer n is even if there exists an integer k such that n = 2k.
• An integer n is odd if there exists an integer k such that n = 2k + 1.
• The floor of x, denoted by ⌊x⌋, is the largest integer that is less than or equal to x.
• The ceiling of x, denoted by ⌈x⌉, is the smallest integer that is greater than or equal to x.
• For every positive integer x, the double factorial x!! is defined as the product 1 · 3 · 5 · · · (x − 4) · (x − 2) · x when x is odd, and, defined
as the product 2 · 4 · 6 · · · (x − 4) · (x − 2) · x when x is even.

Floors/Ceilings
For any real number x and any real number y ̸= 0:
• −⌊x⌋ = ⌈−x⌉ and −⌈x⌉ = ⌊−x⌋
• for any integer k: ⌊k⌋ = ⌈k⌉ = k
• for any integer k: k + ⌊x/ y⌋ = ⌊k + x/ y⌋
• for any integer k: k + ⌈x/ y⌉ = ⌈k + x/ y⌉
• for any integer k: ⌊(k + 1)/2⌋ = ⌈k/2⌉ and ⌈(k − 1)/2⌉ = ⌊k/2⌋
⌊x/a⌋ x ⌈x/a⌉ x
• for any integers a, b: ⌊ b ⌋ = ⌊ ab ⌋ and ⌈ b ⌉ = ⌈ ab ⌉

Exponentials and Logarithms


For all a, b, c > 0 and all x, y ∈ R:
p
a0 = 1 a1 = a for integer k > 1: a1/k = k a
ax
a x a y = a x+ y a−1 = 1/a = a x− y
x y y x
ay
(a ) = (a ) = a xy
(a b) x = a x b x a < b if and only if a c < b c

For all a, b > 1, and all x, y > 0:

loga (1) = 0 loga €a =Š1 aloga (x) = x and loga (a x ) = x


x log b x
loga (x y) = loga (x) + loga ( y) loga y = loga (x) − loga ( y) loga x = log b a
1
loga (x y ) = y loga (x) loga (1/x) = − loga x log b a = loga b
x < y if and only if loga (x) < loga ( y)

Summation Identities

b n
X X n(n + 1)
1= b−a+1 j=
j=a j=1
2
n n
X
2 n(n + 1)(2n + 1) X n2 (n + 1)2
j = j3 =
j=1
6 j=1
4
n ∞
X q n+1 − 1 X 1
∀q > 0, q ̸= 1, qj = ∀q, 0 < q < 1, qj =
j=0 
q−1 j=0
1−q
  
Xb Xb Xb b
X b
X
( f ( j) + g( j)) =  f ( j) +  g( j) c · f ( j) = c · f ( j) for any constant c
j=a j=a j=a j=a j=a

COMP 2080: Analysis of Algorithms (Winter 2025) Page 7 of 7

You might also like