• 沒有找到結果。

# Slides credited from Hsu-Chun Hsiao

N/A
N/A
Protected

Share "Slides credited from Hsu-Chun Hsiao"

Copied!
56
0
0

(1)
(2)

### Mini-HW 5 released

Due on 10/25 (Thu) 14:20

### Homework 2 released

Due on 11/06 (Tue) 18:00 (3.5 weeks)

A4 hardcopy submitted to a box @R307

Softcopy submitted to NTU COOL before the deadline

Frequently check the website for the updated information!

(3)
(4)

Dynamic Programming

DP #1: Rod Cutting

DP #2: Stamp Problem

DP #3: Knapsack Problem

0/1 Knapsack

Unbounded Knapsack

Multidimensional Knapsack

Fractional Knapsack

DP #4: Matrix-Chain Multiplication

DP #5: Sequence Alignment Problem

Longest Common Subsequence (LCS) / Edit Distance

(5)

(6)

### 每個囚犯皆可聽到之前所有囚犯的猜測內容。

……

Example: 奇數者猜測內 容為前面一位的帽子顏 色  存活期望值為75人 有沒有更多人可以存活的好策略?

(7)
(8)

Textbook Chapter 15.2 – Matrix-chain multiplication

## 8

(9)

1

𝑛

1

2

### … 𝐴

𝑛

𝐴1 𝐴2 𝐴3 𝐴4 …… 𝐴𝑛

𝐴1.cols=𝐴2.rows

𝐴1and 𝐴2are compatible.

(10)

A B C

(11)

(12)

(13)

0

1

### , … , 𝑙

𝑛

𝑙𝑖−1 is the number of rows of matrix 𝐴𝑖

𝑙𝑖 is the number of columns of matrix 𝐴𝑖

1

2

### … 𝐴

𝑛

𝐴1 𝐴2 𝐴3 𝐴4 …… 𝐴𝑛

𝐴1.cols=𝐴2.rows

𝐴1and 𝐴2are compatible.

Do not need to compute the result but find the fast way to get the result!

(14)

𝑛

𝑛

4𝑛

𝑛

3 2

### , or is also Ω 2

𝑛 Exercise 15.2-3

(15)

### Subproblems

M(i, j): the min #operations for obtaining the product of 𝐴𝑖 … 𝐴𝑗

Goal: M(1, n)

### Optimal substructure: suppose we know the OPT to M(i, j), there are k cases:

Case k: there is a cut right after Ak in OPT Matrix-Chain Multiplication Problem

Input: a sequence of integers 𝑙0, 𝑙1, … , 𝑙𝑛indicating the dimensionality of 𝐴𝑖

Output: a order of matrix multiplications with the minimum number of operations

𝐴𝑖𝐴𝑖+1… 𝐴𝑘 𝐴𝑘+1𝐴𝑘+2 … 𝐴𝑗 𝑖 ≤ 𝑘 < 𝑗

(16)

Suppose we know the optimal solution to M(i, j), there are k cases:

Case k: there is a cut right after Ak in OPT

### Recursively define the value

Matrix-Chain Multiplication Problem

Input: a sequence of integers 𝑙0, 𝑙1, … , 𝑙𝑛indicating the dimensionality of 𝐴𝑖

Output: a order of matrix multiplications with the minimum number of operations

𝐴𝑘+1..𝑗 𝐴𝑖.rows

=𝑙𝑖−1

𝐴𝑘.cols=𝑙𝑘

𝐴𝑘+1.rows=𝑙𝑘

𝐴𝑗.cols=𝑙𝑗 𝐴𝑖𝐴𝑖+1… 𝐴𝑘 𝐴𝑘+1𝐴𝑘+2… 𝐴𝑗 =

(17)

Bottom-up method: solve smaller subproblems first

How many subproblems to solve

#combination of the values 𝑖 and 𝑗 s.t. 1 ≤ 𝑖 ≤ 𝑗 ≤ 𝑛

