0% found this document useful (0 votes)
55 views4 pages

Solve Traveling Salesman Problem in Python

The document outlines a task to solve the Traveling Salesman Problem (TSP) using Python, detailing the problem's nature as an NP-hard combinatorial optimization challenge. It describes various algorithms for solving TSP, including brute force and dynamic programming approaches, and provides a sample code implementation using the brute force method. The aim is to find the shortest route that visits all cities exactly once and returns to the origin city.

Uploaded by

neerajaboya21
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)
55 views4 pages

Solve Traveling Salesman Problem in Python

The document outlines a task to solve the Traveling Salesman Problem (TSP) using Python, detailing the problem's nature as an NP-hard combinatorial optimization challenge. It describes various algorithms for solving TSP, including brute force and dynamic programming approaches, and provides a sample code implementation using the brute force method. The aim is to find the shortest route that visits all cities exactly once and returns to the origin city.

Uploaded by

neerajaboya21
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

Artificial Intelligence & Expert Systems Lab Roll No :234G1A05B2

Task 2:

Write a Program to find the solution for traveling salesman Problem.

Aim : To Find the solution for traveling salesman Problem using Python.

Description: The travelling salesman problem is a graph computational problem where


the salesman needs to visit all cities (represented using nodes in a graph) in a list just once and
the distances (represented using edges in the graph) between all these cities are known. The
solution that is needed to be found for this problem is the shortest possible route in which the
salesman visits all the cities and returns to the origin city.

The Traveling Salesman Problem (TSP) is a well-known combinatorial optimization problem


in computer science and artificial intelligence (AI). It involves finding the shortest possible route
that allows a salesman to visit N cities exactly once and return to the starting point. The challenge
lies in the exponential increase in possible routes as the number of cities grows, making TSP a
complex NP-hard problem.

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.

• Input: A list or matrix of distances (or costs) between n cities.

• Goal: Find the shortest route that:

• Starts at a city
• Visits each of the other cities exactly once
• Returns to the starting city

Algorithms Followed:

• Brute Force Approach: The brute force method checks all possible permutations of routes
and selects the shortest one. While it guarantees the optimal solution, its O(N!) complexity
makes it impractical for large datasets.
• Dynamic Programming (Held-Karp Algorithm): The Held-Karp algorithm (also known
as the Bellman-Held-Karp algorithm) improves effciency using memoization and state
transition equations. It reduces complexity to O(N: x2M) but still becomes impractical for
large-scale problems.

Department of Computer Science and Engineering 1


Artificial Intelligence & Expert Systems Lab Roll No :234G1A05B2

Solving Travelling Salesman Problem using Simple Approach

In the following approach, we will solve the problem using the steps mentioned below:

Step 1: Firstly, we will consider City 1 as the starting and ending point. Since the route is cyclic,
any point can be considered a starting point.

Step 2: As the second step, we will generate all the possible permutations of the cities, which are
(n-1)!

Step 3: After that, we will find the cost of each permutation and keep a record of the minimum
cost permutation.

Step 4: At last, we will return the permutation with minimum cost.

1. Brute Force Approach

• Time Complexity:O(NI) - Examines all possible routes, making it impractical for large
inputs.
• Space Complexity:O(N) - Stores path permutations.

[Link] Programming(Held-Karp Algorithm)

• ·Time Complexity: O(N: x2M)-More effcient but still exponential.


• ·Space Complexity:O(N x2) -Requires large memory.

3. AI-Based Methods

• Genetic Algorithm: O(N:)(varies based on iterations).

• Ant Colony Optinization: O(N:x Iterations)-Faster convergence.

Source Code : Brute Force Approach (Exact Algorithm)

from itertools import permutations def

calculate_distance(route, distances):

total_distance = 0 for i in

range(len(route) - 1):

Department of Computer Science and Engineering 2


Artificial Intelligence & Expert Systems Lab Roll No :234G1A05B2

total_distance += distances[route[i]][route[i + 1]]

total_distance += distances[route[-1]][route[0]]

return total_distance def brute_force_tsp(distances):

n = len(distances)

cities = list(range(1, n)) shortest_route = None

min_distance = float('inf') for perm in permutations(cities):

current_route = [0] + list(perm) current_distance =

calculate_distance(current_route, distances)

if current_distance < min_distance:

min_distance = current_distance

shortest_route = current_route

shortest_route.append(0) return

shortest_route, min_distance

# Input: Distance Matrix

distances = [

[0, 2, 2, 5, 6, 3],

[0, 0, 4, 5, 7, 8],

[0, 4, 9, 8, 6, 3],

[0, 6, 8, 0, 4, 9],

[0, 7, 6, 4, 0, 10],

[0, 8, 3, 9, 4, 5]

Department of Computer Science and Engineering 3


Artificial Intelligence & Expert Systems Lab Roll No :234G1A05B2

# Output route, total_distance =

brute_force_tsp(distances) print("Route:", route)

print("Total distance:", total_distance)

Output :

Conclusion :

Thus,I have successfully implemented a Program to find the solution for traveling
salesman Problem.

Department of Computer Science and Engineering 4

You might also like