Sunteți pe pagina 1din 15

COMP90038 Practice Exam Paper (2)

Share Link:
https://docs.google.com/document/d/1260hQItOx5O3T_JUBgOVgth4N81EjFscuo4YBBmeuxI/e
dit?usp=sharing

1. (1 mark) What is the fundamental difference between the stack ADT and a queue ADT?

The fundamental difference is the order of taking elements out form the data structure... Stack
ADT goes with Last in First out approach, while in Queue ADT, First in First out. For exampling
inserting 1,2,3 in stack would result in 3,2,1 while taking items out. But with queue if we insert
1,2,3, they will be taken out as 3,2,1.

2. (2 marks) Consider the following algorithm.

i. What does this algorithm compute? ​The minimum element in the array
ii. What is its basic operation? ​Comparison “temp <= A[n-1]”
iii. How many times is the basic operation executed? ​n-1 times
iv. What is the efficiency class of this algorithm? Show your working.
M(0) = 0
M(n) = M(n-1) + 1
= (M(n-2) + 1) +1
= M(n-2) + 2
= M(n-3) + 3
= M(n-n) + n
= M(0) + n
=0+n
=n
So efficiency is O(n)
Maybe another possible approach: Sum from i=1 to n of basic operaration 1 equals n
3. (2 marks) Use the Master Theorem to find the order of growth for the recurrence
T(n) = 4T(n/2) + n2
a = 4, b = 2, d=2 ⇒ a = b^d (4 = 2^2)
So based on master theorem ⇒ O(n^b log n) = O(n^2 log n)

4. (1mark) What is the worst case complexity of selection sort? Use big-Oh notation.
O(n^2)

5. (1 mark) What is the best case complexity of merge sort? Use big-Oh notation.
O(n log n)

6. (4 marks) For each of the following statements, indicate whether it is true or false:
a. “Heap sort is a stable sorting algorithm”. ​False (it is not stable)
b. “The worst case complexity of merge sort is O(n²).​ False (n log n)
c. “Insertion sort is a stable algorithm”. ​True
d. “The height of a complete binary tree with N nodes is N”. ​False (log n). ​Example:
BT[1,2,3] has height 1

7. (2 marks) Explain briefly when a sorting algorithm is said to be in-place.


When the sorting algorithm does not require extra memory/space for sorting it is called in-place.
For example Heap sort does the sorting by swapping elements in the same array without using
extra array thus it is in-place, however sort by counting is not in place because the content of
the original array get copied to new arrays during the sorting process.
A few auxiliary variables are allowed though.

Section B (32 marks): Answer all the questions

8. (3 mark) Sequential search can be used with about the same efficiency whether a list is
implemented as an array or a linked list. Is this statement true or false for binary search? Justify
your answer.
False, binary search depends on random access of items (using index locations) for which an
array would have O(1) complexity while a linked list would require O(n) to get the item at a
certain position. This will lead to increasing the algorithm efficiency for getting the item at the
mid position when using linked list ⇒ So the complexity can reach to O(n log n)
9. (3 marks) You are managing a software project that involves building a computer-assisted
instrument for medical surgery. The exact placement of the surgical knife is dependent on a
number of different parameters, usually at least 25, sometimes more. Your programmer has
developed two algorithms for positioning the cutting tool, and is seeking your advice about
which algorithm to use:
Algorithm A has an average – case run time of n, and a worst case run time of n 4 ,
where n is the number of input parameters.
Algorithm B has an average case run time of n(log n) 3 , and a worst case of n 2 .

Which algorithm would you favour for inclusion in the software? Justify your answer.

Algorithm B?

I would suggest Algorithm B for two reasons:


1) B is much more efficient than A in the worst scenario, and it is not too bad when
compared with A in the average case scenario.
2) This project is for medical surgery so the feasibility is very important in all kinds of
scenarios. Thus we do care about the worst case performance.​ (Actually for this reason I
am doubtful that we should go with Algorithm A, as in practice the average case is the
one that would occur practically most of the time, so since it is medical surgery, it might
be better to go with the algorithm that have better average runtime, sacrificing the rare
maybe never occurring possible incidents where the algorithm would run into the terrible
worst case?) ​The placement of the medical instruments might be critically time
dependant so I’d pick B too. Wouldn’t want the machine to be stuck for 20 minutes
during an operation because it can’t calculate where to put the knife...
10. (4 marks) This question is about graph algorithms

a. (2 marks) Sketch a graph that could be used to model the friendship network of a group of
four people. Draw the corresponding adjacency matrix representation of your graph.

There are three assumptions in this question:


1) A person is not friend of him/herself;
2) If A and B are friends, and B and C are friends, it is not necessary that A and C are
friends.
3) If A is a friend of B, then B must be a friend of A.
4) Weighted graph is also possible to model the level of friendship, assuming the level is
mutual..
0 1 1 1

1 0 1 1

1 1 0 1

1 1 1 0

b. (2 marks) Traverse the graph by Breadth First Search (BFS). You may start the traversal at
vertex 10 and construct the corresponding BFS forest:

10, 15, 20, 60, 45, 70, 90


