dynamic programming in Date Structures and Algorithm Analysis in C

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

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值