主題 : Dynamic Programming (II)

  • View
    67

  • Download
    0

Embed Size (px)

DESCRIPTION

主題 : Dynamic Programming (II). 經典問題 Longest Common Subsequence Longest Increasing Subsequence Matrix-chain Multiplication Optimal Polygon Triangulation 例題講解 : H.89.4 歷年題目. a. b. c. b. d. a. b. Longest Common Subsequence. - PowerPoint PPT Presentation

Text of 主題 : Dynamic Programming (II)

  • : Dynamic Programming (II)Longest Common SubsequenceLongest Increasing SubsequenceMatrix-chain MultiplicationOptimal Polygon Triangulation

    : H.89.4

  • Longest Common SubsequenceSubsequence: T T subsequence T

  • Longest Common Subsequence X = x1x2xn Y = y1y2ym subsequence

    X Y ba, bca, bcba, bdab, common subsequence LCS bcba, bdab,

  • LCS: Optimal Substructure xn = ym LCS

  • LCS: Optimal Substructure (cont.) xn = ym LCS X[1n-1] Y[1m-1] LCS

    xn ym LCS X[1n] Y[1m-1] LCS X[1n-1] Y[1m] LCS

  • LCS: Recurrence L[i, j] X[1i] Y[1j] LCS L[0, j] = 0 L[i, 0] = 0Boundary condition

  • LCS: Build TableL[i, j] row-major column-major Time = O(nm)

    j0123456iyjbdcaba0xi00000001a00001112b01111223c01122224b01122335d01222336a01223347b0122344

  • LCS: LCS mn recurrence row-major (column-major) row (column) row (column) LCS

    backtracking row (column)

  • Longest Increasing Subsequence a1, a2, , ansubsequenceA

  • LIS: LIS subsequence

    a1, a2, , an B = b1, b2, , bn LIS LIS B subsequence

    LIS A B ( A sort ) LCS

  • LIS: ExampleTime complexity = O(n2): ?Hint: B

  • Matrix Multiplication a b b c (a c) b a c

  • Matrix-chain Multiplication n A1 A2 An i Ai pi-1 pi()

    Ex: (p0, p1, p2, p3) = (50, 5, 100, 10) ((A1A2)A3) (A1(A2A3)) 505100+ 5010010 = 75000510010+ 50510 = 7500

  • MCM: Optimal Substructure A1 Ai Ai+1 An A1 Ai MCM Ai+1 An MCM A1A2A3A4A5A6A7

  • MCM: Recurrence m[i, j] Ai Aj MCM

    m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj}

    m[i, i] = 0 for all ii k < jTime complexity = O(n3)

  • MCM: Build Table row-major column-major MCM O(n2)

  • MCM: Backtracking m[i, j] k Ai Aj Ai Ak Ak+1 Aj (Ai Ak)(Ak+1 Aj)

    c[i, j] m[i, j] k

  • Polygon Triangulation n n 3 n 2

  • Triangulation cost abc cost w(abc )

    triangulation cost cost : w(abc ) = triangulation cost cost w(abc ) = triangulation cost

  • Optimal Polygon Triangulation n v0, v1, , vn-1 cost function w cost triangulation

    triangulation vi, vj, vk vivjvk

  • OPT: Optimal Substructurev1vkvn-2 edge edgew(v0vkvn-1)vn-1v0

  • OPT: Recurrence P[i, j] vi-1 vj vi-1 t[i, j] P[i, j] optimal polygon triangulation cost

    t[i, j] = min {t[i, k] + t[k+1, j] + w(vi-1vkvj)}

    t[i, i] = 0 for 1 i n 1i k < j

  • OPT: AnalysisTime: O(n3)

    Space: O(n2)

    backtracking t[i, j] k

  • : H.89.4(http://www.cc.nccu.edu.tw/info_race89/doc/final_program.doc) n (n 10) a1, a2, , an (50 ai 1000) 7 10 n

  • Definition f(i, j) i j

    f(i, j) = (ai 7j) / 10, for ai 7j , otherwise

  • Recurrence (cont.) c[i, j] i j

    c[i, j] = max {c[i 1, j k] + f(i, k)}

    c[1, j] = f(1, j)0 k j

  • = max {j | 2j c[n, j]}

    backtracking c[i, j] k

  • Analysis recurrence n n 7 S = (1 i n ai) / 7 < 1500

    Time: O(nS2) Space: O(nS)

  • A.526 String Distance and Transform Processhttp://acm.uva.es/p/v5/526.htmlA.348 Optimal Array Multiplication Sequencehttp://acm.uva.es/p/v3/348.htmlA.10003 Cutting Stickshttp://acm.uva.es/p/v100/10003.htmlH.88.5 http://www.cc.nccu.edu.tw/info_race88/Q.pdfH.89.4 http://www.cc.nccu.edu.tw/info_race89/doc/final_program.doc

    A.10379 Pit Stop Strategyhttp://acm.uva.es/p/v103/10379.htmlA.757 Gone Fishinghttp://acm.uva.es/p/v7/757.html

  • A.111, A.103, A.116, A.136, A.164, A.231, A.242, A.357, A.323, A.443, A.481, A.497, A.417, A.452, A.531, A.585, A.580, A.590, A.640, A.674, A.682, A.702, A.707, A.751, A.861, A.10000, A.10036, A.10051, A.10066, A.10007, A.10069, A.10076, A.10100, A.10120, A.10130, A.10131, A.10154, A.10164, A.10192, A.10111, A.10128, A.10157, A.10198, A.10213, A.10223, A.10232, A.10237, A.10238, A.10239, A.10259, A.10261, A.10271, A.10280, A.10337, A.10303, A.10304, A.10312, A.10313, A.10328, A.10358, A.10373, A.10381, A.10399, A.10405, A.10404, A.10440, A.10453, A.10534, A.10564, A.10578, A.10516, A.10518, A.10520, A.10532, A.10536, A.10541, A.10549, A.10593, A.10599, A.10604, A.10616, A.10617, A.10626, A.10635, A.10655, A.10664