我希望使用Python中的动态规划算法来解决TSP问题。该问题如下:
- 输入:表示为点列表的城市。例如,[(1,2), (0.3, 4.5), (9, 3)...]。城市之间的距离定义为欧几里得距离。
- 输出:此实例的旅行商最小成本,向下舍入到最近的整数。
伪代码如下:
Let A = 2-D array, indexed by subsets of {1, 2, ,3, ..., n} that contains 1 and destinations j belongs to {1, 2, 3,...n}
1. Base case:
2. if S = {0}, then A[S, 1] = 0;
3. else, A[S, 1] = Infinity.
4.for m = 2, 3, ..., n: // m = subproblem size
5. for each subset of {1, 2,...,n} of size m that contains 1:
6. for each j belongs to S and j != 1:
7. A[S, j] = the least value of A[S-{j},k]+the distance of k and j for every k belongs to S that doesn't equal to j
8.Return the least value of A[{1,2..n},j]+the distance between j and 1 for every j = 2, 3,...n.
我的困惑是:
如何使用子集索引列表,即如何有效地实现伪代码中的第5行。
d[(1, 2, 3)] = somevalue
是完全有效的。 - Konstantin