COMP90038 Practice Exam Paper (2) : Dit?usp Sharing
COMP90038 Practice Exam Paper (2) : Dit?usp Sharing
COMP90038 Practice Exam Paper (2) : Dit?usp Sharing
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.
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
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?
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.
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:
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
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.
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
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
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.
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
e) Analyze the complexity of each algorithm and determine the efficiency class. You must show
how you arrived at your answer.
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