0% found this document useful (0 votes)
148 views

Traveling Salesman Problem

The document discusses the Traveling Salesman Problem (TSP) which aims to find the shortest route for a salesman to visit a number of destinations. It describes TSP as an NP-complete problem where the time to calculate the optimal route increases greatly with more destinations. The document then summarizes several approaches to solve TSP, including the brute force, branch and bound, and nearest neighbor methods. It provides examples of naive and dynamic programming solutions in C++ code.

Uploaded by

Wili Channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Traveling Salesman Problem

The document discusses the Traveling Salesman Problem (TSP) which aims to find the shortest route for a salesman to visit a number of destinations. It describes TSP as an NP-complete problem where the time to calculate the optimal route increases greatly with more destinations. The document then summarizes several approaches to solve TSP, including the brute force, branch and bound, and nearest neighbor methods. It provides examples of naive and dynamic programming solutions in C++ code.

Uploaded by

Wili Channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Matakuliah: Algoritma Pemrograman II

Topik: Traveling Salesman Problem


Tanggal Pertemuan: Senin, 20 April 2020

Traveling Salesman Problem

The Travelling Salesman Problem (TSP) is the challenge of finding the shortest yet
most efficient route for a person to take given a list of specific destinations. It is a
well-known algorithmic problem in the fields of computer science and operations
research.

There are obviously a lot of different routes to choose from, but finding the best
one—the one that will require the least distance or cost—is what mathematicians and
computer scientists have spent decades trying to solve for.

TSP has commanded so much attention because it’s so easy to describe yet so difficult
to solve. In fact, TSP belongs to the class of combinatorial optimization problems known
as NP-complete. This means that TSP is classified as NP-hard because it has no
“quick” solution and the complexity of calculating the best route will increase when you
add more destinations to the problem.

The problem can be solved by analyzing every round-trip route to determine the
shortest one. However, as the number of destinations increases, the corresponding
number of roundtrips surpasses the capabilities of even the fastest computers. With 10
destinations, there can be more than 300,000 roundtrip permutations and combinations.
With 15 destinations, the number of possible routes could exceed 87 billion.

Here are some of the most popular solutions to the Traveling Salesman Problem:

The Brute-Force Approach

The Brute Force approach, also known as the Naive Approach, calculates and
compares all possible permutations of routes or paths to determine the shortest unique
solution. To solve the TSP using the Brute-Force approach, you must calculate the total
number of routes and then draw and list all the possible routes. Calculate the distance
of each route and then choose the shortest one—this is the optimal solution.

The Branch and Bound Method

This method breaks a problem to be solved into several sub-problems. It’s a system for
solving a series of sub-problems, each of which may have several possible solutions
and where the solution selected for one problem may have an effect on the possible
solutions of subsequent sub-problems. To solve the TSP using the Branch and Bound
method, you must choose a start node and then set bound to a very large value (let’s
say infinity). Select the cheapest arch between the unvisited and current node and then
add the distance to the current distance. Repeat the process while the current distance
is less then the bound. If the current distance is less than the bound, you’re done. You
may now add up the distance so that the bound will be equal to the current distance.
Repeat this process until all the arcs have been covered.

The Nearest Neighbor Method

This is perhaps the simplest TSP heuristic. The key to this method is to always visit the
nearest destination and then go back to the first city when all other cities are visited. To
solve the TSP using this method, choose a random city and then look for the closest
unvisited city and go there. Once you have visited all cities, you must return to the first
city.

Naive Solution 

1) Consider city 1 as the starting and ending point. 


2) Generate all (n-1)! Permutations of cities. 
3) Calculate cost of every permutation and keep track of minimum cost permutation. 
4) Return the permutation with minimum cost. 
Time Complexity: Θ(n!) 
Example Code in C++

// CPP program to implement traveling salesman


// problem using naive approach.
#include <bits/stdc++.h>
using namespace std;
#define V 4

// implementation of traveling Salesman Problem


int travllingSalesmanProblem(int graph[][V], int s)
{
// store all vertex apart from source vertex
vector<int> vertex;
for (int i = 0; i < V; i++)
if (i != s)
vertex.push_back(i);

// store minimum weight Hamiltonian Cycle.


int min_path = INT_MAX;
do {

// store current Path weight(cost)


int current_pathweight = 0;

// compute current path weight


int k = s;
for (int i = 0; i < vertex.size(); i++) {
current_pathweight += graph[k][vertex[i]];
k = vertex[i];
}
current_pathweight += graph[k][s];

// update minimum
min_path = min(min_path, current_pathweight);

} while (next_permutation(vertex.begin(), vertex.end()));

return min_path;
}

// driver program to test above function


int main()
{
// matrix representation of graph
int graph[][V] = { { 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 } };
int s = 0;
cout << travllingSalesmanProblem(graph, s) << endl;
return 0;
}

 
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 be cost(i), the cost of corresponding Cycle would be 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 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.

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 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. Space required is also exponential. So this approach is 
also infeasible even for slightly higher number of vertices. 

Resources:

https://blog.routific.com/travelling-salesman-problem
https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/
https://www.geeksforgeeks.org/traveling-salesman-problem-tsp-implementation/

You might also like