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

Assignment No 2

The Traveling Salesman Problem (TSP) is relevant in various real-life scenarios such as logistics, ride-hailing, and tourism, where optimizing travel routes is crucial. The document illustrates a TSP example involving four cities and provides a brute force algorithm implemented in Python to find the optimal route and minimum distance. The output of the algorithm shows the best route and the total distance for the journey.

Uploaded by

bushrajalil88
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)
3 views

Assignment No 2

The Traveling Salesman Problem (TSP) is relevant in various real-life scenarios such as logistics, ride-hailing, and tourism, where optimizing travel routes is crucial. The document illustrates a TSP example involving four cities and provides a brute force algorithm implemented in Python to find the optimal route and minimum distance. The output of the algorithm shows the best route and the total distance for the journey.

Uploaded by

bushrajalil88
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/ 6

Assignment no # 2

QUESTION#1
Travelling sales man problem appears in different real life scenarios mention these.

Solution:
The Traveling Salesman Problem (TSP) appears in many real-life scenarios where
optimizing a route or minimizing travel costs is essential. Here are some common examples:

1. Logistics and Delivery Services

2. Ride-Hailing and Taxi Services

3. Circuit Board Manufacturing

4. Tourism and Sightseeing

5. Airline Route Planning

6. Drone-Based Delivery

7. Sales Representative Route Optimization

8. Garbage Collection

10. Robotics and Path Planning

11. School Bus Routing

Here I am taking a scenario from logistics and delivery service. Companies like Amazon,
FedEx, or DHL need to plan optimal delivery routes to minimize distance, time, and fuel
costs for their delivery trucks or drones.

EXAMPLE:
A salesman has to travel to all these 4 cities, his starting point is A
and he has to visit all the remaining 3 cities atleast once and then
he has to come back to the starting city.
We can represent this as a graph
where:
 Nodes represent the locations (including the node A).
 Edges represent the paths between the locations, and the
weights on the edges represent the distance (or time, cost)
between the two locations.
Graph Representation
Locations (Nodes):
1. Customer A (A)
2. Customer B (B)
3. Customer C (C)
4. Customer D (D)

As a matrix to check the cost of travelling:


s
By using brute force we are drawing a hierarchy chart

B C D

C D B D B C

D C D B C B

A A A A A A

Steps
Step 1: Generate All Possible Routes (Permutations)

Using brute force, we will generate all possible routes starting from A and visiting all other
nodes exactly once, then returning to A. There are (n−1)!(n - 1)!(n−1)! possible routes, where
n is the number of nodes (locations). In our case, for 4 nodes, we have (4−1)!=3!=6(4 - 1)! = 3!
= 6(4−1)!=3!=6 possible routes.
Step 2: Calculate Total Distance for Each Route.
Step 3: Find the Optimal Solution.
By using brute force we will find out the optimal force.

Implementing algorithm using python


Code
from itertools import permutations

# Function to calculate total distance of a route


def calculate_distance(route, distance_matrix):
total_distance = 0
for i in range(len(route) - 1):
# Add distance between consecutive cities
total_distance += distance_matrix[route[i]][route[i + 1]]
# Add distance to return to the starting city
total_distance += distance_matrix[route[-1]][route[0]]
return total_distance

# Function to solve the Traveling Salesman Problem


def solve_tsp(cities, distance_matrix):
all_routes = list(permutations(cities[1:])) # Generate all possible routes, excluding starting
city
best_route = None
min_distance = float('inf') # Set initial minimum distance as infinity

for route in all_routes:


# Add starting city to the beginning and end of the route
route = (cities[0],) + route + (cities[0],)
current_distance = calculate_distance(route, distance_matrix)

if current_distance < min_distance: # If current route is shorter, update


min_distance = current_distance
best_route = route

return best_route, min_distance

# Distance matrix for 4 locations (a, b, c, d)


distance_matrix = {
'a': {'a': 0, 'b': 10, 'c': 15, 'd': 20},
'b': {'a': 10, 'b': 0, 'c': 35, 'd': 25},
'c': {'a': 15, 'b': 35, 'c': 0, 'd': 30},
'd': {'a': 20, 'b': 25, 'c': 30, 'd': 0}
}

# List of cities (represented as variables)


cities = ['a', 'b', 'c', 'd']

# Solve the TSP problem


best_route, min_distance = solve_tsp(cities, distance_matrix)

# Output the results


print("Best Route:", best_route)
print("Minimum Distance:", min_distance)

Output
Best Route: ('a', 'b', 'd', 'c', 'a')
Minimum Distance: 80

You might also like