Dynamic Programming Lecture 6 September 12, 2013 Sariel (UIUC) - - PowerPoint PPT Presentation

dynamic programming
SMART_READER_LITE
LIVE PREVIEW

Dynamic Programming Lecture 6 September 12, 2013 Sariel (UIUC) - - PowerPoint PPT Presentation

CS 573: Algorithms, Fall 2013 Dynamic Programming Lecture 6 September 12, 2013 Sariel (UIUC) CS573 1 Fall 2013 1 / 72 Part I . Maximum Weighted Independent Set in Trees . Sariel (UIUC) CS573 2 Fall 2013 2 / 72 Maximum Weight


slide-1
SLIDE 1

CS 573: Algorithms, Fall 2013

Dynamic Programming

Lecture 6

September 12, 2013

Sariel (UIUC) CS573 1 Fall 2013 1 / 72

slide-2
SLIDE 2

Part I

. .

Maximum Weighted Independent Set in Trees

Sariel (UIUC) CS573 2 Fall 2013 2 / 72

slide-3
SLIDE 3

Maximum Weight Independent Set Problem

Input Graph G = (V, E) and weights w(v) ≥ 0 for each v ∈ V Goal Find maximum weight independent set in G

A B C D E F

20 5 2 2 10 15

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

Sariel (UIUC) CS573 3 Fall 2013 3 / 72

slide-4
SLIDE 4

Maximum Weight Independent Set Problem

Input Graph G = (V, E) and weights w(v) ≥ 0 for each v ∈ V Goal Find maximum weight independent set in G

A B C D E F

20 5 2 2 10 15

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

Sariel (UIUC) CS573 3 Fall 2013 3 / 72

slide-5
SLIDE 5

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 4 4 9 2 7 8 11 3

Maximum weight independent set in above tree: ??

Sariel (UIUC) CS573 4 Fall 2013 4 / 72

slide-6
SLIDE 6

Towards a Recursive Solution

For an arbitrary graph G: . .

1

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

2

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

3

Saw that if graph G is arbitrary there was no good ordering that resulted in a small number of subproblems. What about a tree? Natural candidate for vn is root r of T?

Sariel (UIUC) CS573 5 Fall 2013 5 / 72

slide-7
SLIDE 7

Towards a Recursive Solution

For an arbitrary graph G: . .

1

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

2

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

3

Saw that if graph G is arbitrary there was no good ordering that resulted in a small number of subproblems. What about a tree? Natural candidate for vn is root r of T?

Sariel (UIUC) CS573 5 Fall 2013 5 / 72

slide-8
SLIDE 8

Towards a Recursive Solution

For an arbitrary graph G: . .

1

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

2

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

3

Saw that if graph G is arbitrary there was no good ordering that resulted in a small number of subproblems. What about a tree? Natural candidate for vn is root r of T?

Sariel (UIUC) CS573 5 Fall 2013 5 / 72

slide-9
SLIDE 9

Towards a Recursive Solution

Natural candidate for vn is root r of T? Let O be an optimum solution to the whole problem. Case r ̸∈ O : Then O contains an optimum solution for each subtree 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 T hanging at a grandchild of r. Subproblems? Subtrees of T hanging at nodes in T.

Sariel (UIUC) CS573 6 Fall 2013 6 / 72

slide-10
SLIDE 10

Towards a Recursive Solution

Natural candidate for vn is root r of T? Let O be an optimum solution to the whole problem. Case r ̸∈ O : Then O contains an optimum solution for each subtree 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 T hanging at a grandchild of r. Subproblems? Subtrees of T hanging at nodes in T.

Sariel (UIUC) CS573 6 Fall 2013 6 / 72

slide-11
SLIDE 11

Towards a Recursive Solution

Natural candidate for vn is root r of T? Let O be an optimum solution to the whole problem. Case r ̸∈ O : Then O contains an optimum solution for each subtree 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 T hanging at a grandchild of r. Subproblems? Subtrees of T hanging at nodes in T.

Sariel (UIUC) CS573 6 Fall 2013 6 / 72

slide-12
SLIDE 12

A Recursive Solution

T(u): subtree of T hanging at node u OPT(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) CS573 7 Fall 2013 7 / 72

slide-13
SLIDE 13

A Recursive Solution

T(u): subtree of T hanging at node u OPT(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) CS573 7 Fall 2013 7 / 72

slide-14
SLIDE 14

Iterative Algorithm

. .

1

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

2

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

Sariel (UIUC) CS573 8 Fall 2013 8 / 72

slide-15
SLIDE 15

Iterative Algorithm

. .

1

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

2

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

Sariel (UIUC) CS573 8 Fall 2013 8 / 72

