You are on page 1of 44

Subject Name: Algorithm Design and Analysis

Subject Code: TCP2101

Table Of Contents
{

Lec01 Analysis of Algorithms


Lec02 Hash Tables
Lec03 Binary Search Trees (BSTs)
Lec04 Priority Queues and Heaps
Lec05 Graphs, Graph Algorithms
Lec06 Sorting Algorithms Part 1, Quick-Sort
Lec07 Sorting Algorithms Part 2, Selection Algorithm

1
Lec01 Analysis of Algorithms
Question
L1.1 An algorithm named LMN is provided below. Fill in the right column named Number of
Primitive Operations, and then find the time complexity T(n) of the algorithm in big-O
notation.
L1.2 What is the function of this algorithm LMN? Given an array A = {7, 2, 9, 3, 9, 4},
write the output of the algorithm on this array A
L1.1
Algorithm LMN(A, n)
Input: A is an array with n integers
Number of primitive operations in big-Oh notation
for i ← n-1 to 0 do n
for j ← 1 to i do n2
if A[j-1] > A[j] n2
temp ← A[j-1] n2
A[j-1] ← A[j] n2
A[j] ← temp n2
return A n2
T(n) O( n2 )
L1.2

To sort the numbers in the array


Output A = {2,3,4,7,9,9}

Question
Show that the function f(n) = 12n2 + 6n is o(n3).
Note that f(n) is o(g(n)) if and only if
𝑓(𝑛)
𝑙𝑖𝑚𝑛 →∞ 𝑔(𝑛)
= 0, provided this is true.

To show that f(n) = 12n2 + 6n is O(n3), we need to show that

Assume g(n) = n3

lim n→∞ f(n)/n3 = 0.

2
To do this, we can use algebraic manipulation and L'Hopital's rule:
f(n)/n3 = (12n2 + 6n)/n3 = 12/n + 6/n2

Taking the limit as n approaches infinity, we get:


lim n→∞ f(n)/n3 = lim n→∞ (12/n + 6/n2) = 0.
Since the limit is 0, we can conclude that f(n) = 12n2 + 6n is O(n3).

3
Question
An algorithm Z is provided below. Fill in the column named Number of Primitive
Operations, and find the time complexity T(n) of the algorithm in big-O notation.
Algorithm Z (A, n) Number of primitive operations
Input: A is an array with n ≥ 5 integers
Output: Return the product of the
multiplication of first 5 integers in A

product ←1 1

for i ← 0 to 4 do n

product ← product * A[i] n2

return product n

T(n) O(n)

Question
We can use either one of the two techniques below for analyzing the running time of an
algorithm. Describe each of them and state one limitation in each technique.
- Experimental Study
- Theoretical analysis

Description Limitation

Experimental Study Involves measuring the This technique may not be


actual running time of an practical for very large
algorithm by implementing inputs or for algorithms with
it and running it on various very long running times.
inputs

Theoretical Study Involves analyzing the The analysis may be


algorithm mathematically to complex and require
determine its running time advanced mathematical
as a function of the input knowledge
size.

4
Given the sum of the arithmetic sequence below, what is the Big-Oh notation for the function,
f(n)?
f(n)=3 + 32 + 33 + . . . + 3n where n = 1, 2, …
It is a geometric sequence where the sum of the geometric function formula is :
Sn = a(rn - 1) / (r - 1), where a = 3 and r = 3

Sn = 3(3n - 1) / (2)

The dominant term in the expression is 3n, so the Big-Oh notation for f(n) is O(3n).

Question
Below is the sum of an arithmetic sequence, what is the Big-Oh notation for the function,
f(n)?
f(n)=1 + 2 + 3 + 4 +. . . + n
The sum of the arithmetic function formula is n(n+1)/2
f(n) = (n2 + n) / 2

Therefore, the Big-Oh notation for f(n) is O(n2), since the dominant term in the expression
is n2.

Lec02 Hash Tables


Question
L2.1 Explain briefly what uniform hashing is.

Uniform hashing occurs when the elements are spread evenly or near evenly among the
indexes of hash table.

L2.2 Among the two hash functions below, which hash function do you think is better for
uniform hashing? Explain your answer.
(L2.2.1) h(k) = k % 101
(L2.2.2) h(k) = k % 128

5
L2.2.1 is better because 101 is a prime number and it is not too close to power of 2
while 128 is not a prime number and it is a power of 2.

L2.3 Insert the elements in the following order: 34, 55, 12, 17, 45, 37, 75, 86, 65, 21
into a hash table using the hash function, h(x) = x % 10.
Use quadratic probing to handle collisions in this hash table.
What is the load factor for this hash table?

key 86 65 12 34 55 45 17 37 75

index 0 1 2 3 4 5 6 7 8 9

key location Collision

34 34 % 10 = 4 N

55 55 % 10 = 5 N

12 12 % 10 = 2 N

17 17 % 10 = 7 N

45 45 % 10 = 5 Y

Location = 6 (5 + 1) % 10 = 6

37 37 % 10 = 7 Y
Location = 8 (7+1) % 10 = 8

75 75 % 10 = 5 Y

(5+1) % 10 = 6
Location = 9 (5+4) % 10 = 9

86 86 % 10 = 6 Y

(6+1) % 10 = 7
Location = 0 (6+4) % 10 = 0

65 65 % 10 = 5 Y

(5+1) % 10 = 6
(5+4) % 10 = 9
(5+9) % 10 = 4
Location = 1 (5+16) % 10 = 1

6
21 21 % 10 = 1 Y

(1+1) % 10 = 2
(1+4) % 10 = 5
(1+9) % 10 = 0
(1+16) % 10 = 7
(1+25) % 10 = 6
(1+36) % 10 = 7
(1+49) % 10 = 0
(1+64) % 10 = 5
(1+81) % 10 = 2
(1+100) % 10 = 1
(1+121) % 10 = 2
(1+144) % 10 = 4

No finding slots

Load factor : 9/10 = 0.9

Question

Insert these numbers 3, 14, 34, 13, 23, 55 into a Hash Table using the Hash function,
h(key) = key % 10. Use linear probing to resolve the collision.
key 3 14 34 13 23 55

index 0 1 2 3 4 5 6 7 8 9
Show your working steps below.

Key Location Collision

3 3 % 10 = 3 N

7
14 14 % 10 = 4 N

34 34 % 10 = 4 Y

Location = 5 Next index = 5

13 13 % 10 = 3 Y

Next index = 4 → Full


Next index = 5 → Full
Location = 6 Next index = 6 → Empty

23 23 % 10 = 3 Y

Next index = 4 → Full


Next index = 5 → Full
Next index = 6 → Full
Location = 7 Next index = 7 → Empty

55 55 % 10 = 5 Y

Next index = 5 → Full


Next index = 6 → Full
Next index = 7 → Full
Location = 8 Next index = 8 → Empty

Question
The following sequence of 5 numbers are inserted into a hash table using hash
function
h(k) = k % 5. Linear probing is used to resolve collisions. Fill in the following tables.
k 36 99 80 21 34

h(k) 1 4 0 1 4

No of N N N Y Y
collision
Hash Table
k 80 36 21 34 99

index 0 1 2 3 4

8
Question
Insert the numbers in the following order: 8, 27, 34, 30, 46, 62, 17, 126, 50, 32 into a
Hash Table with the Hash function, h(k) = k % 16. Use linear probing to overcome
the collisions in the Hash Table.
62 17 34 126 50 32 8 27 30 46

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Your working steps

Key Location Collisions

8 8 % 16 = 8 N

27 27 % 16 = 11 N

34 34 % 16 = 2 N

30 30 % 16 = 14 N

46 46 % 16 = 14 Y

Location = 15 Next index = 15 → Empty

62 62 % 16 = 14 Y

Next index = 15 → Full


Location = 0 Next index = 0 → Empty

17 17 % 16 = 1 N

126 126 % 16 = 14 Y

Next index = 15 → Full


Next index = 0 → Full
Next index = 1 → Full
Next index = 2 → Full
Location = 3 Next index = 3 → Empty

50 50 % 16 = 2 Y

Next index = 3 → Full


Location = 4 Next index = 4 → Empty

32 32 % 16 = 0 Y

9
Next index = 1 → Full
Next index = 2 → Full
Next index = 3 → Full
Next index = 4 → Full
Location = 5 Next index = 5 → Empty

Both the Chaining method and Probing method are used to solve the collisions in Hash
Table. List down two (2) differences between the methods.
Chaining method Probing method

Uses linked list - requires extra memory Uses fixed amount of memory

Search time is shorter Better suited for caching


Search time is longer if got many collisions

10
Lec03 Binary Search Trees (BSTs)

Question
L3.1
Which traversal method outputs the sorted result in a binary search tree (BST)?
Write the pseudocode for this traversal method.

In-Order traversal outputs the sorted result in a BST

Pseudocode :

Algorithm inOrder(p)
if p is not null
inOrder(leftChild(p))
visit(p)
inOrder(rightChild(p))

Question
Provide ONE (1) advantage of using AVL tree over the Hash table.

AVL is easier to create

11
Question
Calculate the balance factor for every node in the following BST. Is the BST balanced?
State the worst-case running times (in big-O) for performing a search in this BST.
Show in steps how to delete the node 15 (root node) from the BST so that the resulting
structure is still a BST.

O (lg n)

1. Node 15 has two children


2. Find the greatest node in its left subtree to delete it.
3. Node 12 is the greatest node in its left subtree.

12
4. When we delete node 15, the new root node is 12.
5. Copy the object at node 12 into node 15.

6. New BST

13
Question
Given a sequence of numbers below, draw a Binary Search Tree (BST) by inserting
the numbers.
15, 13, 19, 8, 5, 16, 34
1. 15 is the root node.
2. 13 < 15. It will be in the left child of node 15.
3. 19 > 15. It will be in the right child of node 15.
4. 8 < 15, 8 < 13. It will be left child of node 13.
5. 5 < 15, 5 < 13, 5 < 8.So, 5 is the left child of node 8.
6. 16 > 15, 16 < 19. It will be the left child of node 19.
7. 34 > 15, 34 > 19. 34 is the right child of node 19.

14
Fill in the empty rectangles with the balance factor.
Which node causes the BST not an AVL tree (or is not a balanced tree)?
Question

15
Apply rotation to convert the unbalanced tree in question above to an AVL tree.

Question
Find the balance factor for the BST below. Fill in the blanks provided.

16
Rotate the unbalanced BST to an AVL tree. Show the balance factor again.

17
Lec04 Priority Queues and Heaps

Question
Define the term maxheap.

A maxheap is a complete binary tree with the value of each node is greater than or equal
to the value of its children.

Explain the steps to enqueue 16 into this heap tree.

1. Place the value to enqueue,16 in the last node.

18
2. If 16 > than its parents node, swap it.

3. 16 < 17. So don’t swap.

Question
State the time complexity for forming an initial heap.
O(lg n)
When using an array to implement a heap, both the parent of a child and the
children of a parent can be located through mathematical formulas. Write down the
mathematical formulas.
Left child# = 2 * (parent#) + 1
Right child# = 2 * (parent#) + 2
Parent# = (child# - 1)/2

19
Question
Given the initial tree below, re-build the tree to a binary max heap.

20
21
Question
Convert the following initial tree to a max heap tree.

22
23
Lec05 Graphs, Graph Algorithms

Question
Spanning trees are produced when Depth-First Search is applied to the graph
below (start vertex A). Draw all FOUR produced spanning trees.

24
25
Question
State whether the following statement is true or false. Explain your answer using
time complexity.
Both adjacency list and adjacency matrix have the same time complexity when
inserting an edge into a graph.
False. Both of them have different time complexity.

Adjacency list is generally more efficient than adjacency matrix in terms of time complexity
when inserting an edge into a graph.

26
Question
Construct an adjacency matrix that best represents the graph below.

A B C D
A | 0 3 0 8 |
B | 3 0 5 9 |
C | 0 5 0 4 |
D | 8 9 4 0 |
Two spanning trees could be produced when Breadth-First Search (BFS) is applied
to the graph above. Produce the two spanning trees starting from the node A.

27
28
Question
Find the Adjacency Matrix and Adjacency List for the graph below:

Adjacency Matrix

A B C D E F
A | 0 10 0 9 0 0 |
B | 0 0 5 6 0 0 |
C | 0 0 0 0 7 9 |
D | 0 0 0 0 5 0 |
E | 0 0 0 0 0 6 |
F | 0 0 0 0 0 0 |

Adjacency List

Define the following terms.


- Depth-first search
- Breadth-first search
- Topological sorting
1. Depth-first search (DFS) is a graph traversal algorithm that visits all the nodes of a
graph by exploring as far as possible along each branch before backtracking

29
2. Breadth-first search (BFS) is a graph traversal algorithm that visits all the nodes of
a graph by exploring all the nodes at the current depth before moving to the nodes
at the next depth.

3. Topological sorting is a linear ordering of the nodes in a directed acyclic graph


(DAG) such that for every directed edge from node u to node v, u comes before v
in the ordering.

Find the order of the elements in the graph below if the Topological Sorting method is
applied.

Order : B, A, D, C, E

30
Lec06 Sorting Algorithms Part 1, Quick-Sort

Lec07 Sorting Algorithms Part 2, Selection


Algorithm

Question
Show in steps how to use SelectionSort to sort the sequence provided below.
Explain briefly why SelectionSort is faster or slower than MergeSort.
61 30 7 44 25
61 is the largest in the sequence. Swap it with 25
New sequence : 25 30 7 44 61

44 is the largest. No need to swap as it is in the correct position


New sequence : 25 30 7 44 61

30 is the largest in the sequence. Swap it with 7


New sequence : 25 7 30 44 61

25 is the largest in the sequence. Swap it with 7


New sequence : 7 25 30 44 61

MergeSort time complexity : O(n log n)


SelectionSort time complexity : O(n2)

MergeSort is faster than SelectionSort for large arrays because its time complexity grows
more slowly than SelectionSort.

Question
Show in steps how to use RadixSort to sort the sequence S provided below.
State one disadvantage of RadixSort when compared with QuickSort.
S = {(2,b), (11,c), (2,a), (39,d)}
Extract the numbers from S, N : 2 11 2 39
Sorted N : 2 2 11 39

31
According to alphabetical order a comes first before b
Hence sorted S using RadixSort : -
S = {(2,a), (2,b), (11,c), (39,d) }

Disadvantage : RadixSort requires additional memory to store the buckets used for each
digit place during the sorting process

Question
Show in steps how to use MergeSort to sort the following sequence.
State the worst-case time complexity for MergeSort.
61 30 7 44 25

Worst-case time complexity for MergeSort = O(n2)

32
Question
QuickSort has two steps: quicksort (recursive), and partition.
Write the pseudocode for the quicksort step. You may refer to the following header,
input,
and output.

Algorithm quicksort (S, p, r)


Input: Array S, start index p, end index r.
Output: S is sorted.
if p < r
pi = partition (S,p,r)
quicksort (S, p, pi-1)
quicksort (S, pi+1,r)
Provide sample data (array) and pivot that trigger the worst-case in QuickSort.
Sample data array :
[1,2,3,4,5,6,7,8,9,10]

If we use the first element as the pivot, then the left subarray will be empty and the right
subarray will have n-1 elements. This means that the recursive call on the right subarray
will be made with an array of size n-1 and an array of size 1, leading to a worst-case time
complexity of O(n2).

33
Question
Perform Selection-sort on the following numbers. Show the steps.
Between Selection-sort and Quick-sort, which algorithm has better running time?
64 59 33 49
64 is the largest . Swap it with 49

New Sequence : 49 59 33 64

59 is the largest now. So, swap it with 33

New Sequence : 49 33 59 64

49 is larger than 33. Swap it with 33

New Sequence : 33 49 59 64

Quick-sort has better running time than Selection-sort

Use the Quick-sort algorithm to sort the following sequence of integers. Write down the
intermediate outputs and the corresponding pivot. Assume that the first element is the pivot.
What is the running time (in big-O) for the Quick-sort on this sequence of integers?
35 49 65 68 75
Pivot = 35
35* 49 65 68 75

Left subarray = []
Right subarray = [49 65 68 75]

Current subarray = [49 65 68 75]


Pivot = 49
49* 65 68 75

Left subarray = []
Right subarray = [65 68 75]

Current subarray = [65 68 75]


Pivot = 65
65* 68 75

Left subarray = []
Right subarray = [68 75]

34
Current subarray = [68 75]
Pivot = 68
68* 75

Left subarray = []
Right subarray = [75]

Current subarray = [75]


Pivot = 75
75*

Left subarray = []
Right subarray = []

Since the left subarray and right subarray is empty, it is sorted.

Sorted array = 35 49 65 68 75

Time complexity = O(n2)

Show in steps how to use Radix-Sort to sort the following 3-bit binary numbers.
State the time complexity of Radix-Sort.
110, 101, 010, 011

A B C

1 1 0

1 0 1

0 1 0

0 1 1

35
Sort according to bits in C

A B C

1 1 0

0 1 0

1 0 1

0 1 1

Sort according to bits in B

A B C

1 0 1

1 1 0

0 1 0

0 1 1

Sort according to bits in A

A B C

0 1 0

0 1 1

1 0 1

1 1 0

The table above is equivalent to : 2, 3, 5, 6


Hence, it is sorted.

36
Show in steps how to use Merge-Sort to sort the following integers.
78 52 39 80 24

37
An incomplete Quick-Select algorithm is provided below.
Suggest the code for LINE3 to LINE6 to complete the algorithm.

Algorithm QuickSelect (A, p, r, k) {


Input Parameters: array A, start index p, end index r,
target smallest index k.
Output Parameter: Target smallest element A[k].
LINE1: if (p <= r)
LINE2: pi = Partition (A, p, r)
LINE3: if (k == pi)
LINE4: return A[pi]
LINE5: if (k < pi)
LINE6: QuickSelect (A,p, pi-1, k)
LINE7: else
LINE8: QuickSelect (A, pi+1, r, k)

38
Question
Complete the pseudo-code for the recursive quicksort algorithm below.
/* low Starting index, high Ending index */
void quicksort(arr[ ], low, high)
{
if ( low < high ) // check if the low and high relationship is true
{

pi = partition(arr[], low, high) ; // pi is partitioning index, pivot index

quicksort(arr, low, pi-1) ; // Recur before pi

quicksort(arr, pi+1, high) ; // Recur after pi

}
}

39
Question
Using the Radix sorting method, show the steps to sort the following numbers.
249, 458, 391, 352, 822, 423

40
Question
Define the following terms and give an example for each of them.
i) Stable sorting algorithm
ii) In-place algorithm
i) Stable sorting algorithm = an algorithm that maintains the relative order of equal
elements in the sorted output as they appeared in the original unsorted input.

