This preview shows pages 1–3. Sign up to view the full content.
HW5 SOLUTIONS
1.
Running time of the given algorithm.
T(n) = T(n1) + T(n2) + T(n3) + Θ(1)
Since T(n1) >= T(n3) and T(n2) >= T(n3), we can write
T(n) ≥ 3T(n3)
= 3
2
T(n6)
by T(n3) = 3T(n6)
= 3
3
T(n9)
by T(n6) = 3T(n9)
...................
= 3
k
T(n3k)
≥ 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];
}
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThe 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).
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '09
 yagizseven
 Data Structures

Click to edit the document details