Exam Examples
Exam Examples
I. Suppose you are choosing between the following three algorithms: (15 points)
1. Algorithm A solves problems by dividing them into five sub problems of half
the size, recursively solving each sub problem, and then combining the
solutions in linear time.
2. Algorithm B solves problems of size n by recursively solving two sub problems
of size
n − 1 and then combining the solutions in constant time.
3. Algorithm C solves problems of size n by dividing them into nine sub
problems of size n/3, recursively solving each sub problem, and then
combining the solutions in O(n2 ) time.
What are the running times of each of these algorithms (in big-O notation), and which
would you choose?
II. 3-way-Merge Sort: Suppose that instead of dividing in half at each step of Merge
Sort, you divide into thirds, sort each third, and finally combine all of them using
a three-way merge subroutine. What is the overall asymptotic running time of
this algorithm? (Hint: Note that the merge step can still be implemented in O(n)
time.)
(5 points)
Q5 (15 points) Get MST using prim and Kruskal [7.5 for each one]
start 12 8
9 1 9
2
5 3 11
8 7 5
3
10
1
Q6 )10 points) Get shortest paths from node S to each node using Dijkstra algorithm
, show each step and show the color of each node and its BH.
Q8 [5 points ] Construct variable length code using Huffman for the following characters
statistics A = 20 , B =30 , C = 15 , D=5, E=8, and F=22 .
Q9 [10 marks]
There is a row of n coins whose values are some positive integers c₁, c₂,...,cn, not necessarily distinct.
The goal is to pick up the maximum amount of money subject to the constraint that no two coins
adjacent in the initial row can be picked up. Determine the best selection which giving the maximum
amount for the following data 5, 1, 2, 10, 6, 2, using dynamic programming
1 2 3 4 5 6
5
Question (1) [ 39 points ] Write T for True or F False.
# Item
1 For two algorith s A and B, if O(A) = n^100 and O(B) = 10^n, then
algorithm A is the better choice
4 The master method can be used to solve all recurrence equations that
have the form T(n) = aT(n/b) + f(n).
13 We do not have to make any key comparisons to find the min or max
nodes of a binary search tree.
18. What is the name of the technique that stores computed values for future lookup, rather than
recomputing them?
20. Which of the following is true regarding Prim's algorithm for finding a minimum spanning
tree?
A. The starting node must be connected to the edge with the least weight.
C. "Marked" nodes indicate nodes that will not be in the final tree.
D. When selecting the next edge, we consider only edges that connect unmarked nodes to the
other unmarked nodes.
21. What distinguishing feature should cause us to choose dynamic programming rather than a
divide-and-conquer approach to a problem?
A. It is an optimization problem. B. The problem can be solved recursively.
C. The problem does not have a brute force solution. D. The subproblems for the solution
overlap.
C All simple paths from a node to the leaves contain the same number of red and black nodes.
A. Best case running time for Quicksort B. Worst-case running time for Quicksort
C. Best-case running time for Heapsort D. Worst-case running time for Heapsort
25. Given the algorithm for Bubblesort below, what is an accurate description of it's running
time? BUBBLESORT(A) for I = 1 to A.length -1 for j = A.length downto I + 1 If A[j] <
A[j-1] exchange A[j] with A[j-1]
26. What is the smallest value of n such that an algorithm whose running time is n^2 runs slower
than an algorithm whose running time is 3n + 1?
A. 2 B. 25 C. 4 D. 15
k-way-Merge Sort. Suppose you are given k sorted arrays, each with n elements, and you want
to combine them into a single array of kn elements. Consider the following approach. Using the
merge subroutine taught in lecture, you merge the first 2 arrays, then merge the 3rdgiven array
with this merged version of the first two arrays, then merge the 4th given array with the merged
version of the first three arrays, and so on until you merge in the final (kth) input array.
27. the is the running time taken by this successive merging algorithm, as a function of k and
n, is.
A. nk B. n2k C. nk2 D. 1
28. the amortized analysis for dynamic hash table which start by one size and extend 2
power i I in 0 1 ,2 4 8 ……. By aggregation methods
A. n B. n2 C. nlgn D. 1
Question (5) [4 points ]
29. inserting these numbers in (1,2,3,4 ) in red-blact tree will give the follwing tree
A. b B.
C. D.
Finding the shortest path for the following graph from node 1 (one) to each nodes using
Dijkstra’s algorithm which of the following solution steps are true and which steps are false .
30. parent 3 1 2 4 8 3 7 6
node 1 2 3 4 5 6 7 8 9
31. 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
32. 5 2 ∞ ∞ 45 ∞ ∞ ∞
33. 4 ∞ 46 ∞ 11 ∞ ∞
34. 7 11 90 11 ∞ ∞
35. 9 ∞ 11 91 ∞
36. 17 11 16 96
37. 17 13 ∞
38. 16 ∞
39. 20
Paths
Each of the figures below represents a partial spanning tree. Determine whether it could
possibly be obtained from, Prim’s algorithm, Kruskal’s algorithm, both or neither.
Question (2) Analysis of Algorithms (10 points)
For each code fragment on the left, check the best matching order of growth of the running time.
You may use an answer more than once or not at all. Explain your selection.
Question (3)- five sorting algorithms (10 points)
The column on the left is the original input of strings to be sorted the column on the right are the
strings in sorted order; the other columns are the contents at some intermediate step during one
of the algorithms listed below. Match up each algorithm by writing its number under the
corresponding column. Use each number exactly once, explain your answer.
0 1
(0) Original input (1) Sorted (2) Selection sort (3) Insertion sort
Q7 You are given a unimodal array of n distinct elements, meaning that its entries are in increasing
order up until its maximum element, after which its elements are in decreasing order. Give an
algorithm to compute the maximum element that runs in O(logn) time. (5 points) and give an
example