Example :
Suppose we have a list of 5-letter word :
peach straw apple spork

If we sort the words by the first letter of the word, it can be stable :
apple straw spork peach

The word straw comes before spork. So, it will be sorted first in the list.

ii) In-place algorithm = an algorithm that operates directly on the input data structure
without requiring any extra space to store intermediate results.

Example : Quick-sort

Using the Quick-Select algorithm to find the 7th smallest element in the following array.
Make the last element of the current sub-array to be the pivot element.
Array: 26, 93, 3, 87, 44, 61, 17, 52, 38
Note: Fill in the table below and the underlines. The first row answer has been provided.
We look for k = 6.
The 7th smallest element is 61.
Pivot Array after partition pi k is equal to / smaller / large than pi

38 26, 3, 17, 38*, 93, 87, 44, 61, 52 3 k > pi

52 26, 3, 17, 38*, 44, 52*, 93, 87, 61 5 k > pi

61 26, 3, 17, 38*, 44, 52*, 61*, 93, 87 6 k == pi

41
Apply Radix sort algorithm to sort the original array below. Fill in the blanks in the table
including the header for each column.
Original array Array after d = Ones Array after d = Tens Array after d =
Hundreds

912 621 912 327


621 912 621 524
327 693 524 621
693 524 327 693
524 327 693 912

Array after d = Ones

42
Array after d = Tens

43
Array after d = Hundreds

44

You might also like