Dynamic Programming Reporter : 林明慧.

Slides:



Advertisements
Similar presentations
Knapsack Problem Section 7.6. Problem Suppose we have n items U={u 1,..u n }, that we would like to insert into a knapsack of size C. Each item u i has.
Advertisements

COMP8620 Lecture 8 Dynamic Programming.
Chapter 7 Dynamic Programming.
Chapter 7 Dynamic Programming 7.
§ 8 Dynamic Programming Fibonacci sequence
Dynamic Programming Reading Material: Chapter 7..
Knapsack Problem Section 7.6. A 8 lbs $ B 6 lbs $ C 4 lbs $ D 2 lbs $ E 1 lb $ lbs capacity A 8 lbs $ B 6 lbs $6.
7 -1 Chapter 7 Dynamic Programming Fibonacci Sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence (1) 0,1,1,2,3,5,8,13,21,34,... Leonardo Fibonacci ( ) 用來計算兔子的數量 每對每個月可以生產一對 兔子出生後,
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
1 A Linear Space Algorithm for Computing Maximal Common Subsequences Author: D.S. Hirschberg Publisher: Communications of the ACM 1975 Presenter: Han-Chen.
1 Dynamic Programming Jose Rolim University of Geneva.
Longest Common Subsequence
Dynamic Programming (16.0/15) The 3-d Paradigm 1st = Divide and Conquer 2nd = Greedy Algorithm Dynamic Programming = metatechnique (not a particular algorithm)
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
8 -1 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if i  2 Solved.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Algorithm Design Methods (II) Fall 2003 CSE, POSTECH.
Unit-3: Greedy Algorithm Knapsack Algorithm Code Assignment: Implementation of code using Java and approach discussed in class: Roll no’s to submit assignment:
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
CSC 213 Lecture 19: Dynamic Programming and LCS. Subsequences (§ ) A subsequence of a string x 0 x 1 x 2 …x n-1 is a string of the form x i 1 x.
8 -1 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if i  2 Solved.
Dynamic Programming … Continued
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
David Meredith Dynamic programming David Meredith
CS 3343: Analysis of Algorithms
Advanced Algorithms Analysis and Design
Robbing a House with Greedy Algorithms
Least common subsequence:
Search
CS200: Algorithm Analysis
Algorithm Design Methods
Dynamic Programming.
السيولة والربحية أدوات الرقابة المالية الوظيفة المالية
Dynamic Programming General Idea
Exam 2 LZW not on syllabus. 73% / 75%.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
ICS 353: Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Merge Sort Dynamic Programming
Dynamic Programming.
CS6045: Advanced Algorithms
Longest Common Subsequence
Ch. 15: Dynamic Programming Ming-Te Chi
Trevor Brown DC 2338, Office hour M3-4pm
Dynamic Programming General Idea
Algorithm Design Methods
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Knapsack Problem Truck – 10t capacity Optimum cargo combination:
Longest common subsequence (LCS)
Knapsack Problem Truck – 10t capacity Optimum cargo combination:
Dynamic Programming Sequence of decisions. Problem state.
Lecture 5 Dynamic Programming
Analysis of Algorithms CS 477/677
Lecture 5 Dynamic Programming
Advanced Analysis of Algorithms
Chapter 15: Dynamic Programming
Data Structures and Algorithm Analysis Lecture 15
Chapter 15: Dynamic Programming
Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
Longest Common Subsequence
Knapsack Problem A dynamic approach.
Algorithm Course Dr. Aref Rashad
Presentation transcript:

Dynamic Programming Reporter : 林明慧

Outline The Longest Common Subsequence Problem 0/1 Knapsack Problem

The Longest Common Subsequence Problem The longest common subsequence problem is to find a longest common subsequence between two strings. A straightforward method is to find all common subsequences by an exhaustive search.

The Longest Common Subsequence Problem Example A = a b a a d e c B = b a f c Try subsequences chosen from B with length three : a f c b f c b a c b a f

The Longest Common Subsequence Problem Disadvantage Exceedingly time-consuming New method By tracing back the procedure finding the length of the longest common subsequence.

The Longest Common Subsequence Problem A = x1 x2 … xm , B = y1 y2 … yn Two possibilities : xm = yn. xm is contained in the longest common subsequence. x1 x2 … xm-1 y1 y2 … yn-1 xm yn. x1 x2 … xm with y1 y2 … yn-1 x1 x2 … xm-1 with y1 y2 … yn Example : A = a b a a d e c B = b a f c e c f c

