CS 473: Fundamental Algorithms - courses.engr.illinois.edu

74
CS 473: Fundamental Algorithms, Spring 2011 More Dynamic Programming Lecture 9 February 17, 2011 Sariel (UIUC) CS473 1 Spring 2011 1 / 37

Transcript of CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Page 1: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

CS 473: Fundamental Algorithms, Spring 2011

More Dynamic ProgrammingLecture 9February 17, 2011

Sariel (UIUC) CS473 1 Spring 2011 1 / 37

Page 2: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Part I

Maximum Weighted Independent Set in Trees

Sariel (UIUC) CS473 2 Spring 2011 2 / 37

Page 3: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Maximum Weight Independent Set Problem

Input Graph G = (V,E) and weights w(v) ≥ 0 for eachv ∈ V

Goal Find maximum weight independent set in G

A

B

C

DE

F

20

5

2

2

10

15

Maximum weight independent set in above graph: {B,D}

Sariel (UIUC) CS473 3 Spring 2011 3 / 37

Page 4: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Maximum Weight Independent Set Problem

Input Graph G = (V,E) and weights w(v) ≥ 0 for eachv ∈ V

Goal Find maximum weight independent set in G

A

B

C

DE

F

20

5

2

2

10

15

Maximum weight independent set in above graph: {B,D}

Sariel (UIUC) CS473 3 Spring 2011 3 / 37

Page 5: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Maximum Weight Independent Set in a Tree

Input Tree T = (V,E) and weights w(v) ≥ 0 for each v ∈ V

Goal Find maximum weight independent set in T

r

a b

c d e f g

h i j

10

5 8

44

9

2 78

11

3

Maximum weight independent set in above tree: ??

Sariel (UIUC) CS473 4 Spring 2011 4 / 37

Page 6: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Towards a Recursive Solution

For an arbitrary graph G:

Number vertices as v1, v2, . . . , vn

Find recursively optimum solutions without vn (recurse onG− vn) and with vn (recurse on G− vn−N(vn) & include vn).

Saw that if graph G is arbitrary there was no good ordering thatresulted in a small number of subproblems.

What about a tree? Natural candidate for vn is root r of T?

Sariel (UIUC) CS473 5 Spring 2011 5 / 37

Page 7: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Towards a Recursive Solution

For an arbitrary graph G:

Number vertices as v1, v2, . . . , vn

Find recursively optimum solutions without vn (recurse onG− vn) and with vn (recurse on G− vn−N(vn) & include vn).

Saw that if graph G is arbitrary there was no good ordering thatresulted in a small number of subproblems.

What about a tree? Natural candidate for vn is root r of T?

Sariel (UIUC) CS473 5 Spring 2011 5 / 37

Page 8: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Towards a Recursive Solution

For an arbitrary graph G:

Number vertices as v1, v2, . . . , vn

Find recursively optimum solutions without vn (recurse onG− vn) and with vn (recurse on G− vn−N(vn) & include vn).

Saw that if graph G is arbitrary there was no good ordering thatresulted in a small number of subproblems.

What about a tree? Natural candidate for vn is root r of T?

Sariel (UIUC) CS473 5 Spring 2011 5 / 37

Page 9: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Towards a Recursive Solution

Natural candidate for vn is root r of T? Let O be an optimumsolution to the whole problem.

Case r 6∈ O : Then O contains an optimum solution for eachsubtree of T hanging at a child of r.

Case r ∈ O : None of the children of r can be in O. O − {r}contains an optimum solution for each subtree of Thanging at a grandchild of r.

Subproblems? Subtrees of T hanging at nodes in T.

Sariel (UIUC) CS473 6 Spring 2011 6 / 37

Page 10: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Towards a Recursive Solution

Natural candidate for vn is root r of T? Let O be an optimumsolution to the whole problem.

Case r 6∈ O : Then O contains an optimum solution for eachsubtree of T hanging at a child of r.

Case r ∈ O : None of the children of r can be in O. O − {r}contains an optimum solution for each subtree of Thanging at a grandchild of r.

Subproblems? Subtrees of T hanging at nodes in T.

Sariel (UIUC) CS473 6 Spring 2011 6 / 37

Page 11: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Towards a Recursive Solution

