Hw5 Solutions

Hw5 Solutions - HW5 SOLUTIONS 1. Running time of the given...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
HW5 SOLUTIONS 1. Running time of the given algorithm. T(n) = T(n-1) + T(n-2) + T(n-3) + Θ(1) Since T(n-1) >= T(n-3) and T(n-2) >= T(n-3), we can write T(n) ≥ 3T(n-3) = 3 2 T(n-6) by T(n-3) = 3T(n-6) = 3 3 T(n-9) by T(n-6) = 3T(n-9) ................... = 3 k T(n-3k) ≥ 3 n/3 = Ω(3 n/3 ) The algorithm given has a exponential running time because it redundantly calculates the values that was previously calculated. We can improve the algoritm’s efficiency by keeping the values calculated in an array of size n (T[n]) and get rid of these redundant calculations. minCost (i) { if (T[i] != empty) return T[i]; if (i>n) return 0; a = A[1,i] + minCost(i+1); b = A[1,i] + minCost(i+2); c = A[1,i] + minCost(i+3); if (A[2,i] == 1) T[i] = a; if (A[2,i] == 2) T[i] = min{a,b}; if (A[2,i] == 3) T[i] = min{a,b,c}; return T[i]; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The algorithm fills each cell of the array T once and a recursive call to procedure have constant running time O(1). So the total running time of the algorithm is O(n). 2. The running time of Dijkstra’s algorithm (by using Fibonacci heaps) is O(VlogV + E). In a graph like in the question each vertex has at most two outgoing edges. Therefore total number of edges in the graph is at most 2V (E = O(V))so running time of Dijkstra’s algorithm for such graphs is O(VlogV + 2V) = O(VlogV) It can be noticed that more efficient algorithms may be possible for such graphs after making some observations. There is only one vertex in the graph that has indegree > 1 and let’s call this vertex r. (This vertex is the root of the binary tree from which the graph has been constructed).
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Hw5 Solutions - HW5 SOLUTIONS 1. Running time of the given...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online