Docsity

Optimization & Dynamic Programming: Matrix Chain & Longest Subsequence, Study notes of Algorithms and Programming

Toggle sidebar
bg1
1
Optimization
Optimization problems have the form: Find a
solution minimizing/maximizing some value.
Examples:
Multiply a sequence of matrices minimbizing the
number of multiplications.
From two strings, find a common subsequence
of maximum length.
Give change minimizing the number of coins.
Schedule a room to maximize the number of
meetings.
Find a character code to minimize the length of
a string.
Find a spanning tree with the minimum sum of
weights.
Find a path between two vertices of minimum
weight.
Find the smallest set of edges covering all ver-
tices.
pf3
pf4
pf5

Partial preview of the text

Download Optimization & Dynamic Programming: Matrix Chain & Longest Subsequence and more Study notes Algorithms and Programming in PDF only on Docsity!

Optimization

Optimization problems have the form: Find a solution minimizing/maximizing some value.

Examples:

Multiply a sequence of matrices minimbizing the number of multiplications.

From two strings, find a common subsequence of maximum length.

Give change minimizing the number of coins.

Schedule a room to maximize the number of meetings.

Find a character code to minimize the length of a string.

Find a spanning tree with the minimum sum of weights.

Find a path between two vertices of minimum weight.

Find the smallest set of edges covering all ver- tices.

Dynamic Programming

Dynamic programming is a technique for solving some optimization problems.

A dynamic programming solution to a problem usually involves:

  1. Characterize the structure of an optimal so- lution.
  2. Recursively define an optimal solution in terms of optimal solutions for smaller problems.
  3. Design an algorithm that computes the opti- mal value for each subproblem once.
  4. Construct an optimal solution.

Notation: Use opt(.. .) to denote optimal value. Use arg(.. .) to denote optimal parameters.

Algorithm: P [0... n] are the dimensions.

Matrix-Chain-Order(P [0... n]) for i ← 1 to n do opt[i, i] ← 0 for j ← 2 to n do for i ← j − 1 downto 1 do opt[i, j] ← ∞ for k ← i to j − 1 do q ← opt[i, k] + opt[k + 1, j]

  • P [i − 1]P [k]P [j] if q < opt[i, j] then opt[i, j] ← q arg[i, j] ← k

Construction: A[i] is the ith matrix.

Matrix-Chain-Multiply(A, i, j) if i = j then return A[i] else X ← M.-C.-M.(A, i, arg[i, j]) Y ← M.-C.-M.(A, arg[i, j] + 1, j) return Matrix-Multiply(X, Y )

Longest Common Subseqeunce

Longest common subsequence (LCS) of “kahijedcdelln” and “hzpidysvdquen” is “hidden”. An example application is diff.

Structure: Let X = 〈x 1 ,... , xm〉 and Y = 〈y 1 ,... , yn〉. Let Z = 〈z 1 ,... , zk〉 be an LCS of X and Y.

  1. If xm = yn, then zk = xm = yn, and Z[1... k −1] is an LCS of X[1... m−1] and Y [1... n − 1].
  2. If xm 6 = yn, then zk 6 = xm or zk 6 = yn, so Z is an LCS of X[1... m − 1] and Y , or Z is an LCS of X and Y [1... n − 1].

Recursive Definition: Let opt(m, n) be the LCS length of 〈x 1 ,... , xm〉 and 〈y 1 ,... , yn〉

if m = 0 or n = 0, then opt(m, n) = 0 else if xm = yn, then

opt(m, n) = 1 + opt(m − 1 , n − 1)

else

opt(m, n) = max(opt(m − 1 , n), opt(m, n − 1)