给定2D矩阵tsp [] [] ,其中每行具有从该索引城市到所有其他城市的距离的数组,并且-1表示在这两个索引城市之间不存在路径。任务是在TSP周期中打印最低成本。
例子:
Input:
tsp[][] = {{-1, 10, 15, 20},
{10, -1, 35, 25},
{15, 35, -1, 30},
{20, 25, 30, -1}};
Below is the given graph:
Output: 80
Explanation:
We are trying to find out the path/route with the minimum cost such that our aim of visiting all cities once and return back to the source city is achieved. The path through which we can achieve that, can be represented as 1 -> 2 -> 4 -> 3 -> 1. Here, we started from city 1 and ended on the same visiting all other cities once on our way. The cost of our path/route is calculated as follows:
1 -> 2 = 10
2 -> 4 = 25
4 -> 3 = 30
3 -> 1 = 15
(All the costs are taken from the given 2D Array)
Hence, total cost = 10 + 25 + 30 + 15 = 80
Input:
tsp[][] = {{-1, 30, 25, 10},
{15, -1, 20, 40},
{10, 20, -1, 25},
{30, 10, 20, -1}};
Output: 50
在上一篇文章中,我们介绍了旅行商问题,并讨论了该问题的朴素和动态编程解决方案。两种解决方案都不可行。实际上,由于该问题是已知的NP-Hard问题,因此没有多项式时间解可用于该问题。虽然有解决该问题的近似算法。
这个问题可能与哈密顿循环问题有关,在某种程度上,我们知道图中存在哈密顿循环,但是我们的工作是找到成本最小的循环。同样,在特定的TSP图中,可以有许多哈密顿循环,但我们只需要输出一个满足问题所需目标的循环即可。
方法:可以使用贪婪技术解决此问题。步骤如下:
- 创建两个主要数据持有人:
- 根据城市之间的距离输入矩阵来保存城市索引的列表。
- 结果数组,其中所有城市都可以以任何方式显示到控制台。
- 对所有城市在给定的邻接矩阵tsp [] []上进行遍历,如果从当前城市到达任何城市的成本小于当前成本,则更新成本。
- 使用以上步骤生成最小路径周期,并在此返回最小成本。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum
// cost path for all the paths
void findMinRoute(vector > tsp)
{
int sum = 0;
int counter = 0;
int j = 0, i = 0;
int min = INT_MAX;
map visitedRouteList;
// Starting from the 0th indexed
// city i.e., the first city
visitedRouteList[0] = 1;
int route[tsp.size()];
// Traverse the adjacency
// matrix tsp[][]
while (i < tsp.size() && j < tsp[i].size())
{
// Corner of the Matrix
if (counter >= tsp[i].size() - 1)
{
break;
}
// If this path is unvisited then
// and if the cost is less then
// update the cost
if (j != i && (visitedRouteList[j] == 0))
{
if (tsp[i][j] < min)
{
min = tsp[i][j];
route[counter] = j + 1;
}
}
j++;
// Check all paths from the
// ith indexed city
if (j == tsp[i].size())
{
sum += min;
min = INT_MAX;
visitedRouteList[route[counter] - 1] = 1;
j = 0;
i = route[counter] - 1;
counter++;
}
}
// Update the ending city in array
// from city which was last visited
i = route[counter - 1] - 1;
for (j = 0; j < tsp.size(); j++)
{
if ((i != j) && tsp[i][j] < min)
{
min = tsp[i][j];
route[counter] = j + 1;
}
}
sum += min;
// Started from the node where
// we finished as well.
cout << ("Minimum Cost is : ");
cout << (sum);
}
// Driver Code
int main()
{
// Input Matrix
vector > tsp = { { -1, 10, 15, 20 },
{ 10, -1, 35, 25 },
{ 15, 35, -1, 30 },
{ 20, 25, 30, -1 } };
// Function Call
findMinRoute(tsp);
}
// This code is contributed by grand_master.
Java
// Java program for the above approach
import java.util.*;
public class TSPGreedy {
// Function to find the minimum
// cost path for all the paths
static void findMinRoute(int[][] tsp)
{
int sum = 0;
int counter = 0;
int j = 0, i = 0;
int min = Integer.MAX_VALUE;
List visitedRouteList
= new ArrayList<>();
// Starting from the 0th indexed
// city i.e., the first city
visitedRouteList.add(0);
int[] route = new int[tsp.length];
// Traverse the adjacency
// matrix tsp[][]
while (i < tsp.length
&& j < tsp[i].length) {
// Corner of the Matrix
if (counter >= tsp[i].length - 1) {
break;
}
// If this path is unvisited then
// and if the cost is less then
// update the cost
if (j != i
&& !(visitedRouteList.contains(j))) {
if (tsp[i][j] < min) {
min = tsp[i][j];
route[counter] = j + 1;
}
}
j++;
// Check all paths from the
// ith indexed city
if (j == tsp[i].length) {
sum += min;
min = Integer.MAX_VALUE;
visitedRouteList.add(route[counter] - 1);
j = 0;
i = route[counter] - 1;
counter++;
}
}
// Update the ending city in array
// from city which was last visited
i = route[counter - 1] - 1;
for (j = 0; j < tsp.length; j++) {
if ((i != j) && tsp[i][j] < min) {
min = tsp[i][j];
route[counter] = j + 1;
}
}
sum += min;
// Started from the node where
// we finished as well.
System.out.print("Minimum Cost is : ");
System.out.println(sum);
}
// Driver Code
public static void
main(String[] args)
{
// Input Matrix
int[][] tsp = {
{ -1, 10, 15, 20 },
{ 10, -1, 35, 25 },
{ 15, 35, -1, 30 },
{ 20, 25, 30, -1 }
};
// Function Call
findMinRoute(tsp);
}
}
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class TSPGreedy{
// Function to find the minimum
// cost path for all the paths
static void findMinRoute(int[,] tsp)
{
int sum = 0;
int counter = 0;
int j = 0, i = 0;
int min = int.MaxValue;
List visitedRouteList = new List();
// Starting from the 0th indexed
// city i.e., the first city
visitedRouteList.Add(0);
int[] route = new int[tsp.Length];
// Traverse the adjacency
// matrix tsp[,]
while (i < tsp.GetLength(0) &&
j < tsp.GetLength(1))
{
// Corner of the Matrix
if (counter >= tsp.GetLength(0) - 1)
{
break;
}
// If this path is unvisited then
// and if the cost is less then
// update the cost
if (j != i &&
!(visitedRouteList.Contains(j)))
{
if (tsp[i, j] < min)
{
min = tsp[i, j];
route[counter] = j + 1;
}
}
j++;
// Check all paths from the
// ith indexed city
if (j == tsp.GetLength(0))
{
sum += min;
min = int.MaxValue;
visitedRouteList.Add(route[counter] - 1);
j = 0;
i = route[counter] - 1;
counter++;
}
}
// Update the ending city in array
// from city which was last visited
i = route[counter - 1] - 1;
for(j = 0; j < tsp.GetLength(0); j++)
{
if ((i != j) && tsp[i, j] < min)
{
min = tsp[i, j];
route[counter] = j + 1;
}
}
sum += min;
// Started from the node where
// we finished as well.
Console.Write("Minimum Cost is : ");
Console.WriteLine(sum);
}
// Driver Code
public static void Main(String[] args)
{
// Input Matrix
int[,] tsp = { { -1, 10, 15, 20 },
{ 10, -1, 35, 25 },
{ 15, 35, -1, 30 },
{ 20, 25, 30, -1 } };
// Function call
findMinRoute(tsp);
}
}
// This code is contributed by Amit Katiyar
Minimum Cost is : 80
时间复杂度: O(N 2 * log 2 N)
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。