UBI 534 Fundamental Algorithmic Techniques Spring 2013/2014 1

UBI 534
Fundamental Algorithmic Techniques
Spring 2013/2014
1. Is 2n+1 = O(2n )? Is 22n = O(2n )?
2. Show that for any real constants a and b, where b > 0,
(n + a)b = Θ(nb )
(1)
3. Rank the following functions by order of growth:
n3
en
ln n
(3/2)n
lg2 n
n · 2n
2n
n!
4. Use a recursion tree to give an asymptotically tight solution to the recurrence
T (n) = T (αn) + T ((1 − α)n) + cn, where α is a constant in the range 0 < α < 1 and c > 0 is also a
constant.
5. Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Assume that
T (n) is constant for n ≤ 2. Make your bounds as tight as possible and justify your answers.
(a) T (n) = 2T (n/2) + n3
(b) T (n) = 16T (n/4) + n2
(c) T (n) = T (n − 1) + n
(d) T (n) = 7T (n/3) + n2
6. Argue that the solution to the recurrence T (n) = T (n/3) + T (2n/3) + cn where c is a constant, is
Ω(n lg n) by appealing to a recursion tree.
7. What are the minimum and maximum number of elements in a heap of height h?
8. Illustrate the operation of Heapsort on the array A = h5, 13, 2, 25, 7, 17, 20, 8, 4i
9. The procedure Build-Max-Heap can be implemented by repeatedly using Max-Heap-Insert to
insert the elements into the heap. Consider the following implementation:
Build-Max-Heap0(A)
heap-size[A] ← 1
for i ← 2 to length[A]
do Max-Heap-Insert(A, A[i])
(a) Do the procedures Build-Max-Heap and Build-Max-Heap0 always create the same heap
when run on the same input array? Prove that they do, or provide a counter example.
(b) Show that in the worst case, Build-Max-Heap0 requires Θ(n lg n) time to build an n-element
heap.
10. Show that the running time of Quicksort is Θ(n2 ) when the array A contains distinct elements
and is sorted in decreasing order.
11. Show that quicksort’s best-case running time is Ω(n lg n).
12. How can randomizing help quicksort?
13. Describe an algorithm that, given n integers in the range 0 to k, preprocesses its input and then
answers any query about how many of the n integers fall into a range [a..b] in O(1) time. Your
algorithm should use Θ(n + k) preprocessing time.
14. Show how to sort n integers in the range 0 to n2 − 1 in O(n) time.
15. You are given an array of integers, where different integers may have different numbers of digits, but
the total number of digits over all the integers in the array is n. Show how to sort the array in O(n)
time.
16. Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming
simple uniform hashing, what is the expected number of collisions? More precisely, what is the
expected cardinality of {{k, l} : k 6= l and h(k) = h(l)}?
UBI 534
Fundamental Algorithmic Techniques
2/3
17. A hash table of size m is used to store n items, with n ≤ m/2. Open addressing is used for collision
resolution.
(a) Assuming uniform hashing, show that for i = 1, 2, . . . , n, the probability that the ith insertion
requires strictly more than k probes is at most 2−k .
(b) Show that for i = 1, 2, . . . , n, the probability that the ith insertion requires more than 2 lg n
probes is O(1/n2 ).
18. Suggest how storage for elements can be allocated and deallocated within the hash table itself by
linking all unused slots into a free list. Assume that one slot can store a flag and either one element
plus a pointer or two pointers. All dictionary and free-list operations should run in O(1) expected
time. Does the free list need to be doubly linked, or does a single linked free list suffice?
19. Show that if a node in a binary search tree has two children, then its successor has no left child and
its predecessor has no right child.
20. We can sort a given set of n numbers by first building a binary search tree containing these numbers
(using Tree-Insert repeatedly to insert the numbers one by one) and then printing the numbers by
an inorder tree walk. What are the worst-case and best-case running times for this sorting algorithm?
21. An inorder tree walk of an n-node binary search tree can be implemented by finding the minimum
element in the tree with Tree-Minimum and then making (n − 1) calls to Tree-Successor. Prove
that this algorithm runs in Ω(n) time.
22. Suppose that we “absorb” every red node in a red-black tree into its black parent, so that the children
of the red node become children of the black parent (Ignore what happens to keys). What are the
possible degrees of a black node after all its red children are absorbed? What can you say about the
depths of the leaves of the resulting tree?
23. Show that the longest simple path from a node x in a red-black tree to a descendant leaf has length
at most twice that of the shortest simple path from node x to a descendant leaf.
24. Suppose that a node x is inserted into a red-black tree with RB-insert and then immediately deleted
with RB-delete. Is the resulting red-black tree the same as the initial red-black tree? Justify your
answer.
25. Show how to compute the length of an Longest common subsequence (LCS) using only 2 · min(m, n)
entries in the c table plus O(1) additional space. Then show how to do this using min(m, n) entries
plus O(1) additional space.
26. Consider the problem of neatly printing a paragraph on a printer. The input text is a sequence of
n words of lengths l1 , l2 , . . . , ln , measured in characters. We want to print this paragraph neatly
on a number of lines that hold a maximum of M characters each. Our criterion of “neatness” is
as follows. If a given line contains words i through j where i ≤ j, and we leave exactly one
Pj space
between words, the number of extra space characters at the end of the line is M − j + i − k=1 lk ,
which must be nonnegative so that the words fit on the line. We wish to minimize the sum, over all
lines except the last, of the cubes of the numbers of extra space characters at the ends of lines. Give
a dynamic-programming algorithm to print a paragraph of n words neatly on a printer. Analyze the
running time and space requirements of your algorithm.
27. Let R(i, j) be the number of times that table entry m[i, j] is referenced while computing other table
entries in a call of Matrix-Chain-Order. Show that the total number of references for the entire
table is
n X
n
X
i=1 j=1
R(i, j) =
n3 − n
3
(2)
28. Suppose that instead of always selecting the first activity to finish, we instead select the last activity
to start that is compatible with all previously selected activities. Describe how this approach is a
greedy algorithm, and prove that it yields an optimal solution.
UBI 534
Fundamental Algorithmic Techniques
3/3
29. Give a dynamic-programming solution to the 0 − 1 knapsack problem that runs in O(nW ) time,
where n is the number of itesm and W is the maximum weight of items that the thief can put in his
knapsack.
30. Professor Midas drives an automobile from Newark to Reno along Interstate 80. His car’s gas tank,
when full, holds enough gas to travel n miles, and his map gives the distances between gas stations
on his route. The professor wishes to make as few gas stops as possible along the way. Give an
efficient method by which Professor Midas can determine at which gas stations he should stop, and
prove that your strategy yields an optimal solution.
31. Give an example of a directed graph G = (V, E), a source vertex s ∈ V , and a set of tree edges
Eπ ⊆ E such that for each vertex v ∈ V , the unique path in the graph (V, Eπ ) from s to v is a
shortest path in G, yet the set of edges Eπ cannot be produced by running BFS on G, no matter
how the vertices are ordered in each adjacency list.
32. Give a counter example to the conjecture that if there is a path from u to v in a directed graph G,
and if d[u] < d[v] in a depth-first search of G, then v is a descendant of u in the depth-first forest
produced.
33. Give an algorithm that determines whether or not a given undirected graph G = (V, E) contains a
cycle. Your algorithm should run in O(V ) time, independent of |E|.
34. Suppose that all edge weights in a graph are integers in the range from 1 to |V |. How fast can you
make Kruskal’s algorithm run? What if the edge weights are integers in the range from 1 to W for
some constant W ?
35. Show that a graph has a unique minimum spanning tree if, for every cut of the graph, there is a
unique light edge crossing the cut. Show that the converse is not true by giving a counter example.
36. Give a simple example of a connected graph such that the set of edges {(u, v) : there exists a cut
(S, V − S) such that (u, v) is a light edge crossing (S, V − S)} does not form a minimum spanning
tree.
37. Prove that (AB)T = B T AT and that AT A is always a symmetric matrix.
38. Prove that matrix inverses are unique, that is, if B and C are inverses of A, then B = C.
39. Solve the equation below using forward substitution.

   
1 0 0
x1
3
 4 1 0 x2  =  14 
−6 5 1
x3
−7
(3)