You are on page 1of 5

COMP4500/7500 2009

Question 1
a)
We Know:
ExtractMin: c ' (H )=lg (n)+Φ (H (n−1))−Φ ( H (n))=0=Θ(1)
Insert: c ' (H )=lg (n)+Φ ( H (n))−Φ(H ( n−1))=lg( n)+lg ( n)=Θ(lg (n))

Therefore, we want:
Φ ( H (n−1))−Φ (H (n))=−lg(n)during extract min.
Φ ( H (n))−Φ(H ( n−1))=lg( n)during insert.

Solve:
| H|
Φ ( H)=∑ lg (i)
i=1

b)
Does this show it:
ExtractMin: c ' (H )=lg (n)+Φ (H (n−1))−Φ ( H (n))=0=Θ(1)
Insert: c ' (H )=lg (n)+Φ ( H (n))−Φ(H ( n−1))=lg( n)+lg ( n)=Θ(lg (n))

c)
We are effectively paying double on insert to get ExtractMin for free later. You can’t get less than
Θ(lg n) for insert and Θ(1)for ExtractMin because then you wouldn’t be paying for the operations
anywhere. We are not actually reducing the complexity of the operations, just performing some
creative accounting - but we have to put the cost somewhere.

^^ Should try to answer this question a bit more mathematically.

Since exact costs for both operations are O(lg (n)) then say the exact cost of extract min is c 1 lg (n)
and the exact cost of insert is c 2 lg( n) for some constants c 1 , c2 >0 .

n −1 n−1

∑ c ' (H i)=∑ c 2 lg(i)+Φ ( H n−1)


i=0 i =0
n −1 n −1

∑ c ' (H i)≥ ∑ c 2 lg(i)


i=0 i=0

c ' (H i)
≥ c2
lg (i)
lim c ' (H i)
⇒ i→∞ ≥ c 2> 0
lg(i)
This is true since Φ ( H n−1)>0 as the extract min needs to use up potential (O(1) means it doesn’t
charge enough to cover the actual cost of operation).

Question 2
a)
ln n 1
li mn →∞ (by L’Hospital’s rule) ¿ 2 ×li m n →∞ =0, so ln n ∈ o ¿and the statement is false
√❑ n

b)
● Worst-case:
○ the longest running time/highest space required for any input of size n
● Average-case:
○ normally requires probabilistic analysis applied to
○ limited, because it may not be apparent what constitutes an “average” input for a
particular problem.
● Best-case:
○ the shortest running time/lowest space required for any input of size n

c)
n
Recursion for strategy i: T (n)=2 T ( )+ n2
2
Solve this recursion using master method:
a=2, b=2, f (n)=n 2
nlo g a=nlo g 2=n
b 2

f (n) ϵ f (n) n2 2
WTS: ∃ ϵ >0 : lo g a
∈O( n ). lo g a
= ∈O(n) ⇒ϵ =1⇒ T ( n) ∈Θ( n ) by
n b
n ❑b
n
case 3 of master theorem

n
Recursion for strategy ii: T (n)=3 T ( )+n
2
Solve this recursion using master method:
a=3, b=2, f (n)=n
nlo g a=nlo g 3
b 2

f (n)
WTS: ∃ ϵ >0 : lo g a
∈O( n−ϵ ).
n b

f (n) n
lo g a
= lo g 3 ∈O(n−(lo g 3−1) )⇒ ϵ=lo g 2 3−1⇒ T (n) ∈Θ(nlo g 3) by case 1 of
2 2

n ❑b
n 2

master theorem
nlo g 3 ∈O(n 2), so strategy i is better
2

d)
1)
The following doesn’t work for negative numbers, but could easily be extended to
without changing the time complexity

find()
a = 1
b = 2

Sort(A) // quicksort or similar, O(n lg n)

while b <= n
if (A[a] + A[b] < k)
b++
else if (A[a] + A[b] > k)
a++
else
return true

if (a == b)
b++ // need two different numbers (from the question)

return false
2)
Sorting can be done in O(n lg n) time using quicksort or similar, and the inner loop
will run at most O(n) times
Question 3
a)
Adjacency list - a tree is not a dense graph, so the adjacency list will have lower space
requirements. Also, the time complexity isn’t an issue as there is no need to randomly
check if there is an edge connecting two vertices.

b)
(modified depth first search)

Check-Forest(G)
for each Vertex u : G.V
u.colour = WHITE
u.pi = NULL

for each Vertex u : G.V


if u.colour == WHITE
stop = not Check-Forest-Visit(G, u)

if stop:
return false
--------------------
Check-Forest-Visit(G, u):
u.colour = GRAY

for each Vertex v : G.adj[u]


if v.colour == WHITE
v.pi = u
DFS-Visit(G, v)
else
return false

u.colour = BLACK
return true

c)
DFS (as used above) is O ¿, but a back edge will be found not later than seeing ¿ V ∨¿
distinct vertices, since in an undirected forest ¿ E∨≤∨V ∨−1

Question 4
a)
b)

c)

Question 5
a)

Question 6
a)

b)

c)

You might also like