Matrix-Chain Multiplication Problem

Input: a sequence of integers 𝑙0, 𝑙1, … , 𝑙𝑛indicating the dimensionality of 𝐴𝑖

Output: a order of matrix multiplications with the minimum number of operations

(18)

Matrix-Chain(n, l)

initialize two tables M[1..n][1..n] and B[1..n-1][2..n]

for i = 1 to n

M[i][i] = 0 // boundary case

for p = 2 to n // p is the chain length

for i = 1 to n – p + 1 // all i, j combinations j = i + p – 1

M[i][j] = ∞

for k = i to j – 1 // find the best k

q = M[i][k] + M[k + 1][j] + l[i - 1] * l[k] * l[j]

if q < M[i][j]

M[i][j] = q return M

(19)

How to decide the order of the matrix

multiplication?

1 2 3 4 5 6 n

1 0

2 0

3 0

4 0

5 0

6 0

0

n 0

(20)

Matrix-Chain(n, l)

initialize two tables M[1..n][1..n] and B[1..n-1][2..n]

for i = 1 to n

M[i][i] = 0 // boundary case

for p = 2 to n // p is the chain length

for i = 1 to n – p + 1 // all i, j combinations j = i + p – 1

M[i][j] = ∞

for k = i to j – 1 // find the best k

q = M[i][k] + M[k + 1][j] + l[i - 1] * l[k] * l[j]

if q < M[i][j]

M[i][j] = q

B[i][j] = k // backtracking return M and B

Print-Optimal-Parens(B, i, j) if i == j

print 𝐴𝑖

(21)

Matrix 𝑨𝟏 𝑨𝟐 𝑨𝟑 𝑨𝟒 𝑨𝟓 𝑨𝟔 Dimension 30 x 35 35 x 15 15 x 5 5 x 10 10 x 20 20 x 25

1 2 3 4 5 6

1 0

2 0

3 0

4 0

5 0

6 0

15,750

2,625 750

1,000

5,000 7,875

4,375

2,500

3,500 9,375

7,125

53,75 11,875

10,500 15,125

1 2 3 4 5 6

1 2 3 4 5 6

1

2

3

4

5 1

3

3

5 3

3

3 3

3 3

(22)

Textbook Chapter 15.4 – Longest common subsequence Textbook Problem 15-5 – Edit distance

## 22

(23)

banana

(24)

### Output: longest common subsequence of two sequences

The maximum-length sequence of characters that appear left-to-right (but not necessarily a continuous string) in both sequences

4 5

(25)

### Output: the minimum cost of transformation from X to Y

Quantifier of the dissimilarity of two strings

### Y = svkbrlvpnzanczyqzaX → ---ba---n-an---aY → svkbrlvpnzanczyqzaX = banana

1 deletion, 7 insertions, 1 substitution 12 insertions, 1 substitution

9 13

(26)

𝑚,𝑛

### for aligning two sequences

Cost = #insertions × 𝐶INS + #deletions × 𝐶DEL + #substitutions × 𝐶𝑝,𝑞

(27)

Subproblems

SA(i, j): sequence alignment between prefix strings 𝑥1, … , 𝑥𝑖 and 𝑦1, … , 𝑦𝑗

Goal: SA(m, n)

Optimal substructure: suppose OPT is an optimal solution to SA(i, j), there are 3 cases:

Case 1: 𝑥𝑖 and 𝑦𝑗 are aligned in OPT (match or substitution)

OPT/{𝑥𝑖, , 𝑦𝑗} is an optimal solution of SA(i-1, j-1)

Case 2: 𝑥𝑖 is aligned with a gap in OPT (deletion)

OPT is an optimal solution of SA(i-1, j)

Case 3: 𝑦𝑗 is aligned with a gap in OPT (insertion)

OPT is an optimal solution of SA(i, j-1)

Sequence Alignment Problem Input: two sequences

Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences

