1. 程式人生 > >ADA演算法知識(十)動態規劃相應的問題

ADA演算法知識(十)動態規劃相應的問題

[How Many Paths?] You are given a map in the form of an undirected graph G = (V,E) and would like to know how many possible ways to walk (that are, paths with possible repeated nodes) between two nodes s and t, using k number of edges. You would like to use dynamic programming to solve the problem.

For any nodes u,v belongs to V, and i>=0, let P(u,v,i) denote the number of walks that starts from u and ends at v that use i edges. Note that question now is to compute P(s,t,k).

(a) What is the valu of P(u,v,1) for any u,v belong to V?

(b) Write down a recurrence for the value of P(u,v,i+1).

(c) Describe an algorithm for computing P(u,v,k) for every u,v belong to V. What is the complexity of the algorithm?

 

(a)Obviously, when i=1, P(u,v,1)=1

(b)use dfs

(c)use floydwarshall algorithm

and the complexity of the algorithm is O(n^3)