11. (5 marks) This question is about heaps and heap sort.
a. (3 marks) Sort the following list (into increasing order) by heap sort, using array
representation of heaps. Show your workings. 13, 21, 16, 40, 30, 2, 10
13, 21, 16, 40, 30, 2, 10
13, 40, 16, 21, 30, 2, 10
40, 13, 16, 21, 30, 2, 10
40, 30, 16, 21, 13, 2, 10 (Heap Constructed, start deleting to sort)

10, 30, 16, 21, 13, 2| 40


30, 10, 16, 21, 13, 2| 40
30, 21, 16, 10, 13, 2| 40
2, 21, 16, 10, 13| 30, 40
21, 2, 16, 10, 13| 30, 40
21, 13, 16, 10, 2| 30, 40
2, 13, 16, 10| 21, 30, 40
16, 13, 2, 10| 21, 30, 40
10, 13, 2| 16, 21, 30, 40
13, 10, 2| 16, 21, 30, 40
2, 10| 13, 16, 21, 30, 40
10, 2| 13, 16, 21, 30, 40
2| 10, 13, 16, 21, 30, 40
2, 10, 13, 16, 21, 30, 40 (Sorted)

b. (2 marks) State whether the following statements are true or false:


i. A heap is a complete binary tree. ​True
ii. The heap structure must satisfy either the structural constraint or the value relationship
constraint. ​False (It must satisfy both structural and value relationship). ​Structural:
Complete binary tree, value relationship: child nodes smaller or bigger than parent node
for max-heap and min-heap respectively.

12. (8 marks) This question is about search trees


a. (2 marks) What is the relationship between the value stored at a node and the values stored
at its children in a Binary Search tree? ​Smaller or equal value child is stored to the left and
greater is stored to the right of the node.
b. (2 marks) Suggest one limitation of using a Binary Search Tree in search applications? ​Worst
case scenario has the complexity of O(n), which happen if all items are to one side of the tree (
unbalanced tree)
c. (2 marks) Build an AVL tree for the list of items: 1 6 2 1 5 8 7 4 2.

The task did not even mention how to handle duplicate values. I guess we can pick whether we
choose the left or right side for duplicates or use a count variable for duplicates…
https://www.geeksforgeeks.org/how-to-handle-duplicates-in-binary-search-tree/

d. (2 marks) Show one possible binary search tree that can result from deleting 60 in the

following binary search tree:

Exercices for AVL trees:

13. (9 marks) This question is about hashing.


a. Insert items with the following keys into a hash table of size 7
12 7 6 8 Use the hash function h(k) = k(k+3) mod 7.
i. (2 marks) Use separate chaining for collision resolution. Show the hash value you have
calculated for each input key, and show the hash table after all items have been inserted.
h(12) = 12 * (12+3) mod 7 = 180 mod 7 = 5
h(7) = 0
h(6) = 5
h(8) = 4

0 1 2 3 4 5 6

7 8 12

ii. (4 marks) Use linear probing for collision resolution. Show the hash value you have
calculated for each input key, and show the hash table after all items have been inserted.

0 1 2 3 4 5 6

7 8 12 6

iii. (1 mark) For the resulting hash table using linear probing, how many probes are needed in
a search for the key 5.
4 probs​​ as the probing starts at index 5 then 6 then 0 and finally will stop at 1 as the
cell is empty. + 1 ---- ​Source

b. (2 marks) Why is it not a good idea for a hash function to depend on just one letter (say the
first one) of a natural language word?
The chances of collision will be very high as the english letters are limited and the
number of possible words are too high​. Using one letter for the hash function would mean that
there will be 26 possible different addresses, based on the nature of english language some
letters would be more commonly used as starting letter (i.e. E, A,...) Thus there won’t be proper
even distribution to the data based on such hasing function.

Section C (25 marks) Answer all the questions

14. (6 marks) Write a correct and detailed algorithm that takes as input a Binary Search Tree
(BST) T and finds the second largest value in the given BST. You may assume that the BST
has at least two nodes, and that all of the node values are distinct. You must use clear,
appropriately commented pseudo-code, Java or C to write your algorithm. Then, analyze the
time and space complexity of your algorithm. Show your working for the complexity analysis.
In BST the larger elements will be to the right side of the tree so if we have an algorithm similar
to in order traversal but going from the right most first instead of left most then second item
traversed item will be the 2nd largest value of the tree

function FindSecondLargest(T, count){


If (T is null or count >=2)
Return;
FindSecondLargest(T.right, count)
Count++ //traversal of right most is complete
If count = 2
Return T.value;

FindSecondLargest(T.left, count)
}
15. (9 marks) Building the east-west road link created a budget blowout for the Victorian
Government even before the project started. However, in the quest to provide better road
infrastructure for suburbs in Melbourne, the Victorian Government in partisan with the road
authorities, have now decided to lay bus routes connecting suburbs between the east and west
of Melbourne. The road authorities have asked the project manager to start work on this project.
The project manager has called on you – the software engineer – to design and implement an
algorithm that manages a bus router problem between suburbs in Melbourne. The road
authorities want to have bus routes in the suburb such that there is at least one bus route
connecting every pair of suburbs. Furthermore, as laying roads is very expensive, the road
authorities want to minimize theTmernnecting suburbs. You must use clear, appropriately
commented pseudo-code, Java or C to write your algorithm.