(28)

### Suppose OPT is an optimal solution to SA(i, j), there are 3 cases:

Case 1: 𝑥𝑖 and 𝑦𝑗 are aligned in OPT (match or substitution)

OPT/{𝑥𝑖, , 𝑦𝑗} is an optimal solution of SA(i-1, j-1)

Case 2: 𝑥𝑖 is aligned with a gap in OPT (deletion)

OPT is an optimal solution of SA(i-1, j)

Case 3: 𝑦𝑗 is aligned with a gap in OPT (insertion)

OPT is an optimal solution of SA(i, j-1)

Sequence Alignment Problem Input: two sequences

Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences

(29)

Bottom-up method: solve smaller subproblems first

X\Y 0 1 2 3 4 5 n

0 1 : m

Sequence Alignment Problem Input: two sequences

Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences

(30)

Bottom-up method: solve smaller subproblems first

X\Y 0 1 2 3 4 5 6 7 8 9 10 11 12

0 0 4 8 12 16 20 24 28 32 36 40 44 48

1 4 7 11 15 19 23 27 31 35 39 43 47 51

2 8 4 8 12 16 20 23 27 31 35 39 43 47

3 12 8 12 8 12 16 20 24 28 32 36 40 44

Sequence Alignment Problem Input: two sequences

Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences

a e n i q a d i k j a z

b a n

(31)

Seq-Align(X, Y, CDEL, CINS, Cp,q) for j = 0 to n

M[j] = j * CINS // |X|=0, cost=|Y|*penalty for i = 1 to m

M[i] = i * CDEL // |Y|=0, cost=|X|*penalty for i = 1 to m

for j = 1 to n

M[i][j] = min(M[i-1][j-1]+Cxi,yi, M[i-1][j]+CDEL, M[i][j-1]+CINS)

Bottom-up method: solve smaller subproblems first Sequence Alignment Problem

Input: two sequences

Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences

(32)

Bottom-up method: solve smaller subproblems first Sequence Alignment Problem

Input: two sequences

Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences

X\Y 0 1 2 3 4 5 6 7 8 9 10 11 12

0 0 4 8 12 16 20 24 28 32 36 40 44 48

1 4 7 11 15 19 23 27 31 35 39 43 47 51

2 8 4 8 12 16 20 23 27 31 35 39 43 47

3 12 8 12 8 12 16 20 24 28 32 36 40 44

a e n i q a d i k j a z

b a n

(33)

Bottom-up method: solve smaller subproblems first Sequence Alignment Problem

Input: two sequences

Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences

Find-Solution(M) if m = 0 or n = 0

return {}

v = min(M[m-1][n-1] + Cxm,yn, M[m-1][n] + CDEL, M[m][n-1] + CINS) if v = M[m-1][n] + CDEL // ↑: deletion

return Find-Solution(m-1, n)

if v = M[m][n-1] + CINS // ←: insertion return Find-Solution(m, n-1)

(34)

Find-Solution(M) if m = 0 or n = 0

return {}

v = min(M[m-1][n-1] + Cxm,yn, M[m-1][n] + CDEL, M[m][n-1] + CINS) if v = M[m-1][n] + CDEL // ↑: deletion

Seq-Align(X, Y, CDEL, CINS, Cp,q) for j = 0 to n

M[j] = j * CINS // |X|=0, cost=|Y|*penalty for i = 1 to m

M[i] = i * CDEL // |Y|=0, cost=|X|*penalty for i = 1 to m

for j = 1 to n

M[i][j] = min(M[i-1][j-1]+Cxi,yi, M[i-1][j]+CDEL, M[i][j-1]+CINS) return M[m][n]

(35)

### If only keeping the most recent two rows: Space-Seq-Align(X, Y)

X\Y 0 1 2 3 j n

i - 1 i

X\Y 0 1 2 3 4 5 n

0 1 : m

(36)

### Problem: find the min-cost alignment  find the shortest path

