Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

University of Toronto

ECE-345: Algorithms and Data Structures


Solutions to Final Examination (Fall 2017)

1. Multiple Choice, 20 points

(a) False, then we could sort in O(n) time using an in-order traversal of the BST.
(b) True, see CLRS p.153 Build-Max-Heap.
(c) O(n)
(d) True.
(e) True, heapsort is not stable.
(f) A is in P since it’s poly-reducible to B. Nothing can be said of C.
(g) True.
(h) Θ(lg n).
(i) True.
(j) False. One only is guaranteed to be in an MST.

2. Short Answers, 6+8+6 points

(a) Final table:

0 1 2 3 4 5 6 7 8 9 10
100 X 25 X 61 50 X 20 43

(b) Keep track of max, the maximum-index 1 in the counter.


operation actual cost amortized cost
Increment n $4
Reset n $1
Charge $4 for each increment. $1 goes to flipping a 0 to 1, and an additional $1 is saved on the bit itself
to pay for flipping to a 0 during a later Increment operation. $1 is paid to update max, and the final
$1 is saved on the max-index bit, to pay for flipping to a 0 during a later Reset operation.
Charge $1 for each reset, which will pay for resetting max.
Alternatively, don’t keep track of max and only change $3 for Increment and $0 for Reset. You are
guaranteed that the number of bits used by the counter will be less than the number of Increment
operations (and therefore spare change).
(c) If G is acyclic, then it is a tree by definition. Then the MST is the graph G itself. The edge weights of
G in this case need not be distinct. For example, consider the case where G is a triangle with 3 nodes
and 2 edges, where both edges have weight 1.

3. Dynamic Programming, 15+5 points



max(A(i − 1) + ai , A(i − 2) + ai ai−1 ) if i > 1

(a) A(i) = ai if i = 1

0 if i = 0

A(i) is the maximum sum-product when considering only the first i numbers.
(b) function MaxSumProduct(a)

1
A[0] ← 0
A[1] ← a[i]
for 2 ≤ i ≤ n do
A[i] ← max(A[i − 1] + a[i], A[i − 2] + a[i] × a[i − 1])
end for
return A[n]
end function
Both running time and space requirements are O(n).

4. Shortest Paths, 20 points


Change the Relax function to update bottleneck[v] with the bottleneck weight of the shortest path from
s to v seen so far. Also add a check when the length of the shortest path seen so far (d[v]) is equal to the
current path length, and update bottleneck[v] accordingly.
5. NP-Completeness, 5+15 points

(a) Certificate: set of vertices and associated edges C.


Verify:
i. Check that all edges in C are in E.
ii. Check that the edges form a simple path.
iii. Check that |C| = |V | − 1.
These can all be done in polynomial time O(E).
(b) Given an instance of Hamiltonian-Path hG, u, vi, construct an instance of Directed-Hamiltonian-
Path hG0 , u, vi. Construct G0 by replacing every undirected edge in G with two directed edges.
Now we need to show that a Hamiltonian path can be found ⇐⇒ a directed Hamiltonian path can be
found.
⇒ Suppose that a Hamiltonian path P exists between u and v. Then a directed Hamiltonian path P 0
can be constructed by starting at u, and replacing each edge in turn with a directed edge.
⇐ Suppose that a directed Hamiltonian path D exists between u and v. Then an undirected Hamiltonian
path D0 can be constructed by replacing each edge in D with an undirected edge.

You might also like