I would solve this using the following approach:
- Compute the shortest path with a MIP, with an additional constraint to limit the number of arcs in the path.
- If a path is found, store it, add a no good cut to exclude this path in the next iterations, and go to step 1. If no path is found, you are done.
The tricky part is to use the right no good cut for a given path $P$. It needs to exclude the path that has been found, except if it includes other edges of the network without forming a subtour (Erwin Kalvelagen used MTZ constraints to forbid subtours).
For example, if $P=1-8-10$, path $Q=1-8-3-8-10$ is a candidate (if $M=4$). On the other hand, a solution with edges $(1,8),(8,10),(4,4)$ (that is, $P$ with an isolated self-loop) must be forbidden. In other words, the no good cuts must ensure some sort of contiguity if edges from $P$ are used again.
This can be done as follows:
$$
\sum_{(i,j)\in P}(1-x_{ij}) + \sum_{(i,j)\not \in P, i\in P \mbox{ or } j \in P}x_{ij} \ge 1
$$
This means that
- Either one of the edges of $P$ must take value $0$ (and so the path will be different from $P$) or
- Either one of the edges of the network not in $P$, but linked to $P$ must be used (in which case $P$ will have extra edges, without subtours).
My simulations with this approach match Erwin's results. Downside: you have to solve a series of MIPs, possibly many. Upside: no graph transformation (line graph), and no MTZ constraints.
EDIT
This strategy has the following flaw for graphs that have multiple subtours originating at a same node. For example consider paths $P=1-u-a-u-b-u-10$ and $Q=1-u-b-u-a-u-10$: they have the exact same edge set, but the order of the nodes differ. Once path $P$ is found, path $Q$ will become infeasible with respect to the no good cut associated with $P$. In other words the no good cuts are too strong.
There are at least two ways to fix this:
- Once a path is generated, check if this situation occurs and deduce all possible paths. This is doable but a bit tedious.
- Use the line graph. This is handy because with the line graph, no loops are possible. So the MIP can be solved on the line graph, with the following no good cuts for a given path $P$:
\begin{align*}
\sum_{((u,v),(x,y))\in P}(1-x_{u,v,x,y}) &\ge 1 \tag{1} \\
\sum_{((u,v),(x,y))\not \in P}x_{u,v,x,y} &\ge 2 \tag{2}
\end{align*}
Constraints $(1)$ impose that at least one edge from $P$ must be removed and constraints $(2)$ impose that at least $2$ new edges must be selected.
With these new cuts, I get the following results, which match with the other approaches suggested in the other answers:
M |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
paths |
1 |
4 |
9 |
21 |
32 |
53 |
98 |
165 |
268 |