1 / 59

Dynamic Programming

Dynamic Programming. Chapter 7. Fibonacci sequence. f n = 0 if n = 0 f n = 1 if n = 1 f n = f n-1 + f n-2 if n  2. http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html. 0,1,1,2,3,5,8,13,21,34,... Fibonacci's Rabbits, Cows and Bees Family Trees

dmanriquez
Download Presentation

Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Programming Chapter 7

  2. Fibonacci sequence fn = 0 if n = 0 fn = 1 if n = 1 fn = fn-1 + fn-2 if n  2 http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html • 0,1,1,2,3,5,8,13,21,34,... • Fibonacci's Rabbits, Cows and Bees Family Trees • The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances.

  3. Fibonacci's Rabbits • Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was

  4. Fibonacci sequence (2) • 0,1,1,2,3,5,8,13,21,34,...

  5. Fibonacci sequence and golden number fn = 0 if n = 0 fn = 1 if n = 1 fn = fn-1 + fn-2 if n  2 • 0,1,1,2,3,5,8,13,21,34,... 1 x-1 x

  6. Computation of Fibonacci sequence fn = 0 if n = 0 fn = 1 if n = 1 fn = fn-1 + fn-2 if n  2 • Solved by arecursiveprogram: • Much replicated computation is done. • It should be solved by a simple loop.

  7. 7-0 Dynamic Programming • Dynamic Programming is an algorithm design method that can be used when the solution to a problem may be viewed as the result of a sequence of decisions (Recall Greedy method)

  8. 7-0 Dynamic Programming • E.g. to find a shortest path in a multi-stage graph • Apply the greedy method (Chapter 3): • the shortest path from S to T : 1 + 2 + 5 = 8

  9. 7-0 Multistage graph • A multistage graph G=(V,E) is a directed graph in which the vertices are partitioned into k2 disjoint sets Vi, 1i  k • In addition, if <u,v> is an edge in E then uVi and vVi+i for some i, 1i<k • The set V1 and Vk(Start and Target) are such that V1 =Vk=1 • The multistage graph problem is to find a minimum cost path from s in V1 to t in Vk (Start to Target) • Each set Vi defines a stage in the graph

  10. 7-0 Dynamic ProgrammingThinking: Branch and Bound solution (Chapter 5.5) • E.g. • The greedy methodcan not be applied to this case: S A D T 1+4+18 = 23. • Recall: Branch and Bound (Chap 5.16 slide) to solve • The shortest path is: S C F T 5+2+2 = 9.

  11. 7-0 Dynamic Programming • Dynamic programming approach: backward reasoning. • d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}

  12. 7-0 Dynamic Programming • d(A, T) = min{4+d(D, T), 11+d(E, T)} • = min{4+18, 11+13} = 22.

  13. 7-0 Dynamic Programming • d(B, T) = min{9+d(D, T), 5+d(E, T), 16+d(F, T)} = min{9+18, 5+13, 16+2} = 18. • d(C, T) = min{ 2+d(F, T) } = 2+2 = 4 • d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)} = min{1+22, 2+18, 5+4} = 9. • The above way of reasoning is called backward reasoning.

  14. 7-0 Forward reasoning • d(S, A) = 1 d(S, B) = 2 d(S, C) = 5 • d(S,D)=min{d(S, A)+d(A, D),d(S, B)+d(B, D)} = min{ 1+4, 2+9 } = 5 d(S,E)=min{d(S, A)+d(A, E),d(S, B)+d(B, E)} = min{ 1+11, 2+5 } = 7 d(S,F)=min{d(S, A)+d(A, F),d(S, B)+d(B, F)} = min{ 2+16, 5+2 } = 7

  15. 7-0 Forward reasoning • d(S,T) = min{d(S, D)+d(D, T),d(S,E)+ d(E,T), d(S, F)+d(F, T)} = min{ 5+18, 7+13, 7+2 } = 9

  16. 7-0 Principle of optimality • Suppose that in solving a problem, we have to make a sequence of decisions D1, D2, …, Dn • If this sequence is optimal, then the last k decisions, 1  k  n must be optimal(Comparing Greedy.) • E.g. the shortest path problem If i, i1, i2, …, j is a shortest path from i to j, then i1, i2, …, j must be a shortest path from i1 to j • In summary, if a problem can be described by a multistage graph then it can be solved by dynamicprogramming

  17. 7-0 Summary… • To solve a problem by using dynamic programming • Find out the recurrence relations • E.g. (recall Chap slide 7-7) • d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)} • Represent the problem by a multistage graph • In the text, they can be shown to be equivalent

  18. 7-1 The resource allocation problem • Problem: m resources, n projects profit p(i, j) : j resources are allocated to project i. maximize the total profit. • E.g. • Thinking: Straightforward method ?

  19. 7-1 Multistage graph • (i, j) : i resources allocated to projects 1, 2, …, j • E.g. node H=(3, 2) : 3 resources allocated to projects 1, 2. (3,4) node The Resource Allocation Problem Described as a Multi-Stage Graph.

  20. 7-1 Multistage graph • Find the longest path from S to T : S C H L T , 8+5=13 2 resources allocated to project 1 1 resource allocated to project 2 0 resource allocated to projects 3, 4

  21. Recall: 3-3 Dijkstra’s algorithm : Other issues • The longest path(critical path) problem can be solved by the critical path method(CPM). • Step 1. Find a topological ordering. • (Activity on vertex , AOV network) • Step 2. Find the critical path.

  22. Final examination • Given a problem • Present the corresponding multi-stage graph • Present the corresponding recurrence relation

  23. 7-2 The traveling salesperson problem(Recall Chap5 Branch-Bound) • E.g. a directed graph : • Cost matrix :

  24. 7-2 TSP • Find the shortest path: 1, 4, 3, 2, 1 5+7+3+2=17 Fig. A Multi-Stage Graph Describing All Possible Tours of a Directed graph. ( (n-1)! too expensive)

  25. 7-2 The traveling salesperson problem • Suppose that we have 6 vertices in the graph. • We can combine {1, 2, 3, 4} and {1, 3, 2, 4} into one node. • The last vertex visited is 4 The remaining vertices to be visited are 5, 6

  26. 7-2 The traveling salesperson problem(Reducing the number of nodes)

  27. Concept: The traveling salesperson problem How to use dynamic programming Let Path P : ViVi +1 ……  Vj-1 Vj be a Subpath in an optimal solution . * C(P) : must be minimum among all paths from Vi to Vjincluding vertices { Vi+1 , … , Vj-1} * Method : 2 3 1 1 n

  28. Concept: The traveling salesperson problem • Let g(i, S) is short path from node i to node 1through all nodes in S。Thus, g(1, V-{1}) is the TSP solution。 (1) g(1,V-{1}) = min{c1k + g(k,V-{1,k})},2≦k≦n (2) g(i,S)=min{cij + g(j, S-{j})},jS, i S (3) g(i, Φ) = ci1, 2 ≦ i ≦ n。

  29. Example: The traveling salesperson problem (Starting node 1 TSP to node 1) 1 2 4 3 g(2,)=C21=5 g(3,)=C31=6 g(4,)=C41=8 =>g(1,{2,3,4})=min{C12+g(2,{3,4}), C13+g(3,{2,4}), C14+g(4,{2,3}),} =>g(2,{3,4})=min{C23+g(3,4) ,C24+g(4,3)} g(3,4)=C34+g(4, )=12+8=20 g(4,3)=C43+g(3, )=9+6=15 g(2,{3,4})=C24+15=25 g(1,{2,3,4})=C12+g(2,{3,4})=35 1-2-4-3-1

  30. 7-2 The traveling salesperson problem • Let g(i, S) be the length of a shortest path starting at vertex i, going through all vertices in S and terminating at vertex 1. • The length of an optimal tour : • The general form: • Time complexity: recurrence relations ( ),( ) (n-k)  (n-1)

  31. 7-3 The longest common subsequence (LCS) problem • A string : A = b a c a d • A subsequence of A: deleting 0 or more symbols from A (not necessarily consecutive). • E.g. ad, ac, bac, acad, bacad, bcd. • Common subsequences of A = b a c a d and B = a c c b a d c b : ad, ac, bac, acad. • The longest common subsequence of A and B: a c a d. • Thinking: Straightforward method ? Write a program? • Generating all possible subsequence in B • Checking A

  32. 7-3 The longest common subsequence (LCS) problem • Let A = a1 a2 am and B = b1 b2 bn • Let Li,j denote the length of the longest common subsequence of a1 a2 ai and b1 b2 bj. • Li,j = Li-1,j-1 + 1 if ai=bj max{ Li-1,j, Li,j-1 } if aibj • L0,0 = L0,j = Li,0 = 0 for 1im, 1jn. recurrence relations

  33. 7-3 The longest common subsequence (LCS) problem Fig. 7-17 The Dynamic Programming Approach to Solve the Longest Common Subsequences Problem.

  34. 7-3 The longest common subsequence (LCS) problem • E.g. A = b a c a d, B = a c c b a d c b • After all Li,j’s have been found, we can trace back to find the longest common subsequence of A and B. => O(n2) Stages

  35. 7-4 0/1 knapsack problem in multistage representation (Recall Chap 5.8 Branch-Bound) • n objects , weight W1, W2, ,Wn profit P1, P2, ,Pn capacity M maximize subject to M xi = 0 or 1, 1in • E.g.

  36. 7-4 0/1 knapsack problem in multistage representation • Multi-stage graph: • Note: if T is deleted from the graph,the new graph ? => Tree (2n nodes)

  37. 7-4 0/1 knapsack problem in multistage representation • The longest path represents the optimal solution: x1=0, x2=1, x3=1 = 20+30 = 50 • Let fi(Q) be the value of an optimal solution to objects 1,2,3,…,i with capacity Q. • fi(Q) = max{ fi-1(Q), fi-1(Q-Wi)+Pi } • The optimal solution is fn(M) recurrence relations

  38. Recall: Data Structure => binary search trees • Binary search tree (BST) • Def: • a binary tree • every element has an unique key • the keys in the left subtree < the key in the root • the keys in the right subtree > the key in the root • the left and right subtrees are also binary search tree • Constructing BST E.g.: 23, 35, 12, 46, 58, 66 • Dependent on the order of input data elements • Recall: Binary search method (Sorted list) • Recall: Huffman Tree, binary tree (formal, complete, Full, skewed), Heap-Tree

  39. 7-5 Optimal binary search trees E.g. binary search trees for 3, 7, 9, 12;

  40. 7-5 Optimal binary search trees • n identifiers : a1 <a2 <a3 <…< an Pi, 1in : the probability that ai is searched. Qi, 0in : the probability that x is searched where ai < x < ai+1 (a0=-, an+1=).

  41. Recall: Data structure • Properties of Binary Tree • Max(#(nodes)) on level i of a binary tree: 2i-1 (i 1) • Max(#(nodes)) in a binary tree of depth k: 2k -1 (k  1) • #(leaf nodes): n0 , #(nodes with degree 2): n2 • n0=n2 + 1 • Proof.

  42. 7-5 Optimal binary search trees • E.g. identifiers : 4, 5, 8, 10, 11, 12, 14 • Internal node : successful search, Pi • External node : unsuccessful search, Qi • The expected cost of a binary tree: *level(ai) + *(level(Ei)-1) • The level of the root : 1 Fig. A Binary Tree with Added External Nodes.

  43. 7-5 Optimal binary search trees • Let C(i, j) denote the cost of an optimal binary search tree containing ai,…,aj . • The cost of the optimal binary search treewith akas its root : C(1,n)= {Pk+[Q0+ +C(1,k-1)] +[Qk+ +C(k+1, n)]} Selecting min. from n values =1

  44. 7-5 Optimal binary search trees • General formula : C(i, j) = {Pk + [Qi-1+ +C(i, k-1)] +[Qk + +C(k+1, j)]} = {C(i, k-1)+C(k+1, j)+Qi-1 + } recurrence relations • Each C(i, j) with j-i=m can be computed in O(m) time.

  45. 7-5 Optimal binary search trees(Final examination => example a1~a4 given Probabilities => find Optimal binary search trees) • E.g. n=4 • Relationships of subtrees • C(1,4) => C(2,4)(a1 as root), C(3,4)(a2 as root), C(1,2)(a3 as root), C(1,3)(a4 as root) • C(2,4) => C(3,4)(a2 as root), C(2,3)(a4 as root) • C(1,3) => C(2,3)(a1 as root), C(1,2)(a3 as root) Fig. Computation Relationships of Subtrees.

  46. 7-5 Optimal binary search trees • How to compute C(1,n) ? • Step 1: computing all C(i,j) with j-i=1 • Step 2: computing all C(i,j) with j-i=2 • Step 3~n-2: computing all C(i,j) with j-i=3,…..n-2 • How many C(i,j)s do the above procedure require? • When j-i=m, there are (n-m) C(i, j)’s to compute. Thus=> (n-1)+(n-2)+…+2 C(i, j)s required to be computed = (n+1)(n-2)/2 C(i, j)s • Each C(i, j) with j-i=m can be computed in O(m) time. O( ) = O(n3)

  47. 7-6 Matrix-chain multiplication • n matrices A1, A2, …, An with size p0  p1, p1  p2, p2  p3, …, pn-1  pn To determine the multiplication order such that # of scalar multiplications is minimized. • To compute Ai  Ai+1, we need pi-1pipi+1 scalar multiplications. • How many kinds of multiplication orders • like the previous problem BST • to compute C(1,n) requires How many C(i,j)s • It requires (n+1)(n-2)/2 C(i, j)s • Thus, (n+1)(n-2)/2 multiplication orders

  48. 7-6 Matrix-chain multiplication How many kinds of multiplication orders • (n+1)(n-2)/2 multiplication orders • n=4, there are 5 multiplication orders e.g. n=4, A1: 3  5, A2: 5  4, A3: 4  2, A4: 2  5 1. ((A1 A2)  A3)  A4, # of scalar multiplications: 3 * 5 * 4 + 3 * 4 * 2 + 3 * 2 * 5 = 114 2. (A1 (A2 A3))  A4, # of scalar multiplications: 3 * 5 * 2 + 5 * 4 * 2 + 3 * 2 * 5 = 100 3. (A1 A2)  (A3 A4), # of scalar multiplications: 3 * 5 * 4 + 3 * 4 * 5 + 4 * 2 * 5 = 160 4. A1 (A2 (A3 A4)) 5. A1 ((A2 A3)  A4)

  49. Let m(i, j) denote the minimum cost for computing Ai  Ai+1  … Aj • Computation sequence : • Time complexity : O(n3)

  50. Extra: Binary search trees=> Problem-1: nth Catalan number C(n) • Problem 1:The number of binary search trees with n nodes, C(n), is the nth Catalan number. • Solution: • Concept: • consider the integers 1, 2, ..., n. • The number of trees with root k is C(k-1)C(n-k) • This is because there are k-1 integers below k and n-k above k. • Recurrence relation: • Then the total is found by adding these up for k from 1 to n:C(n) = C(0)C(n-1) + C(1)C(n-2) + ... + C(n-1)C(0)with the trivial cases given by C(0)=1 and C(1)=1. • Catalan number = C(n)

More Related