Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Semester 2, 2022

Tutorial 2

COMP3600/6466 - Algorithms

Exercise 1                                                 Complexity Classes

1. Briefly explain your answer to the following questions.

• If I prove that an algorithm takes O(n2 ) worst-case-time, is it possible that it takes O(n) on all inputs?

• If I prove that an algorithm takes Θ(n2 ) worst-case-time, is it possible that it takes O(n) on some inputs?

2. True or False?

 Is 2n+1  = O(2n )?

• Is 22n  = O(2n )?

• Is log n = o(nϵ ) for all ϵ > 0?

• Is n = Ω(n)?

• Is log n = O(n )?

• Is log 3n  = Θ(log 2n )

• There exists a function f(n) which is in both O(g(n)) and ω(g(n))

• There exists a function f(n) which is in both Θ(g(n)) and o(g(n))

• If f(n) = Θ(g(n)) then limn →∞   = C


Exercise 2                                         Math Refresher: Proof by Induction

1. Use induction to prove  i =

2. Use induction to prove   = 


Exercise 3                                                      Correctness

Algorithm 1 BubbleSort(An array of integers A)

1.   1:  for i = 1 to A.length do

2:       for j = A.length downto i + 1 do

3:            if A[j] < A[j − 1] then

4:                 Swap A[j] and A[j − 1]

Use a loop invariant to prove that the above implementation of BubbleSort is correct. That is, after execution, A[1] ≤ A[2] ≤ · · · ≤ A[A.length].

2. Recall the problem of finding an element in an array A whose value is v, as defined in slide 7 of the Introduction - part 2 lecture slides. Please write pseudocode for linear search, which scans through the array sequentially from the beginning, looking for v . Then use a loop invariant to prove that your algorithm is correct.


Exercise 4                                                       Recurrence


1. Suppose T(n) = 2T(⌊⌋) + 1 with T(1) = 1. Show that T(n) = O(n).

2. Please find the tightest asymptotic upper bound you could for the recurrence T(n) = T(n − 2) + log(n), with initialization T(1) = 1 and T(2) = 1.

 

Exercise 5                                           Recurrence Analysis: Substitution

Use the substitution method to prove the following:

1. T(n) = T(⌈⌉) + 1 = O(log n)

2. T(n) = 2T(⌊⌋ + 17) + n = O(nlog n)