Chapter6算法概论概要1.ppt

Chapter6算法概论概要1

6.5 Chain matrix multiplication The optimal substructure property: for a tree to be optimal, its subtree must also be optimal. Subproblem corresponding to a subtree: product of the form Ai? Ai+1?… ? Aj For 1?i ?j?n, the optimal value array C(i,j) = minimum cost of multiplying Ai? Ai+1?… ? Aj The splitting point k: split the product into two pieces, of the form Ai? … ? Ak and Ak+1?… ? Aj The total cost is the cost of these two partial products plus the cost of combining them * 6.5 Chain matrix multiplication A dynamic programming algorithm: The number of subproblem: O(n2), each takes O(n) to compute The overall running time: O(n3) * 6.6 Shortest path Shortest reliable paths Avoid paths with too many edges All-pairs shortest paths Find the shortest path not just between s and t but between all pairs of vertices The traveling salesman problem Each city is visited exactly once Minimize the overall distance traveled * 6.6 Shortest path Shortest reliable paths Problem: the shortest path from s to t that uses at most k edges Subproblem: dist(v,i) to the length of the shortest path from s to v that uses i edges, i?k The general update equation: * 6.6 Shortest path All-pairs shortest paths Find the shortest path between all pairs of vertices. If execute the Bellman-Ford algorithm (maybe negative edges) for |V| times, one for each starting node, the running time would be O(|V|2|E|). A better way: the O(|V|3) dynamic programming-based Floyd-Warshall algorithm * 6.6 Shortest path dynamic programming-based Floyd-Warshall algorithm Number the vertices in V as {1,2,…,n} dist(i, j, k): the length of the shortest path from i to j in which only nodes {1,2,…,k} can be used as intermediates dist(i, j, 0): the length of the direct edge between i and j, if it exists, and is ? otherwise Expand the intermediate set to include an extra node k A shorter path from i to j that uses k goes through k just once (assume that there are no negative cycles) Update dist

文档评论(0)

1亿VIP精品文档

相关文档