Divide-and-Conquer +

Dynamic Programming

### a

X\Y 0 1 2 3

0 0 4 8 12

1 4 7 11 15

2 8 4 8 12

3 12 8 12 8

4 16 12 15 12 5 20 16 19 15 a p e

p p l e a

START

END

(37)

𝐹 2,3 = distance of the shortest path

### 5

𝐵 2,3 = distance of the shortest path

(38)

(39)

### Observation 1: the length of the shortest path from 0,0 to 𝑚, 𝑛 that passes through 𝑖, 𝑗 is 𝐹 𝑖, 𝑗 + 𝐵 𝑖, 𝑗

39

𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛

𝐹 𝑖, 𝑗

𝐵 𝑖, 𝑗

(40)

### Observation 2: for any 𝑣 in {0, … , 𝑛}, there exists a 𝑢 s.t. the shortest path between (0,0) and 𝑚, 𝑛 goes through (𝑢, 𝑣)

𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛

(41)

### Observation 1+2:

𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛

(42)

### Goal: finds optimal solution

How to find the value of 𝑢?

### Idea: utilize sequence alignment algo.

Call Space-Seq-Align(X,Y[1:v]) to find 𝐹 0, 𝑣 , 𝐹 1, 𝑣 , … , 𝐹 𝑚, 𝑣

Call Back-Space-Seq-Align(X,Y[v+1:n]) to find 𝐵 0, 𝑣 , 𝐵 1, 𝑣 , … , 𝐵 𝑚, 𝑣

Let 𝑢 be the index minimizing 𝐹 𝑢, 𝑣 + 𝐵 𝑢, 𝑣

(43)

### Goal: finds optimal solution – DC-Align(X, Y)

1. Divide

2. Conquer

3. Combine

Divide the sequence of size n into 2 subsequences

Find 𝑢 to minimize 𝐹 𝑢, 𝑣 + 𝐵 𝑢, 𝑣

Recursive case (𝑛 > 1)

prefix

= DC-Align(X[1:u], Y[1:v])

suffix

= DC-Align(X[u+1:m], Y[v+1:n])

Base case (𝑛 = 1)

Return Seq-Align(X, Y)

Return prefix + suffix

𝑇 𝑚, 𝑛 = time for running DC-Align(X, Y) with 𝑋 = 𝑚, 𝑌 = 𝑛

Space Complexity:

(44)

### Proof

There exists positive constants 𝑎, 𝑏 s.t. all

Use induction to prove

Inductive

Practice to check the initial condition

(45)

1

2

𝑛

### ㄨ ㄅ ㄒ ㄎ ㄕ

START

END

Find the path from START to END with

highest prob

(46)

1

2

### … … 𝜎

𝑛

START END

produce 𝜎1

produce 𝜎𝑗

V: vocabulary size

(47)

## 47

(48)

𝑖

𝑖

### The interval scheduling problem can be solved using an “early-finish-time-first” greedy algorithm in 𝑂(𝑛) time

“Greedy Algorithm”

Next topic!

1 2 3 4

job index

(49)

𝑖

𝑖

𝑖

### Output: the maximum total value obtainable from compatible jobs

time 49

1

3

3 4

3 1 1

2 3 4 5 6

job index

Assume that the requests are sorted in non-decreasing order (𝑓𝑖 ≤ 𝑓𝑗 when 𝑖 < 𝑗) 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible

e.g. 𝑝 1 = 0, 𝑝 2 = 0, 𝑝 3 = 1, 𝑝 4 = 1, 𝑝 5 = 4, 𝑝 6 = 3

(50)

### Subproblems

WIS(i): weighted interval scheduling for the first 𝑖 jobs

Goal: WIS(n)

### Optimal substructure: suppose OPT is an optimal solution to WIS(i), there are 2 cases:

Case 1: job 𝑖 in OPT

OPT\{𝑖} is an optimal solution of WIS(p(i))

