Lecture Notes: Dynamic Programming, Slides of Algorithms and Programming

IOE 691: Approximation & Online Algorithms. Lecture Notes: Dynamic Programming. Instructor: Viswanath Nagarajan. Scribe: Gian-Gabriel Garcia, Miao Yu.

Typology: Slides

2022/2023

Uploaded on 05/11/2023

geryle
geryle 🇺🇸

4.5

(23)

277 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
IOE 691: Approximation & Online Algorithms
Lecture Notes: Dynamic Programming
Instructor: Viswanath Nagarajan Scribe: Gian-Gabriel Garcia, Miao Yu
A third algorithmic technique in approximation algorithms is dynamic programming. Dynamic
programming (DP) involves solving problems incrementally, starting with instances of size one and
working up to instances of generic size n. It is similar to the method of induction in proofs. A key
step in DP is to identify a recursive (or inductive) structure that helps reduce one instance of size
ninto several instances of size at most n1.
1 Basic Dynamic Programming
We first illustrate some exact DP algorithms.
1.1 Stable Set
The input to the stable (or independent) set problem is given by an undirected graph G= (V, E)
and weights w:VR+.
Definition 1.1 A set of vertices Sis called independent if no pair of vertices in Sshare an edge.
The goal is to find an independent set Swhich maximizes the sum of the weights in S.
Here we show a simple dynamic program that solves the problem exactly on trees. Results for
many graph problems on trees often extend to larger classes of graphs (eg. planar); so trees are a
natural special class of graphs to consider. We consider the tree Gto be rooted at some vertex r,
where all other nodes are descendants of r(i.e., appear below rin the tree).
The recursive problems. Let Gvbe the subtree below any vertex v; so G=Gr. Let T[v, 0] be
the maximum weight of the independent set in Gvsuch that vis not included in the independent
set. Similarly, let T[v, 1] be the maximum weight of the independent set in Gvsuch that vis
included. The following DP gives an exact optimal solution:
Algorithm 1 Dynamic programming algorithm for Stable Set
Re-number vertices by 1,..., n such that the index is increasing from any leaf to the root. So the
root of Gis vertex n. Let Cidenote all the children of vertex i.
Initialize T[u, 0] = 0 and T[u, 1] = w(u) for all uVwhich are leaves.
1. For all non-leaf vertices i= 1, ..., n:
Set T[i, 0] = PkCimax {T[k, 0], T [k , 1]}.Remember the maximizing choices.
Set T[i, 1] = w(i) + PkCiT[k, 0].
2. The optimal value is given by max{T[n, 0], T [n, 1]}at the root.
The optimal solution can also be found by maintaining the maximizing choices (which represent
the best decisions) in calculating T, and “backtracking” from the root entry max{T[n, 0], T [n, 1]}.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Notes: Dynamic Programming and more Slides Algorithms and Programming in PDF only on Docsity!

IOE 691: Approximation & Online Algorithms

Lecture Notes: Dynamic Programming

Instructor: Viswanath Nagarajan Scribe: Gian-Gabriel Garcia, Miao Yu

A third algorithmic technique in approximation algorithms is dynamic programming. Dynamic programming (DP) involves solving problems incrementally, starting with instances of size one and working up to instances of generic size n. It is similar to the method of induction in proofs. A key step in DP is to identify a recursive (or inductive) structure that helps reduce one instance of size n into several instances of size at most n − 1.

1 Basic Dynamic Programming

We first illustrate some exact DP algorithms.

1.1 Stable Set

The input to the stable (or independent) set problem is given by an undirected graph G = (V, E) and weights w : V → R+.

Definition 1.1 A set of vertices S is called independent if no pair of vertices in S share an edge.

The goal is to find an independent set S which maximizes the sum of the weights in S. Here we show a simple dynamic program that solves the problem exactly on trees. Results for many graph problems on trees often extend to larger classes of graphs (eg. planar); so trees are a natural special class of graphs to consider. We consider the tree G to be rooted at some vertex r, where all other nodes are descendants of r (i.e., appear below r in the tree).

The recursive problems. Let Gv be the subtree below any vertex v; so G = Gr. Let T [v, 0] be the maximum weight of the independent set in Gv such that v is not included in the independent set. Similarly, let T [v, 1] be the maximum weight of the independent set in Gv such that v is included. The following DP gives an exact optimal solution:

