• 沒有找到結果。

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

N/A
N/A
Protected

Academic year: 2022

Share "Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai"

Copied!
15
0
0

加載中.... (立即查看全文)

全文

(1)
(2)

Dynamic Programming

DP #1: Rod Cutting

DP #2: Stamp Problem

DP #3: Matrix-Chain Multiplication

DP #4: Sequence Alignment Problem

Longest Common Subsequence (LCS) / Edit Distance

Viterbi Algorithm

Space Efficient Algorithm

DP #5: Weighted Interval Scheduling

DP #6: Knapsack Problem

0/1 Knapsack

Unbounded Knapsack

Multidimensional Knapsack

Fractional Knapsack 2

(3)

Do not focus on “specific algorithms”

But “some strategies” to “design” algorithms

First Skill: Divide-and-Conquer (各個擊破)

Second Skill: Dynamic Programming (動態規劃)

(4)

Textbook Chapter 15 – Dynamic Programming

Textbook Chapter 15.3 – Elements of dynamic programming

4

(5)

 Dynamic programming, like the divide-and-conquer

method, solves problems by combining the solutions to subproblems

 用空間換取時間

 讓走過的留下痕跡

 “Dynamic”: time-varying

“Programming”: a tabular method

Dynamic Programming: planning over time

(6)

Divide-and-Conquer

partition the problem into independent or disjoint subproblems

repeatedly solving the common subsubproblems

 more work than necessary

Dynamic Programming

partition the problem into dependent or overlapping subproblems

avoid recomputation

Top-down with memoization

Bottom-up method

6

(7)

 Apply four steps

1.

Characterize the structure of an optimal solution

2.

Recursively define the value of an optimal solution

3.

Compute the value of an optimal solution, typically in a bottom- up fashion

4.

Construct an optimal solution from computed information

(8)

Fibonacci sequence (費波那契數列)

Base case: F(0) = F(1) = 1

Recursive case: F(n) = F(n-1) + F(n-2)

8

Fibonacci(n)

if n < 2 // base case return 1

// recursive case

return Fibonacci(n-1)+Fibonacci(n-2) F(5)

F(4) F(3)

F(3) F(2) F(2) F(1)

F(2) F(1) F(1) F(0) F(1) F(0)

F(1) F(0) Calling overlapping subproblems result in poor efficiency

F(3) was computed twice

F(2) was computed 3 times

(9)

Solve the overlapping subproblems recursively with memoization

Check the memo before making the calls

F(5)

F(4) F(3)

F(3) F(2)

F(2) F(1)

備忘錄

n 0 1 2 3 4 5

(10)

10

Memoized-Fibonacci(n)

// initialize memo (array a[]) a[0] = 1

a[1] = 1

for i = 2 to n a[i] = 0

return Memoized-Fibonacci-Aux(n, a) Memoized-Fibonacci-Aux(n, a)

if a[n] > 0 return a[n]

// save the result to avoid recomputation

a[n] = Memoized-Fibonacci-Aux(n-1, a) + Memoized-Fibonacci-Aux(n-2, a) return a[n]

(11)

Building up solutions to larger and larger subproblems

Bottom-Up-Fibonacci(n) if n < 2

return 1 a[0] = 1 a[1] = 1

for i = 2 … n

a[i] = a[i-1] + a[i-2]

return a[n]

F(5) F(4) F(3) F(2) F(1)

(12)

Principle of Optimality

Any subpolicy of an optimum policy must itself be an optimum policy with regard to the initial and terminal states of the subpolicy

Two key properties of DP for optimization

Overlapping subproblems

Optimal substructure – an optimal solution can be constructed from optimal solutions to subproblems

Reduce search space (ignore non-optimal solutions)

12

If the optimal substructure (principle of optimality) does not hold, then it is incorrect to use DP

(13)

Shortest Path Problem

Input: a graph where the edges have positive costs

Output: a path from S to T with the smallest cost

Taipei (T)

M

CSM CMT

C’SM < CSM?

The path costing CSM+ CMT is the shortest path from S to T

 The path with the cost CSM must be a shortest path from S to M

(14)

14

(15)

15

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

& post to the course website

參考文獻

相關文件

▪ 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

Part (a) can be established based on the density formula for variable transforma- tions.. Part (b) can be established with the moment

(Why do we usually condemn the person who produces a sexually explicit material to make money but not a person who does the same thing in the name of art?). • Do pornographic

To avoid the circuit complication in the four-terminal measurement with high frequency operation, two-terminal approach was developed by elimination of spin independent

If the best number of degrees of freedom for pure error can be specified, we might use some standard optimality criterion to obtain an optimal design for the given model, and