#Dynamic Programming Pt.1

#Shortest path as network flow

Given undirected graph , start node , and end node , we want to find the shortest path .

  • Each edge has a non-negative cost . Typically the distance.
  • The arrow means the path from to , including arbitrarily many intermediate vertices.

#Decision Variables

We want to find which edge to take from .

#Objective

Minimize the path cost.

We could solve the shortest path problem with a general LP solver, but we have more efficient algorithms.

#Dijkstra’s Algorithm

The following pseudocode is from my own algorithm notes but it does the same thing as what we did in class.

Pseudocode
Complexity
function Initialize(G, start): for each vertex v in G: dist[v] = Infinity pred[v] = nothing dist[start] = 0 pred[start] = nothing return dist, pred function NonNegativeDijkstras(G, start): dist, pred = Initialize(G, start) dist[start] = 0 queue = PriorityQueue() for each vertex v in G: put v in queue with priority dist[v] while queue is not empty: u = queue.ExtractMin() for each adjacent vertex v: if dist[v] > dist[u] + weight(uv): dist[v] = dist[u] + weight(uv) pred[v] = u queue.UpdatePriority(v, dist[v])
// matched to each line O(V) O(V log V) O(E) with inner for loop O(log V) for heaps already counted O(log V) for heaps

where queue.ExtractMin() grabs the vertex with the shortest distance.

#Example. In-class practice graph

Source: Canvas, Shortest_path_problem_in_class.pdf
Source: Canvas, Shortest_path_problem_in_class.pdf

Running Dijkstra’s algorithm with gives us shortest path from to every other node:

==> Selected start: A ╭──────────────┬────────────────────────────────┬────────────╮ │ End Vertex │ path │ distance │ ├──────────────┼────────────────────────────────┼────────────┤ │ A │ [] │ 0 │ │ B │ ['A', 'B'] │ 10 │ │ C │ ['A', 'B', 'C'] │ 39 │ │ D │ ['A', 'E', 'F', 'G', 'D'] │ 48 │ │ E │ ['A', 'E'] │ 8 │ │ F │ ['A', 'E', 'F'] │ 22 │ │ G │ ['A', 'E', 'F', 'G'] │ 34 │ │ H │ ['A', 'E', 'F', 'G', 'H'] │ 53 │ │ I │ ['A', 'E', 'I'] │ 20 │ │ J │ ['A', 'E', 'F', 'J'] │ 37 │ │ K │ ['A', 'E', 'F', 'G', 'K'] │ 43 │ │ L │ ['A', 'E', 'F', 'G', 'K', 'L'] │ 61 │ <== Path from A to L ╰──────────────┴────────────────────────────────┴────────────╯

#Optimal Substructure

Suppose is the shortest path from to , and is an intermediate node.

Then the sub-path and are both shortest paths from to , to respectively.

#Asymmetric Traveling Salesman Problem

For a directed graph , a non-negative cost for each arc , we want to find the shortest tour (visit all nodes exactly once and return to the starting node)

#Decision Variable

#Objective

Minimize the total cost.

#Constraints

For each , let the set of edges entering be , edges leaving be :

We should pass through exactly once, so 1 edge in 1 edge out.

#Sub-tour elimination

The graph should also be connected, no isolated subgraphs

We don’t want (1) basically, since there’s complete tour. Source

#The Dantzig-Fulkerson-Johnson (DFJ) formulation

This constraint is what makes TSP an NP-Hard problem, the number of possible ’s is the size of the power set of , which is

#The Miller–Tucker–Zemlin (MTZ) formulation

For each node , label with except the starting node.

The idea is we must go to a node that has a larger label than the current node.

See image link for source
See image link for source

This prevents us from going into cycles, since if we do, we will eventually travel to a node with a smaller label, which violates the constraint.

#Symmetric TSP

Now we consider a undirected graph , a non-negative cost for each edge . We also know that .

#Objective

Same as the asymmetric case.

#Constraints

Let be the set of edges connected to node :

The one edge in, one edge out constraint is:

Let be the set of edges that have one node in and one node not in :

Using the DFJ formulation for sub-tour elimination, we have:

#Dynamic Programming (DP)

#General formulation

Every DP problem has the following properties:

Stages :

State at stage :

Value function :

Transition cost :

#Theorem. Principle of optimality

If is optimal, then all the subproblems are also optimal.

#0-1 Knapsack

Given a backpack with capacity , and items where each item takes up capacity and is worth dollars. Decide which items to take.

#Objective

Let be the indicator variable of whether to take item , we want to maximize total profit:

If we don’t have the integer constraint , we can just take the items with maximum profit-to-capacity ratio: .

#DP Formulation

Stage
: The maximum item index . If we are in stage , we only consider items .

State
: , remaining capacity of the backpack

Value function : , max profit given capacity and items

We can try to take item if it fits, or we can skip .

#Bounded Knapsack

Now suppose each items has a maximum of copies.

In this case we include the number of copies as part of the state, now represents the maximum profit with capacity , copies of item , and using item .

#Direct TSP with dynamic programming

Consider the graph

1
2
3
4
5

The subproblem is a graph with the sam vertices, but less edges.