Recursive formula x1 x2 … xi-1 xi xm y1 y2 yj-1 yj yn 1 1

Example A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 3 4 1 2 3 4 5 6 7 L(i,j) 1 2 1 2 1 2 3 1 2 3 1 2 3 4 4 1 2 3 1 1 2 1 1 1 1 1 2 1 2 1 1 2

Experiment_1 A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 1 2 3 4 5 6 7 L(i,j) 1 2 3 1 2 3 1 2 3 4 1 1 1 2 1 2 3

Experiment_2 A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 1 2 3 4 5 6 7 L(i,j) 1 2 3 1 2 3 1 2 3 4 1 1 1 2 1 2

Result_1 A = a b a a d e c , B = c a a c e d c A longest common subsequence is a a e c. A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 3 4 5 6 7 L(i,j)

Result_2 A = a b a a d e c , B = c a a c e d c A longest common subsequence is a a d c. A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 3 4 5 6 7 L(i,j)

0/1 Knapsack Problem The 0/1 knapsack problem is to maximize the total profit under the constraint that the total weight of all chosen objects is at most capacity.

0/1 Knapsack Problem It is defined as follows: Given n objects and a knapsack. Object i has a weight Wi. Knapsack has a capacity M. If object i is placed into the knapsack, we will obtain a profit Pi.

Example Capacity M = 10 i Wi Pi 1 10 40 2 3 20 5 30 x2=0 x3=0 A C F 40 10-10=0 G S x3=1 30 D 7-5=2 x2=1 20 H T 10-3=7 x3=0 x1=0 B 7-0=7 I 10-0=10 x3=1 E 30 x2=0 10-5=5 10-0=10 J x3=0 10-0=10

Example d(S,T) = max{40+d(A,T) , 0+d(B,T)} d(A,T) d(B,T) A 40 S T B S C D E F G H I J T d(S,T) = max{40+d(A,T) , 0+d(B,T)} A 40 d(A,T) S T B d(B,T)

Example d(A,T) = 0+0+0 = 0 d(B,T) = max{20+d(D,T) , 0+d(E,T)} d(D,T) S A B C D E F G H I J T d(A,T) = 0+0+0 = 0 d(B,T) = max{20+d(D,T) , 0+d(E,T)} A C F T D d(D,T) 20 B T d(E,T) E

Example S A B C D E F G H I J T d(D,T) = max{30+d(G,T) , 0+d(H,T)} = max{30+0 , 0+0} = max{30 , 0} = 30 d(E,T) = max{30+d(I,T) , 0+d(J,T)} = max{30+0 , 0+0} = max{30 , 0} = 30 G 30 D T H E I J T 30

Example S A B C D E F G H I J T d(B,T) = max{20+d(D,T) , 0+d(E,T)} = max{20+30 , 0+30} = max{50 , 30} = 50 d(S,T) = max{40+d(A,T) , 0+d(B,T)} = max{40+0 , 0+50} = max{40 , 50} = 50 B D E T 20 d(D,T) d(E,T) S A B T 40 d(A,T) d(B,T)

Example Capacity M = 10 0 + 20 + 30 + 0 = 50 i Wi Pi 1 10 40 2 3 20 5 0 + 20 + 30 + 0 = 50 x2=0 x3=0 A C F x1=1 40 G S x3=1 30 D x2=1 20 H T x3=0 x1=0 B x3=1 I E 30 x2=0 J x3=0

Another Expression Recursive formula

Example Capacity M = 10 Capacity j i 1 2 3 4 5 6 7 8 9 10 W1=10, P1=40 1 2 3 4 5 6 7 8 9 10 W1=10, P1=40 40 W2=3, P2=20 20 W3=5, P3=30 30 50 P[1,10] = max{P[1-1,10],40+P[1-1,10-10]} = 40 P[2,10] = max{P1,P2} = 40 P[2,3] = max{P[2-1,3],20+P[2-1,3-3]} = 20 P[3,3] = P2 = 20 P[3,5] = max{P[3-1,5],30+P[3-1,5-5]} = 30 P[3,8] = P2+P3 = 50 P[3,10] = max{P1,(P2+P3)} = 50

The End Have A Nice Holiday!