0% found this document useful (0 votes)
16 views7 pages

AADA Expt-10

The document outlines an experiment to implement the Nearest Neighbor approximation algorithm for the Travelling Salesman Problem (TSP) in Java. It aims to provide an understanding of TSP's complexity, explore approximation algorithms, and analyze the quality of generated solutions. The expected outcome includes a working implementation that computes approximate tours and evaluates their total distance compared to optimal solutions.

Uploaded by

VIDIT SHAH
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)
16 views7 pages

AADA Expt-10

The document outlines an experiment to implement the Nearest Neighbor approximation algorithm for the Travelling Salesman Problem (TSP) in Java. It aims to provide an understanding of TSP's complexity, explore approximation algorithms, and analyze the quality of generated solutions. The expected outcome includes a working implementation that computes approximate tours and evaluates their total distance compared to optimal solutions.

Uploaded by

VIDIT SHAH
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/ 7

Somaiya Vidyavihar University

(Constituent College – K J Somaiya College of Engineering)

Batch: A Roll No.:16030724019

Experiment/assignment / tutorial
No. 10

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

Experiment No.: 10

Title: To Implement Approximation Algorithm for Travelling Salesman Problem

Objectives:
1. Understand the TSP and Its Complexity:
• Gain a clear understanding of the Travelling Salesman Problem (TSP),
including its real-world applications and its classification as an NP-hard
problem.
• Recognize why finding an exact solution to TSP is computationally expensive,
especially as the number of cities (nodes) increases.
2. Explore Approximation Algorithms:
• Investigate various approximation approaches for TSP, focusing on algorithms
that provide feasible solutions within a reasonable time for large instances.
• Specifically, understand the Nearest Neighbor algorithm and other heuristic
approaches, which can provide near-optimal solutions with lower computational
costs.
3. Implement the Nearest Neighbor Approximation Algorithm in Java:
• Develop a Java program that implements the Nearest Neighbor algorithm as an
approximation for solving TSP.
• Structure the code to start at an arbitrary city, iteratively select the nearest
unvisited city, and return to the starting city to complete the tour.

Expected Outcome of Experiment:

CO.

1. Working Implementation of Approximation Algorithm:


• You will successfully implement a working version of the Nearest Neighbor
approximation algorithm for solving the Travelling Salesman Problem (TSP) in
Java.
• The program will be able to compute a solution where the cities are visited in a
specific order, starting and ending at the same city, while following the

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

approximation algorithm’s strategy of always choosing the nearest unvisited


city.
2. Generation of Approximate Solutions for TSP:
• For small test cases, the program will generate approximate tours, where each
tour will be a valid sequence of cities visited exactly once, ending back at the
start.
• The total distance of the tour generated by the Nearest Neighbor algorithm will
be calculated.
3. Analysis of the Approximation Quality:
• You will observe the quality of the approximation in terms of the total tour
length. For smaller graphs, you can manually or programmatically compare the
results to the optimal solution if known.
• The total distance of the tour produced by the approximation algorithm will be
analyzed, and you should expect it to be suboptimal (longer than the optimal
route). However, the solution should still be reasonably close.

Books/ Papers/Websites referred:

1. https://www.geeksforgeeks.org/travelling-salesman-problem-using-dynamic-
programming/
2. https://www.tutorialspoint.com/data_structures_algorithms/travelling_salesman
_problem.htm
3. https://www.javatpoint.com/travelling-salesman-problem

Pre Lab/ Prior Concepts:

1. Graph Theory:
• Graph Representation:
Understand how to represent a graph as a set of vertices (nodes) and edges (connections
between nodes). For TSP, cities are vertices, and edges represent the distances between them.
Learn about adjacency matrices and adjacency lists, which are commonly used to represent the
distances between cities.
• Complete Graph:
TSP is defined on a complete graph, where each city is connected to every other city. In the
case of a complete graph, the number of edges is n(n−1)/2n(n-1)/2n(n−1)/2, where nnn is the
number of cities.
• Weighted Graph:

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

