0% found this document useful (0 votes)
67 views3 pages

Adsa TSP

Hi

Uploaded by

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

Adsa TSP

Hi

Uploaded by

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

The Travelling Salesperson Problem (TSP) is an optimization problem where a salesperson

must visit a given set of cities exactly once, starting and ending at the same city.

The goal is to find the shortest possible route that covers all the cities and returns to the
starting point.

TSP is an NP-hard problem, meaning there is no known efficient solution for large datasets,
but various algorithms can provide exact or approximate solutions.

Travelling Salesman Problem: Example


we have a salesperson who needs to visit four popular Indian cities: Mumbai, Delhi,
Bengaluru, and Chennai, and they want to find the shortest route that visits each city
exactly once and returns to the starting city.

The distances between the cities are:

Mumbai Delhi Bengaluru Chennai

Mumbai 0 1,400 km 980 km 1,330 km

Delhi 1,400 km 0 2,150 km 2,200 km

Bengalur 980 km 2,150 km 0 350 km


u

Chennai 1,330 km 2,200 km 350 km 0

Problem: The salesperson starts in Mumbai and must visit Delhi, Bengaluru, and
Chennai exactly once, then return to Mumbai. The goal is to find the shortest route.

Possible Routes: Let’s calculate the total distance for a few possible routes:

1. Mumbai → Delhi → Bengaluru → Chennai → Mumbai

 Mumbai → Delhi = 1,400 km


 Delhi → Bengaluru = 2,150 km
 Bengaluru → Chennai = 350 km
 Chennai → Mumbai = 1,330 km

Total distance = 1,400 + 2,150 + 350 + 1,330 = 5,230 km

2. Mumbai → Delhi → Chennai → Bengaluru → Mumbai

 Mumbai → Delhi = 1,400 km


 Delhi → Chennai = 2,200 km
 Chennai → Bengaluru = 350 km
 Bengaluru → Mumbai = 980 km

Total distance = 1,400 + 2,200 + 350 + 980 = 4,930 km

3. Mumbai → Bengaluru → Chennai → Delhi → Mumbai

 Mumbai → Bengaluru = 980 km


 Bengaluru → Chennai = 350 km
 Chennai → Delhi = 2,200 km
 Delhi → Mumbai = 1,400 km

Total distance = 980 + 350 + 2,200 + 1,400 = 4,930 km

The shortest routes are:

 Mumbai → Delhi → Chennai → Bengaluru → Mumbai with a total distance of


4,930 km.
 Mumbai → Bengaluru → Chennai → Delhi → Mumbai with a total distance of
4,930 km.

Ex. TSP using 8 cities

#include <iostream>

#include <vector>

#include <climits>

#include <cstring>

const int MAX_CITIES = 8;

vector<pair<int, int>> graph[MAX_CITIES];

bool visited[MAX_CITIES];

int dfs(int node) {

visited[node] = true;

int total_distance = 0;

for (auto& neighbor : graph[node]) {

int next_node = neighbor.first;

int weight = neighbor.second;

if (!visited[next_node]) {

total_distance += weight + dfs(next_node);

}
return total_distance;

int tsp_tree(int start)

memset(visited, false, sizeof(visited));

int total_distance = dfs(start);

return total_distance;

You might also like