Docsity

Lecture Slides on Dynamic Programming - Algorithms Design | ECS 122A, Study notes of Computer Science

Partial preview of the text

Download Lecture Slides on Dynamic Programming - Algorithms Design | ECS 122A and more Study notes Computer Science in PDF only on Docsity!

Discussion # 4

Yuanbo Zhu

Outline

• Review of Assembly-line scheduling • Review of Longest common sequence • Dynamic programming

VS

Greedy

algorithm

Sequence in the development of a dynamic programming algorithm

Characterize the structure of an optimalsolution

Recursively define the value of an optimalsolution

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

fashion

(smaller subproblems first)

Construct an optimal solution from computedinformation

Step 4 omitted if only the value of the best solutionsis required

Assembly-line scheduling

Assembly-line scheduling

dichotomy

Assembly-line scheduling

Longest Common Subsequence (LCS)

Problem: Given sequences x[1..m] andy[1..n], find a longest commonsubsequence of both. - Example: x=ABCBDAB andy=BDCABA,

BCA is a common subsequence and

BCBA and BDAB are two LCSs

X

i

and

Y

j

end with x

i

y

j

X

i^ x x … xi- xi

Y

j y y

yj- yj =x i

Z

k z 1 z 2 …z k- z k =y j =x i

Z

k

is

Z

k -

followed by z

k

y

j

x

i^

where

Z

k-

is an LCS of

X

i-

and

Y

j^

and

C

i

j

C

i

j

The recurrence equation

otherwise

max{

and

if

or

if

j i j i C j i C

y x j i j i C

j

i

j

i

C

Example

y j^ B D C A x j^ 0 0 0 0 0 A 0 0 0 0 1 B 0 1 1 1 1 C 0 1 1 2 2 B 0 1 1 2 2 To find an LCS follow the arrows, for each diagonal arrow there is a member of the LCS

16

Interval scheduling

Input:

set of intervals on the line,

represented by pairs of points (ends ofintervals)

Output:

the largest set of intervals such that

none two of them overlap

Generic greedy solution: • Consider intervals one after another using

some rule

17

Rule 1

Select the interval that starts earliest (

but is not overlapping the already chosen intervals

Underestimated solution!

optimal algorithm

19

Rule 3

Select the interval intersecting the smallest number

of remaining intervals

but still is not overlapping the already chosen

intervals

Underestimated solution!

optimal algorithm

20

Rule 4

Select the interval that ends first ( but still is not overlapping the already chosen intervals