Docsity

Algorithm Analysis: Activity Selection and Sequence Alignment, Slides of Computer Science

Toggle sidebar
bg1
Dynamic Programming
2
Weighted Activity Selection
Weighted activity selection problem (generalization of CLR 17.1).
■Job requests 1, 2, … , N.
■Job j starts at sj, finishes at fj, and has weight wj.
■Two jobs compatible if they don't overlap.
■Goal: find maximum weight subset of mutually compatible jobs.
Time
0
A
C
F
B
D
G
E
1234567891011
H
3
Activity Selection: Greedy Algorithm
Recall greedy algorithm works if all weights are 1.
Sort jobs by increasing finish times so that
f1≤f2≤... ≤fN.
S = φ
FOR j = 1 to N
IF (job j compatible with A)
S ←S∪{j}
RETURN S
Greedy Activity Selection Algorithm
S = jobs selected.
4
Weighted Activity Selection
Notation.
■Label jobs by finishing time: f1 ≤f2 ≤. . . ≤fN.
■Define qj= largest index i < j such that job i is compatible with j.
–q7= 3, q2= 0
Time
0
3
2
6
1
5
7
4
1234567891011
8
Docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Algorithm Analysis: Activity Selection and Sequence Alignment and more Slides Computer Science in PDF only on Docsity!

Dynamic Programming

Weighted Activity Selection

Weighted activity selection problem (generalization of CLR 17.1). n^

Job requests 1, 2, … , N. n^

Job j starts at s

, finishes at f j

, and has weight w j

j^

n^

Two jobs compatible if they don't overlap. n^

Goal: find maximum weight subset of mutually compatible jobs.

Time

A

C

F

B

D

G

E

H

3

Activity Selection: Greedy Algorithm

Recall greedy algorithm works if all weights are 1.

Sort

jobs

by increasing

finish

times so that

f

1

f

2

f

.N

S

φ

FOR j

to N

IF (job

j compatible

with A)

S

S

{j}

RETURN S

Greedy Activity Selection Algorithm

S = jobs selected.

Weighted Activity Selection

Notation. n^

Label jobs by finishing time: f

1

f^2

f

N

n^

Define q

j^ = largest index i < j such that job i is compatible with j.

q

7

= 3, q

2

Time

Docsity.com

5

Weighted Activity Selection: Structure

Let OPT(j) = value of optimal solution to the problem consisting of

job requests {1, 2,... , j }.

n^

Case 1: OPT selects job j.

can’t use incompatible jobs { q

j^

  • 1, q

j^

  • 2,... , j-1 }

