In [1] a graph problem is proposed:
- Given an undirected network,
- Find all paths between two nodes,
- Such that no arc is used more than once,
- And the path does not contain more than \(M\) arcs
Preliminaries
---- 30 SET n
nodes n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 , n10 ---- 30 SET a directed arcs n1 n2 n3 n4 n5 n6 n1 YES YES + n7 n8 n9 n10 n1 YES |
Shortest Path Model |
---|
\[\begin{align}\min&\sum_{i,j|a(i,j)}\color{darkred}f_{i,j}\\ & \sum_{i|a(i,n)} \color{darkred}f_{i,n} + \color{darkblue}{\mathit{inflow}}_n = \sum_{j|a(n,j)}\color{darkred}f_{n,j} &&\forall n\\ & \color{darkred}f_{i,j} \in [0,1] \end{align}\] |
where \[ \color{darkblue}{\mathit{inflow}}_n = \begin{cases} 1 & \text{if $n=n_1$}\\ -1 & \text{if $n=n_{10}$} \\ 0 & \text{otherwise}\end{cases}\] When we run this model, we will see:
---- 82 VARIABLE count.L = 2.000 length of shortest path (hops) ---- 82 VARIABLE f.L flow n8 n10 n1 1.000 |
---- 127 PARAMETER fsol solution from solution
pool INDEX 1 = soln_sp_p1 n8 n10 n1 1 INDEX 1 = soln_sp_p2 n4 n8 n10 n1 1 INDEX 1 = soln_sp_p3 n2 n8 n10 n1 1 INDEX 1 = soln_sp_p4 n4 n6 n10 n1 1 INDEX 1 = soln_sp_p5 n2 n6 n10 n1 1 INDEX 1 = soln_sp_p6 n3 n8 n10 n1 1 |
All-Paths Model V1
SP + MTZ Model |
---|
\[\begin{align} \min\>&\color{darkred}z \\ &\color{darkred}z= \sum_{i,j|a(i,j)}\color{darkred}f_{i,j} \\ &\color{darkred}z \le \color{darkblue}M\\ & \sum_{i|a(i,n)} \color{darkred}f_{i,n} + \color{darkblue}{\mathit{inflow}}_n = \sum_{j|a(n,j)}\color{darkred}f_{n,j} &&\forall n \\ & \color{darkred}t_j \ge \color{darkred}t_i + 1 + (\color{darkblue}N-1)(\color{darkred}f_{i,j}-1) && \forall a_{i,j}, i\ne n_1,j \ne n_{10} \\ & \color{darkred}f_{i,j} \in \{0,1\} \\ & \color{darkred}t_n\ge 0 \end{align}\] |
---- 125 PARAMETER fsol solution from solution pool INDEX 1 = soln_sp2_p1 n8 n10 n1 1 INDEX 1 = soln_sp2_p2 n2 n6 n10 n1 1 INDEX 1 = soln_sp2_p3 n2 n8 n10 n1 1 INDEX 1 = soln_sp2_p4 n4 n6 n10 n1 1 |
All-Paths Model V2
- I added nodes: \(src \text{-} n_1\) or \(n_{10} \text{-} snk\).
- The arcs are indicating adjacency. I.e. we have an arc \(n_1 \text{-} n_2 \rightarrow n_2 \text{-} n_8\).
- I removed self-loops such as : \(n_4 \text{-} n_4 \rightarrow n_4 \text{-} n_4\). They will not be used. (I could have left them in).
- A picture of the new graph is added as an appending (it is relatively large).
- As you can see in the augmented graph each node corresponds to an original arc. Each arc is a particular case of an original node. For instance: an arc related to \(n_2\) occurs two times: \(n_1 \text{-} n_2 \rightarrow n_2 \text{-} n_6\) and \(n_1 \text{-} n_2 \rightarrow n_2 \text{-} n_8\) .
SP + MTZ Model |
---|
\[\begin{align} \min\>&\color{darkred}z \\ &\color{darkred}z= \sum_{i,j,p,q|a(i,j,p,q)}\color{darkred}f_{i,j,p,q} \\ &\color{darkred}z \le \color{darkblue}M+1\\ & \sum_{i,j|a(i,j,p,q)} \color{darkred}f_{i,j,p,q} + \color{darkblue}{\mathit{inflow}}_{p,q} = \sum_{i,j|a(p,q,i,j)}\color{darkred}f_{p,q,i,j} &&\forall p,q \\ & \color{darkred}t_{p,q} \ge \color{darkred}t_{i,j} + 1 + (\color{darkblue}{N}-1)(\color{darkred}f_{i,j,p,q}-1) && \forall a_{i,j,p,q}, \text{except src and snk} \\ & \color{darkred}f_{i,j,p,q} \in \{0,1\} \\ & \color{darkred}t_{i,j}\ge 0 \end{align}\] |
---- 206 PARAMETER fsoln solution from solution pool INDEX 1 = soln_sp3_p1 n1.n8 n8.n10 n10.snk n1
.n8 1 INDEX 1 = soln_sp3_p2 n1.n2 n2.n8 n8.n10 n10.snk n1
.n2 1 INDEX 1 = soln_sp3_p3 n1.n4 n4.n6 n6.n10 n10.snk n1
.n4 1 INDEX 1 = soln_sp3_p4 n1.n2 n2.n6 n6.n10 n10.snk n1
.n2 1 INDEX 1 = soln_sp3_p5 n1.n8 n3.n8 n8.n3 n8.n10 n10.snk n1
.n8
1 INDEX 1 = soln_sp3_p6 n1.n8 n3.n9 n8.n3 n9.n10 n10.snk n1
.n8
1 INDEX 1 = soln_sp3_p7 n1.n4 n3.n9 n4.n3 n9.n10 n10.snk n1
.n4
1 INDEX 1 = soln_sp3_p8 n1.n4 n3.n8 n4.n3 n8.n10 n10.snk n1
.n4
1 INDEX 1 = soln_sp3_p9 n1.n4 n4.n4 n4.n6 n6.n10 n10.snk n1
.n4 1 |
Conclusion
References
- Given an undirected graph, what is the optimal way to find all paths between node A and node B given a maximum amount of arcs? (in python), https://stackoverflow.com/questions/70684721/given-an-undirected-graph-what-is-the-optimal-way-to-find-all-paths-between-nod
- Longest path problem, https://yetanothermathprogrammingconsultant.blogspot.com/2020/02/longest-path-problem.html
Appendix A: augmented graph
Appendix B: GAMS model
$ontext |
A model for the same problem in MiniZinc that uses the pre-defined global constraint bounded_path (https://www.minizinc.org/doc-2.5.5/en/lib-globals.html#index-127): https://gist.github.com/zayenz/c4a779fa0e3a087c997b8962d7e0e5d5 By setting the solver configuration to find all solutions, it finds 19 paths of length at most 4. See statistics in the Gist.
ReplyDeleteNote that this model will not use self-loops either, as it uses the definition of a path that a node can only be visited once.
Are you using a the undirected version of the graph ? the last path shown in the Gist is not feasible in the directed version (which is why you have 19 paths and not 9 I suppose)
DeleteHow about sequentially solving the shortest path model with 1/ binary variables 2/ a constraint to limit the number of arcs 3/ no good cuts to exclude previous solutions ?
ReplyDeleteThat should be essentially the same as using the solution pool.
DeleteI was thinking that perhaps with this approach the MTZ constraints would not be necessary, as the no good cuts would exclude the "first shortest path + an isolated tour".
DeleteAt some stage the shortest path model may introduce subtours just to be different.
DeleteNot necessarily if we use the right no good cuts: https://or.stackexchange.com/questions/7966/how-to-compute-all-paths-between-two-given-nodes-in-a-network?noredirect=1#comment16869_7966
Delete