Algorithm 1 Dynamic programming algorithm for Stable Set Re-number vertices by 1, ..., n such that the index is increasing from any leaf to the root. So the root of G is vertex n. Let Ci denote all the children of vertex i. Initialize T [u, 0] = 0 and T [u, 1] = w(u) for all u ∈ V which are leaves.

  1. For all non-leaf vertices i = 1, ..., n: Set T [i, 0] =

k∈Ci max^ {T^ [k,^ 0], T^ [k,^ 1]}.^ Remember the maximizing choices. Set T [i, 1] = w(i) +

k∈Ci T^ [k,^ 0].

  1. The optimal value is given by max{T [n, 0], T [n, 1]} at the root.

The optimal solution can also be found by maintaining the maximizing choices (which represent the best decisions) in calculating T , and “backtracking” from the root entry max{T [n, 0], T [n, 1]}.

1.2 Longest Path in DAG

As another example of dynamic programming we consider the longest path problem on directed acyclic graphs (DAGs). The input is given by a DAG G = (V, E) and two vertices s, t ∈ V. The objective is to find the s − t path with the maximum number of nodes in it. Since G is a DAG, a topological ordering for the vertices can be created as:

Algorithm 2 DAG Topological Ordering

  1. Set v 1 = s. Remove s and all edges attached to it from G. Set i ← 2.
  2. Do until any vertex remains: Find a vertex v with zero in-degree. Set vi ← v. Set i ← i + 1. Remove v and all any edge attached to it from G.

The recursive problems. Assume that all nodes are re-numbered via Algorithm 2. Let

T [i] := length of the longest path from s to vi, ∀i ∈ [n].

The following DP algorithm can be used to solve the longest path problem exactly.

Algorithm 3 Dynamic programming for longest path in DAG

  1. Initialize T [1] = 1.
  2. For i = 2, ..., n: T [i] ← 1 + max j:(vj ,vi)∈E

T [j].

2 Knapsack

The knapsack problem is concerned with picking a maximum-value subset from n items, each with value vi and size si, into a knapsack with capacity B (for sizes). We assume that all values/sizes are integer. In homework 1, we derived a polynomial time approximation scheme (PTAS). Using dynamic programming, we can derive a fully polynomial time approximation scheme (FPTAS), which is more efficient. In the FPTAS, for any  > 0, we can get a (1 − ) approximation in time polynomial in N and (^1)  , where N = n × log maxi∈[n]{vi, si} is the input size. On the other hand, a PTAS has runtime polynomial in N 1 /; while this is a polynomial for any constant , it is much slower than the FPTAS runtime. First, we will show that the knapsack problem can be solved exactly using dynamic programming in “psuedo polynomial” time poly(n, maxi vi).

The recursive problems. Let V :=

∑n i=1 vi^ ≤^ n^ ·^ vmax^ denote the maximum objective value. Consider the related problem where given a target number u, we want to find the minimum size subset of total value at least u. We can guess the optimal value OP T (at most V many choices)

Theorem 2.2 The above algorithm is an FPTAS for the knapsack problem.

Proof: Note that the new values v′^ are integer and

∑n i=1 v ′ i =^

∑n i=1b^

vi M/n c ≤^

∑n i=

n  ≤^

n^2 . So, the running time of ExactKnapsack on the rounded instance is poly( n 2  ) =^ poly(n,^

1  ), which means the running time of our FPTAS is also poly(n, (^1)  ). Now, we will show that if OP T denotes the optimal value of the original instance (sizes si and values vi) then our solution A has value at least (1 − )OP T. Let OP T ′^ denote the optimal value of the rounded instance. Let S be the subset of items in the optimal solution under values {vi}i∈[n]. We have:

OP T =

i∈S

vi =

i∈S

vi M n

M

n

i∈S

(v′ i + 1)

M

n

i∈S

v i′)

M

n

+ M ≤

M

n

OP T ′^ + OP T

Where the last inequality is true because OP T ≥ maxi vi = M as any single item fits in the knapsack (by our assumption). Now by rearranging inequalities we have OP T ′^ ≥ (1 − ) (^) Mn · OP T. It follows that

i∈A v

′ i =^ OP T^ ′ (^) ≥ (1 − ) n M ·^ OP T^. So, ∑

i∈A

vi ≥

M

n

i∈A

v′ i =

