In integer programming, difference constraints is a problem where we’re interested in satisfying a system of difference equations, i.e.,:

Our goal is to find values of such that a solution exists, or to show that no solution exists. We can use traditional linear programming techniques, like the simplex method. We can also model this as a shortest path problem and solve with the Bellman-Ford algorithm.

As a shortest path problem

We can model each as a node in a directed, weighted graph. We construct a constraint graph with this in mind, where we have one vertex per variable. We also define a pseudo-start that connects to all nodes. We have one edge per constraint.

Then, we assign weights. The weight from the pseudo-start to all nodes is set to 0. Then:

for all constraints in the form:

i.e., for an equation , we model it as an edge to from with a weight of 3.

Some relevant theorems:

  • If has no negative weight cycle, then is a feasible solution.
  • If has a negative weight cycle, then there is no feasible solution.

Then, we run Bellman-Ford to find the shortest path from to all . This gives us the solution, or it reports a negative cycle.