1 / 27

CSCI 3160 Design and Analysis of Algorithms Tutorial 4

CSCI 3160 Design and Analysis of Algorithms Tutorial 4. Fei Chen. Outline. Dynamic programming Chain Matrix Multiplication Longest increasing subsequence (LIS) Goal: help understand the lecture materials further. Dynamic programming.

eara
Download Presentation

CSCI 3160 Design and Analysis of Algorithms Tutorial 4

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. CSCI 3160 Design and Analysis of AlgorithmsTutorial 4 Fei Chen

  2. Outline • Dynamic programming • Chain Matrix Multiplication • Longest increasing subsequence (LIS) • Goal: help understand the lecture materials further

  3. Dynamic programming • “A method for solving complex problems by breaking them down into simpler subproblems” (from Wikipedia) • Three steps • Find the optimal substructure • Formulate the problem recursively • Compute the values bottom up

  4. Chain Matrix Multiplication • You want to multiply N matrices A1, …, AN • Suppose the cost of multiplying an m-by-n matrix with an n-by-l matrix is mnl. • The product is an m-by-l matrix • What is the minimum total cost to get the product of the N matrices P = A1…AN?

  5. Chain Matrix Multiplication • Each order of multiplication corresponds to a parenthesization ((A1(A2A3))(A4((A5A6)A7))) • Optimal substructure • If the above parenthesization is optimal, then • (A1(A2A3)) is optimal for multiplying A1, …, A3 • (A4((A5A6)A7)) is optimal for multiplying A4, …, A7 • ((A5A6)A7)is optimal for multiplying A5, …, A7 • Every “subparenthesization” of an optimal parenthesization is optimal

  6. Chain Matrix Multiplication • Recurrence • Let C(i,j) be the optimal cost of multiplying Ai, … Aj • C(i,i) = 0 for any i (No multiplication needed) • C(i,j) = mini ≤ k ≤ j-1 {C(i,k) + C(k+1,j) + mi-1mkmj } for i ≤ j-1 • Build an N-by-N matrix to store the C(i,j)’s • Our optimal value is then C(1,N) mi (Ai) mi-1

  7. Example • Let A1 be a 5-by-20 matrix, A2 be a 20-by-10 matrix, A3 be a 10-by-3 matrix, A4 be a 3-by-2 matrix • m0 = 5, m1 = 20, m2 = 10, m3 = 3, m4 = 2

  8. Example • m0 = 5, m1 = 20, m2 = 10, m3 = 3, m4 = 2 • C(1,2) = C(1,1) + C(2,2) + m0m1m2= 5 · 20 · 10 = 1000 • C(2,3) = , C(3,4) = .

  9. Example • m0 = 5, m1 = 20, m2 = 10, m3 = 3, m4 = 2 • C(1,2) = C(1,1) + C(2,2) + m0m1m2 = 5 · 20 · 10 = 1000 • C(2,3) = 600, C(3,4) = 60.

  10. Example • m0 = 5, m1 = 20, m2 = 10, m3 = 3, m4 = 2 • C(1,3) = min { C(1,1) + C(2,3)+ m0m1m3, C(1,2) + C(3,3) + m0m2m3 } = min { 0 + 600 + 300, 1000 + 0 + 150 } = 900

  11. Example • m0 = 5, m1 = 20, m2 = 10, m3 = 3, m4 = 2 • C(2,4) = min { C(2,2) + C(3,4) + m1m2m4, C(2,3) + C(4,4) + m1m3m4 } = min { 0 + 60 + 400, 600+ 0 + 30 } = 460

  12. Example • m0 = 5, m1 = 20, m2 = 10, m3 = 3, m4 = 2 • C(1,4) = min { C(1,1) + C(2,4) + m0m1m4, , } = min { 0 + 460 + 200, , } = .

  13. Example • m0 = 5, m1 = 20, m2 = 10, m3 = 3, m4 = 2 • C(1,4) = min { C(1,1) + C(2,4) + m0m1m4, C(1,2) + C(3,4) + m0m2m4, C(1,3) + C(4,4) + m0m3m4} = min { 0 + 460 + 200, 1000 + 60 + 100, 900 + 0 + 30 } = 660.

  14. Example • Hence, the optimal cost of computing P = A1…A4is 660.

  15. Analysis • Space complexity = O(N2) • Time complexity = O(N3) • A for loop (k = i .. j-1) is run every time we compute the value of a table entry

  16. Longest increasing subsequence (LIS) • Given a sequence of N integers a1, …, aN, find a subsequence ai1, …, aik (1 ≤ i1 < … < ik ≤ N) such that • Increasing: ai1 < … < aik • Longest: its length k is maximum among all increasing subsequences • Example: 3, 1, 6, 0, 8 • Can you give another LIS?

  17. Longest increasing subsequence (LIS) • We can construct a graph • Optimal substructure • If ai1, …, aik is an LIS ending at position ik, then ai1, …, aij is an LIS ending at position ij for any j≤k • In the language of longest paths: a subpath of a longest path is longest 3 1 6 0 8

  18. Longest increasing subsequence (LIS) • Add an imaginary node “-∞” at the front • Recurrence • Let L(j) be the length of the LIS ending at position j • L(0) = 0 • L(j) = 1 + maxi : ai < aj L(i), for j ≥ 1 • Our longest length is then maxj L(j) -∞ 3 1 6 0 8

  19. Example • Use a linear array to store the values -∞ 3 1 6 0 8

  20. Example -∞ 3 1 6 0 8

  21. Example -∞ 3 1 6 0 8

  22. Example -∞ 3 1 6 0 8

  23. Example -∞ 3 1 6 0 8

  24. Example -∞ 3 1 6 0 8

  25. Example • Length of LIS = maxj L(j) = 3 -∞ 3 1 6 0 8

  26. Analysis • Space complexity = O(N) • O(N2) if you store the graph, since there can be Θ(N) arrows pointing to a node • Time complexity = O(N2) • Each arrow is gone through once

  27. End • Questions

More Related