slide-16
SLIDE 16

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 vi M[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 T Running time: .

1

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

2

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

Sariel (UIUC) CS573 9 Fall 2013 9 / 72

slide-17
SLIDE 17

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 vi M[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 T Running time: .

1

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

2

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

Sariel (UIUC) CS573 9 Fall 2013 9 / 72

slide-18
SLIDE 18

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 vi M[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 T Running time: .

1

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

2

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

Sariel (UIUC) CS573 9 Fall 2013 9 / 72

slide-19
SLIDE 19

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 vi M[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 T Running time: .

1

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

2

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

Sariel (UIUC) CS573 9 Fall 2013 9 / 72

slide-20
SLIDE 20

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 vi M[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 T Running time: .

1

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

2

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

Sariel (UIUC) CS573 9 Fall 2013 9 / 72

slide-21
SLIDE 21

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 vi M[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 T Running time: .

1

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

2

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

Sariel (UIUC) CS573 9 Fall 2013 9 / 72

slide-22
SLIDE 22

Example

r a b c d e f g h i j 10 5 8 4 4 9 2 7 8 11 3

Sariel (UIUC) CS573 10 Fall 2013 10 / 72

slide-23
SLIDE 23

Dominating set

.

Definition

. . G = (V, E). The set X ⊆ V is a dominating set, if any vertex v ∈ V is either in X or is adjacent to a vertex in X.

r a b c d e f g h i j 10 5 8 4 4 9 2 7 8 11 3

Sariel (UIUC) CS573 11 Fall 2013 11 / 72

slide-24
SLIDE 24

Dominating set

.

Definition

. . G = (V, E). The set X ⊆ V is a dominating set, if any vertex v ∈ V is either in X or is adjacent to a vertex in X.

r a b c d e f g h i j 10 5 8 4 4 9 2 7 8 11 3

.

Problem

. . Given weights on vertices, compute the minimum weight dominating set in G.

Sariel (UIUC) CS573 11 Fall 2013 11 / 72

slide-25
SLIDE 25

Dominating set

.

Definition

. . G = (V, E). The set X ⊆ V is a dominating set, if any vertex v ∈ V is either in X or is adjacent to a vertex in X.

r a b c d e f g h i j 10 5 8 4 4 9 2 7 8 11 3

.

Problem

. . Given weights on vertices, compute the minimum weight dominating set in G. Dominating Set is

NP-Hard!

Sariel (UIUC) CS573 11 Fall 2013 11 / 72

slide-26
SLIDE 26

Part II

. .

DAGs and Dynamic Programming

Sariel (UIUC) CS573 12 Fall 2013 12 / 72

slide-27
SLIDE 27

Recursion and DAGs

.

Observation

. . Let A be a recursive algorithm for problem Π. For each instance I

  • f Π there is an associated DAG G(I).

. .

1

Create directed graph G(I) as follows... . .

2

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

3

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

4

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

Sariel (UIUC) CS573 13 Fall 2013 13 / 72

slide-28
SLIDE 28

Recursion and DAGs

.

Observation

. . Let A be a recursive algorithm for problem Π. For each instance I

  • f Π there is an associated DAG G(I).

. .

1

Create directed graph G(I) as follows... . .

2

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

3

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

4

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

Sariel (UIUC) CS573 13 Fall 2013 13 / 72

slide-29
SLIDE 29

Iterative Algorithm for...

Dynamic Programming and DAGs

.

Observation

. . An iterative algorithm B obtained from a recursive algorithm A for a problem Π does the following: For each instance I of Π, it computes a topological sort

  • f G(I) and evaluates sub-problems according to the

topological ordering. . .

1

Sometimes the DAG G(I) can be obtained directly without thinking about the recursive algorithm A . .

2

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

3

Topological sort based shortest/longest path computation is dynamic programming!

Sariel (UIUC) CS573 14 Fall 2013 14 / 72

slide-30
SLIDE 30

A quick reminder...

A Recursive Algorithm for weighted interval scheduling

Let Oi be value of an optimal schedule for the first i jobs.

Schedule(n):

if n = 0 then return 0 if n = 1 then return w(v1)

Op(n) ←Schedule(p(n)) On−1 ←Schedule(n − 1)

if (Op(n) + w(vn) < On−1) then

On = On−1

else

On = Op(n) + w(vn)

return On

Sariel (UIUC) CS573 15 Fall 2013 15 / 72

slide-31
SLIDE 31

Weighted Interval Scheduling via...

Longest Path in a DAG

Given intervals, create a DAG as follows: . .

1

Create one node for each interval, plus a dummy sink node 0 for interval 0, plus a dummy source node s. . .

2

For each interval i add edge (i, p(i)) of the length/weight of vi. . .

3

Add an edge from s to n of length 0. . .

4

For each interval i add edge (i, i − 1) of length 0.

Sariel (UIUC) CS573 16 Fall 2013 16 / 72

slide-32
SLIDE 32

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

1 2 3 4 5 s

30 20 70 80 10

Sariel (UIUC) CS573 17 Fall 2013 17 / 72

slide-33
SLIDE 33

Relating Optimum Solution

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

Claim

. . Optimum solution to weighted interval scheduling instance I is given by longest path from s to 0 in G(I). Assuming claim is true, .

1

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) for

each i. How? . .

2

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

Sariel (UIUC) CS573 18 Fall 2013 18 / 72

slide-34
SLIDE 34

Relating Optimum Solution

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

Claim

. . Optimum solution to weighted interval scheduling instance I is given by longest path from s to 0 in G(I). Assuming claim is true, . .

1

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) for

each i. How? . .

2

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

Sariel (UIUC) CS573 18 Fall 2013 18 / 72

slide-35
SLIDE 35

DAG for Longest Increasing Sequence

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

1

add sentinel a0 to sequence where a0 is less than smallest element in sequence . .

2

for each i there is a node vi . .

3

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

4

find longest path from v0

6 3 5 2 7 8 1 a0

Sariel (UIUC) CS573 19 Fall 2013 19 / 72

slide-36
SLIDE 36

DAG for Longest Increasing Sequence

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

1

add sentinel a0 to sequence where a0 is less than smallest element in sequence . .

2

for each i there is a node vi . .

3

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

4

find longest path from v0

6 3 5 2 7 8 1 a0

Sariel (UIUC) CS573 19 Fall 2013 19 / 72

slide-37
SLIDE 37

Part III

. . Edit Distance and Sequence Alignment

Sariel (UIUC) CS573 20 Fall 2013 20 / 72

slide-38
SLIDE 38

Spell Checking Problem

Given a string “exponen” that is not in the dictionary, how should a spell checker suggest a nearby string? What does nearness mean? Question: Given two strings x1x2 . . . xn and y1y2 . . . ym what is a distance between them? Edit Distance: minimum number of “edits” to transform x into y.

Sariel (UIUC) CS573 21 Fall 2013 21 / 72

slide-39
SLIDE 39

Spell Checking Problem

Given a string “exponen” that is not in the dictionary, how should a spell checker suggest a nearby string? What does nearness mean? Question: Given two strings x1x2 . . . xn and y1y2 . . . ym what is a distance between them? Edit Distance: minimum number of “edits” to transform x into y.

Sariel (UIUC) CS573 21 Fall 2013 21 / 72

slide-40
SLIDE 40

Spell Checking Problem

Given a string “exponen” that is not in the dictionary, how should a spell checker suggest a nearby string? What does nearness mean? Question: Given two strings x1x2 . . . xn and y1y2 . . . ym what is a distance between them? Edit Distance: minimum number of “edits” to transform x into y.

Sariel (UIUC) CS573 21 Fall 2013 21 / 72

slide-41
SLIDE 41

Edit Distance

.

Definition

. . Edit distance between two words X and Y is the number of letter insertions, letter deletions and letter substitutions required to obtain Y from X. .

Example

. . The edit distance between FOOD and MONEY is at most 4: FOOD → MOOD → MONOD → MONED → MONEY

Sariel (UIUC) CS573 22 Fall 2013 22 / 72

slide-42
SLIDE 42

Edit Distance: Alternate View

.

Alignment

. . Place words one on top of the other, with gaps in the first word indicating insertions, and gaps in the second word indicating deletions. F O O D M O N E Y Formally, an alignment is a set M of pairs (i, j) such that each index appears at most once, and there is no “crossing”: i < i′ and i is matched 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 is the number of mismatched columns plus number of unmatched indices in both strings.

Sariel (UIUC) CS573 23 Fall 2013 23 / 72

slide-43
SLIDE 43

Edit Distance: Alternate View

.

Alignment

. . Place words one on top of the other, with gaps in the first word indicating insertions, and gaps in the second word indicating deletions. F O O D M O N E Y Formally, an alignment is a set M of pairs (i, j) such that each index appears at most once, and there is no “crossing”: i < i′ and i is matched 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 is the number of mismatched columns plus number of unmatched indices in both strings.

Sariel (UIUC) CS573 23 Fall 2013 23 / 72

slide-44
SLIDE 44

Edit Distance: Alternate View

.

Alignment

. . Place words one on top of the other, with gaps in the first word indicating insertions, and gaps in the second word indicating deletions. F O O D M O N E Y Formally, an alignment is a set M of pairs (i, j) such that each index appears at most once, and there is no “crossing”: i < i′ and i is matched 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 is the number of mismatched columns plus number of unmatched indices in both strings.

Sariel (UIUC) CS573 23 Fall 2013 23 / 72

slide-45
SLIDE 45

Edit Distance Problem

.

Problem

. . Given two words, find the edit distance between them, i.e., an alignment of smallest cost.

Sariel (UIUC) CS573 24 Fall 2013 24 / 72

slide-46
SLIDE 46

Applications

. .

1

Spell-checkers and Dictionaries . .

2

Unix diff . .

3

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

Sariel (UIUC) CS573 25 Fall 2013 25 / 72

slide-47
SLIDE 47

Similarity Metric

.

Definition

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

1

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

2

[Mismatch cost] For each pair p and q that have been matched in M, we incur cost αpq; typically αpp = 0. Edit distance is special case when δ = αpq = 1.

Sariel (UIUC) CS573 26 Fall 2013 26 / 72

slide-48
SLIDE 48

Similarity Metric

.

Definition

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

1

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

2

[Mismatch cost] For each pair p and q that have been matched in M, we incur cost αpq; typically αpp = 0. Edit distance is special case when δ = αpq = 1.

Sariel (UIUC) CS573 26 Fall 2013 26 / 72

slide-49
SLIDE 49

An Example

.

Example

. .

  • c

u r r a n c e

  • c

c u r r e n c e Cost = δ + αae Alternative:

  • c

u r r a n c e

  • c

c u r r e n c e Cost = 3δ Or a really stupid solution (delete string, insert other string):

  • c

u r r a n c e

  • c

c u r r e n c e Cost = 19δ.

Sariel (UIUC) CS573 27 Fall 2013 27 / 72

slide-50
SLIDE 50

Sequence Alignment

Input Given two words X and Y, and gap penalty δ and mismatch costs αpq Goal Find alignment of minimum cost

Sariel (UIUC) CS573 28 Fall 2013 28 / 72

slide-51
SLIDE 51

Edit distance

Basic observation

Let X = αx and Y = βy α, β: strings. 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

  • r

α x βy

  • r

αx β y .

Observation

. . Prefixes must have optimal alignment!

Sariel (UIUC) CS573 29 Fall 2013 29 / 72

slide-52
SLIDE 52

Problem Structure

.

Observation

. . Let X = x1x2 · · · xm and Y = y1y2 · · · yn. If (m, n) are not matched then either the mth position of X remains unmatched or the nth position of Y remains unmatched. . .

1

Case xm and yn are matched.

. . .

1

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

. .

2

Case xm is unmatched.

. . .

1

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

. .

3

Case yn is unmatched.

. . .

1

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

Sariel (UIUC) CS573 30 Fall 2013 30 / 72

slide-53
SLIDE 53

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) CS573 31 Fall 2013 31 / 72

slide-54
SLIDE 54

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) CS573 31 Fall 2013 31 / 72

slide-55
SLIDE 55

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

. . .

1

Running time is O(mn).

Sariel (UIUC) CS573 32 Fall 2013 32 / 72

slide-56
SLIDE 56

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

. . .

1

Running time is O(mn).

Sariel (UIUC) CS573 32 Fall 2013 32 / 72

slide-57
SLIDE 57

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

. . .

1

Running time is O(mn). . .

2

Space used is O(mn).

Sariel (UIUC) CS573 32 Fall 2013 32 / 72

slide-58
SLIDE 58

Matrix and DAG of Computation

. . . . . . . . . . . . . . . . . . ... ... i, j

m, n

αxixj δ δ

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) CS573 33 Fall 2013 33 / 72

slide-59
SLIDE 59

Sequence Alignment in Practice

. .

1

Typically the DNA sequences that are aligned are about 105 letters long! . .

2

So about 1010 operations and 1010 bytes needed . .

3

The killer is the 10GB storage . .

4

Can we reduce space requirements?

Sariel (UIUC) CS573 34 Fall 2013 34 / 72

slide-60
SLIDE 60

Optimizing Space

. .

1

Recall M(i, j) = min

      

αxiyj + M(i − 1, j − 1), δ + M(i − 1, j), δ + M(i, j − 1) . .

2

Entries in jth column only depend on (j − 1)st column and earlier entries in jth column . .

3

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

Sariel (UIUC) CS573 35 Fall 2013 35 / 72

slide-61
SLIDE 61

Computing in column order to save space

. . . . . . . . . . . . . . . . . . ... ... i, j

m, n

αxixj δ δ

0, 0

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

Sariel (UIUC) CS573 36 Fall 2013 36 / 72

slide-62
SLIDE 62

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 do

Copy N[i, 0] = N[i, 1]

.

Analysis

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

Sariel (UIUC) CS573 37 Fall 2013 37 / 72

slide-63
SLIDE 63

Analyzing Space Efficiency

. .

1

From the m × n matrix M we can construct the actual alignment (exercise) . .

2

Matrix N computes cost of optimal alignment but no way to construct the actual alignment . .

3

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

Sariel (UIUC) CS573 38 Fall 2013 38 / 72

slide-64
SLIDE 64

Takeaway Points

. .

1

Dynamic programming is based on finding a recursive way to solve the problem. Need a recursion that generates a small number of subproblems. . .

2

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

3

The space required to evaluate the answer can be reduced in some cases by a careful examination of that dependency DAG

  • f the subproblems and keeping only a subset of the DAG at

any time.

Sariel (UIUC) CS573 39 Fall 2013 39 / 72

slide-65
SLIDE 65

Part IV

. .

All Pairs Shortest Paths

Sariel (UIUC) CS573 40 Fall 2013 40 / 72

slide-66
SLIDE 66

Shortest Path Problems

.

Shortest Path Problems

. . Input A (undirected or directed) graph G = (V, E) with edge lengths (or costs). For edge e = (u, v), ℓ(e) = ℓ(u, v) is its length. . .

1

Given nodes s, t find shortest path from s to t. . .

2

Given node s find shortest path from s to all other nodes. . .

3

Find shortest paths for all pairs of nodes.

Sariel (UIUC) CS573 41 Fall 2013 41 / 72

slide-67
SLIDE 67

Single-Source Shortest Paths

.

Single-Source Shortest Path Problems

. . Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), ℓ(e) = ℓ(u, v) is its length. . .

