





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






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
3
Recall greedy algorithm works if all weights are 1.
Sort
jobs
by increasing
finish
times so that
f
1
f
2
f
φ
FOR j
to N
IF (job
j compatible
with A)
{j}
Greedy Activity Selection Algorithm
S = jobs selected.
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
5
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^
j^
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 }
j
j
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
Compute q
q
2
, ... , q
N
r-compute(j) {
(j
return max(w
j^
r-compute(q
j ), r-compute(j-1))
Recursive Activity Selection
7
Spectacularly redundant subproblems
exponential algorithms.
φ
φ
φ
φ
φ
Independent subproblems
efficient algorithms.
Docsity.com
13
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. 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
i^
i^
i
i
i^
i^
i
i
i^
i^
i
i^
i
i i^
2
2
15
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
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
c
j i e
j
j i
Docsity.com
17
Running time: n^
Bottleneck = computing e(i, n) for O(N
2 ) pairs, O(N) per pair using
previous formula.
n^
3 ) overall. INPUT: N,
p
1 ,…,p
N^
,^
c
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])
Bottom-Up Segmented Least Squares
Bottleneck = computing e(i, j). 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
−
=
1
2
i
j
j i k^
k
k
∑
∑
=
=
i j
n
n
x
a
y
b
ij
j i k
k
j i k
k
ij
Preprocessing
19
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
Greedy = 35: { 5, 2, 1 }OPT value = 40: { 3, 4 }
v
i^
/ w
i
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.
{
}
n
n
n
Docsity.com
26
δ
α
CA
α
TC
α
GT
α
AG
α
CA
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.
n^
Goal: find alignment of minimum cost.
gap
) , (:
) , (:
mismatch ) , (
∉
∉
∈
M j i j M j i i M j
i^
y x^
j i
28
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 yi x
O(MN) time and space.
INPUT: M, N, x
1 x
...x 2
M , y
1 y
...y 2
N
δ
α
i = 0 to M OPT[0, i] =
i
δ
j = 0 to N OPT[j, 0] =
j
δ
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])
Bottom-Up Sequence Alignment N]
Docsity.com
30
Straightforward dynamic programming takes
(MN) time and space.
n^
English words or sentences
may not be a problem.
n^
Computational biology
huge problem.
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).
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).
min
min
j i
j i
j
i
j
i
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).
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