recursion is easy to code but always slowin practice.
improvement → dynamic programing
rewrite the recursive algorithm as anonrecursive algorithm that systematically records the answers to thesubproblem in a table.
#fibonacci
for example, when computing the Fibonaccinumbers, T(N) is explosive with recursion.
On the other hand, we only need to recordF(N-1) and F(N-2) to compute F(N).We can solve the problem in linear time.
for( i=1->N)
{
F = A + B;
A = F;
B= A;
}
return F;
# ordering matrix multiplication
M[p*q] * M[q*r] needs pqr scalarmultiplications.
-> In which order can we have minimalcomputing time?
If the number of possible orders issmall, we can use exhaustive search.(evaluating each case)
however, if the number islarge?
T(N)=O(N^3)
#optimal binary search tree
given a list of words and place them intoa binary search tree.
the cost of visiting the node of depth di,with the occurrence possibility is pi
-> the total cost is Σpi(1+di)
->>>the problem is to minimizethe total cost
# All-Pairs Shortest Path
The all-pairs shortest path problem can be considered the mother of allrouting problems. It aims to compute the shortest path from each vertex v toevery other u.
single-source -> Dijkstra single-source algorithm
O(V^2) time on dense graphs, fasteron sparse graph.
so ,we need totally O(V^3) by V iterationsof Dijkstra.
>> dynamic programming
On a complete graph, where every pair ofvertices is connected(in both direction),this algorithm is faster than Viterations of Dijkstra.
>>>> works well on a verydense graph