M

n

OP T ′^ ≥ (1 − )OP T.

This completes the proof.

3 Euclidean Traveling Salesman Problem

In the Euclidean Traveling Salesman Problem, there are n points in Rd^ space with Euclidean distance between any two points, i.e. d(x, y) = ‖x − y‖ 2. We are tasked to find a tour of minimum length visiting each point. In this lecture, we only focus on the case that d = 2. For the metric Traveling Salesman Problem (TSP), there cannot be any polynomial-time ap- proximation scheme (unless P=NP). For a long time, the best approximation bound for metric TSP was 1.5 given by [Chr76]; very recently, this has been improved to 1. 5 − δ for some (tiny) constant δ > 0. However, we can design a better approximation algorithm for TSP on Euclidean metrics. In this lecture, we will provide a dynamic programming (DP) scheme for Euclidean TSP that for any  > 0 finds a (1 + )-approximate solution in polynomial time. This algorithm was developed by [Aro98].

Theorem 3.1 There is a Polynomial-Time Approximation Scheme (PTAS) for Euclidean Travel- ing Salesman Problem.

The strategy to design the PTAS for Euclidean TSP is as follows. Firstly, we round the instance and exploit the structure of the problem to construct a new simpler instance having close-to-optimal objective. Secondly, we design a DP to find an exact solution for the new instance. To simplify the notation, we use OP T to denote both optimal solution and optimal objective value of the Euclidean TSP. We also use the equivalent view of a TSP tour as an Eulerian graph (i.e. connected and even degree everywhere).

3.1 Assumptions

To design our PTAS, we make four important assumptions. First, we notice that scaling the input instance does not affect approximation ratio since the cost of all tours are scaled by same factor. We apply the following scaling on the original input. Let L = 4n^2 and scale the input such that L × L is the smallest square containing all input points. Without loss of generality, we focus on the input of Euclidean TSP after scaling. We make following assumption.

Assumption 3.1 All input points are on ZL × ZL with L = 4n^2 and OP T ≥ L

We make following transformation to ensure Assumption 3.1. For each input point x = (x 1 , y 1 ) in original input, we “round” it down to the point x′^ = (bx 1 c, bx 2 c)). Figure 1 provides an example of transformation with L = 4. Algorithm Rounding For each point v in(xv, yv) from 1, 2 ,... , n v′^ ← (bxvc, byvc)) Create an instance with the collection of v′’s

L

L

Figure 1: “Rounding” transforms all input points to integer node in L = 4 network

Note OP T ≥ L is trivial since L × L is the smallest square containing all input points. Let P 1 denote the instance after “rounding” and OP T 1 be its optimal objective. The following lemma bounds the effect of the transformation.

Lemma 3.1 OP T 1 ≤ (1 + (^1) n )OP T

Proof: Let v be any original point and v′^ be its correspondent in the grid network. We find a feasible solution to P 1 from OP T by repeating following process for all points: after visiting point v in OP T , go to v′^ and then go back to v. The total increase in such solution to visit point v′^ from v is bounded by 2

2 and hence overall cost increase is bounded by 2

2 n. Since we have L ≥ OP T and L = 4n^2 , we have relative error no greater than 2

√ 2 n OP T =^

2 √ 2 n L =^

2 √ 2 n 4 n^2 <^

1 n.^ Since^ OP T^1 is the optimal objective, it is no greater than the cost of such solution. Therefore, we conclude OP T 1 ≤ (1 + (^1) n ).

We will view each portal as comprising of c mini-portals that are located very close to each other. We then require that each mini-portal is crossed at most once. Finally, we will require that the tour does not intersect itself.

Assumption 3.4 The tour is non self-intersecting when all entry/exit at squares occur at mini- portals.

We will show later that enforcing Assumptions 3.2-3.4 on the optimal tour only increases cost by a small amount.

3.2 DP Algorithm under Assumptions

Here we provide an exact algorithm under Assumptions 3.1-3.3. Consider a square at level i. By Assumption 3.2 and 3.3, there are 4m portals for the square. Since each portal can be crossed at most c times, by considering each portal as c distinct mini- portals the number of entry/exit ways for each square is 3^4 cm^ (each of the 4cm mini portals has 3 possible states, i.e, in, out or unused). Next, we need to pair the entries and exits noticing that not all pairings will be valid. Since there is no self-intersection in the tour (Assumption 3.4), we only consider “valid parings” where the enter/exit pairs do not create crossings within the square.