1

Given nodes s, t find shortest path from s to t. . .

2

Given node s find shortest path from s to all other nodes. Dijkstra’s algorithm for non-negative edge lengths. Running time: O((m + n) log n) with heaps and O(m + n log n) with advanced priority queues. Bellman-Ford algorithm for arbitrary edge lengths. Running time: O(nm).

Sariel (UIUC) CS573 42 Fall 2013 42 / 72

slide-68
SLIDE 68

Single-Source Shortest Paths

.

Single-Source Shortest Path Problems

. . Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), ℓ(e) = ℓ(u, v) is its length. . .

1

Given nodes s, t find shortest path from s to t. . .

2

Given node s find shortest path from s to all other nodes. Dijkstra’s algorithm for non-negative edge lengths. Running time: O((m + n) log n) with heaps and O(m + n log n) with advanced priority queues. Bellman-Ford algorithm for arbitrary edge lengths. Running time: O(nm).

Sariel (UIUC) CS573 42 Fall 2013 42 / 72

slide-69
SLIDE 69

All-Pairs Shortest Paths

.

All-Pairs Shortest Path Problem

. . Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), ℓ(e) = ℓ(u, v) is its length. .

1

Find shortest paths for all pairs of nodes. Apply single-source algorithms n times, once for each vertex. . .

1

Non-negative lengths. O(nm log n) with heaps and O(nm + n2 log n) using advanced priority queues. . .

2

Arbitrary edge lengths: O(n2m). Θ(n4) if m = Ω(n2). Can we do better?

Sariel (UIUC) CS573 43 Fall 2013 43 / 72

slide-70
SLIDE 70

All-Pairs Shortest Paths

.

All-Pairs Shortest Path Problem

. . Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), ℓ(e) = ℓ(u, v) is its length. .

1

Find shortest paths for all pairs of nodes. Apply single-source algorithms n times, once for each vertex. . .

1

Non-negative lengths. O(nm log n) with heaps and O(nm + n2 log n) using advanced priority queues. . .

2

Arbitrary edge lengths: O(n2m). Θ(n4) if m = Ω(n2). Can we do better?

Sariel (UIUC) CS573 43 Fall 2013 43 / 72

slide-71
SLIDE 71

All-Pairs Shortest Paths

.

All-Pairs Shortest Path Problem

. . Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), ℓ(e) = ℓ(u, v) is its length. .

1

Find shortest paths for all pairs of nodes. Apply single-source algorithms n times, once for each vertex. . .

1

Non-negative lengths. O(nm log n) with heaps and O(nm + n2 log n) using advanced priority queues. . .

2

Arbitrary edge lengths: O(n2m). Θ(n4) if m = Ω(n2). Can we do better?

Sariel (UIUC) CS573 43 Fall 2013 43 / 72

slide-72
SLIDE 72

Shortest Paths and Recursion

. .

1

Compute the shortest path distance from s to t recursively? . .

2

What are the smaller sub-problems? .

Lemma

. . Let G be a directed graph with arbitrary edge lengths. If s = v0 → v1 → v2 → . . . → vk is a shortest path from s to vk then for 1 ≤ i < k: . .

1

s = v0 → v1 → v2 → . . . → vi is a shortest path from s to vi Sub-problem idea: paths of fewer hops/edges

Sariel (UIUC) CS573 44 Fall 2013 44 / 72

slide-73
SLIDE 73

Shortest Paths and Recursion

. .

1

Compute the shortest path distance from s to t recursively? . .

2

What are the smaller sub-problems? .

Lemma

