Documente Academic
Documente Profesional
Documente Cultură
(d) (2 points) Recall that we spent a lecture showing that if we choose the “pivot” in quicksort uniformly at random from the 𝑛
numbers being sorted, we obtain a randomized algorithm that is correct with probability 1, and which has expected runtime 𝑂(𝑛
log 𝑛). In one sentence describe how we could modify the randomized quicksort algorithm to obtain a deterministic quicksort
algorithm that also runs in time 𝑂(𝑛 log 𝑛). Justify why your modified algorithm still runs in time 𝑂(𝑛 log 𝑛) in at most two
sentences. (Feel free to refer to other algorithms that we studied in class.)
At each recursive step, rather than picking the pivot randomly, pick the median as the pivot using the Select algorithm, which runs in 𝑂(𝑛)
time. Then at each step we are guaranteed to recurse on problems half the size, and at each level we have need linear time to find the pivot
and partition the array, so this version of quicksort obeys the deterministic recurrence 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑂(𝑛) = 𝑂(𝑛 log 𝑛).
(f) (2 points) What are the key similarities and differences between balanced binary search trees, and hash tables? For full credit,
describe at least one similarity, and at least one difference.
Similarity: Hash table and a balanced binary search tree both data structures that allow fast insert, lookup, and delete operations.
Difference: In hash table, insert, lookup, and delete operations take constant time (in expectation). However, these operations take 𝑂(log 𝑛)
time in balanced binary search trees.
(g) (2 points) Suppose I have a deterministic algorithm for Problem-X that interacts with an input via a series of Yes/No/Maybe
questions, and then produces the correct output. Suppose that 1 there are 𝑛 possible inputs to the problem, and 𝑚 possible outputs,
with 𝑚 < 𝑛 (and that each input has exactly one correct output). There exists an input such that my algorithm will ask at least
XXXX questions. Which of the following quantities is the correct value of XXXX? (a) log2 𝑛 (b) log2 𝑚 (c) log3 𝑛 (d) log3 𝑚
Choose one of these four options, and give a one-sentence proof/justification for this lower-bound.
Suppose the algorithm asks at most 𝑠 questions on all inputs; since the algorithm is deterministic, for each of the 3𝑠 sequences of 𝑠 answers
there can be out most one output, hence we must have 𝑚 ≤ 3 𝑠 , which implies the algorithm must ask at least 𝑠 ≥ log3 𝑚 questions.
(h) (2 points) Consider a set of hash functions, 𝐻 = {ℎ1, . . . , ℎ𝑘}, with each function ℎ𝑖 : 𝑈 → {1, 2, . . . , 𝑛} mapping a universe 𝑈 of
“keys” into 𝑛 buckets. This set, 𝐻, is a “Universal” family if, for all 𝑘, 𝑘′ ∈ 𝑈 with 𝑘 =
̸ 𝑘 ′ , if ℎ is drawn uniformly at random from
the set 𝐻, Pr[ℎ(𝑘) = ℎ(𝑘 ′ )] ≤ 1/𝑛. For any 𝑈 and 𝑛, there exists a universal family, 𝐻𝑈,𝑛, such that if ℎ is chosen uniformly at
random from 𝐻𝑈,𝑛, then Pr[ℎ(𝑘) = ℎ(𝑘 ′ )] = 1/𝑛. [Note the STRICT equality sign—this is not a typo!!] Describe how to construct
the set 𝐻𝑈,𝑛 in at most one sentence. No proof necessary.
𝐻𝑈,𝑛 is simply the set of all hash functions from 𝑈 to {1, 2, . . . , 𝑛}.
(i) (3 points) Djikstra’s algorithm allows us to find the shortest paths between a “source” vertex, 𝑠, and every other vertex in a
weighted graph. Consider a slightly easier version of this problem, where all the edges have weight 1. In this case, does Djikstra’s
algorithm visit nodes in the same order as Depth-First-Search, Breadth-First-Search, neither DFS nor BFS, or both? Justify your
answer in one or two sentences—no need for a formal proof.
We accepted two answers (either way, a convincing 1- or 2-sentence justification is required):
∙ “BFS”. BFS, starting at node 𝑠, visits vertices in order of their unweighted distance, or unweighted shortest path length, from 𝑠. When all
edges have weight 1, Dijkstra’s algorithm also visits nodes by unweighted distance from 𝑠. ∙ “Neither”. Dijkstra’s algorithm visits nodes in
the order they are popped from its priority queue (a black box). Therefore, nodes with the same distance from 𝑠 may not be visited in the
same order by Dijkstra’s algorithm as by BFS. Note that DFS does not proceed by distance, so the answers “DFS” and “either/both BFS
and DFS” are incorrect.
Problem 1
F, F, skipped bc I don't think we covered this, skipped bc I don't think we covered this, F, F, T, F, T, T
T F n 1/10 = O((log n) 5 )
T F The worst-case running time of Randomized Quicksort on an array of n elements is O(n log n).
T F A heap can be used to sort an array of n elements with worst-case running time O(n log n).
T F A hash table can be used to sort an array of n elements with expected running time O(n).
T F Every algorithm that always correctly computes the median of an array of n elements has worst-case running time Ω(n log n).
T F Dijkstra’s algorithm is always correct even in graphs with negative edge weights.
T F Prim’s algorithm is always correct even in graphs with negative edge weights.
T F Using a suitable data structure, Dijkstra’s algorithm can be implemented in O(m log n) time in graphs with m edges and n vertices.
T F Using a suitable data structure, Prim’s algorithm can be implemented in O(m log n) time in graphs with m edges and n vertices.
T F Linearity of expectation (that the expectation of a sum of random variables equals the sum of the random variables’ expectations) holds even for random variables that are not independent.