#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.
where queue.ExtractMin()
grabs the vertex with the shortest distance.
#Example. In-class practice graph

Shortest_path_problem_in_class.pdf
Running Dijkstra’s algorithm with gives us shortest path from to every other node:
#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.

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
The subproblem is a graph with the sam vertices, but less edges.