Problem 1 1.1 Consider the following function of n: \begin{aligned} &n^2 \quad \sum_{i=0}^{n}5\cdot{i} \quad n^3\cdot \sqrt{\frac{1}{n^3}} \quad n^2 + 2^n \quad (\Pi_{i=1}^{9}i) ...
Group the above functions that have identical growth rate and order these groups on increasing
growth. Hence:
If you place functions f1(n) and f2(n) in the same group, then we must have f1(n)=Θ(f2(n));
If you place function f1(n) in a group ordered before the group in which you place function f2(n),
then we must have f1(n)=O(f2(n))∧f1(n)=Ω(f2(n)).
Solution:
Theorem 3.25 states the following:
n→∞limg(n)f(n)is defined and is⎩⎨⎧∞c, with c > 0 a constant0thenf(n)=Ω(g(n));thenf(n)=Θ(g(n));thenf(n)=O(g(n));
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:
n2: polynomial growth, n2=Θ(n2) based on rule 3.
∑i=0n5⋅i: polynomial growth, ∑i=0n5⋅i=5⋅2n(n+1)=25n2+25n, which is Θ(n2) based on rule 7.
n3⋅n31: polynomial growth, =n3⋅n231→Θ(n23) based on rule 9,3
n2+2n: exponential growth, =Θ(2n) based on rule 7
(Πi=19i): constant, =1⋅2…9 based on rule 9
∑i=0log2(n)2i+1: linear, since the term is a geometrics series, such that ∑i=0log2(n)2i+1=2−11⋅(2log2(n)+1−1)+1=2⋅2log2(n)=2n. Therefore based on rule 1 we have Θ(n)
7ln(n): polynomial, since 7ln(n)=nln(7). Apply rule 3 we have 7ln(n)=Θ(nln7)→O(n2)
−ln(n1): logarithmic, since −ln(n1)=ln(n). Apply rule 1 yield Θ(lnn).
ln(2n): linear, since ln(2n)=n⋅ln2 and rule 1 yield Θ(n)
10: constant
nlog2(n7): polynomial, since nlog2(n7)=n⋅7log2(n)=7nlog2(n). Based on rule 9 and 1 yield O(n2)
Algorithm Count(L, v):Pre: L is an array, v is a valuei, c := 0, 0while i neq |L| do if L[i] = v then c := c + 1 end if i := i + 1end whilereturn c.Post: return the number of copies of v in L
2.1
Provide an invariant for the while loop at Line 2
Solution:
0≤i≤∣L∣,c=j=0∑i−1[L[j]=v]
2.2
Provide a bound function for the while loop at Line 2
Solution:
f(i)=∣L∣−i
2.3
Prove that Count algorithm is correct.
Solution:
Line 1: i, c := 0, 0
L[0,i) with i=0 is L[0,0)
L[0,0) is empty, hence c=∑j=0i−1[L[j]=v]=0
bound function f(i)=∣L∣−i starts at ∣L∣,∣L∣≥0
Line 2: while i neq |L| do
bound function f(i) stops at 0
invariant still holds, with i=∣L∣
Now prove invariant holds again til reach the end of the m-th loop:
Line 3-5: if L[i] = v then case:
L[i]=v hence v∈L[0,i]
invariant still holds, with i=∣L∣ and L[v]=v
inew=i+1 hence 0<inew≤∣L∣ implies 0≤i∗new≤∣L∣ and f(i∗new)=f(i)−1
cnew=c+1=∑j=0i−1[L[j]=v]+1=∑j=0inew[L[j]=v]
f(i) strictly decreases after each iteration, inew:=i+1
Therefore the invariant still holds within the if statement.
L7: end while
i=∣L∣ hence f(i)=0, the loop stops
c=∑j=0i−1[L[j]=v]=∑j=0∣L∣−1[L[j]=v]
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 ∣L∣+1 times,
L3-5 (if loop) implies 4 instructions ∣L∣ times
L6 implies 2 instructions ∣L∣ times
Therefore number of work is 5+8N, thus runtime complexity would be Θ(5+8N).
Memory complexity is Θ(1), 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 O(log2(∣L∣))
Solution:
Algorithm FastCount(L, v):Pre: L is an ordered array, v is a valuefunction 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 resultsend functionfunction 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 resultend functionfirstIndex := binarySearchFirst(L, v)if firstIndex = -1 then return 0end iflastIndex := binarySearchLast(L, v)return lastIndex - firstIndex + 1Post: return the number of copies of v in L