Tutorial Questions: Algorithms, The University of Sydney, 2019 Semester 1, Tutorial 2, Exercises of Algorithms and Programming

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

2018/2019

Uploaded on 04/20/2019

kefart
kefart 🇺🇸

4.4

(11)

55 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tutorial questions
Algorithms
2019 Semester 1Tutorial 2
The University of Sydney
School of Computer Science
Problem 1
a) Show that log(n!) is O(nlog n).
b) (Harder) Show that log(n!) is Ω(nlog n).
Solution:
a) n! = n.(n1). . . . .2.1n.n. . . . .n =nn. Taking logs of both sides gives the required
answer.
b) n!n
2. . . . . n
2(n
2times) = n
2n/2. Taking logs of both sides gives log(n!) n
2log n
2=
n
2(log(n)log(2)) Ω(nlog n).
Problem 2
Which operations are standard for the following data structures? How much time does each operation
require?
1. Queue
2. Stack
3. Binary search tree
4. Balanced binary search tree (AVL)
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 Qand the output when the following sequence of operations are performed:
enqueue(5)
enqueue(2)
first()
enqueue(3)
dequeue()
first()
size()
(b) Draw the stack Sand the output when the following sequence of operations are performed:
push(5)
push(2)
top()
push(3)
1
pf3
pf4

Partial preview of the text

Download Tutorial Questions: Algorithms, The University of Sydney, 2019 Semester 1, Tutorial 2 and more Exercises Algorithms and Programming in PDF only on Docsity!

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?

  1. Queue
  2. Stack
  3. Binary search tree
  4. Balanced binary search tree (AVL)

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.