Floyd’s Algorithm to find shortest path (where the path weight is the cost of construction)
Let’s say all possible connections between bus rutes is stored in Distance Matrix C.

Floyd’s algorithm would only give the weight of the shortest paths between stops, so most
probably we need to use Dijkstra algorithm to identify the shortest route from a certain
starting point

function findShortestPaths(c[][])
{
for k ← 1 to n
for i ← 1 to n
for j ← 1 to n
if c[i,j] > c[i,k] + c[k,j] 4c[i,j] ← c[i,k] + c[k,j]

return c
}

I would suggest using the Prim Algorithm here. The reason is for Prim Algorithm, we build up a
minimum spanning tree, which connects all vertices, while the total weight is the shortest.
Each suburb is abstracted as a vertex, connecting any two of the suburbs with an edge, while
the distance between two suburbs is assigned as the weight to each edge.

Function shortestRoute(G)
//input: G = (V, E)
//output: E’ (edges) and total(total route length)
Total = 0
V’ = {v0}
E’ = null
For i = 1 to |V| - 1
Find a minimum edge e’ = (v’, u’) among all the edges (v, u), where v is in V’, and u is
not in V’
V’ = V’ join {u’}
E’ = E’ join {e’}
Total = total + weight of e’
Return E’, total

If the above graph is represented as adjacency matrix, the complexity is O(|V|^2);


If is represented as adjacency list and priority queue, the complexity is O(|E| log|V|)

16. (10 marks) Let A[0 ..n-1] be an array of n distinct numbers. If i < j and A[i] > A[j], then the
pair (i, j) is called an inversion of A.

a) List the inversions of the array (2, 3, 8, 6, 1).


(0, 4), (1, 4), (2, 3), (2, 4), (3, 4)

b) What arrays of size n have the largest number of inversions and what is this number?
arrays sorted in descending order
n(n-1)/2.​ This can be derived by summing up i from i=1 to n-1. In the case i=1 to n, the sum
would be equal to n*(n+1)/2 (can be proven by induction). By replacing n with n-1 , we get
that formula.

Assume there are n distinct elements, so n(n-1)/2 ordered pairs - (A[i], A[j]) with i < j , thus the
maximum cannot exceed n(n-1)/2

c) What arrays of size n have the smallest number of inversions and what is this number?
arrays sorted in ascending order
0

d) Write two different algorithms that you could use to count the number of inversions in a list of
n elements, one based on each of the following design strategies:
(i) Brute force

(ii) Divide and conquer


You must use clear, appropriately commented pseudo-code, Java or C to write your
algorithm.

e) Analyze the complexity of each algorithm and determine the efficiency class. You must show
how you arrived at your answer.

The complexity of the Brute Force algorithm is Theta(n^2).


The input size: n (length of the array)
Basic Operation: comparison of two elements in the array.
Mathematical derivation shown on p.64 letivin. Sum from i=0 to n-2 of sum of j=i+1 to n-1 of
basic operation 1 leads to (n*(n-1))/2.
Thus the Time complexity is T(n) = sum_(i = 0 to n-2) (n-1-i) = n(n-1)/2, which is in Theta(n^2)
for best and worst cases.

The complexity of the Merge Sort algorithm is Theta(nlogn)


The input size: n (length of the array)
T(n) = 2 T(n/2) + T_merge
T_merge is in Theta(n), according to the Master Theorem, this is in Theta(nlogn)
Other Past Papers Questions:

M(0) = 0
M(n) = M(n - 2) + 1
M(n) = M(n - 2) + k

M(n) = M(n - 2) + 1
= (M(n - 4) + 1) + 1
= M(n - 4) + 2
= M(n - 6) + 3
= M(n - 8) + 4
= M(n - n) + (n/2)
= M(0) + (n/2)
= (n/2)
So the asymptotic time complexity is O(n)
On the assumption that we only need to return any vertex and -1 if no vertex found, following is
the algorithm:

Function FIND_VERTEX(G){
For i ⇐ 0 to n
For j ⇐ 0 to n
If G[i][j] != 0
return i
return -1
}

Binary Search has a complexity of O(log n), if we introduce caching using unsorted linked-list,
this increases the complexity to O(n) as searching an unsorted linked-list first (lead to O(n)) then
the binary search if the element is not in the linked list will have complexity of n+log n ⇒ O(n).​
Assumption: as stated the array is assumed to be bitonic, so if the array only have elemented
sorted only in ascending or descending order it will fail due to out of bound index

Function FindMax(A[], lo, hi)


{
Mid = lo + (hi-lo)/2
if A[mid] > A[mid-1] and A[mid] > A[mid+1] //the highest point found
return mid
if A[mid] > A[mid+1] //the array is decreasing so m would be on the left side of the array
return FindMax(A[], lo, mid)
else
return FindMax(A[], mid, hi)
}

S-ar putea să vă placă și