Lemma 3.2 Given a fixed crossing setting (ways to enter/exits the portals), the number of valid parings is bounded by O(2^4 cm)

Proof: Let N (2t) be the number of parings with 2t entry/exit portals. Then after fixing a paring (1, j) for the first entry/exit portal, the number of possible parings is N (j − 2)N (2t − j). By summing over all possible choice of j, We have N (2t) ≤

∑ 2 t j=2 N^ (j^ −^ 2)N^ (2t^ −^ j) where the sum is only over even j. We also have N (0) = 1. This can be bounded by the Catalan number C(t) because Catalan numbers also satisfy the relationship C(0) = 1 and C(t + 1) =

∑t i=0 C(i)^ C(t^ −^ i) for^ t^ ≥

  1. Therefore we conclude the number of valid parings for each entries/exits settings for a square at level i is bounded by C(2t) = O(2^2 t) = O(2^4 cm).

We now build the DP to calculate each OPT for modified network. We define the state of DP as a square and its possible ways to enter/exit such square. Since at each level i, we have 4i^ squares, and there are log(L) + 1 = O(log n) levels. Then the size of states space is O(4L 34 cm 24 cm) which is polynomial as long as m = O(log n). The Bellman equation (DP recursion) involves computing the DP entry for a level i square with entry/exit pairing σ using the entries for its 4 level i− squares. This can be done by enumerating over all possible entry/exist pairings ω 1 , ω 2 , ω 3 , ω 4 for the smaller squares and checking of they are compatible with σ (eg. each entry of an ωk must be either an entry of σ or an exit of another ωk′ , there should be no subtours etc). This can be easily done in time polynomial in m. Therefore, we can find the optimal tour by applying such a DP in poly(n) · O(6^4 cm) time.

Theorem 3.2 Dynamic Programming given above can solve Euclidean TSP with Assumptions 3.1- 3.4 to optimality with O(n^464 cm) time

point

portal

line at level i

optimal tour

optimal tour with Assumption 2

Figure 4: Example of a detour for Assumption 3.

3.3 Analysis

Now we bound the losses incurred by making Assumptions 3.1-3.4. By Lemma 3.1, we only lose a factor 1 + (^) n^1 by making Assumption 3.1. To enforce Assumption 3.2, for each square crossing, we create a detour to its nearest portal. See Figure 4 for an example. Note that for any crossing on a level i line, we need to detour to a “level i” portal (i.e. on a square of level i) as this is the strictest requirement (any portal on a level i square is also one on any level j ≥ i + 1 square). Since the distance between two adjacent portals on line at level i is (^) mL 2 i , the increase of the overall cost for this detour is at most (^) mL 2 i. We define x(q) to be the number of crossings through line q (either horizontal or vertical) in an optimal tour. Let i(q) be the level of line q. After enforcing Assumption 3.2, by summing over all detours, we obtain the following.

Lemma 3.3 The increase in length of an optimal tour due to Assumption 3.2 is at most

q x(q)^ L m 2 i(q) Now we look at the effect of the Assumption 3.3. We only give an informal proof here. We argue that the number of crossings is at most c = 4. Consider any portal with t crossings (i.e. t entry and t exit points). Consider an infinitesimal circle around the portal and let C denote the 2 t entry/exit points on this circle. By removing the edges of the optimal tour inside the circle, we obtain a graph with vertices being the original input points, portals and C where (i) all vertices in C have degree one and all other vertices have even degree; and (ii) if C is contracted (representing the portal itself) then the graph will be connected. We now add some edges within the circle to ensure the graph becomes Eulerian with at most c crossings: this would imply the existence of a tour of no larger cost. The edges A to add are (i) a matching on C and (ii) a cycle on C, both of which are in the cyclic order of these points. The cost increase is infinitesimal because all these edges are contained in the infinitesimal circle around the portal. Moreover, the resulting graph is Eulerian. Finally, the resulting tour crosses each portal (on horizontal or vertical line) at most 3 times: note that these crossings will correspond to certain added edges A. See Figure 5. To enforce Assumption 3.4, we remove self-intersections one at a time. Consider first a self- intersection at a non-portal point p. Consider an infinitesimal circle around this point and let (s 1 , t 1 ) and (s 2 , t 2 ) be the (portions) of the edges through this circle that cause the intersection at p. Suppose the optimal tour is t 1 → D 1 → s 2 → t 2 → D 2 → s 1 → t 1 where D 1 (resp. D 2 ) is a path from t 1 to s 2 (resp. t 2 to s 1 ). Then the modified tour t 1 → D 1 → s 2 → s 1 → Dreverse 2 → t 2 → t 1

