Normally you would be searching for the shortest path from A to B. At least this is what navigation systems do. But here we want to look at the opposite problem:
What is the longest possible path to get from A to B?
The longest path problem is the problem of finding a simple path of maximum length in a given graph. In contrast to the shortest path problem, which can be solved in polynomial time in graphs without negative-weight cycles, the longest path problem is NP-hard.
We are looking for a simple path, i.e. every node must only be visited (maximal) once. This sounds familiar to the Traveling Salesman Problem. Indeed, the problem can be modelled in a similar form 1:
Network routing often gets modelled as a flow optimization problem. Hence, the typical flow balance constraints apply.
Node Flow Balance: flow in equals flow out of node:
Outflow of each node must only go to one arc/edge:
Sub-tour elimination constraints are well-known from the Traveling Salesman Problem: Miller-Tucker-Zemlin formulation.
As input parameter we specify start and end node of our path:
The sum of all edges in our network is 5.7660.
For a network this size the optimizer has no difficulty to find the solution in a split-second. However, this is not true for larger real world problems.
Understanding how to extend a well known TSP Mixed Integer problem formulation to the Longest Path problem provides insights into Linear Programming. Applying NetworkX to handle network topology and visualization added another joyful aspect to this endeavor. Graphs made easy for everybody.
Special thanks to Yet Another Math Programming Consultant, whose blog is an inspiration for everybody who fancies Mathematical Programming.
If you are interested in the Pyomo model or the Python code contact me via mail.