4.2. Improve the Dynamic Programming Algorithm
The following proves the feasibility of using dynamic programming algorithm for this problem.
Lemma 1 . If is the shortest path from v 1 to v k, then for any two nodes and in the path, the path is also the shortest path from to .
Proof of Lemma 1 : Since Q is the TSP path from B to E, Lemma 1 indicates that Q possesses the property of optimal substructure. Suppose there is a common point in P and Q, and . Then, the subsequence of Q forms the TSP path from B to , and the subsequence of Q forms the TSP path from to E. According to the definitions of P and Q, is a subsequence of , and is a subsequence of . This means that the optimal solution to this problem can be broken down into the optimal solutions of its subproblems. In summary, this problem also possesses the property of optimal substructure, so it can be solved using dynamic programming algorithms.□
Due to the fact that this problem not only requires obtaining the path length but also storing the shortest path, an additional array is needed during the dynamic programming process to record the historical optimal paths. However, as the algorithm runs, the number of nodes in the historical optimal path continues to increase, meaning the array length is constantly changing, which is not conducive to pre-allocating space. Moreover, when the problem scale is large, it requires more space for storage. To address this issue, we leverage the characteristic of optimal substructures in candidate point sets and set up an auxiliary array to store predecessor node indices instead of storing paths. As shown in
Figure 11.
It is also noted that the candidate point set has the following properties:
Property 1 : The subsequent adjacent points of a point in the candidate set extracted by the SGP path are continuous in the candidate set.
Proof of Property 1 (reductio ad absurdum) : Suppose that the subsequent adjacent points of a candidate point in the candidate set of a SGP path are discontinuous in the set, that is, there exists a continuous subsequence , where are adjacent points, A and C are not adjacent points, D is a subsequent candidate point of C, and A and D are adjacent points.
Because A and C are not adjacent points, the line connecting A and C must pass through some obstacle. The boundary points between A and this obstacle form two straight lines, a and b, as shown in
Figure 12. Since A and D are adjacent points, D should be located outside of a and b. If D is on the outside of a, due to the adjacency between A and D, the line AD does not pass through the obstacle or the wall gap, so there exists a shorter SGP between AD that does not need to bypass the obstacle between AC, which contradicts the definition that the SGP is the shortest path in 8 directions. Similarly, if D is on the outside of b, there also exists a shorter SGP, leading to another contradiction. In conclusion, A and D are not adjacent points, thus, Property 1 holds.□
Inference 1 : For any two nodes and () in the candidate point set , where the set of pre-adjacent points of is and the set of pre-adjacent points of is , if , then .
Proof of Inference 1 (Proof by Contradiction) : Assume and . According to Property 1, the set of adjacent points after is , and the set of adjacent points after is . Since and , it follows that , so and are adjacent points. Furthermore, since , , which contradicts . Therefore, Inference 1 holds.□
Inference 1 indicates that the boundary of the set of previous adjacent points for a path point will shift backward as the point moves further in the candidate point set. Therefore, a sliding window can be used to optimize dynamic programming algorithms, thereby improving algorithm efficiency. The sliding window is used to define the range of the set of previous adjacent points, and it gradually advances toward the endpoint with each iteration.
The pseudocode of the optimized dynamic programming algorithm is shown in
Figure 13.