dynamic programming : traversal of cities

醉酒当歌 提交于 2019-12-06 03:13:04

Q:: Two-Person Traversal of a Sequence of Cities: You are given an ordered sequence of n cities, and the distances between every pair of cities. Design an algorithm to partition the cities into two subsequences (not necessarily contiguous) such that person A visits all cities in the first subsequence (in order), person B visits all cities in the second subsequence (in order), and the sum of the total distances travelled by A and B is minimised. Assume that person A and person B start initially at the first city in their respective subsequences.

Here is how I understood the solution ::

Let us say the cities are number from 1 to n. We recurse on C(i, j), the minimum distance traveled if person A ends at city i and person B ends at city j. Assume without loss of generality i < j.

Let C(0, n) denote that person A does not visit any city, while person B visits all the cities from [1, n].

Hence, C(0, j) = d(1,2) + d(2,3) + .... + d(j-1, j) (B starting from city 1, going in order till city j).

C(i, j), where i is not 0 = minimum of following two cases :

case 1 : person A starts and stops at city i. In which case, C(i, j) = distance travelled by person B, travelling to all cities from 1 to j in order, skipping city i = d(1,2) + d(2,3) + ... + d(i-1, i+1) + d(i+1, i+2) + ... + d(j-1, j)

case 2 : person A starts at some city before i, and hence there is a city k which he travels just before going to city i (second last city in his traversal). In this case, C(i, j) = minimum {C(k, j) + d(k, i)} where k can vary from 1 to i-1.

The solution to the problem is minimum { C(i,n) } where i varies from 0 to n-1.

We can fill the DP matrix in row major order, as each calculation of C(i,j) requires either the distances d, or C(k,j) where k < i.

The running time of the algorithm will be O(n^3), as there are O(n^2) entries for which we do the calculation, and each calculation takes O(n) time.

Edit :: I think the solution given in the handout is missing case1.

You're right that the proposed solution is somewhat messy and incorrect.

The way to think about the problem is, as always, inductive: If I need to solve the problem of size n, how can I reduce it to smaller problems (0,..., n-1). If you'd like to solve this problem for size n, you note that one of the players needs to take the node n into their path.

The C[i, j] function is a helper function denoting as you described "minimal cost with A stopping at i and B stopping at j".

To arrive to the state C[i,j] player B had to come to j from j-1, unless of course j-1 = i. In the case that j-1 = i, then j had to come from some k < i. Therefore the function C[i, j] can be described as follows:

C[i,j] = {
   C[i,j-1] + d[j-1,j]                   (if i < j-1)
   min C[k,i] + d[k,j] for 0 <= k < i    (if i = j-1)
}

With this setting your base case is simply C[0, 1] = d[0,1].

Your final answer is then min C[k, n] for 0 <= k < n.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!