# Review questions for final exam ```Final Exam Review Questions
1. Consider finding a minimum spanning forest under the restriction that a specified subset of
the edges must be included or excluded. Can Kruskal's or Prim's algorithm be adapted to
handle these restrictions? Explain your reasoning.
Yes, with the understanding that the specified subset not include a cycle and that
the resulting tree (or forest) is not necessarily minimum because some of the
specified edges may be longer than those that would appear in a minimum tree. In
this sense, if a set of edges must be included, the resulting tree or forest is minimal
but not necessarily minimum.
2. Set up and solve the recurrence relation for the function below.
function g(n)
if n  1 then return (n)
else return(5•g(n-1) – 6•g(n-2))
Recurrence: an = 5an-1 - 6an-2. Basis: a0 = 0 and a1 = 1
The roots of the characteristic equation are 2 and 3 and the solution is a n = 3n - 2n.
3. Given an adjacency-list representation of a directed graph G = (V, E), how long does it
take to compute the outdegree of every vertex? |V| + |E| time since each edge appears
in exactly one list and all lists must be examined. I've included the |V| because each
vertex must have its outdegree calculated.
How long does it take to compute the indegree of each vertex? Assuming
supplementary storage is used to hold the indegree values, one pass through the
edges will enable us to determine all of the indegrees.
4. An array T contains n elements. You want to find the m elements beginning with the
median i.e. n/2, n/2 + 1, ... n/2 + m - 1 where m is much smaller than n. What is the most
a. sort T and pick the first m elements
b. use the select algorithm
c. use some other method?
d. use a combination of methods?
The one that I feel is best is to use a combination of methods. Use the select
algorithm to find the n/2 element. You could then use that same method on the
remaining numbers that are greater than or equal to n/2 and look for the mth element
in that range. However, since m is much smaller than n, you could use selection
sort to find the next m elements.
5. Use a divide and conquer approach to devise a procedure to find the largest number in a
set of n distinct integers. Give a recurrence relation for the number of comparisons
performed and solve the recurrence relation.
Approach: Repeatedly divide the list in half until the list size is two. In that case
one comparison is sufficient to determine which of the two numbers is larger. In
general, take the largest number in each half, compare them and return the larger of
those two values. The basis case is f(2) = 1. The recurrence relation is f(n) = 2f(n/2)
+ 1. Assuming that n = 2k and using backward substitution we get
f(n) = 2k-1•f(2) + 2k-2 + 2k-3 + … + 2 + 1 = 2k-1 +2k-1 - 1 = n - 1
6. Suppose that f(n) is O(g(n)) and f(n)  (g(n)). Why is a program with time complexity of
O(f(n)) not necessarily better than one with time complexity O(g(n))?
Suppose h(n) = n + 5, f(n) = n2 and g(n) = n3. Then, h(n) = O(f(n) and the other
conditions are satisfied. Now, suppose h'(n) = n + 6. Clearly, h'(x) = O(g(n)) but the
complexity of h(n) is not better than the complexity of h'(n).
7. Use definitions to prove or disprove the following:
a. If f(n) = (g(n)) and g(n) = O(h(n)) then f(n) = O(h(n)).
This is true. If f(n) = (g(n)) then f(n) = O(g(n) so there are constants c1 and k1
such that f(n) ≤ c1•g(n) for all n ≥ k1. If g(n) = O(h(n)) then there are constants c2
and k2 such that g(n) ≤ c2•h(n) for all n ≥ k2. Thus, f(n) ≤ c1•c2•h(n) for all n ≥
max(k1, k2).
b. (n + 1)! = O(n!)
This is false. Suppose (n + 1)! = O(n!). Then, there are constants c and k such
that (n + 1)! ≤ c•n! for all n ≥ k. Since (n + 1)! = (n + 1)•n!, we find that this
implies (n + 1) ≤ c. If we choose n to be the larger of c and k, then we have a
8. A graph G has n vertices and e edges. In terms of n and/or e, what is the worst case
complexity of determining if u and v are adjacent if G is represented by an adjacency
9. Solve the following recurrence an = 2an-1 + 3 for n  2, a1 = 2
Using backward substitution, we have
an = 2an-1 + 3
= 2[2an-2 + 3] + 3 = 22an-2 + 2•3 + 3
=…
= 2n-1a1 + 2n-2•3 + … + 2•3 + 3 Remember a1 = an-(n-1)
= 2n-1a1 + 3•(2n-2 + … + 2 + 1)
= 2n + 3(2n-1 -1) = 5•2n-1 - 3
10. Suppose the elements in A[1..n] are all distinct and are sorted. The following sequential
search is used to determine if an element x is in the list:
i1
A[n+1]  x
while A[i] &lt; x do
ii+1
if (i  n) and (x = A[i]) then return i
else return 0
How many comparisons are required in the best case? 3-- x is the smallest number.
worst case? n+ 2--x is not on the list so it is &quot;found&quot; in A[n+1]. (This assumes that the x =
A[i] test is not made in this case.)
11. Describe a function that returns the height of an AVL tree by tracing only one path to a leaf,
not by examining all nodes in the tree.
Beginning at the root, check the balance factor at each node encountered on the
path. Then take the left or right branch depending on whether the left or right
subtree is taller. If the balance factor is 0, take either branch.
12. Consider the median of medians method for finding the kth element of a sorted list.
Suppose each sublist has size 3. Set up the recurrence relation for the select algorithm in
this case.
T(n) = T(n/3) + T(2n/3) + c•n
13. Show the result of the following sequence of instructions: union(1, 2), union(3, 4), union(3, 5).
union(1, 7), union(3, 6), union(8, 9), union(1, 8), union(3, 10), union(3, 11), union(3, 12),
union(3, 13), union(14, 15), union(1, 3), union(1, 14) where
a. union by height is used
b. union by size is used
14. Perform a find with path compression on the deepest node in each of the trees in your answer
to problem 13.
```