


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Tutorial questions and solutions for algorithms, taught at the university of sydney during the 2019 semester 1. The tutorial covers topics such as the time complexity of operations for various data structures, including queues, stacks, binary search trees, and balanced binary search trees (avl). The document also includes examples of drawing and output for these data structures after performing specific operations.
Typology: Exercises
1 / 4
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.