Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)

23 Jun, 2022
Travelling Salesman Problem (TSP): 
Given a set of cities and the distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point. Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exists a tour that visits every city exactly once. Here we know that Hamiltonian Tour exists (because the graph is complete) and in fact, many such tours exist, the problem is to find a minimum weight Hamiltonian Cycle. 
 
For example, consider the graph shown in the figure on the right side. A TSP tour in the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80. The problem is a famous NP-hard problem. There is no polynomial-time know solution for this problem. The following are different solutions for the traveling salesman problem. 
Naive Solution: 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
1) Consider city 1 as the starting and ending point.
2) Generate all (n-1)! Permutations of cities. 
3) Calculate the cost of every permutation and keep track of the minimum cost permutation. 
4) Return the permutation with minimum cost. 
Time Complexity: Θ(n!) 
Dynamic Programming: 
Let the given set of vertices be {1, 2, 3, 4,....n}. Let us consider 1 as starting and ending point of output. For every other vertex I (other than 1), we find the minimum cost path with 1 as the starting point, I as the ending point, and all vertices appearing exactly once. Let the cost of this path cost (i), and the cost of the corresponding Cycle would cost (i) + dist(i, 1) where dist(i, 1) is the distance from I to 1. Finally, we return the minimum of all [cost(i) + dist(i, 1)] values. This looks simple so far. 
Now the question is how to get cost(i)? To calculate the cost(i) using Dynamic Programming, we need to have some recursive relation in terms of sub-problems. 
Let us define a term C(S, i) be the cost of the minimum cost path visiting each vertex in set S exactly once, starting at 1 and ending at i. We start with all subsets of size 2 and calculate C(S, i) for all subsets where S is the subset, then we calculate C(S, i) for all subsets S of size 3 and so on. Note that 1 must be present in every subset.
If size of S is 2, then S must be {1, i},
 C(S, i) = dist(1, i) 
Else if size of S is greater than 2.
 C(S, i) = min { C(S-{i}, j) + dis(j, i)} where j belongs to S, j != i and j != 1.
