0% found this document useful (0 votes)
18 views

Comp Sci analysis Assignment3-Questions

The document outlines Homework Assignment 3 for a course on Analysis of Algorithms, due on February 7, 2025. It includes multiple algorithm proofs and loop invariant tasks, requiring students to demonstrate the correctness and termination of given algorithms. Submissions must be made via Crowdmark, with specific instructions for file organization.

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)
18 views

Comp Sci analysis Assignment3-Questions

The document outlines Homework Assignment 3 for a course on Analysis of Algorithms, due on February 7, 2025. It includes multiple algorithm proofs and loop invariant tasks, requiring students to demonstrate the correctness and termination of given algorithms. Submissions must be made via Crowdmark, with specific instructions for file organization.

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/ 3

Homework Assignment 3

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


Submit solutions on Crowdmark, not UM Learn. A Crowdmark link will be sent to your UM e-mail.
When uploading to Crowdmark, a separate file will be required for each part of each question.

(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

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

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.

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


(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.
(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.

(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

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


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/a⌉
• for any integers a, b: ⌊ b ⌋ = ⌊ ab
x
⌋ and ⌈ b ⌉ = ⌈ ab
x

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
x
a
a x a y = a x+ y a−1 = 1/a = a x− y
ay
(a x ) y = (a y ) x = a x y (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


log b x
loga (x y) = loga (x) + loga ( y) loga x
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 n(n + 1)(2n + 1) X n2 (n + 1)2
j =
2
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
b
– b ™ – b ™ b b
X X X X 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 3 of 3

You might also like