In TSP, the graph is weighted, meaning each edge has an associated weight (distance between
two cities). The goal is to find the shortest path that visits every city once and returns to the
starting point.
2. Travelling Salesman Problem (TSP):
• The Travelling Salesman Problem is a classical optimization problem where the goal is
to find the shortest possible route that visits every city exactly once and returns to the
starting city.
• NP-Hardness:
TSP is an NP-hard problem, meaning there is no known polynomial-time algorithm
that guarantees an optimal solution for large instances of the problem.
Understanding computational complexity and why exact solutions to TSP are
infeasible for large inputs.
3. Approximation Algorithms:
• Heuristic Methods:
Understand the concept of heuristics and approximation algorithms, which are methods
designed to find near-optimal solutions to NP-hard problems in a reasonable amount of time.
Approximation algorithms aim to provide a solution close to the optimal one with a guarantee
of how far the solution is from the optimal one.
• Nearest Neighbor Algorithm:
A basic approximation algorithm for TSP, where the salesman starts at a random city, and at
each step, visits the nearest unvisited city until all cities are visited. Understand the greedy
nature of this algorithm, which always makes the locally optimal choice (choosing the nearest
city) but may not lead to the globally optimal solution.
• Performance of Approximation Algorithms:
Understand how to measure the performance ratio of an approximation algorithm, which is the
ratio of the length of the solution found by the approximation algorithm to the length of the
optimal solution. The goal is to minimize this ratio.

Related Theory:
1. Travelling Salesman Problem (TSP):
The Travelling Salesman Problem (TSP) is a classical problem in optimization
and combinatorial mathematics. In TSP, you are given a set of cities, and the
task is to determine the shortest possible route that visits every city exactly once

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

and returns to the starting city. The problem is classified as NP-hard, meaning
no efficient algorithm is known that can solve the problem exactly for all cases
in polynomial time.
TSP can be modeled as a graph, where:
• Each city is represented as a vertex (node).
• Each path between cities is represented as an edge, and the weight of the edge is
the distance between two cities.
The goal of the TSP is to find the Hamiltonian cycle (a cycle that visits each
city exactly once and returns to the start) with the minimum total weight
(distance).
2. Computational Complexity:
• NP-Hard Problem: TSP is a problem for which no polynomial-time algorithm is
known. As the number of cities increases, the number of possible routes grows
factorially (O(n!)), making it computationally infeasible to calculate the optimal
solution for large instances.
• Exact Algorithms:
o Brute Force: Check all possible permutations of cities to find the
minimum route. The time complexity is O(n!), which is impractical for
large n.
o Dynamic Programming (Held-Karp algorithm): This provides an exact
solution with a time complexity of O(n^2 * 2^n), which is still
exponential but more efficient than brute force.

3. Approximation Algorithms:

• Due to the NP-hardness of the TSP, approximation algorithms are often used to
find near-optimal solutions in a reasonable amount of time, especially for large
problem instances. These algorithms do not guarantee an optimal solution but
provide solutions that are close enough.
• The goal of an approximation algorithm is to produce a solution that is close to
the optimal one but with much lower computational effort. The performance of
an approximation algorithm is usually measured in terms of its approximation
ratio.

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

4. Approximation Ratio:
• An approximation algorithm has an approximation ratio that compares the
solution it finds to the optimal solution. The approximation ratio is the ratio of
the length of the tour found by the algorithm to the length of the optimal tour.
• For example, a 2-approximation algorithm for TSP guarantees that the length of
the tour produced will be at most twice the length of the optimal solution.

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

Implementation details:

Code:
def approximate_tsp(start, distance_matrix):
n = len(distance_matrix)
visited = [False] * n
tour = []
current_city = start
visited[current_city] = True
tour.append(current_city)

for _ in range(1, n):


next_city = -1
min_distance = float('inf')
for j in range(n):
if not visited[j] and distance_matrix[current_city][j] <
min_distance:
next_city = j
min_distance = distance_matrix[current_city][j]

visited[next_city] = True
tour.append(next_city)
current_city = next_city

tour.append(start)
return tour

def calculate_tour_distance(tour, distance_matrix):


total_distance = 0
for i in range(len(tour) - 1):
total_distance += distance_matrix[tour[i]][tour[i + 1]]
return total_distance

def main():
distance_matrix = [
[0, 10, 15, 20, 25],
[10, 0, 35, 25, 30],
[15, 35, 0, 30, 20],
[20, 25, 30, 0, 15],
[25, 30, 20, 15, 0]
]

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

start_city = 0
tour = approximate_tsp(start_city, distance_matrix)
print("Tour:", tour)
print("Total Distance:", calculate_tour_distance(tour,
distance_matrix))

if __name__ == "__main__":
main()

Output:
PS D:\notes\College\M.Tech\sem-1\AADA\practical> python -u
Tour: [0, 1, 3, 4, 2, 0]
Total Distance: 85

Conclusion:
In conclusion, the Nearest Neighbor algorithm provides a practical approximation for
the Travelling Salesman Problem (TSP), balancing efficiency with reasonable solution
quality. With a time complexity of O(n²), it can handle larger instances, though it may
yield suboptimal solutions due to its greedy nature. Despite this, it remains useful for
approximating TSP solutions when exact methods are computationally impractical.

Date: Signature of faculty in-charge

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024

You might also like