3.4 Randomization to bound cost increase

Now it only remains to bound the cost increase (from Lemma 3.3) in terms of the optimal cost. Recall that the increase is bounded by

L m

q:line

x(q) · 2 −i(q).

Unfortunately, this might even be much larger than OP T as the layout of the points may be such that the optimal tour crosses a line of low level (eg. level 1) several times at non-portal points. A clean idea to handle this issue is via randomization. In randomized dissection, we first randomly select integers a, b in range [−L, 0]. Then we consider a 2L × 2 L grid with “bottom left” at the random integer point (a, b) and “top right” at (a + 2L, b + 2L). Note that irrespective of the (a, b) choice, the integer points ZL × ZL (and hence the optimal TSP tour on the given instance) are contained in the resulting 2L × 2 L grid. Crucially, we now define the levels of lines and squares in the dissection w.r.t. the randomly shifted square

Z[a,a+2L] × Z[b,b+2L].

In particular, for a fixed line q (vertical or horizontal), we have

Pr a,b [ line q has level i] ≤

2 i L , ∀i = 1, 2 · · · log 2 (2L).

See Figure 8.

L × L grid input points

(a, b) Random 2L × 2 L grid

Figure 8: Randomized dissection.

We are now ready to bound the expected increase in cost using Lemma 3.3.

Lemma 3.4 Suppose m ≥ log^2 (2 L). Then the expected cost increase

Ea,b

 L

m

q:line

x(q) · 2 −i(q)

q:line

x(q).

Proof: Using the observations (above) on the random dissection, for any fixed line q,

Ea,b[x(q) · 2 −i(q)] ≤ x(q)

log ∑ 2 (2L)

i=

2 −i^ · Pr a,b [i(q) = i] ≤ x(q)

log ∑ 2 (2L)

i=

L

log 2 (2L) L · x(q).

Now, adding this over all lines q, we obtain:

Ea,b

 L

m

q:line

x(q) · 2 −i(q)

 ≤ L

m

log 2 (2L) L

q:line

x(q) ≤ 

q:line

x(q),

where the last inequality uses the assumption m ≥ log^2 (2 L).

Finally, we relate the total number of line crossings

q:line x(q) to the optimal cost.

Lemma 3.

q:line x(q)^ ≤^4 ·^ OP T^.

Proof: Note that the optimal tour consists of several edges connecting pairs of points in the ZL × ZL grid. Consider any edge from point (x 1 , x 2 ) to (y 1 , y 2 ) in OP T. The distance of this edge is d(x, y) =

(x 1 − y 1 )^2 + (x 2 − y 2 )^2 and the number of line crossings for this edge is at most |x 1 − y 1 | + |x 2 − y 2 | + 2. Since α + β ≤

2(α^2 + β^2 ) for any α, β ≥ 0, we have

|x 1 − y 1 | + |x 2 − y 2 | + 2 ≤

2((x 1 − y 1 )^2 + (x 2 − y 2 )^2 ) + 2 ≤ 4

(x 1 − y 1 )^2 + (x 2 − y 2 )^2

Summing over all edges in OP T completes the proof.

Finally, combining the above two lemmas, the expected increase in cost in order to ensure Assumptions 3.1-3.4 is at most 4 · OP T , when m = Θ(log n/). Thus we obtain an algorithm running in time nO(1/)^ that provides an expected 1 +  approximation ratio for any  > 0. To obtain a deterministic algorithm, we can simply try all choices of a, b and run the DP algorithm for each. Therefore we obtain Theorem 3.1.

References

[Aro98] Sanjeev Arora. Polynomial time approximation schemes for euclidean traveling salesman and other geometric problems. Journal of the ACM (JACM), 45(5):753–782, 1998.

[Chr76] Nicos Christofides. Worst-case analysis of a new heuristic for the travelling salesman problem. Carnegie-Mellon Univ Pittsburgh Pa Management Sciences Research Group.,