Natural candidate for vn is root r of T? Let O be an optimumsolution to the whole problem.

Case r 6∈ O : Then O contains an optimum solution for eachsubtree of T hanging at a child of r.

Case r ∈ O : None of the children of r can be in O. O − {r}contains an optimum solution for each subtree of Thanging at a grandchild of r.

Subproblems? Subtrees of T hanging at nodes in T.

Sariel (UIUC) CS473 6 Spring 2011 6 / 37

Page 12: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

A Recursive Solution

T(u): subtree of T hanging at node uOPT(u): max weighted independent set value in T(u)

OPT(u) = max

{∑v child of u OPT(v),

w(u) +∑

v grandchild of u OPT(v)

Sariel (UIUC) CS473 7 Spring 2011 7 / 37

Page 13: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

A Recursive Solution

T(u): subtree of T hanging at node uOPT(u): max weighted independent set value in T(u)

OPT(u) = max

{∑v child of u OPT(v),

w(u) +∑

v grandchild of u OPT(v)

Sariel (UIUC) CS473 7 Spring 2011 7 / 37

Page 14: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

Compute OPT(u) bottom up. To evaluate OPT(u) need tohave computed values of all children and grandchildren of u

What is an ordering of nodes of a tree T to achieve above?Post-order traversal of a tree.

Sariel (UIUC) CS473 8 Spring 2011 8 / 37

Page 15: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

Compute OPT(u) bottom up. To evaluate OPT(u) need tohave computed values of all children and grandchildren of u

What is an ordering of nodes of a tree T to achieve above?Post-order traversal of a tree.

Sariel (UIUC) CS473 8 Spring 2011 8 / 37

Page 16: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

MIS-Tree(T):

Let v1, v2, . . . , vn be a post-order traversal of nodes of T

for i = 1 to n do

M[vi] = max(∑

vj child of viM[vj], w(vi) +

∑vj grandchild of vi

M[vj])

return M[vn] (* Note: vn is the root of T *)

Space: O(n) to store the value at each node of TRunning time:

Naive bound: O(n2) since each M[vi] evaluation may takeO(n) time and there are n evaluations.

Better bound: O(n). A value M[vj] is accessed only by itsparent and grand parent.

Sariel (UIUC) CS473 9 Spring 2011 9 / 37

Page 17: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

MIS-Tree(T):

Let v1, v2, . . . , vn be a post-order traversal of nodes of T

for i = 1 to n do

M[vi] = max(∑

vj child of viM[vj], w(vi) +

∑vj grandchild of vi

M[vj])

return M[vn] (* Note: vn is the root of T *)

Space: O(n) to store the value at each node of TRunning time:

Naive bound: O(n2) since each M[vi] evaluation may takeO(n) time and there are n evaluations.

Better bound: O(n). A value M[vj] is accessed only by itsparent and grand parent.

Sariel (UIUC) CS473 9 Spring 2011 9 / 37

Page 18: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

MIS-Tree(T):

Let v1, v2, . . . , vn be a post-order traversal of nodes of T

for i = 1 to n do

M[vi] = max(∑

vj child of viM[vj], w(vi) +

∑vj grandchild of vi

M[vj])

return M[vn] (* Note: vn is the root of T *)

Space: O(n) to store the value at each node of TRunning time:

Naive bound: O(n2) since each M[vi] evaluation may takeO(n) time and there are n evaluations.

Better bound: O(n). A value M[vj] is accessed only by itsparent and grand parent.

Sariel (UIUC) CS473 9 Spring 2011 9 / 37

Page 19: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

MIS-Tree(T):

Let v1, v2, . . . , vn be a post-order traversal of nodes of T

for i = 1 to n do

M[vi] = max(∑

vj child of viM[vj], w(vi) +

∑vj grandchild of vi

M[vj])

return M[vn] (* Note: vn is the root of T *)

Space: O(n) to store the value at each node of TRunning time:

Naive bound: O(n2) since each M[vi] evaluation may takeO(n) time and there are n evaluations.

Better bound: O(n). A value M[vj] is accessed only by itsparent and grand parent.

Sariel (UIUC) CS473 9 Spring 2011 9 / 37

Page 20: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

MIS-Tree(T):

Let v1, v2, . . . , vn be a post-order traversal of nodes of T

for i = 1 to n do

M[vi] = max(∑

vj child of viM[vj], w(vi) +

∑vj grandchild of vi

M[vj])

return M[vn] (* Note: vn is the root of T *)

Space: O(n) to store the value at each node of TRunning time:

Naive bound: O(n2) since each M[vi] evaluation may takeO(n) time and there are n evaluations.

Better bound: O(n). A value M[vj] is accessed only by itsparent and grand parent.

Sariel (UIUC) CS473 9 Spring 2011 9 / 37

Page 21: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm

MIS-Tree(T):

Let v1, v2, . . . , vn be a post-order traversal of nodes of T

for i = 1 to n do

M[vi] = max(∑

vj child of viM[vj], w(vi) +

∑vj grandchild of vi

M[vj])

return M[vn] (* Note: vn is the root of T *)

Space: O(n) to store the value at each node of TRunning time:

Naive bound: O(n2) since each M[vi] evaluation may takeO(n) time and there are n evaluations.

Better bound: O(n). A value M[vj] is accessed only by itsparent and grand parent.

Sariel (UIUC) CS473 9 Spring 2011 9 / 37

Page 22: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Example

r

a b

c d e f g

h i j

10

5 8

44

9

2 78

11

3

Sariel (UIUC) CS473 10 Spring 2011 10 / 37

Page 23: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Part II

DAGs and Dynamic Programming

Sariel (UIUC) CS473 11 Spring 2011 11 / 37

Page 24: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Recursion and DAGs

ObservationLet A be a recursive algorithm for problem Π. For each instance I ofΠ there is an associated DAG G(I).

Create directed graph G(I) as follows

For each sub-problem in the execution of A on I create a node

If sub-problem v depends on or recursively calls sub-problem uadd directed edge (u, v) to graph

G(I) is a DAG. Why? If G(I) has a cycle then A will notterminate on I

Sariel (UIUC) CS473 12 Spring 2011 12 / 37

Page 25: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Recursion and DAGs

ObservationLet A be a recursive algorithm for problem Π. For each instance I ofΠ there is an associated DAG G(I).

Create directed graph G(I) as follows

For each sub-problem in the execution of A on I create a node

If sub-problem v depends on or recursively calls sub-problem uadd directed edge (u, v) to graph

G(I) is a DAG. Why? If G(I) has a cycle then A will notterminate on I

Sariel (UIUC) CS473 12 Spring 2011 12 / 37

Page 26: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Iterative Algorithm in Dynamic Programming and

DAGs

ObservationAn iterative algorithm B obtained from a recursive algorithm A for aproblem Π does the following: for each instance I of Π, it computesa topological sort of G(I) and evaluates sub-problems according tothe topological ordering.

Sometimes the DAG G(I) can be obtained directly withoutthinking about the recursive algorithm A

In some cases (not all) the computation of an optimal solutionreduces to a shortest/longest path in DAG G(I)

Topological sort based shortest/longest path computation isdynamic programming!

Sariel (UIUC) CS473 13 Spring 2011 13 / 37

Page 27: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Weighted Interval Scheduling via Longest Path in a

DAG

Given intervals, create a DAG as follows

one node for each interval plus a dummy source node for interval0 plus a dummy sink node t.

for each interval i add edge (p(i), i) of length/weight vi

for each interval i add edge (i, t) of length 0

Sariel (UIUC) CS473 14 Spring 2011 14 / 37

Page 28: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Example

30

70

80

20 10

1

2

3

4

5

p(5) = 2, p(4) = 1, p(3) = 1, p(2) = 0, p(1) = 0

0 1

2

3

4

5

t30

20

7080

10

Sariel (UIUC) CS473 15 Spring 2011 15 / 37

Page 29: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Relating Optimum Solution

Given interval problem instance I let G(I) denote the DAGconstructed as described.

Claim: Optimum solution to weighted interval scheduling instance Iis given by longest path from s to t in G(I).

Assuming claim is true,

If I has n intervals, DAG G(I) has n + 2 nodes and O(n)edges. Creating G(I) takes O(n log n) time: to find p(i) foreach i. How?

Longest path can be computed in O(n) time — recallO(m + n) algorithm for shortest/longest paths in DAGs.

Sariel (UIUC) CS473 16 Spring 2011 16 / 37

Page 30: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Relating Optimum Solution

Given interval problem instance I let G(I) denote the DAGconstructed as described.

Claim: Optimum solution to weighted interval scheduling instance Iis given by longest path from s to t in G(I).

Assuming claim is true,

If I has n intervals, DAG G(I) has n + 2 nodes and O(n)edges. Creating G(I) takes O(n log n) time: to find p(i) foreach i. How?

Longest path can be computed in O(n) time — recallO(m + n) algorithm for shortest/longest paths in DAGs.

Sariel (UIUC) CS473 16 Spring 2011 16 / 37

Page 31: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

DAG for Longest Increasing Sequence

Given sequence a1, a2, . . . , an create DAG as follows:

add sentinel a0 to sequence where a0 is less than smallestelement in sequence

for each i there is a node vi

if i < j and ai < aj add an edge (vi, vj)

find longest path from v0

6 3 5 2 7 8 10

Sariel (UIUC) CS473 17 Spring 2011 17 / 37

Page 32: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Part III

Edit Distance and Sequence Alignment

Sariel (UIUC) CS473 18 Spring 2011 18 / 37

Page 33: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Spell Checking Problem

Given a string “exponen” that is not in the dictionary, how should aspell checker suggest a nearby string?

What does nearness mean?

Question: Given two strings x1x2 . . . xn and y1y2 . . . ym what is adistance between them?

Edit Distance: minimum number of “edits” to transform x into y.

Sariel (UIUC) CS473 19 Spring 2011 19 / 37

Page 34: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Spell Checking Problem

Given a string “exponen” that is not in the dictionary, how should aspell checker suggest a nearby string?

What does nearness mean?

Question: Given two strings x1x2 . . . xn and y1y2 . . . ym what is adistance between them?

Edit Distance: minimum number of “edits” to transform x into y.

Sariel (UIUC) CS473 19 Spring 2011 19 / 37

Page 35: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Spell Checking Problem

Given a string “exponen” that is not in the dictionary, how should aspell checker suggest a nearby string?

What does nearness mean?

Question: Given two strings x1x2 . . . xn and y1y2 . . . ym what is adistance between them?

Edit Distance: minimum number of “edits” to transform x into y.

Sariel (UIUC) CS473 19 Spring 2011 19 / 37

Page 36: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Edit Distance

DefinitionEdit distance between two words X and Y is the number of letterinsertions, letter deletions and letter substitutions required to obtainY from X.

ExampleThe edit distance between FOOD and MONEY is at most 4:

FOOD→MOOD→MONOD→MONED→MONEY

Sariel (UIUC) CS473 20 Spring 2011 20 / 37

Page 37: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Edit Distance: Alternate View

AlignmentPlace words one on top of the other, with gaps in the first wordindicating insertions, and gaps in the second word indicatingdeletions.

F O O DM O N E Y

Formally, an alignment is a set M of pairs (i, j) such that each indexappears at most once, and there is no “crossing”: i < i′ and i ismatched to j implies i′ is matched to j′ > j. In the above example,this is M = {(1, 1), (2, 2), (3, 3), (4, 5)}. Cost of an alignment isthe number of mismatched columns plus number of unmatchedindices in both strings.

Sariel (UIUC) CS473 21 Spring 2011 21 / 37

Page 38: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Edit Distance: Alternate View

AlignmentPlace words one on top of the other, with gaps in the first wordindicating insertions, and gaps in the second word indicatingdeletions.

F O O DM O N E Y

Formally, an alignment is a set M of pairs (i, j) such that each indexappears at most once, and there is no “crossing”: i < i′ and i ismatched to j implies i′ is matched to j′ > j. In the above example,this is M = {(1, 1), (2, 2), (3, 3), (4, 5)}. Cost of an alignment isthe number of mismatched columns plus number of unmatchedindices in both strings.

Sariel (UIUC) CS473 21 Spring 2011 21 / 37

Page 39: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Edit Distance: Alternate View

AlignmentPlace words one on top of the other, with gaps in the first wordindicating insertions, and gaps in the second word indicatingdeletions.

F O O DM O N E Y

Formally, an alignment is a set M of pairs (i, j) such that each indexappears at most once, and there is no “crossing”: i < i′ and i ismatched to j implies i′ is matched to j′ > j. In the above example,this is M = {(1, 1), (2, 2), (3, 3), (4, 5)}. Cost of an alignment isthe number of mismatched columns plus number of unmatchedindices in both strings.

Sariel (UIUC) CS473 21 Spring 2011 21 / 37

Page 40: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Edit Distance Problem

ProblemGiven two words, find the edit distance between them, i.e., analignment of smallest cost.

Sariel (UIUC) CS473 22 Spring 2011 22 / 37

Page 41: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Applications

Spell-checkers and Dictionaries

Unix diff

DNA sequence alignment . . . but, we need a new metric

Sariel (UIUC) CS473 23 Spring 2011 23 / 37

Page 42: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Applications

Spell-checkers and Dictionaries

Unix diff

DNA sequence alignment . . . but, we need a new metric

Sariel (UIUC) CS473 23 Spring 2011 23 / 37

Page 43: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Applications

Spell-checkers and Dictionaries

Unix diff

DNA sequence alignment . . . but, we need a new metric

Sariel (UIUC) CS473 23 Spring 2011 23 / 37

Page 44: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Applications

Spell-checkers and Dictionaries

Unix diff

DNA sequence alignment . . . but, we need a new metric

Sariel (UIUC) CS473 23 Spring 2011 23 / 37

Page 45: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Similarity Metric

DefinitionFor two strings X and Y, the cost of alignment M is

[Gap penalty] For each gap in the alignment, we incur a cost δ

[Mismatch cost] For each pair p and q that have been matchedin M, we incur cost αpq; typically αpp = 0

Edit distance is special case when δ = αpq = 1

Sariel (UIUC) CS473 24 Spring 2011 24 / 37

Page 46: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Similarity Metric

DefinitionFor two strings X and Y, the cost of alignment M is

[Gap penalty] For each gap in the alignment, we incur a cost δ

[Mismatch cost] For each pair p and q that have been matchedin M, we incur cost αpq; typically αpp = 0

Edit distance is special case when δ = αpq = 1

Sariel (UIUC) CS473 24 Spring 2011 24 / 37

Page 47: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

An Example

Example

o c u r r a n c eo c c u r r e n c e Cost = δ + αae

Alternative:

o c u r r a n c eo c c u r r e n c e Cost = 3δ

Or a really stupid solution (delete string, insert other string):

o c u r r a n c eo c c u r r e n c e

Cost = 19δ.

Sariel (UIUC) CS473 25 Spring 2011 25 / 37

Page 48: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Sequence Alignment

Input Given two words X and Y, and gap penalty δ andmismatch costs αpq

Goal Find alignment of minimum cost

Sariel (UIUC) CS473 26 Spring 2011 26 / 37

Page 49: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Edit distanceBasic observation

Let X = αx and Y = βyα, β: stings.x and y single characters.Think about optimal edit distance between X and Y as alignment,and consider last column of alignment of the two strings:

α xβ y

orα xβy

orαxβ y

ObservationPrefixes must have optimal alignment!

Sariel (UIUC) CS473 27 Spring 2011 27 / 37

Page 50: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Problem Structure

ObservationLet X = x1x2 · · · xm and Y = y1y2 · · · yn. If (m, n) are notmatched then either the m’th position of X remains unmatched orthe n’th position of Y remains unmatched.

Case xm and yn are matched.

Pay mismatch cost αxmyn plus cost of aligning stringsx1 · · · xm−1 and y1 · · · yn−1

Case xm is unmatched.

Pay gap penalty plus cost of aligning x1 · · · xm−1 and y1 · · · yn

Case yn is unmatched.

Pay gap penalty plus cost of aligning x1 · · · xm and y1 · · · yn−1

Sariel (UIUC) CS473 28 Spring 2011 28 / 37

Page 51: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Subproblems and Recurrence

Optimal Costs

Let Opt(i, j) be optimal cost of aligning x1 · · · xi and y1 · · · yj. Then

Opt(i, j) = min

αxiyj

+ Opt(i− 1, j− 1),

δ + Opt(i− 1, j),

δ + Opt(i, j− 1)

Base Cases: Opt(i, 0) = δ · i and Opt(0, j) = δ · j

Sariel (UIUC) CS473 29 Spring 2011 29 / 37

Page 52: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Subproblems and Recurrence

Optimal Costs

Let Opt(i, j) be optimal cost of aligning x1 · · · xi and y1 · · · yj. Then

Opt(i, j) = min

αxiyj

+ Opt(i− 1, j− 1),

δ + Opt(i− 1, j),

δ + Opt(i, j− 1)

Base Cases: Opt(i, 0) = δ · i and Opt(0, j) = δ · j

Sariel (UIUC) CS473 29 Spring 2011 29 / 37

Page 53: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Dynamic Programming Solution

for all i do M[i, 0] = iδfor all j do M[0, j] = jδfor i = 1 to m do

for j = 1 to n do

M[i, j] = min

αxiyj + M[i− 1, j− 1],

δ + M[i− 1, j],

δ + M[i, j− 1]

Analysis

Running time is O(mn)

Space used is O(mn)

Sariel (UIUC) CS473 30 Spring 2011 30 / 37

Page 54: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Dynamic Programming Solution

for all i do M[i, 0] = iδfor all j do M[0, j] = jδfor i = 1 to m do

for j = 1 to n do

M[i, j] = min

αxiyj + M[i− 1, j− 1],

δ + M[i− 1, j],

δ + M[i, j− 1]

Analysis

Running time is O(mn)

Space used is O(mn)

Sariel (UIUC) CS473 30 Spring 2011 30 / 37

Page 55: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Dynamic Programming Solution

for all i do M[i, 0] = iδfor all j do M[0, j] = jδfor i = 1 to m do

for j = 1 to n do

M[i, j] = min

αxiyj + M[i− 1, j− 1],

δ + M[i− 1, j],

δ + M[i, j− 1]

Analysis

Running time is O(mn)

Space used is O(mn)

Sariel (UIUC) CS473 30 Spring 2011 30 / 37

Page 56: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Matrix and DAG of Computation

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

· · · · · ·

· · · · · ·

· · · · · ·

· · · · · ·

· · · · · ·

. . .. . .

i, j

δ

δ

αxiyj

m, n

0, 0

Figure: Iterative algorithm in previous slide computes values in row order.Optimal value is a shortest path from (0, 0) to (m, n) in DAG.

Sariel (UIUC) CS473 31 Spring 2011 31 / 37

Page 57: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Sequence Alignment in Practice

Typically the DNA sequences that are aligned are about 105

letters long!

So about 1010 operations and 1010 bytes needed

The killer is the 10GB storage

Can we reduce space requirements?

Sariel (UIUC) CS473 32 Spring 2011 32 / 37

Page 58: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Sequence Alignment in Practice

Typically the DNA sequences that are aligned are about 105

letters long!

So about 1010 operations and 1010 bytes needed

The killer is the 10GB storage

Can we reduce space requirements?

Sariel (UIUC) CS473 32 Spring 2011 32 / 37

Page 59: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Sequence Alignment in Practice

Typically the DNA sequences that are aligned are about 105

letters long!

So about 1010 operations and 1010 bytes needed

The killer is the 10GB storage

Can we reduce space requirements?

Sariel (UIUC) CS473 32 Spring 2011 32 / 37

Page 60: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Sequence Alignment in Practice

Typically the DNA sequences that are aligned are about 105

letters long!

So about 1010 operations and 1010 bytes needed

The killer is the 10GB storage

Can we reduce space requirements?

Sariel (UIUC) CS473 32 Spring 2011 32 / 37

Page 61: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Optimizing Space

Recall

M(i, j) = min

αxiyj

+ M(i− 1, j− 1),

δ + M(i− 1, j),

δ + M(i, j− 1)

Entries in jth column only depend on (j− 1)’st column andearlier entries in jth column

Only store the current column and the previous column reusingspace; N(i, 0) stores M(i, j− 1) and N(i, 1) stores M(i, j)

Sariel (UIUC) CS473 33 Spring 2011 33 / 37

Page 62: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Optimizing Space

Recall

M(i, j) = min

αxiyj

+ M(i− 1, j− 1),

δ + M(i− 1, j),

δ + M(i, j− 1)

Entries in jth column only depend on (j− 1)’st column andearlier entries in jth column

Only store the current column and the previous column reusingspace; N(i, 0) stores M(i, j− 1) and N(i, 1) stores M(i, j)

Sariel (UIUC) CS473 33 Spring 2011 33 / 37

Page 63: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Optimizing Space

Recall

M(i, j) = min

αxiyj

+ M(i− 1, j− 1),

δ + M(i− 1, j),

δ + M(i, j− 1)

Entries in jth column only depend on (j− 1)’st column andearlier entries in jth column

Only store the current column and the previous column reusingspace; N(i, 0) stores M(i, j− 1) and N(i, 1) stores M(i, j)

Sariel (UIUC) CS473 33 Spring 2011 33 / 37

Page 64: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Computing in column order to save space

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

· · · · · ·

· · · · · ·

· · · · · ·

· · · · · ·

· · · · · ·

. . .. . .

i, j

δ

δ

αxiyj

m, n

0, 0

Figure: M(i, j) only depends on previous column values. Keep only twocolumns and compute in column order.

Sariel (UIUC) CS473 34 Spring 2011 34 / 37

Page 65: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Space Efficient Algorithm

for all i do N[i, 0] = iδfor j = 1 to n do

N[0, 1] = jδ (* corresponds to M(0, j) *)

for i = 1 to m do

N[i, 1] = min

αxiyj + N[i− 1, 0]

δ + N[i− 1, 1]

δ + N[i, 0]for i = 1 to m doCopy N[i, 0] = N[i, 1]

Analysis

Running time is O(mn) and space used is O(2m) = O(m)

Sariel (UIUC) CS473 35 Spring 2011 35 / 37

Page 66: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Analyzing Space Efficiency

From the m× n matrix M we can construct the actualalignment (exercise)

Matrix N computes cost of optimal alignment but no way toconstruct the actual alignment

Space efficient computation of alignment? More complicatedalgorithm — see text book.

Sariel (UIUC) CS473 36 Spring 2011 36 / 37

Page 67: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Analyzing Space Efficiency

From the m× n matrix M we can construct the actualalignment (exercise)

Matrix N computes cost of optimal alignment but no way toconstruct the actual alignment

Space efficient computation of alignment? More complicatedalgorithm — see text book.

Sariel (UIUC) CS473 36 Spring 2011 36 / 37

Page 68: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Analyzing Space Efficiency

From the m× n matrix M we can construct the actualalignment (exercise)

Matrix N computes cost of optimal alignment but no way toconstruct the actual alignment

Space efficient computation of alignment? More complicatedalgorithm — see text book.

Sariel (UIUC) CS473 36 Spring 2011 36 / 37

Page 69: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Analyzing Space Efficiency

From the m× n matrix M we can construct the actualalignment (exercise)

Matrix N computes cost of optimal alignment but no way toconstruct the actual alignment

Space efficient computation of alignment? More complicatedalgorithm — see text book.

Sariel (UIUC) CS473 36 Spring 2011 36 / 37

Page 70: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Takeaway Points

Dynamic programming is based on finding a recursive way tosolve the problem. Need a recursion that generates a smallnumber of subproblems.

Given a recursive algorithm there is a natural DAG associatedwith the subproblems that are generated for given instance; thisis the dependency graph. An iterative algorithm simply evaluatesthe subproblems in some topological sort of this DAG.

The space required to evaluate the answer can be reduced insome cases by a careful examination of that dependency DAGof the subproblems and keeping only a subset of the DAG atany time.

Sariel (UIUC) CS473 37 Spring 2011 37 / 37

Page 71: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Notes

Sariel (UIUC) CS473 38 Spring 2011 38 / 37

Page 72: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Notes

Sariel (UIUC) CS473 39 Spring 2011 39 / 37

Page 73: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Notes

Sariel (UIUC) CS473 40 Spring 2011 40 / 37

Page 74: CS 473: Fundamental Algorithms - courses.engr.illinois.edu

Notes

Sariel (UIUC) CS473 41 Spring 2011 41 / 37