. . Let G be a directed graph with arbitrary edge lengths. If s = v0 → v1 → v2 → . . . → vk is a shortest path from s to vk then for 1 ≤ i < k: . .

1

s = v0 → v1 → v2 → . . . → vi is a shortest path from s to vi Sub-problem idea: paths of fewer hops/edges

Sariel (UIUC) CS573 44 Fall 2013 44 / 72

slide-74
SLIDE 74

Shortest Paths and Recursion

. .

1

Compute the shortest path distance from s to t recursively? . .

2

What are the smaller sub-problems? .

Lemma

. . Let G be a directed graph with arbitrary edge lengths. If s = v0 → v1 → v2 → . . . → vk is a shortest path from s to vk then for 1 ≤ i < k: . .

1

s = v0 → v1 → v2 → . . . → vi is a shortest path from s to vi Sub-problem idea: paths of fewer hops/edges

Sariel (UIUC) CS573 44 Fall 2013 44 / 72

slide-75
SLIDE 75

Hop-based Recur’: Single-Source Shortest Paths

Single-source problem: fix source s. OPT(v, k): shortest path dist. from s to v using at most k edges. Note: dist(s, v) = OPT(v, n − 1). Recursion for OPT(v, k): OPT(v, k) = min

  

minu∈V(OPT(u, k − 1) + c(u, v)). OPT(v, k − 1) Base case: OPT(v, 1) = c(s, v) if (s, v) ∈ E otherwise ∞ Leads to Bellman-Ford algorithm — see text book. OPT(v, k) values are also of independent interest: shortest paths with at most k hops

Sariel (UIUC) CS573 45 Fall 2013 45 / 72

slide-76
SLIDE 76

Hop-based Recur’: Single-Source Shortest Paths

Single-source problem: fix source s. OPT(v, k): shortest path dist. from s to v using at most k edges. Note: dist(s, v) = OPT(v, n − 1). Recursion for OPT(v, k): OPT(v, k) = min

  

minu∈V(OPT(u, k − 1) + c(u, v)). OPT(v, k − 1) Base case: OPT(v, 1) = c(s, v) if (s, v) ∈ E otherwise ∞ Leads to Bellman-Ford algorithm — see text book. OPT(v, k) values are also of independent interest: shortest paths with at most k hops

Sariel (UIUC) CS573 45 Fall 2013 45 / 72

slide-77
SLIDE 77

Hop-based Recur’: Single-Source Shortest Paths

Single-source problem: fix source s. OPT(v, k): shortest path dist. from s to v using at most k edges. Note: dist(s, v) = OPT(v, n − 1). Recursion for OPT(v, k): OPT(v, k) = min

  

minu∈V(OPT(u, k − 1) + c(u, v)). OPT(v, k − 1) Base case: OPT(v, 1) = c(s, v) if (s, v) ∈ E otherwise ∞ Leads to Bellman-Ford algorithm — see text book. OPT(v, k) values are also of independent interest: shortest paths with at most k hops

Sariel (UIUC) CS573 45 Fall 2013 45 / 72

slide-78
SLIDE 78

Hop-based Recur’: Single-Source Shortest Paths

Single-source problem: fix source s. OPT(v, k): shortest path dist. from s to v using at most k edges. Note: dist(s, v) = OPT(v, n − 1). Recursion for OPT(v, k): OPT(v, k) = min

  

minu∈V(OPT(u, k − 1) + c(u, v)). OPT(v, k − 1) Base case: OPT(v, 1) = c(s, v) if (s, v) ∈ E otherwise ∞ Leads to Bellman-Ford algorithm — see text book. OPT(v, k) values are also of independent interest: shortest paths with at most k hops

Sariel (UIUC) CS573 45 Fall 2013 45 / 72

slide-79
SLIDE 79

Hop-based Recur’: Single-Source Shortest Paths

Single-source problem: fix source s. OPT(v, k): shortest path dist. from s to v using at most k edges. Note: dist(s, v) = OPT(v, n − 1). Recursion for OPT(v, k): OPT(v, k) = min

  

minu∈V(OPT(u, k − 1) + c(u, v)). OPT(v, k − 1) Base case: OPT(v, 1) = c(s, v) if (s, v) ∈ E otherwise ∞ Leads to Bellman-Ford algorithm — see text book. OPT(v, k) values are also of independent interest: shortest paths with at most k hops

Sariel (UIUC) CS573 45 Fall 2013 45 / 72

slide-80
SLIDE 80

Hop-based Recur’: Single-Source Shortest Paths

Single-source problem: fix source s. OPT(v, k): shortest path dist. from s to v using at most k edges. Note: dist(s, v) = OPT(v, n − 1). Recursion for OPT(v, k): OPT(v, k) = min

  

minu∈V(OPT(u, k − 1) + c(u, v)). OPT(v, k − 1) Base case: OPT(v, 1) = c(s, v) if (s, v) ∈ E otherwise ∞ Leads to Bellman-Ford algorithm — see text book. OPT(v, k) values are also of independent interest: shortest paths with at most k hops

Sariel (UIUC) CS573 45 Fall 2013 45 / 72

slide-81
SLIDE 81

All-Pairs: Recursion on index of intermediate nodes

. .

1

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

2

dist(i, j, k): shortest path distance between vi and vj among all paths in which the largest index of an intermediate node is at most k i 4 1 100 1 10 2 j 3 5 1 1 2 dist(i, j, 0) = 100 dist(i, j, 1) = 9 dist(i, j, 2) = 8 dist(i, j, 3) = 5

Sariel (UIUC) CS573 46 Fall 2013 46 / 72

slide-82
SLIDE 82

All-Pairs: Recursion on index of intermediate nodes

. .

1

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

2

dist(i, j, k): shortest path distance between vi and vj among all paths in which the largest index of an intermediate node is at most k i 4 1 100 1 10 2 j 3 5 1 1 2 dist(i, j, 0) = 100 dist(i, j, 1) = 9 dist(i, j, 2) = 8 dist(i, j, 3) = 5

Sariel (UIUC) CS573 46 Fall 2013 46 / 72

slide-83
SLIDE 83

All-Pairs: Recursion on index of intermediate nodes

. .

1

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

2

dist(i, j, k): shortest path distance between vi and vj among all paths in which the largest index of an intermediate node is at most k i 4 1 100 1 10 2 j 3 5 1 1 2 dist(i, j, 0) = 100 dist(i, j, 1) = 9 dist(i, j, 2) = 8 dist(i, j, 3) = 5

Sariel (UIUC) CS573 46 Fall 2013 46 / 72

slide-84
SLIDE 84

All-Pairs: Recursion on index of intermediate nodes

. .

1

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

2

dist(i, j, k): shortest path distance between vi and vj among all paths in which the largest index of an intermediate node is at most k i 4 1 100 1 10 2 j 3 5 1 1 2 dist(i, j, 0) = 100 dist(i, j, 1) = 9 dist(i, j, 2) = 8 dist(i, j, 3) = 5

Sariel (UIUC) CS573 46 Fall 2013 46 / 72

slide-85
SLIDE 85

All-Pairs: Recursion on index of intermediate nodes

. .

1

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

2

dist(i, j, k): shortest path distance between vi and vj among all paths in which the largest index of an intermediate node is at most k i 4 1 100 1 10 2 j 3 5 1 1 2 dist(i, j, 0) = 100 dist(i, j, 1) = 9 dist(i, j, 2) = 8 dist(i, j, 3) = 5

Sariel (UIUC) CS573 46 Fall 2013 46 / 72

slide-86
SLIDE 86

All-Pairs: Recursion on index of intermediate nodes

i j k dist(i, k, k − 1) dist(k, j, k − 1) dist(i, j, k − 1)

dist(i, j, k) = min

  

dist(i, j, k − 1) dist(i, k, k − 1) + dist(k, j, k − 1) Base case: dist(i, j, 0) = c(i, j) if (i, j) ∈ E, otherwise ∞ Correctness: If i → j shortest path goes through k then k occurs

  • nly once on the path — otherwise there is a negative length cycle.

