Poscat seminar 7

55
POSCAT Seminar 7 : Dynamic Programming 1 yougatup @ POSCAT 1

Transcript of Poscat seminar 7

Page 1: Poscat seminar 7

POSCAT Seminar 7 :Dynamic Programming 1

yougatup @ POSCAT

1

Page 2: Poscat seminar 7

Topic

Topic today

− Dynamic Programming

• Basic Concept

• Longest Increasing Subsequence

• Longest Common Subsequence

POSCAT Seminar 1-29 July 2014

yougatup

Page 3: Poscat seminar 7

Dynamic Programming

Problem Solving Paradigm

− Identifying a collection of subproblems

− Tackling them one by one

• smallest first

• using the answer already calculated to help figure out the

larger ones

• until the whole problem is solved

One of the most prominent issue

− You can be familiar with PS with Dynamic Programming

− There are so many issues to cover

POSCAT Seminar 1-39 July 2014

yougatup

Page 4: Poscat seminar 7

Dynamic Programming

Procedure

1. Define the Table

2. Find recurrence relation

3. Calculate !

POSCAT Seminar 1-49 July 2014

yougatup

Page 5: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

POSCAT Seminar 1-59 July 2014

yougatup

Page 6: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

POSCAT Seminar 1-69 July 2014

yougatup

Page 7: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

POSCAT Seminar 1-79 July 2014

yougatup

Page 8: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

Let 𝑇(𝑖) = The length of LIS when 𝑎𝑖 is the last element

∴ 𝑎𝑖 have to be last element of subsequence

POSCAT Seminar 1-89 July 2014

yougatup

Page 9: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

You can fill the table manually. What is the answer ?

POSCAT Seminar 1-99 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 10: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

Can you see the recurrence relation on 𝑇 ?

POSCAT Seminar 1-109 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 11: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

Suppose that 𝑗 < 𝑖 and 𝐷𝑎𝑡𝑎 𝑗 < 𝐷𝑎𝑡𝑎(𝑖).

Then we can add one more element to Increasing Subsequence

whose last element is 𝐷𝑎𝑡𝑎(𝑗)

POSCAT Seminar 1-119 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 12: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

Consider element 6 in the example.

POSCAT Seminar 1-129 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 13: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

We can make a Increasing Subsequence by adding 6 whose last

element is 5. ∴ we can make “5 6”

POSCAT Seminar 1-139 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 14: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

However, we can make another Increasing Subsequence by

considering 2 ! Still the length is 2

POSCAT Seminar 1-149 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 15: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

We can’t make IS with 8 because 8 > 6

POSCAT Seminar 1-159 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 16: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

We can’t

POSCAT Seminar 1-169 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 17: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

1. Define the Table

We can make ! Therefore, our length is 3.

Note that we considered all the case.

POSCAT Seminar 1-179 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 18: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

2. Find a recurrence relation

𝑇 𝑖 = max 𝑇 𝑗 + 1 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑗 < 𝑖 & 𝐷𝑎𝑡𝑎 𝑗 < 𝐷𝑎𝑡𝑎(𝑖)

POSCAT Seminar 1-189 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 19: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

3. Calculate !

You have to think about where the answer is

POSCAT Seminar 1-199 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 20: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

3. Calculate !

You have to think about where the answer is

It must be the maximum value of whole Table

POSCAT Seminar 1-209 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 21: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Well, we can calculate the length of LIS.

Can we find subsequence itself? i.e. “2 3 6 9” in the example.

POSCAT Seminar 1-219 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 22: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Well, we can calculate the length of LIS.

Can we find subsequence itself? i.e. “2 3 6 9” in the example.

That’s possible by writing history !

POSCAT Seminar 1-229 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 23: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

What is the answer on this example ?

POSCAT Seminar 1-239 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 24: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

What is the answer on this example ?

POSCAT Seminar 1-249 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 25: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

What is the answer on this example ?

Then the last element of LIS must be 9 because the definition of 𝑇(𝑖)

said that 𝑎𝑖 should be the last element !

POSCAT Seminar 1-259 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 26: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Well, 9 is definitely last element of LIS.

How about other elements on LIS ?

POSCAT Seminar 1-269 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 27: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Well, 9 is definitely last element of LIS.

How about other elements on LIS ?

𝑇(6) is 4 because we add 9 to the end of IS whose last element is 6

POSCAT Seminar 1-279 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 28: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Therefore, 6 have to be located on the left of 9

POSCAT Seminar 1-289 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 29: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Therefore, 6 have to be located on the left of 9

Repeat this procedure until we have no former element

