📜  旅行商问题 |贪婪的方法

📅  最后修改于: 2021-10-26 05:16:23             🧑  作者: Mango

给定一个二维矩阵tsp[][] ,其中每一行都有从该索引城市到所有其他城市的距离数组, -1表示这两个索引城市之间不存在路径。任务是在 TSP 循环中打印最小成本。
例子:

我们在上一篇文章中介绍了旅行商问题并讨论了该问题的朴素和动态规划解决方案。这两种解决方案都不可行。事实上,这个问题没有多项式时间解,因为这个问题是一个已知的 NP-Hard 问题。不过,有一些近似算法可以解决这个问题。
这个问题可能与哈密顿圈问题有关,在某种程度上,我们知道图中存在哈密顿圈,但我们的工作是找到成本最低的圈。此外,在特定的 TSP 图中,可以有许多哈密顿循环,但我们只需要输出一个满足我们要求的问题目标的循环。
方法:这个问题可以使用贪心技术来解决。以下是步骤:

  1. 创建两个主要数据持有者:
    • 根据城市之间的距离输入矩阵保存城市索引的列表。
    • 结果数组,其中包含可以以任何方式显示到控制台的所有城市。
  2. 对所有城市的给定邻接矩阵tsp[][]执行遍历,如果从当前城市到达任何城市的成本小于当前成本,则更新成本。
  3. 使用上述步骤生成最小路径循环并返回最小成本。

下面是上述方法的实现:

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)