This is a question that regularly pops up: how to solve the longest path problem? At first sight, this is easy. Just replace the "minimize" in the shortest path problem by "maximize" and we are good to go. Unfortunately, opposed to the well-known shortest path problem, the longest path problem is not that easy to state and to solve.
The standard shortest path can be formulated as a Mixed Integer Programming problem:
where \(A\) is our network and \(b\) is a sparse data vector indicating the exogenous inflow or outflow at the source or sink node.
There are some implicit assumptions in this model: there are no negative cycles. In the above example, \(d_{i,j}\) represents distance, so we have \(d_{i,j}\ge 0\) and there is no problem of negative cycles.
Shortest path problems are very easy MIPs to solve. The solution is automatically integer, so it can be solved as an LP and these LPs are very sparse (only two non-zero elements per column) and very easy to solve. In addition, specialized network solvers are widely available.
The problem in the picture has 20 nodes and 37 × 2 arcs. In this example, the arcs are duplicated to make sure we can go in two directions. This leads to an LP of 74 variables and 20 constraints. The solution looks like:
When we just turn the above problem in a maximization problem, we get results that are difficult to interpret. But they certainly don't form what we would call a path:
Basically all but a few lines in the plot are traveled in both directions.
Here \(n\) is the number of nodes.
The results look like:
This looks much better.
More formulations can be found in [1].
The conclusion is: "solve the longest path problem" is not as easy as it seems. We need to look at TSP like machinery to solve this problem. That means no easy MIP models. Straight MIP models are both not that simple to formulate and will only work for relatively small models.
Standard Shortest Path Formulation
The standard shortest path can be formulated as a Mixed Integer Programming problem:
| Shortest Path Problem |
|---|
| \[\begin{align} \min& \sum_{(i,j)\in\color{darkblue} A} \color{darkblue}d_{i,j} \color{darkred}x_{i,j} \\ & \sum_{j|(j,i)\in \color{darkblue}A} \color{darkred}x_{j,i} + \color{darkblue}b_i = \sum_{j|(i,j)\in \color{darkblue}A} \color{darkred}x_{i,j} && \forall i \\ &\color{darkred}x_{i,j} \in \{0,1\} \\ & \color{darkblue} b_i = \begin{cases}1 & \text{if $i$ is a source node} \\ -1 & \text{if $i$ is a sink node} \\ 0 & \text{otherwise} \end{cases} \end{align} \] |
where \(A\) is our network and \(b\) is a sparse data vector indicating the exogenous inflow or outflow at the source or sink node.
![]() |
| Network with the shortest path |
There are some implicit assumptions in this model: there are no negative cycles. In the above example, \(d_{i,j}\) represents distance, so we have \(d_{i,j}\ge 0\) and there is no problem of negative cycles.
Shortest path problems are very easy MIPs to solve. The solution is automatically integer, so it can be solved as an LP and these LPs are very sparse (only two non-zero elements per column) and very easy to solve. In addition, specialized network solvers are widely available.
The problem in the picture has 20 nodes and 37 × 2 arcs. In this example, the arcs are duplicated to make sure we can go in two directions. This leads to an LP of 74 variables and 20 constraints. The solution looks like:
---- 75 VARIABLE z.L = 128.538 objective
---- 75 VARIABLE x.L flow
point6(B) point15 point16 point18 point20
point5(A) 1.000
point15 1.000
point16 1.000
point18 1.000
point20 1.000
Just use max
When we just turn the above problem in a maximization problem, we get results that are difficult to interpret. But they certainly don't form what we would call a path:
---- 78 VARIABLE z.L = 1638.406 objective
---- 78 VARIABLE x.L flow
point1 point2 point3 point4 point5(A) point6(B) point7 point8 point9
point1 1.000
point3 1.000
point4 1.000
point5(A) 1.000
point6(B) 1.000
point7 1.000
point8 1.000
point9 1.000
point10 1.000
point12 1.000
point13 1.000
point14 1.0001.000
point15 1.000
point16 1.000
point17 1.0001.000
point18 1.0001.000
point19 1.000
point20 1.000
+ point10 point11 point12 point13 point14 point15 point16 point17 point18
point1 1.000
point2 1.000
point3 1.000
point4 1.0001.000
point5(A) 1.000
point7 1.000
point8 1.0001.000
point9 1.0001.000
point10 1.000
point11 1.000
point12 1.0001.000
point14 1.0001.000
point15 1.000
point16 1.0001.0001.0001.000
point17 1.0001.0001.000
point18 1.0001.0001.000
point19 1.0001.0001.0001.000
point20 1.0001.0001.0001.000
+ point19 point20
point2 1.000
point7 1.000
point10 1.000
point11 1.000
point13 1.0001.000
point15 1.000
point17 1.000
point18 1.0001.000
point19 1.000
point20 1.000
![]() |
| Maximization results |
Basically all but a few lines in the plot are traveled in both directions.
TSP-like solution
To form a proper path (called an elementary path), we need to add two sets of constraints [1]:
- The outflow of each node goes to just one arc: \[\sum_{j|(i,j)\in A} x_{i,j} \le 1\>\forall i\]
- Forbid any sub-tours to be formed. Sub-tour elimination constraints are well-known from the Traveling Salesman Problem.
Using the simple MTZ (Miller, Tucker, and Zemlin) approach, we can formulate:
| Longest Path Problem |
|---|
| \[\begin{align} \max& \sum_{(i,j)\in\color{darkblue} A} \color{darkblue}d_{i,j} \color{darkred}x_{i,j} \\ & \sum_{j|(j,i)\in \color{darkblue}A} \color{darkred}x_{j,i} + \color{darkblue}b_i = \sum_{j|(i,j)\in \color{darkblue}A} \color{darkred}x_{i,j} && \forall i \\ & \sum_{j|(i,j)\in A} \color{darkred}x_{i,j} \le 1 && \forall i\\ & \color{darkred}t_j \ge \color{darkred}t_i + 1 + (\color{darkblue}n-1)(\color{darkred}x_{i,j}-1) && \forall i,j|i\ne\text{source},j\ne\text{sink} \\&\color{darkred}x_{i,j} \in \{0,1\} \\ & \color{darkred}t_i \ge 0 \\ & \color{darkblue} b_i = \begin{cases}1 & \text{if $i$ is a source node} \\ -1 & \text{if $i$ is a sink node} \\ 0 & \text{otherwise} \end{cases} \end{align} \] |
Here \(n\) is the number of nodes.
The results look like:
---- 126 VARIABLE z.L = 432.987 objective
---- 126 VARIABLE x.L flow
point1 point2 point3 point4 point6(B) point7 point8 point9 point10 point12
point2 1.000
point4 1.000
point5(A) 1.000
point9 1.000
point12 1.000
point14 1.000
point15 1.000
point16 1.000
point19 1.000
point20 1.000
+ point13 point14 point15 point16 point17 point18 point19 point20
point1 1.000
point3 1.000
point7 1.000
point8 1.000
point10 1.000
point13 1.000
point17 1.000
point18 1.000
![]() |
| Solution of Longest Elementary Path Model |
This looks much better.
More formulations can be found in [1].
Conclusion
The conclusion is: "solve the longest path problem" is not as easy as it seems. We need to look at TSP like machinery to solve this problem. That means no easy MIP models. Straight MIP models are both not that simple to formulate and will only work for relatively small models.
References
- Leonardo Taccari, Integer programming formulations for the elementary shortest path problem, European Journal of Operational Research, Volume 252, Issue 1, 1 July 2016, Pages 122-130