POSCAT Seminar 1-299 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Page 30: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Therefore, 6 have to be located on the left of 9

Repeat this procedure until we have no former element

POSCAT Seminar 1-309 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

It has no former element !

Page 31: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

We call this procedure as “Backtrace”

To implement Backtrace, we have to write down the history.

POSCAT Seminar 1-319 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Hist. None None 1 1 1 4 5 5

It means that former element of 𝑎5 is 𝑎4 !

Page 32: Poscat seminar 7

Longest Increasing Subsequence

Problem

Find an increasing subsequence of greatest length

5 2 8 6 3 6 9 7

Implement it . It is very easy to make a stress of yours

POSCAT Seminar 1-329 July 2014

yougatup

index 0 1 2 3 4 5 6 7

Data 5 2 8 6 3 6 9 7

Table 1 1 2 2 2 3 4 4

Hist. None None 1 1 1 4 5 5

Page 33: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

POSCAT Seminar 1-339 July 2014

yougatup

Page 34: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

POSCAT Seminar 1-349 July 2014

yougatup

Page 35: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

1. Define the Table

Give me !

POSCAT Seminar 1-359 July 2014

yougatup

Page 36: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

1. Define the Table

Let 𝑇(𝑖, 𝑗) = The length of LCS between (𝑎1~ 𝑎𝑖) and (𝑏1~ 𝑏𝑗)

POSCAT Seminar 1-369 July 2014

yougatup

Page 37: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

1. Define the Table Fill it manually !

POSCAT Seminar 1-379 July 2014

yougatup

S N O W Y

S

U

N

N

Y

Page 38: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

1. Define the Table How can we feel it ?

POSCAT Seminar 1-389 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

Page 39: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

Consider this situation.

POSCAT Seminar 1-399 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

Page 40: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

First of all, we can’t “match” O and U because they are different

POSCAT Seminar 1-409 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

Page 41: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

Therefore, we can remove one or both element

POSCAT Seminar 1-419 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

Page 42: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

What is the length of LCS when we remove O ?

POSCAT Seminar 1-429 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

Page 43: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

What is the length of LCS when we remove O ?

POSCAT Seminar 1-439 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

It knows !

Page 44: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

What is the length of LCS when we remove U ?

POSCAT Seminar 1-449 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

Page 45: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

What is the length of LCS when we remove U ?

POSCAT Seminar 1-459 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1

N

N

Y

It has !

Page 46: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

Therefore, the value of this entry is 1

POSCAT Seminar 1-469 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1 1

N

N

Y

Page 47: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

How about this situation ?

POSCAT Seminar 1-479 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1 1 1 1

N 1

N

Y

Page 48: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

We can match N! because they are the same

POSCAT Seminar 1-489 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1 1 1 1

N 1

N

Y

Page 49: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

Then, the LCS must be (LCS between “S” and “SU”) + N

POSCAT Seminar 1-499 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1 1 1 1

N 1

N

Y

Page 50: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

Therefore, we get SN. In addition, the value of this entry is 2

POSCAT Seminar 1-509 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1 1 1 1

N 1 2

N

Y

Page 51: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

Can you see the recurrence relation ?

POSCAT Seminar 1-519 July 2014

yougatup

S N O W Y

S 1 1 1 1 1

U 1 1 1 1 1

N 1 2 2 2 2

N 1 2 2 2 2

Y 1 2 2 2 3

Page 52: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

2. Find a recurrence relation

POSCAT Seminar 1-529 July 2014

yougatup

Page 53: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

2. Find a recurrence relation

𝑇 𝑖, 𝑗 = 𝑇 𝑖 − 1, 𝑗 − 1 + 1 𝑖𝑓 𝑎𝑖 = 𝑏𝑗

max 𝑇 𝑖 − 1, 𝑗 , 𝑇 𝑖, 𝑗 − 1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

POSCAT Seminar 1-539 July 2014

yougatup

Page 54: Poscat seminar 7

Longest Common Subsequence

Problem

Find an Longest Common Subsequence

S N O W Y

S U N N Y

3. Calculate !

Think about backtrace. It will be easy if you understand DP well.

POSCAT Seminar 1-549 July 2014

yougatup

Page 55: Poscat seminar 7

Dynamic Programming

Remember. 3 step is enough.

− The first step is the most important thing. Finding the

recurrence relation is not that hard commonly.

− I showed somewhat detail explanation of solving procedure

because it is first time to you

− From the next time, I will just let you know what the definition

of Table and the recurrent relation is

Please solve all the problem

POSCAT Seminar 1-559 July 2014

yougatup