must include optimal solution to problem consisting ofremaining compatible jobs { 1, 2,... , q

j^

n^

Case 2: OPT does not select job j.

must include optimal solution to problem consisting ofremaining compatible jobs { 1, 2,... , j - 1 }

otherwise

max

j

if

(^

j

OPT

q

OPT

w

j

OPT

j

j

Weighted Activity Selection: Brute Force INPUT: N,

s

1 ,…,s

N ,

f

,…,f 1

N ,

w

1 ,…,w

N

Sort jobs

by increasing

finish

times

so

that

f

1

f

2

f

.N

Compute q

q

2

, ... , q

N

r-compute(j) {

IF

(j

RETURN 0

ELSE

return max(w

j^

r-compute(q

j ), r-compute(j-1))

Recursive Activity Selection

7

Dynamic Programming Subproblems

Spectacularly redundant subproblems

exponential algorithms.

φ

φ

φ

φ

φ

Divide-and-Conquer Subproblems

Independent subproblems

efficient algorithms.

Docsity.com

13

Dynamic Programming Overview

Dynamic programming. n^

Similar to divide-and-conquer.

solves problem by combining solution to sub-problems

n^

Different from divide-and-conquer.

sub-problems are not independent - save solutions to repeated sub-problems in table

Recipe. n^

Characterize structure of problem.

optimal substructure property

n^

Recursively define value of optimal solution. n^

Compute value of optimal solution. n^

Construct optimal solution from computed information.

Top-down vs. bottom-up. n^

Different people have different intuitions.

Least Squares

Least squares. n^

Foundational problem in statistic and numerical analysis. n^

Given N points in the plane { (x

1 , y

1 ), (x

2 , y

2 ) ,... , (x

N

, y

N

find a line y = ax + b that minimizes the sum of the squared error: n^

Calculus

∑= min error is achieved when:

N i

i

i

b

ax

y

SS

1

N

x a y b x x N

y x y x N a

i^

i^

i

i

i^

i^

i

i

i^

i^

i

i^

i

i i^

2

2

15

Segmented Least Squares

Segmented least squares. n^

Points lie roughly on a sequence of 3 lines. n^

Given N points in the plane p

1 , p

2

,... , p

N

, find a sequence of

lines that minimize:

the sum of the sum of the squared errors E in each segment - the number of lines L

n^

Tradeoff function: e + c L, for some constant c > 0.

3 lines better than one

Segmented Least Squares: Structure

Notation. n^

OPT(j) = minimum cost for points p

1 , p

i+

,... , p

j^

n^

e(i, j)

= minimum sum of squares for points p

, pi

i+

,... , p

j

Optimal solution: n^

Last segment uses points p

, pi

i+

,... , p

j^ for some i.

n^

Cost = e(i, j) + c + OPT(i-1).

New dynamic programming technique. n^

Weighted activity selection: binary choice. n^

Segmented least squares: multi-way choice.

{

}

≤ ≤

otherwise

) 1

(

min

j if

1

i

OPT

c

j i e

j

OPT

j i

Docsity.com

17

Segmented Least Squares: Algorithm

Running time: n^

Bottleneck = computing e(i, n) for O(N

2 ) pairs, O(N) per pair using

previous formula.

n^

O(N

3 ) overall. INPUT: N,

p

1 ,…,p

N^

,^

c

ARRAY: OPT[0..N]OPT[0] =

FOR j

to N

FOR i

1 to j

compute

the

least

square

error e[i,j] for

the

segment

p

,...,i

p

j

OPT[j]

= min

1 ≤

i ≤

j^

(e[i,j] +

c + OPT[i-1])

RETURN OPT[N]

Bottom-Up Segmented Least Squares

Segmented Least Squares: Improved AlgorithmA quadratic algorithm.^ n

Bottleneck = computing e(i, j). n^

O(N

2 ) preprocessing + O(1) per computation.

2

2

2

2

∑

∑

∑

∑

∑

=

=

=

=

=

j i k

k

j i k

k

j i k

k

j i k

k

j i k

k k

ij

x

x

n

y x y x n a

∑

∑= ∑= =

i k

k

k

k

i k

k

k

i k

k

k

y

x

xy

x

xxs

x

xs

1

1

2 1

∑=^ ∑=

= =

i k

k

k

i k

k

k

y

yys

y

ys

1

2 1

1 −

=

∑

i

j

j i k

k

xs

xs

x

v

−

=

1

2

i

j

j i k^

k

k

yys

yys

b

ax

y

j

i

e

∑

∑

=

=

i j

n

n

x

a

y

b

ij

j i k

k

j i k

k

ij

Preprocessing

19

Knapsack Problem

Knapsack problem. n^

Given N objects and a "knapsack." n^

Item i weighs w

i^

0 Newtons and has value v

i^

n^

Knapsack can carry weight up to W Newtons. n^

Goal: fill knapsack so as to maximize total value.

Item

Value

Weight

W = 11

Greedy = 35: { 5, 2, 1 }OPT value = 40: { 3, 4 }

v

i^

/ w

i

Knapsack Problem: Structure

OPT(n, w) = max profit subset of items {1,... , n} with weight limit w. n^

Case 1: OPT selects item n.

new weight limit = w - w

n

OPT selects best of {1, 2,... , n - 1} using this new weight limit

n^

Case 2: OPT does not select item n.

OPT selects best of {1, 2,... , n - 1} using weight limit w

New dynamic programming technique. n^

Weighted activity selection: binary choice. n^

Segmented least squares: multi-way choice. n^

Knapsack: adding a new variable.

{

}

otherwise

max

w

w

if

n

if

(^

n

n

n

w

w

n

OPT

v

w

n

OPT

w

n

OPT

w

n

OPT

Docsity.com

26

δ

α

CA

C

G

A

C

C

T

A

C

C

T

C

T

G

A

C

T

A

C

A

T

T

G

A

C

C

T

A

C

C

T

C

T

G

A

C

T

A

C

A

T

T C

C C

α

TC

α

GT

α

AG

α

CA

Sequence Alignment: Applications

Applications. n^

Spell checkers / web dictionaries.

ocurrance - occurrence

n^

Computational biology.

ctgacctacct - cctgactacat

Edit distance. n^

Needleman-Wunsch, 1970. n^

Gap penalty

δ

n^

Mismatch penalty

α

pq

n^

Cost = sum of gap andmismatch penalties.

Problem. n^

Input: two strings X = x

1

x

2

... x

M

and Y = y

1

y

2

... y

N

n^

Notation: {1, 2,... , M} and {1, 2,... , N} denote positions in X, Y. n^

Matching: set of ordered pairs (i, j) such that each item occurs inat most one pair. n^

Alignment: matching with no crossing pairs.

if (i, j)

M and (i’, j’)

M and i < i’, then j < j’

n^

Example: CTACCG vs. TACATG.

M = { (2,1) (3,2) (4,3), (5,4), (6,6) }

n^

Goal: find alignment of minimum cost.

Sequence Alignment

R

R

R

R

Q

R

R

R

R

P

I

R

R Q

R

R P

I

gap

) , (:

) , (:

mismatch ) , (

cost

∉

∉

∈

M j i j M j i i M j

i^

y x^

j i

M

C

T

A

C

C

T

A

C

A

T

G G

28

Sequence Alignment: Problem Structure

OPT(i, j) = min cost of aligning strings x

1

x

2

... x

i^ and y

1

y 2

... y

j^

n^

Case 1: OPT matches (i, j).

pay mismatch for (i, j) + min cost of aligning two stringsx

1

x

2

... x

i-

and y

1

y

2

... y

j-

n^

Case 2a: OPT leaves m unmatched.

pay gap for i and min cost of aligning x

1

x

2

... x

i-

and y

1

y 2

... y

j

n^

Case 2b: OPT leaves n unmatched.

pay gap for j and min cost of aligning x

1

x

2

... x

i^ and y

1

y

2

... y

j-

j

otherwiseif

min

i

if

i

j

i

OPT

j

i

OPT

j

i

OPT

j

j

i

OPT

j yi x

Sequence Alignment: Algorithm

O(MN) time and space.

INPUT: M, N, x

1 x

...x 2

M , y

1 y

...y 2

N

δ

α

ARRAY: OPT[0..M, 0..N]FOR

i = 0 to M OPT[0, i] =

i

δ

FOR

j = 0 to N OPT[j, 0] =

j

δ

FOR

i = 1 to M FOR j = 1 to N

OPT[i, j] =

min(

α

[x

i,

y

]j

OPT[i-1,

j-1],

δ

OPT[i-1, j],

δ

OPT[i,

j-1])

RETURN OPT[M,

Bottom-Up Sequence Alignment N]

Docsity.com

30

Sequence Alignment: Linear Space

Straightforward dynamic programming takes

(MN) time and space.

n^

English words or sentences

may not be a problem.

n^

Computational biology

huge problem.

M = N = 100,

10 billion ops OK, but 10 gigabyte array?

Optimal value in O(M + N) space and O(MN) time. n^

Only need to remember OPT( i -

1, •) to compute OPT( i, •).

n^

Not clear how to recover optimal alignment itself.

Optimal alignment in O(M + N) space and O(MN) time. n^

Clever combination of divide-and-conquer and dynamicprogramming.

Consider following directed graph (conceptually). n^

Note: takes

(MN) space to write down graph.

Let f(i, j) be shortest path from (0,0) to (i, j). Then, f(i, j) = OPT(i, j).

Sequence Alignment: Linear Space

M-N

x

1 x

2

y

1

x

3

y 2

y 3

y

4

y

5

y 6

ε

ε

δ

δ

0-

j yxi α

i-j

32

Let f(i, j) be shortest path from (0,0) to (i, j). Then, f(i, j) = OPT(i, j). n^

Base case: f(0, 0) = OPT(0, 0) = 0. n^

Inductive step: assume f(i’, j’) = OPT(i’, j’) for all i’ + j’ < i + j. n^

Last edge on path to (i, j) is either from (i-1, j-1), (i-1, j), or (i, j-1).

Sequence Alignment: Linear Space

min

min

(^

j i

OPT

j i

OPT

j

i

OPT

j

i

OPT

j i f j i f j i f j i

f^

j i

j i y x

y x

δ

δ

α

δ

δ

α

M-N

x 1 x 2

y 1

x 3

y 2

y 3

y^4

y^5

y^6

ε

ε

δ

δ

0-

j yi x α

i-j

Let g(i, j) be shortest path from (i, j) to (M, N). n^

Can compute in O(MN) time for all (i, j) by reversing arcorientations and flipping roles of (0, 0) and (M, N).

Sequence Alignment: Linear Space

M-N

x

1 x

2

y

1

x

3

y 2

y 3

y

4

y

5

y 6

ε

ε

δ

δ

0-

j yxi α

i-j

Docsity.com