Sariel (UIUC) CS573 47 Fall 2013 47 / 72

slide-87
SLIDE 87

Floyd-Warshall Algorithm

for All-Pairs Shortest Paths Check if G has a negative cycle // Bellman-Ford: O(mn) time

if there is a negative cycle then return ‘‘Negative cycle’’ for i = 1 to n do for j = 1 to n do

dist(i, j, 0) = c(i, j) (* c(i, j) = ∞ if (i, j) / ∈ E, 0 if i = j *)

for k = 1 to n do for i = 1 to n do for j = 1 to n do

dist(i, j, k) = min { dist(i, j, k − 1), dist(i, k, k − 1) + dist(k, j, k − 1)

Correctness: Recursion works under the assumption that all shortest paths are defined (no negative length cycle). Running Time: Θ(n3), Space: Θ(n3).

Sariel (UIUC) CS573 48 Fall 2013 48 / 72

slide-88
SLIDE 88

Floyd-Warshall Algorithm

for All-Pairs Shortest Paths Check if G has a negative cycle // Bellman-Ford: O(mn) time

if there is a negative cycle then return ‘‘Negative cycle’’ for i = 1 to n do for j = 1 to n do

dist(i, j, 0) = c(i, j) (* c(i, j) = ∞ if (i, j) / ∈ E, 0 if i = j *)

for k = 1 to n do for i = 1 to n do for j = 1 to n do

dist(i, j, k) = min { dist(i, j, k − 1), dist(i, k, k − 1) + dist(k, j, k − 1)

Correctness: Recursion works under the assumption that all shortest paths are defined (no negative length cycle). Running Time: Θ(n3), Space: Θ(n3).

Sariel (UIUC) CS573 48 Fall 2013 48 / 72

slide-89
SLIDE 89

Floyd-Warshall Algorithm

for All-Pairs Shortest Paths Check if G has a negative cycle // Bellman-Ford: O(mn) time

if there is a negative cycle then return ‘‘Negative cycle’’ for i = 1 to n do for j = 1 to n do

dist(i, j, 0) = c(i, j) (* c(i, j) = ∞ if (i, j) / ∈ E, 0 if i = j *)

for k = 1 to n do for i = 1 to n do for j = 1 to n do

dist(i, j, k) = min { dist(i, j, k − 1), dist(i, k, k − 1) + dist(k, j, k − 1)

Correctness: Recursion works under the assumption that all shortest paths are defined (no negative length cycle). Running Time: Θ(n3), Space: Θ(n3).

Sariel (UIUC) CS573 48 Fall 2013 48 / 72

slide-90
SLIDE 90

Floyd-Warshall Algorithm

for All-Pairs Shortest Paths

Do we need a separate algorithm to check if there is negative cycle?

for i = 1 to n do for j = 1 to n do

dist(i, j, 0) = c(i, j) (* c(i, j) = ∞ if (i, j) / ∈ E, 0 if i = j *) not edge, 0 if i = j *)

for k = 1 to n do for i = 1 to n do for j = 1 to n do

dist(i, j, k) = min(dist(i, j, k − 1), dist(i, k, k − 1) + dist(k, j

for i = 1 to n do if (dist(i, i, n) < 0) then

Output that there is a negative length cycle in G

Correctness: exercise

Sariel (UIUC) CS573 49 Fall 2013 49 / 72

slide-91
SLIDE 91

Floyd-Warshall Algorithm

for All-Pairs Shortest Paths

Do we need a separate algorithm to check if there is negative cycle?

for i = 1 to n do for j = 1 to n do

dist(i, j, 0) = c(i, j) (* c(i, j) = ∞ if (i, j) / ∈ E, 0 if i = j *) not edge, 0 if i = j *)

for k = 1 to n do for i = 1 to n do for j = 1 to n do

dist(i, j, k) = min(dist(i, j, k − 1), dist(i, k, k − 1) + dist(k, j

for i = 1 to n do if (dist(i, i, n) < 0) then

Output that there is a negative length cycle in G

Correctness: exercise

Sariel (UIUC) CS573 49 Fall 2013 49 / 72

slide-92
SLIDE 92

Floyd-Warshall Algorithm: Finding the Paths

Question: Can we find the paths in addition to the distances? . .

1

Create a n × n array Next that stores the next vertex on shortest path for each pair of vertices . .

2

With array Next, for any pair of given vertices i, j can compute a shortest path in O(n) time.

Sariel (UIUC) CS573 50 Fall 2013 50 / 72

slide-93
SLIDE 93

Floyd-Warshall Algorithm: Finding the Paths

Question: Can we find the paths in addition to the distances? . .

1

Create a n × n array Next that stores the next vertex on shortest path for each pair of vertices . .

2

With array Next, for any pair of given vertices i, j can compute a shortest path in O(n) time.

Sariel (UIUC) CS573 50 Fall 2013 50 / 72

slide-94
SLIDE 94

Floyd-Warshall Algorithm

Finding the Paths

for i = 1 to n do for j = 1 to n do

dist(i, j, 0) = c(i, j) (* c(i, j) = ∞ if (i, j) not edge, 0 if i = Next(i, j) = −1

for k = 1 to n do for i = 1 to n do for j = 1 to n do if (dist(i, j, k − 1) > dist(i, k, k − 1) + dist(k, j, k − 1)) then

dist(i, j, k) = dist(i, k, k − 1) + dist(k, j, k − 1) Next(i, j) = k

for i = 1 to n do if (dist(i, i, n) < 0) then

Output that there is a negative length cycle in G

Exercise: Given Next array and any two vertices i, j describe an O(n) algorithm to find a i-j shortest path.

Sariel (UIUC) CS573 51 Fall 2013 51 / 72

slide-95
SLIDE 95

Summary of results on shortest paths

Single vertex No negative edges Dijkstra O(n log n + m) Edges cost might be negative But no negative cycles Bellman Ford O(nm) .

All Pairs Shortest Paths

. . No negative edges n * Dijkstra O(n2 log n + nm) No negative cycles n * Bellman Ford O(n2m) = O(n4) No negative cycles Floyd-Warshall O(n3)

Sariel (UIUC) CS573 52 Fall 2013 52 / 72

slide-96
SLIDE 96

Part V

. .

Knapsack

Sariel (UIUC) CS573 53 Fall 2013 53 / 72

slide-97
SLIDE 97

Knapsack Problem

Input Given a Knapsack of capacity W lbs. and n objects with ith object having weight wi and value vi; assume W, wi, vi are all positive integers Goal Fill the Knapsack without exceeding weight limit while maximizing value. Basic problem that arises in many applications as a sub-problem.

Sariel (UIUC) CS573 54 Fall 2013 54 / 72

slide-98
SLIDE 98

Knapsack Problem

Input Given a Knapsack of capacity W lbs. and n objects with ith object having weight wi and value vi; assume W, wi, vi are all positive integers Goal Fill the Knapsack without exceeding weight limit while maximizing value. Basic problem that arises in many applications as a sub-problem.

Sariel (UIUC) CS573 54 Fall 2013 54 / 72

slide-99
SLIDE 99

Knapsack Example

.

Example

. . Item I1 I2 I3 I4 I5 Value 1 6 18 22 28 Weight 1 2 5 6 7 If W = 11, the best is {I3, I4} giving value 40. .

Special Case

. . When vi = wi, the Knapsack problem is called the Subset Sum Problem.

Sariel (UIUC) CS573 55 Fall 2013 55 / 72

slide-100
SLIDE 100

Greedy Approach

. .

1

Pick objects with greatest value

. . .

1

Let W = 2, w1 = w2 = 1, w3 = 2, v1 = v2 = 2 and v3 = 3; greedy strategy will pick {3}, but the optimal is {1, 2}

. .

2

Pick objects with smallest weight

. . .

1

Let W = 2, w1 = 1, w2 = 2, v1 = 1 and v2 = 3; greedy strategy will pick {1}, but the optimal is {2}

. .

3

Pick objects with largest vi/wi ratio

. . .

1

Let W = 4, w1 = w2 = 2, w3 = 3, v1 = v2 = 3 and v3 = 5; greedy strategy will pick {3}, but the optimal is {1, 2} . . .

2

Can show that a slight modification always gives half the

  • ptimum profit: pick the better of the output of this algorithm

and the largest value item. Also, the algorithms gives better approximations when all item weights are small when compared to W.

Sariel (UIUC) CS573 56 Fall 2013 56 / 72

slide-101
SLIDE 101

Towards a Recursive Solution

First guess: Opt(i) is the optimum solution value for items 1, . . . , i. .

Observation

. . Consider an optimal solution O for 1, . . . , i Case item i ̸∈ O O is an optimal solution to items 1 to i − 1 Case item i ∈ O Then O − {i} is an optimum solution for items 1 to n − 1 in knapsack of capacity W − wi. Subproblems depend also on remaining capacity. Cannot write subproblem only in terms of Opt(1), . . . , Opt(i − 1). Opt(i, w): optimum profit for items 1 to i in knapsack of size w Goal: compute Opt(n, W)

Sariel (UIUC) CS573 57 Fall 2013 57 / 72

slide-102
SLIDE 102

Towards a Recursive Solution

First guess: Opt(i) is the optimum solution value for items 1, . . . , i. .

Observation

. . Consider an optimal solution O for 1, . . . , i Case item i ̸∈ O O is an optimal solution to items 1 to i − 1 Case item i ∈ O Then O − {i} is an optimum solution for items 1 to n − 1 in knapsack of capacity W − wi. Subproblems depend also on remaining capacity. Cannot write subproblem only in terms of Opt(1), . . . , Opt(i − 1). Opt(i, w): optimum profit for items 1 to i in knapsack of size w Goal: compute Opt(n, W)

Sariel (UIUC) CS573 57 Fall 2013 57 / 72

slide-103
SLIDE 103

Towards a Recursive Solution

First guess: Opt(i) is the optimum solution value for items 1, . . . , i. .

Observation

. . Consider an optimal solution O for 1, . . . , i Case item i ̸∈ O O is an optimal solution to items 1 to i − 1 Case item i ∈ O Then O − {i} is an optimum solution for items 1 to n − 1 in knapsack of capacity W − wi. Subproblems depend also on remaining capacity. Cannot write subproblem only in terms of Opt(1), . . . , Opt(i − 1). Opt(i, w): optimum profit for items 1 to i in knapsack of size w Goal: compute Opt(n, W)

Sariel (UIUC) CS573 57 Fall 2013 57 / 72

slide-104
SLIDE 104

Dynamic Programming Solution

.

Definition

. . Let Opt(i, w) be the optimal way of picking items from 1 to i, with total weight not exceeding w. Opt(i, w) =

          

if i = 0 Opt(i − 1, w) if wi > w max

  

Opt(i − 1, w) Opt(i − 1, w − wi) + vi

  • therwise

Sariel (UIUC) CS573 58 Fall 2013 58 / 72

slide-105
SLIDE 105

An Iterative Algorithm

for w = 0 to W do

M[0, w] = 0

for i = 1 to n do for w = 1 to W do if (wi > w) then

M[i, w] = M[i − 1, w]

else

M[i, w] = max(M[i − 1, w], M[i − 1, w − wi] + vi)

.

Running Time

. . .

1

Time taken is O(nW) . .

2

Input has size O(n + log W + ∑n

i=1(log vi + log wi)); so

running time not polynomial but “pseudo-polynomial”!

Sariel (UIUC) CS573 59 Fall 2013 59 / 72

slide-106
SLIDE 106

An Iterative Algorithm

for w = 0 to W do

M[0, w] = 0

for i = 1 to n do for w = 1 to W do if (wi > w) then

M[i, w] = M[i − 1, w]

else

M[i, w] = max(M[i − 1, w], M[i − 1, w − wi] + vi)

.

Running Time

. . .

1

Time taken is O(nW) . .

2

Input has size O(n + log W + ∑n

i=1(log vi + log wi)); so

running time not polynomial but “pseudo-polynomial”!

Sariel (UIUC) CS573 59 Fall 2013 59 / 72

slide-107
SLIDE 107

An Iterative Algorithm

for w = 0 to W do

M[0, w] = 0

for i = 1 to n do for w = 1 to W do if (wi > w) then

M[i, w] = M[i − 1, w]

else

M[i, w] = max(M[i − 1, w], M[i − 1, w − wi] + vi)

.

Running Time

. . .

1

Time taken is O(nW) . .

2

Input has size O(n + log W + ∑n

i=1(log vi + log wi)); so

running time not polynomial but “pseudo-polynomial”!

Sariel (UIUC) CS573 59 Fall 2013 59 / 72

slide-108
SLIDE 108

Knapsack Algorithm and Polynomial time

. .

1

Input size for Knapsack: O(n) + log W + ∑n

i=1(log wi + log vi).

. .

2

Running time of dynamic programming algorithm: O(nW). . .

3

Not a polynomial time algorithm. . .

4

Example: W = 2n and wi, vi ∈ [1..2n]. Input size is O(n2), running time is O(n2n) arithmetic/comparisons. . .

5

Algorithm is called a pseudo-polynomial time algorithm because running time is polynomial if numbers in input are of size polynomial in the combinatorial size of problem. . .

6

Knapsack is NP-Hard if numbers are not polynomial in n.

Sariel (UIUC) CS573 60 Fall 2013 60 / 72

slide-109
SLIDE 109

Knapsack Algorithm and Polynomial time

. .

1

Input size for Knapsack: O(n) + log W + ∑n

i=1(log wi + log vi).

. .

2

Running time of dynamic programming algorithm: O(nW). . .

3

Not a polynomial time algorithm. . .

4

Example: W = 2n and wi, vi ∈ [1..2n]. Input size is O(n2), running time is O(n2n) arithmetic/comparisons. . .

5

Algorithm is called a pseudo-polynomial time algorithm because running time is polynomial if numbers in input are of size polynomial in the combinatorial size of problem. . .

6

Knapsack is NP-Hard if numbers are not polynomial in n.

Sariel (UIUC) CS573 60 Fall 2013 60 / 72

slide-110
SLIDE 110

Knapsack Algorithm and Polynomial time

. .

1

Input size for Knapsack: O(n) + log W + ∑n

i=1(log wi + log vi).

. .

2

Running time of dynamic programming algorithm: O(nW). . .

3

Not a polynomial time algorithm. . .

4

Example: W = 2n and wi, vi ∈ [1..2n]. Input size is O(n2), running time is O(n2n) arithmetic/comparisons. . .

5

Algorithm is called a pseudo-polynomial time algorithm because running time is polynomial if numbers in input are of size polynomial in the combinatorial size of problem. . .

6

Knapsack is NP-Hard if numbers are not polynomial in n.

Sariel (UIUC) CS573 60 Fall 2013 60 / 72

slide-111
SLIDE 111

Knapsack Algorithm and Polynomial time

. .

1

Input size for Knapsack: O(n) + log W + ∑n

i=1(log wi + log vi).

. .

2

Running time of dynamic programming algorithm: O(nW). . .

3

Not a polynomial time algorithm. . .

4

Example: W = 2n and wi, vi ∈ [1..2n]. Input size is O(n2), running time is O(n2n) arithmetic/comparisons. . .

5

Algorithm is called a pseudo-polynomial time algorithm because running time is polynomial if numbers in input are of size polynomial in the combinatorial size of problem. . .

6

Knapsack is NP-Hard if numbers are not polynomial in n.

Sariel (UIUC) CS573 60 Fall 2013 60 / 72

slide-112
SLIDE 112

Knapsack Algorithm and Polynomial time

. .

1

Input size for Knapsack: O(n) + log W + ∑n

i=1(log wi + log vi).

. .

2

Running time of dynamic programming algorithm: O(nW). . .

3

Not a polynomial time algorithm. . .

4

Example: W = 2n and wi, vi ∈ [1..2n]. Input size is O(n2), running time is O(n2n) arithmetic/comparisons. . .

5

Algorithm is called a pseudo-polynomial time algorithm because running time is polynomial if numbers in input are of size polynomial in the combinatorial size of problem. . .

6

Knapsack is NP-Hard if numbers are not polynomial in n.

Sariel (UIUC) CS573 60 Fall 2013 60 / 72

slide-113
SLIDE 113

Knapsack Algorithm and Polynomial time

. .

1

Input size for Knapsack: O(n) + log W + ∑n

i=1(log wi + log vi).

. .

2

Running time of dynamic programming algorithm: O(nW). . .

3

Not a polynomial time algorithm. . .

4

Example: W = 2n and wi, vi ∈ [1..2n]. Input size is O(n2), running time is O(n2n) arithmetic/comparisons. . .

5

Algorithm is called a pseudo-polynomial time algorithm because running time is polynomial if numbers in input are of size polynomial in the combinatorial size of problem. . .

6

Knapsack is NP-Hard if numbers are not polynomial in n.

Sariel (UIUC) CS573 60 Fall 2013 60 / 72

slide-114
SLIDE 114

Knapsack Algorithm and Polynomial time

. .

1

Input size for Knapsack: O(n) + log W + ∑n

i=1(log wi + log vi).

. .

2

Running time of dynamic programming algorithm: O(nW). . .

3

Not a polynomial time algorithm. . .

4

Example: W = 2n and wi, vi ∈ [1..2n]. Input size is O(n2), running time is O(n2n) arithmetic/comparisons. . .

5

Algorithm is called a pseudo-polynomial time algorithm because running time is polynomial if numbers in input are of size polynomial in the combinatorial size of problem. . .

6

Knapsack is NP-Hard if numbers are not polynomial in n.

Sariel (UIUC) CS573 60 Fall 2013 60 / 72

slide-115
SLIDE 115

Part VI

. .

Traveling Salesman Problem

Sariel (UIUC) CS573 61 Fall 2013 61 / 72

slide-116
SLIDE 116

Traveling Salesman Problem

Input A graph G = (V, E) with non-negative edge costs/lengths. c(e) for edge e Goal Find a tour of minimum cost that visits each node. No polynomial time algorithm known. Problem is NP-Hard.

Sariel (UIUC) CS573 62 Fall 2013 62 / 72

slide-117
SLIDE 117

Traveling Salesman Problem

Input A graph G = (V, E) with non-negative edge costs/lengths. c(e) for edge e Goal Find a tour of minimum cost that visits each node. No polynomial time algorithm known. Problem is NP-Hard.

Sariel (UIUC) CS573 62 Fall 2013 62 / 72

slide-118
SLIDE 118

Drawings using TSP

Sariel (UIUC) CS573 63 Fall 2013 63 / 72

slide-119
SLIDE 119

Drawings using TSP

Sariel (UIUC) CS573 63 Fall 2013 63 / 72

slide-120
SLIDE 120

Example: optimal tour for cities of a country (which one?)

Sariel (UIUC) CS573 64 Fall 2013 64 / 72

slide-121
SLIDE 121

An Exponential Time Algorithm

How many different tours are there? n! Stirling’s formula: n! ≃ √n(n/e)n which is Θ(2cn log n) for some constant c > 1 Can we do better? Can we get a 2O(n) time algorithm?

Sariel (UIUC) CS573 65 Fall 2013 65 / 72

slide-122
SLIDE 122

An Exponential Time Algorithm

How many different tours are there? n! Stirling’s formula: n! ≃ √n(n/e)n which is Θ(2cn log n) for some constant c > 1 Can we do better? Can we get a 2O(n) time algorithm?

Sariel (UIUC) CS573 65 Fall 2013 65 / 72

slide-123
SLIDE 123

An Exponential Time Algorithm

How many different tours are there? n! Stirling’s formula: n! ≃ √n(n/e)n which is Θ(2cn log n) for some constant c > 1 Can we do better? Can we get a 2O(n) time algorithm?

Sariel (UIUC) CS573 65 Fall 2013 65 / 72

slide-124
SLIDE 124

An Exponential Time Algorithm

How many different tours are there? n! Stirling’s formula: n! ≃ √n(n/e)n which is Θ(2cn log n) for some constant c > 1 Can we do better? Can we get a 2O(n) time algorithm?

Sariel (UIUC) CS573 65 Fall 2013 65 / 72

slide-125
SLIDE 125

An Exponential Time Algorithm

How many different tours are there? n! Stirling’s formula: n! ≃ √n(n/e)n which is Θ(2cn log n) for some constant c > 1 Can we do better? Can we get a 2O(n) time algorithm?

Sariel (UIUC) CS573 65 Fall 2013 65 / 72

slide-126
SLIDE 126

Towards a Recursive Solution

. .

1

Order vertices as v1, v2, . . . , vn . .

2

OPT(S): optimum TSP tour for the vertices S ⊆ V in the graph restricted to S. Want OPT(V). Can we compute OPT(S) recursively? . .

1

Say v ∈ S. What are the two neighbors of v in optimum tour in S? . .

2

If u, w are neighbors of v in an optimum tour of S then removing v gives an optimum path from u to w visiting all nodes in S − {v}. Path from u to w is not a recursive subproblem! Need to find a more general problem to allow recursion.

Sariel (UIUC) CS573 66 Fall 2013 66 / 72

slide-127
SLIDE 127

Towards a Recursive Solution

. .

1

Order vertices as v1, v2, . . . , vn . .

2

OPT(S): optimum TSP tour for the vertices S ⊆ V in the graph restricted to S. Want OPT(V). Can we compute OPT(S) recursively? . .

1

Say v ∈ S. What are the two neighbors of v in optimum tour in S? . .

2

If u, w are neighbors of v in an optimum tour of S then removing v gives an optimum path from u to w visiting all nodes in S − {v}. Path from u to w is not a recursive subproblem! Need to find a more general problem to allow recursion.

Sariel (UIUC) CS573 66 Fall 2013 66 / 72

slide-128
SLIDE 128

Towards a Recursive Solution

. .

1

Order vertices as v1, v2, . . . , vn . .

2

OPT(S): optimum TSP tour for the vertices S ⊆ V in the graph restricted to S. Want OPT(V). Can we compute OPT(S) recursively? . .

1

Say v ∈ S. What are the two neighbors of v in optimum tour in S? . .

2

If u, w are neighbors of v in an optimum tour of S then removing v gives an optimum path from u to w visiting all nodes in S − {v}. Path from u to w is not a recursive subproblem! Need to find a more general problem to allow recursion.

Sariel (UIUC) CS573 66 Fall 2013 66 / 72

slide-129
SLIDE 129

A More General Problem: TSP Path

Input A graph G = (V, E) with non-negative edge costs/lengths(c(e) for edge e) and two nodes s, t Goal Find a path from s to t of minimum cost that visits each node exactly once. Can solve TSP using above. Do you see how? Recursion for optimum TSP Path problem: . .

1

OPT(u, v, S): optimum TSP Path from u to v in the graph restricted to S (here u, v ∈ S).

Sariel (UIUC) CS573 67 Fall 2013 67 / 72

slide-130
SLIDE 130

A More General Problem: TSP Path

Input A graph G = (V, E) with non-negative edge costs/lengths(c(e) for edge e) and two nodes s, t Goal Find a path from s to t of minimum cost that visits each node exactly once. Can solve TSP using above. Do you see how? Recursion for optimum TSP Path problem: . .

1

OPT(u, v, S): optimum TSP Path from u to v in the graph restricted to S (here u, v ∈ S).

Sariel (UIUC) CS573 67 Fall 2013 67 / 72

slide-131
SLIDE 131

A More General Problem: TSP Path

Input A graph G = (V, E) with non-negative edge costs/lengths(c(e) for edge e) and two nodes s, t Goal Find a path from s to t of minimum cost that visits each node exactly once. Can solve TSP using above. Do you see how? Recursion for optimum TSP Path problem: . .

1

OPT(u, v, S): optimum TSP Path from u to v in the graph restricted to S (here u, v ∈ S).

Sariel (UIUC) CS573 67 Fall 2013 67 / 72

slide-132
SLIDE 132

A More General Problem: TSP Path

Continued...

What is the next node in the optimum path from u to v? Suppose it is w. Then what is OPT(u, v, S)? OPT(u, v, S) = c(u, w) + OPT(w, v, S − {u}) We do not know w! So try all possibilities for w.

Sariel (UIUC) CS573 68 Fall 2013 68 / 72

slide-133
SLIDE 133

A More General Problem: TSP Path

Continued...

What is the next node in the optimum path from u to v? Suppose it is w. Then what is OPT(u, v, S)? OPT(u, v, S) = c(u, w) + OPT(w, v, S − {u}) We do not know w! So try all possibilities for w.

Sariel (UIUC) CS573 68 Fall 2013 68 / 72

slide-134
SLIDE 134

A Recursive Solution

OPT(u, v, S) = minw∈S,w̸=u,v

(

c(u, w) + OPT(w, v, S − {u})

)

What are the subproblems for the original problem OPT(s, t, V)? OPT(u, v, S) for u, v ∈ S, S ⊆ V. How many subproblems? . .

1

number of distinct subsets S of V is at most 2n . .

2

number of pairs of nodes in a set S is at most n2 . .

3

hence number of subproblems is O(n22n) Exercise: Show that one can compute TSP using above dynamic program in O(n32n) time and O(n22n) space. Disadvantage of dynamic programming solution: memory!

Sariel (UIUC) CS573 69 Fall 2013 69 / 72

slide-135
SLIDE 135

A Recursive Solution

OPT(u, v, S) = minw∈S,w̸=u,v

(

c(u, w) + OPT(w, v, S − {u})

)

What are the subproblems for the original problem OPT(s, t, V)? OPT(u, v, S) for u, v ∈ S, S ⊆ V. How many subproblems? . .

1

number of distinct subsets S of V is at most 2n . .

2

number of pairs of nodes in a set S is at most n2 . .

3

hence number of subproblems is O(n22n) Exercise: Show that one can compute TSP using above dynamic program in O(n32n) time and O(n22n) space. Disadvantage of dynamic programming solution: memory!

Sariel (UIUC) CS573 69 Fall 2013 69 / 72

slide-136
SLIDE 136

A Recursive Solution

OPT(u, v, S) = minw∈S,w̸=u,v

(

c(u, w) + OPT(w, v, S − {u})

)

What are the subproblems for the original problem OPT(s, t, V)? OPT(u, v, S) for u, v ∈ S, S ⊆ V. How many subproblems? . .

1

number of distinct subsets S of V is at most 2n . .

2

number of pairs of nodes in a set S is at most n2 . .

3

hence number of subproblems is O(n22n) Exercise: Show that one can compute TSP using above dynamic program in O(n32n) time and O(n22n) space. Disadvantage of dynamic programming solution: memory!

Sariel (UIUC) CS573 69 Fall 2013 69 / 72

slide-137
SLIDE 137

A Recursive Solution

OPT(u, v, S) = minw∈S,w̸=u,v

(

c(u, w) + OPT(w, v, S − {u})

)

What are the subproblems for the original problem OPT(s, t, V)? OPT(u, v, S) for u, v ∈ S, S ⊆ V. How many subproblems? . .

1

number of distinct subsets S of V is at most 2n . .

2

number of pairs of nodes in a set S is at most n2 . .

3

hence number of subproblems is O(n22n) Exercise: Show that one can compute TSP using above dynamic program in O(n32n) time and O(n22n) space. Disadvantage of dynamic programming solution: memory!

Sariel (UIUC) CS573 69 Fall 2013 69 / 72

slide-138
SLIDE 138

A Recursive Solution

OPT(u, v, S) = minw∈S,w̸=u,v

(

c(u, w) + OPT(w, v, S − {u})

)

What are the subproblems for the original problem OPT(s, t, V)? OPT(u, v, S) for u, v ∈ S, S ⊆ V. How many subproblems? . .

1

number of distinct subsets S of V is at most 2n . .

2

number of pairs of nodes in a set S is at most n2 . .

3

hence number of subproblems is O(n22n) Exercise: Show that one can compute TSP using above dynamic program in O(n32n) time and O(n22n) space. Disadvantage of dynamic programming solution: memory!

Sariel (UIUC) CS573 69 Fall 2013 69 / 72

slide-139
SLIDE 139

A Recursive Solution

OPT(u, v, S) = minw∈S,w̸=u,v

(

c(u, w) + OPT(w, v, S − {u})

)

What are the subproblems for the original problem OPT(s, t, V)? OPT(u, v, S) for u, v ∈ S, S ⊆ V. How many subproblems? . .

1

number of distinct subsets S of V is at most 2n . .

2

number of pairs of nodes in a set S is at most n2 . .

3

hence number of subproblems is O(n22n) Exercise: Show that one can compute TSP using above dynamic program in O(n32n) time and O(n22n) space. Disadvantage of dynamic programming solution: memory!

Sariel (UIUC) CS573 69 Fall 2013 69 / 72

slide-140
SLIDE 140

Dynamic Programming: Postscript

. . Dynamic Programming = Smart Recursion + Memoization . .

1

How to come up with the recursion? . .

2

How to recognize that dynamic programming may apply?

Sariel (UIUC) CS573 70 Fall 2013 70 / 72

slide-141
SLIDE 141

Dynamic Programming: Postscript

. . Dynamic Programming = Smart Recursion + Memoization . .

1

How to come up with the recursion? . .

2

How to recognize that dynamic programming may apply?

Sariel (UIUC) CS573 70 Fall 2013 70 / 72

slide-142
SLIDE 142

Some Tips

. .

1

Problems where there is a natural linear ordering: sequences, paths, intervals, DAGs etc. Recursion based on ordering (left to right or right to left or topological sort) usually works. . .

2

Problems involving trees: recursion based on subtrees. . .

3

More generally:

. . .

1

Problem admits a natural recursive divide and conquer . . .

2

If optimal solution for whole problem can be simply composed from optimal solution for each separate pieces then plain divide and conquer works directly . . .

3

If optimal solution depends on all pieces then can apply dynamic programming if interface/interaction between pieces is

  • limited. Augment recursion to not simply find an optimum

solution but also an optimum solution for each possible way to interact with the other pieces.

Sariel (UIUC) CS573 71 Fall 2013 71 / 72

slide-143
SLIDE 143

Examples

. .

1

Longest Increasing Subsequence: break sequence in the middle

  • say. What is the interaction between the two pieces in a

solution? . .

2

Sequence Alignment: break both sequences in two pieces each. What is the interaction between the two sets of pieces? . .

3

Independent Set in a Tree: break tree at root into subtrees. What is the interaction between the sutrees? . .

4

Independent Set in an graph: break graph into two graphs. What is the interaction? Very high! . .

5

Knapsack: Split items into two sets of half each. What is the interaction?

Sariel (UIUC) CS573 72 Fall 2013 72 / 72

slide-144
SLIDE 144

Notes

Sariel (UIUC) CS573 73 Fall 2013 73 / 72

slide-145
SLIDE 145

Notes

Sariel (UIUC) CS573 74 Fall 2013 74 / 72

slide-146
SLIDE 146

Notes

Sariel (UIUC) CS573 75 Fall 2013 75 / 72

slide-147
SLIDE 147

Notes

Sariel (UIUC) CS573 76 Fall 2013 76 / 72