


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Algorithms Tutorial questions 2019 Semester 1 Tutorial 2
The University of Sydney School of Computer Science
Problem 1
a) Show that log(n!) is O(n log n).
b) (Harder) Show that log(n!) is ฮฉ(n log n).
Solution:
a) n! = n.(n โ 1)..... 2. 1 โค n.n.... .n = nn. Taking logs of both sides gives the required answer.
b) n! โฅ n 2..... n 2 ( n 2 times) =
( (^) n 2
)n/ 2
. Taking logs of both sides gives log(n!) โค n 2 log
( (^) n 2
n 2 (log(n)^ โ^ log(2))^ โ^ ฮฉ(n^ log^ n).
Problem 2 Which operations are standard for the following data structures? How much time does each operation require?
Solution: 1: enqueue, dequeue, first, size O(1) 2: push, pop, top, size O(1) 3: insert, delete, find O(n) 4: insert, delete, find O(log n)
Problem 3 (a) Draw the queue Q and the output when the following sequence of operations are performed: enqueue(5) enqueue(2) f irst() enqueue(3) dequeue() f irst() size()
(b) Draw the stack S and the output when the following sequence of operations are performed: push(5) push(2) top() push(3)
pop() top() size()
Solution: (a) Q = [5] Q = [5, 2] output 5, Q = [5, 2] Q = [5, 2 , 3] output 5, Q = [2, 3] output 2, Q = [2, 3] output 2, Q = [2, 3] (b) S = [5] S = [2, 5] output 2, Q = [2, 5] Q = [3, 2 , 5] output 3, Q = [2, 5] output 2, Q = [2, 5] output 2, Q = [2, 5]
Next page =โ
Problem 6 Find a recurrence relation for the number of moves for the 4-peg, n-disk Hanoi Tower problem.
Solution: As with the 3-peg problem, we have M (1) = 1 and M (2) = 3. The extra peg allows us to move the largest disk immediately after moving the second-largest disk, so to move a tower of n disks, we only need to move a tower of n โ 2 disks, then take 3 moves to move the two largest disks, then move the tower of n โ 2 disks on top. So M (n) = 2M (n โ 2) + 3.