Data Structures Algorithms 简明教程

Travelling Salesman Problem (Greedy Approach)

旅行商问题是图论中的一个计算问题,其中旅行商需要访问列表中的所有城市(表示为图中节点),并且已知所有城市之间的距离(表示为图中各边)。此问题需要找到一条最短路径,路径中旅行商访问了所有城市并返回至出发城市。

The travelling salesman problem is a graph computational problem where the salesman needs to visit all cities (represented using nodes in a graph) in a list just once and the distances (represented using edges in the graph) between all these cities are known. The solution that is needed to be found for this problem is the shortest possible route in which the salesman visits all the cities and returns to the origin city.

请看下方的图,假设旅行商从顶点“a”出发,他们需要访问剩余的顶点 b、c、d、e、f,并回到“a”,同时确保花费最小。

If you look at the graph below, considering that the salesman starts from the vertex ‘a’, they need to travel through all the remaining vertices b, c, d, e, f and get back to ‘a’ while making sure that the cost taken is minimum.

salesman graph

有各种方法可以找到旅行商问题的解决方案:朴素方法、贪婪方法、动态规划方法等。在本教程中,我们将学习如何使用贪婪方法解决旅行商问题。

There are various approaches to find the solution to the travelling salesman problem: naive approach, greedy approach, dynamic programming approach, etc. In this tutorial we will be learning about solving travelling salesman problem using greedy approach.

Travelling Salesperson Algorithm

如贪婪方法所定义,我们需要找到局部最优解决方案,以找出全局最优解决方案。算法采用的输入是图 G {V, E},其中 V 是顶点集,E 是边集。图 G 从一个顶点出发再回到同一顶点的最短路径作为输出。

As the definition for greedy approach states, we need to find the best optimal solution locally to figure out the global optimal solution. The inputs taken by the algorithm are the graph G {V, E}, where V is the set of vertices and E is the set of edges. The shortest path of graph G starting from one vertex returning to the same vertex is obtained as the output.

Algorithm

  1. Travelling salesman problem takes a graph G {V, E} as an input and declare another graph as the output (say G’) which will record the path the salesman is going to take from one node to another.

  2. The algorithm begins by sorting all the edges in the input graph G from the least distance to the largest distance.

  3. The first edge selected is the edge with least distance, and one of the two vertices (say A and B) being the origin node (say A).

  4. Then among the adjacent edges of the node other than the origin node (B), find the least cost edge and add it onto the output graph.

  5. Continue the process with further nodes making sure there are no cycles in the output graph and the path reaches back to the origin node A.

  6. However, if the origin is mentioned in the given problem, then the solution must always start from that node only. Let us look at some example problems to understand this better.

Examples

考虑以下具有六个城市及城市之间距离的图形 -

Consider the following graph with six cities and the distances between them −

graph six cities

从给定的图中,由于已经提到了起始点,所以解决方案必须始终从该节点开始。在从 A 出发的边中,A → B 具有最短距离。

From the given graph, since the origin is already mentioned, the solution must always start from that node. Among the edges leading from A, A → B has the shortest distance.

graph a to b

然后,B → C 具有它们之间最短且唯一的边,因此它被包含在输出图中。

Then, B → C has the shortest and only edge between, therefore it is included in the output graph.

graph b to c

在 C → D 之间只有一个边,因此它被添加到输出图中。

There’s only one edge between C → D, therefore it is added to the output graph.

graph c to d

D 有两条向外边。即使 D → B 比 D → E 距离更短,B 也已被访问过一次,如果将其添加到输出图中,它将形成一个环。因此,D → E 被添加到输出图中。

There’s two outward edges from D. Even though, D → B has lower distance than D → E, B is already visited once and it would form a cycle if added to the output graph. Therefore, D → E is added into the output graph.

graph d to e

从 e 只有一个边,即 E → F。因此,它被添加到输出图中。

There’s only one edge from e, that is E → F. Therefore, it is added into the output graph.

graph e to f

同样,即使 F → C 比 F → A 距离更短,F → A 也被添加到输出图中,以避免形成环,并且 C 已被访问过一次。

Again, even though F → C has lower distance than F → A, F → A is added into the output graph in order to avoid the cycle that would form and C is already visited once.

graph f to a

从 A 开始并结束于 A 的最短路径是 A → B → C → D → E → F → A

The shortest path that originates and ends at A is A → B → C → D → E → F → A

路径的成本是:16 + 21 + 12 + 15 + 16 + 34 = 114。

The cost of the path is: 16 + 21 + 12 + 15 + 16 + 34 = 114.

即使如果路径从其他节点开始,路径的成本可能会降低,但问题并未就此提出。

Even though, the cost of path could be decreased if it originates from other nodes but the question is not raised with respect to that.

Example

下面给出了使用贪心算法实现旅行商问题的完整代码 -

The complete implementation of Travelling Salesman Problem using Greedy Approach is given below −