Weighted Interval Scheduling Problem

Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible

1 3

3 4 1

2 3 4

job index

4

(51)

### Optimal substructure: suppose OPT is an optimal solution to WIS(i), there are 2 cases:

Case 1: job 𝑖 in OPT

OPT\{𝑖} is an optimal solution of WIS(p(i))

Case 2: job 𝑖 not in OPT

OPT is an optimal solution of WIS(i-1)

### Recursively define the value

Weighted Interval Scheduling Problem

Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible

(52)

Bottom-up method: solve smaller subproblems first

i 0 1 2 3 4 5 n

M[i]

WIS(n, s, f, v, p) M = 0

Weighted Interval Scheduling Problem

Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible

(53)

53

Bottom-up method: solve smaller subproblems first Weighted Interval Scheduling Problem

Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible

i 0 1 2 3 4 5 6

M[i] 0 1 3 4 5 6 7

1

3

3 4

3 1 1

2 3 4 5 6 job index

(54)

WIS(n, s, f, v, p) M = 0

for i = 1 to n

M[i] = max(v[i] + M[p[i]], M[i - 1]) return M[n]

Weighted Interval Scheduling Problem

Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible

Find-Solution(M, n) if n = 0

return {}

(55)

### When to use DP

Whether subproblem solutions can combine into the original solution

When subproblems are overlapping

Whether the problem has optimal substructure

Common for optimization problem

### Two ways to avoid recomputation

Top-down with memoization

Bottom-up method

### Complexity analysis

Space for tabular filling

(56)

## 56

Important announcement will be sent to @ntu.edu.tw mailbox

& post to the course website

Textbook Chapter 4.3 – The substitution method for solving recurrences Textbook Chapter 4.4 – The recursion-tree method for solving recurrences Textbook Chapter 4.5 – The master

Textbook Chapter 33.4 – Finding the closest pair of points.. Closest Pair of

Textbook Chapter 4.3 – The substitution method for solving recurrences Textbook Chapter 4.4 – The recursion-tree method for solving recurrences Textbook Chapter 4.5 – The master

Using

Goal: predict surrounding words within a window of each word Objective function: maximize the probability of any context word given the current center

Goal: predict surrounding words within a window of each word Objective function: maximize the log probability of any context word given the current center

Calculate the amortized cost of each operation based on the potential function. Calculate total amortized cost based on

✓ Combining an optimal solution to the subproblem via greedy can arrive an optimal solution to the original problem.. Prove that there is always an optimal solution to the

vertices’ edges, in this shortest path, the left edge must be relaxed before the right edge.  One phase of improvement

 Combine: find closet pair with one point in each region, and return the best of three

jobs

▪ Step 2: Run DFS on the transpose

Greedy-Choice Property : making locally optimal (greedy) choices leads to a globally optimal

 From a source vertex, systematically follow the edges of a graph to visit all reachable vertices of the graph.  Useful to discover the structure of

 “Greedy”: always makes the choice that looks best at the moment in the hope that this choice will lead to a globally optimal solution.  When to

✓ Express the solution of the original problem in terms of optimal solutions for subproblems. Construct an optimal solution from

✓ Express the solution of the original problem in terms of optimal solutions for subproblems.. Construct an optimal solution from

 If SAT can be solved in deterministic polynomial time, then so can any NP problems  SAT ∈ NP-hard..  If A is an NP-hard problem and B can be reduced from A, then B is an

▪ Approximation algorithms for optimization problems: the approximate solution is guaranteed to be close to the exact solution (i.e., the optimal value)..

Input domain: word, word sequence, audio signal, click logs Output domain: single label, sequence tags, tree structure, probability

◦ Value function: how good is each state and/or action.. ◦ Policy: agent’s

State value function: when using

▪ Can we decide whether a problem is “too hard to solve” before investing our time in solving it.. ▪ Idea: decide which complexity classes the problem belongs to