Docsity

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

Toggle sidebar
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.(nโˆ’1). . . . .2.1โ‰คn.n. . . . .n =nn. Taking logs of both sides gives the required
answer.
b) n!โ‰ฅn
2. . . . . n
2(n
2times) = ๎˜€n
2๎˜n/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.