Comp Sci analysis Assignment2-Questions
Comp Sci analysis Assignment2-Questions
1. For both parts of this question, consult Algorithm 1. Your answers should follow that same style and
format as shown in class.
Algorithm 1
1: //pre: (y == 3) ∧ (n ∈ {1, −1})
2: x ← 2
3: w ← x + y
4: //assert:
5: if n < 0 then
6: w ← −w
7: end if
8: //post: w == 5 · n
(3 marks) (a) Provide an assert statement for line 4. You’ll need to do some planning ahead: read part (b)
to help guide your choice for the assert statement.
(6 marks) (b) Prove that the code satisfies the Partial Correctness property. In particular: assume that the
code terminates, and prove that the following two statements are true:
• (pre is true when line 1 is reached) ⇒ (assert is true when line 4 is reached)
• (assert is true when line 4 is reached) ⇒ (post is true when line 8 is reached)
Note: If you are having trouble proving either of the two statements above, you might need
to go back to part (a) and change your assert statement. Also, make sure that your proof
that post is true does not rely on any information that appears before line 4 (again, you might
need to adjust your assert statement to prevent this).
(6 marks) 3. Using only the facts written on page 4 of this assignment, prove: 6n log n + 2n + log3 n ∈ Θ(n log n).
Notes for this question:
• If not written, it is assumed that the base of each logarithm is 2.
• Facts (1.1), (5.4), and (5.5) on page 4 may differ slightly from the versions you reviewed in
lecture (and they are more useful).
• An example of the type of proof we are looking for is provided on page 5 of this assignment.
(1 mark) (a) For all odd integers n ≥ 3, explain how to construct a "worst-case array" with n entries, by
which we mean: an array that maximizes the number of times the if condition evaluates to
true. For simplicity, have each array entry equal to either 0 or 1.
(2 marks) (b) For all odd integers n ≥ 3, how many times does the if statement evaluate to true when
the code is executed on a worst-case array with n entries? For which values of i will the if
condition evaluate to true when the code is executed on a worst-case array? Your answers
should be in terms of n.
(4 marks) (c) For all m ≥ 1, let g(m) denote the worst-case number of steps performed by otherFunction(m).
Determine the worst-case number of steps performed by myFunction in terms of n and g,
showing and explaining your work. In your final simplified expression, one of the terms should
P#
be a summation of the form , i.e., the lower limit is 0 and no values are skipped.
k=0
(3 marks) (d) Suppose that otherFunction is defined by the pseudocode in Algorithm 3. For all m ≥ 1,
determine the worst-case number of steps g(m) performed by otherFunction(m).
Hint: since m is not necessarily a power of 3, your answer should contain the floor function.
Algorithm 3 otherFunction(int m)
1: //pre: (m ∈ Z) ∧ (m ≥ 1)
2: j ← 1
3: while j ≤ m do
4: j←j∗3
5: end while
(2 marks) (e) Using your answers from parts (c) and (d), determine the worst-case number of steps performed
by myFunction in terms of n when otherFunction is defined by the pseudocode in Algorithm
3. You should simplify your answer as much as possible, but since the answer to part (d) contains
a floor function, onePof the terms will have a summation that you will not be able to simplify,
i.e., it will look like ⌊something⌋.
(2 marks) (f) Take your answer from part (e), and using the fact ⌊x⌋ ≤ x to simplify the remaining summation,
find an upper bound on the worst-case number of steps taken by myFunction. Then, express
your answer asymptotically as O( f (n)) for some function f (n) (no proof is required for your
asymptotic bound).
You may find the following definition helpful: for positive integers 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. Also, you may use the fact that
log((n − c)!!) ∈ Θ(n log n) for any constant c.
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
⌉
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
In these statements, a, b and c are constants (i.e., they are not functions of n), and f , g, h, f1 , f2 , g1
and g2 are functions that map Z+ → R+ .
Group 1: the hierarchy summarized. For all constants a > 0 and b > 0 and c > 0:
(1.1) if a > 1, then c ∈ o(loga (n))
(1.2) if a > 1, then loga (n) ∈ o(n b )
(1.3) if a < b, then na ∈ o(n b )
(1.4) if b > 1, then na ∈ o(b n )
(1.5) if 1 < a < b, then a n ∈ o(b n )
(1.6) a n ∈ o(n!)
Group 2: transitivity.
(2.1) if f ∈ O(g) and g ∈ O(h), then f ∈ O(h)
(2.2) if f ∈ Ω(g) and g ∈ Ω(h), then f ∈ Ω(h)
(2.3) if f ∈ o(g) and g ∈ o(h), then f ∈ o(h)
(2.4) if f ∈ ω(g) and g ∈ ω(h), then f ∈ ω(h)
Group 4: addition.
(4.1) if f ∈ O(h) and g ∈ O(h), then f + g ∈ O(h)
(4.2) if f ∈ Ω(h), then f + g ∈ Ω(h)
(4.3) if f ∈ o(h) and g ∈ o(h), then f + g ∈ o(h)
(4.4) if f ∈ ω(h), then f + g ∈ ω(h)
Group 5: multiplication.
(5.1) for any constant c > 0, c f ∈ Θ( f )
(5.2) if f1 ∈ O(g1 ) and f2 ∈ O(g2 ) then f1 f2 ∈ O(g1 g2 )
(5.3) if f1 ∈ Ω(g1 ) and f2 ∈ Ω(g2 ) then f1 f2 ∈ Ω(g1 g2 )
(5.4) if f1 ∈ O(g1 ) and f2 ∈ o(g2 ) then f1 f2 ∈ o(g1 g2 )
(5.5) if f1 ∈ Ω(g1 ) and f2 ∈ ω(g2 ) then f1 f2 ∈ ω(g1 g2 )
Solution
Proof.
3 n 3
(1) n ∈ o( 2.5 ) by Fact (1.4) since 2.5 > 1.
3 n
(2) n ∈ O( 2.5 ) by Fact (3.4) applied to the outcome of step (1).