Below is the dynamic programming solution for the problem using top down recursive+memoized approach:-
For maintaining the subsets we can use the bitmasks to represent the remaining nodes in our subset. Since bits are faster to operate and there are only few nodes in graph, bitmasks is better to use.
For example: –  
10100 represents node 2 and node 4 are left in set to be processed
010010 represents node 1 and 4 are left in subset.
NOTE:- ignore the 0th bit since our graph is 1-based
Python3
C++
Java
n = 4  # there are four nodes in example graph (graph is 1-based) # dist[i][j] represents shortest distance to go from i to j# this matrix can be calculated for any given graph using# all-pair shortest path algorithmsdist = [[0, 0, 0, 0, 0], [0, 0, 10, 15, 20], [    0, 10, 0, 25, 25], [0, 15, 25, 0, 30], [0, 20, 25, 30, 0]] # memoization for top down recursionmemo = [[-1]*(1 << (n+1)) for _ in range(n+1)]  def fun(i, mask):    # base case    # if only ith bit and 1st bit is set in our mask,    # it implies we have visited all other nodes already    if mask == ((1 << i) | 3):        return dist[1][i]     # memoization    if memo[i][mask] != -1:        return memo[i][mask]     res = 10**9  # result of this sub-problem     # we have to travel all nodes j in mask and end the path at ith node    # so for every node j in mask, recursively calculate cost of    # travelling all nodes in mask    # except i and then travel back from node j to node i taking    # the shortest path take the minimum of all possible j nodes    for j in range(1, n+1):        if (mask & (1 << j)) != 0 and j != i and j != 1:            res = min(res, fun(j, mask & (~(1 << i))) + dist[j][i])    memo[i][mask] = res  # storing the minimum value    return res  # Driver program to test above logicans = 10**9for i in range(1, n+1):    # try to go from node 1 visiting all nodes in between to i    # then return from i taking the shortest route to 1    ans = min(ans, fun(i, (1 << (n+1))-1) + dist[i][1]) print("The cost of most efficient tour = " + str(ans)) # This code is contributed by Serjeel Ranjan
#include <iostream> using namespace std; // there are four nodes in example graph (graph is 1-based)const int n = 4;// give appropriate maximum to avoid overflowconst int MAX = 1000000; // dist[i][j] represents shortest distance to go from i to j// this matrix can be calculated for any given graph using// all-pair shortest path algorithmsint dist[n + 1][n + 1] = {    { 0, 0, 0, 0, 0 },    { 0, 0, 10, 15, 20 },    { 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 },    { 0, 20, 25, 30, 0 },}; // memoization for top down recursionint memo[n + 1][1 << (n + 1)]; int fun(int i, int mask){    // base case    // if only ith bit and 1st bit is set in our mask,    // it implies we have visited all other nodes already    if (mask == ((1 << i) | 3))        return dist[1][i];    // memoization    if (memo[i][mask] != 0)        return memo[i][mask];     int res = MAX; // result of this sub-problem     // we have to travel all nodes j in mask and end the    // path at ith node so for every node j in mask,    // recursively calculate cost of travelling all nodes in    // mask except i and then travel back from node j to    // node i taking the shortest path take the minimum of    // all possible j nodes     for (int j = 1; j <= n; j++)        if ((mask & (1 << j)) && j != i && j != 1)            res = std::min(res, fun(j, mask & (~(1 << i)))                                    + dist[j][i]);    return memo[i][mask] = res;}// Driver program to test above logicint main(){    int ans = MAX;    for (int i = 1; i <= n; i++)        // try to go from node 1 visiting all nodes in        // between to i then return from i taking the        // shortest route to 1        ans = std::min(ans, fun(i, (1 << (n + 1)) - 1)                                + dist[i][1]);     printf("The cost of most efficient tour = %d", ans);     return 0;} // This code is contributed by Serjeel Ranjan
import java.io.*;import java.util.*; public class TSE {    // there are four nodes in example graph (graph is    // 1-based)     static int n = 4;    // give appropriate maximum to avoid overflow     static int MAX = 1000000;     // dist[i][j] represents shortest distance to go from i    // to j this matrix can be calculated for any given    // graph using all-pair shortest path algorithms    static int[][] dist = {        { 0, 0, 0, 0, 0 },    { 0, 0, 10, 15, 20 },        { 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 },        { 0, 20, 25, 30, 0 },    };     // memoization for top down recursion     static int[][] memo = new int[n + 1][1 << (n + 1)];     static int fun(int i, int mask)    {        // base case        // if only ith bit and 1st bit is set in our mask,        // it implies we have visited all other nodes        // already        if (mask == ((1 << i) | 3))            return dist[1][i];        // memoization        if (memo[i][mask] != 0)            return memo[i][mask];         int res = MAX; // result of this sub-problem         // we have to travel all nodes j in mask and end the        // path at ith node so for every node j in mask,        // recursively calculate cost of travelling all        // nodes in mask        // except i and then travel back from node j to node        // i taking the shortest path take the minimum of        // all possible j nodes         for (int j = 1; j <= n; j++)            if ((mask & (1 << j)) != 0 && j != i && j != 1)                res = Math.min(res,                               fun(j, mask & (~(1 << i)))                                   + dist[j][i]);        return memo[i][mask] = res;    }     // Driver program to test above logic    public static void main(String[] args)    {        int ans = MAX;        for (int i = 1; i <= n; i++)            // try to go from node 1 visiting all nodes in            // between to i then return from i taking the            // shortest route to 1            ans = Math.min(ans, fun(i, (1 << (n + 1)) - 1)                                    + dist[i][1]);         System.out.println(            "The cost of most efficient tour = " + ans);    }} // This code is contributed by Serjeel Ranjan
The cost of most efficient tour = 80

For a set of size n, we consider n-2 subsets each of size n-1 such that all subsets don’t have nth in them. Using the above recurrence relation, we can write a dynamic programming-based solution. There are at most O(n*2n) subproblems, and each one takes linear time to solve. The total running time is therefore O(n2*2n). The time complexity is much less than O(n!) but still exponential. The space required is also exponential. So this approach is also infeasible even for a slightly higher number of vertices. We will soon be discussing approximate algorithms for the traveling salesman problem.
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming) | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersTravelling Salesman Problem | Set 1 (Naive and Dynamic Programming) | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:34•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=hvDx7q6vcWM" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Next Article: Traveling Salesman Problem | Set 2 
References: 
http://www.lsi.upc.edu/~mjserna/docencia/algofib/P07/dynprog.pdf 
http://www.cs.berkeley.edu/~vazirani/algorithms/chap6.pdf 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
raj2002
akashcherukuri007
serjeelranjan
Google
Microsoft
NPHard
Opera
Bit Magic
Dynamic Programming
Graph
Microsoft
Google
Opera
Dynamic Programming
Bit Magic
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.