Problem 1

1.1

Consider the following function of :

Group the above functions that have identical growth rate and order these groups on increasing growth. Hence:

  • If you place functions and in the same group, then we must have ;

  • If you place function in a group ordered before the group in which you place function , then we must have .

Solution:

Theorem 3.25 states the following:

The following are the rules of thumb to determine the order of growth of functions:

Let’s start with analysing the functions that have the same growth rate:

  • : polynomial growth, based on rule 3.

  • : polynomial growth, , which is based on rule .

  • : polynomial growth, based on rule

  • : exponential growth, based on rule

  • : constant, based on rule 9

  • : linear, since the term is a geometrics series, such that . Therefore based on rule we have

  • : polynomial, since . Apply rule we have

  • : logarithmic, since . Apply rule yield .

  • : linear, since and rule yield

  • : constant

  • : polynomial, since . Based on rule and yield

  • : polynomial growth, based on rule 3.

  • : super-exponential.

  • : linear based on rule 1, gives

Thus the order from increasing rate:

  • (constant)

  • (logarithmic)

  • (linear)

  • (polynomial)

  • (exponential)

  • (super-exponential)

1.2

Consider the following recurrence

Use induction to prove that with

Solution:

base case:

Given apply for and :

Thus the base case holds.

induction hypothesis:

Assume holds for . We will proves that also holds.

Therefore:

Therefore the induction hypothesis holds.

conclusion:

is true for all .


Problem 2

Consider the following Count algorithm

Algorithm Count(L, v):
Pre: L is an array, v is a value
i, c := 0, 0
while i neq |L| do
  if L[i] = v then
    c := c + 1
 end if
 i := i + 1
end while
return c.
Post: return the number of copies of v in L

2.1

Provide an invariant for the while loop at Line 2

Solution:

2.2

Provide a bound function for the while loop at Line 2

Solution:

2.3

Prove that Count algorithm is correct.

Solution:

Line 1: i, c := 0, 0

  • with is

  • is empty, hence

  • bound function starts at

Line 2: while i neq |L| do

  • bound function stops at

  • invariant still holds, with

Now prove invariant holds again til reach the end of the m-th loop:

Line 3-5: if L[i] = v then case:

  • hence

  • invariant still holds, with and

  • hence implies and

  • strictly decreases after each iteration,

Therefore the invariant still holds within the if statement.

L7: end while

  • hence , the loop stops

Therefore the invariant still holds at the end of the loop.

2.4

What is the runtime and memory complexity of Count algorithm?

Solution:

  • L1 implies 2 instructions
  • L2 implies 2 instructions times,
  • L3-5 (if loop) implies 4 instructions times
  • L6 implies 2 instructions times

Therefore number of work is , thus runtime complexity would be .

Memory complexity is , since only 2 variables are used.

2.5

Provide an algorithm FastCount(L, v) operates on ordered lists L and computes the same results as Count(L, v) but with a

Solution:

Algorithm FastCount(L, v):
Pre: L is an ordered array, v is a value
function binarySearchFirst(L, v)
  low, high := 0, |L| - 1
  results := -1
  while low <= high do
    mid := (low + high) / 2
    if L[mid] < v then
      low := mid + 1
    else if L[mid] > v then
      high := mid - 1
    else
      results := mid
      high := mid - 1
  end while
  return results
end function
 
function binarySearchLast(L, v)
  low, high := 0, |L| - 1
  results := -1
  while low <= high do
    mid := (low + high) / 2
    if L[mid] < v then
      low := mid + 1
    else if L[mid] > v then
      high := mid - 1
    else
      results := mid
      low := mid + 1
  end while
  return result
end function
 
firstIndex := binarySearchFirst(L, v)
if firstIndex = -1 then
  return 0
end if
lastIndex := binarySearchLast(L, v)
return lastIndex - firstIndex + 1
Post: return the number of copies of v in L