I am modifying the Bellman–Held–Karp Algorithm for TSP, using dynamic programming. In this case, the difference of the classical Bellman-Held-Karp algorithm is that some cities must be visited before others, but I need to conserve the minimization path cost. Before implementing it in a programming language, I am modifying the pseudocode, with finality to solve and prove the solution. I am trying with an example something like as cities ordered by [1,2,3,4,5..n], in a complete graph, and starting by first city (first index), and the 5th cities cannot appear before the second, for example.
I am using as base this pseudocode:
function algorithm TSP (G, n) is
for k := 2 to n do
C({k}, k) := d1,k
end for
for s := 2 to n−1 do
for all S ⊆ {2, . . . , n}, |S| = s do
for all k ∈ S do
C(S, k) := minm≠k,m∈S [C(S\{k}, m) + dm,k]
end for
end for
end for
opt := mink≠1 [C({2, 3, . . . , n}, k) + dk, 1]
return (opt)
end function
I am thinking of saving the maximum city index in the list (in the recurrence), to ensure that it will never be higher, but I am not sure if this approach is correct.
minm≠k,m∈S [C(S\{k}, m) + dm,k] + max(S\{k + i < 4})
Is corret this approach? Can someone help me?