You are on page 1of 18

A

Mini Project Report

On

“Different exact and approximation algorithms for Travelling-Sales


Person Problem”

Submitted By

Jadhav Rahul Kalidas 4101079

Under The Guidance of


Mrs. A. R. Kamble

Department of Computer Engineering

Sinhgad Institute of Technology and Science,


Narhe, Pune-41
Savitribai Phule Pune University

2023-2024
Department of Computer Engineering,
Sinhgad Institute of Technology and Science, Narhe

CERTIFICATE

This is to certify that

Jadhav Rahul Kalidas 4101079


studying in BE Computer Engineering Course SEM-VII has successfully completed
his Laboratory Practice III - Design and Analysis of Algorithms Mini Project Work
titled “Different exact and approximation algorithms for Travelling-SalesPerson
Problem” at Sinhgad Institute of Technology and Science, Narhe in the partial
fulfillment of the Bachelor’s Degree in Engineering of Savitribai Phule Pune
University, during the Academic Year 2023-2024.

Mrs. A. R. Kamble Dr. G. S. Navale Dr. S. D. Markande

Guide Head of the Department Principal


Content

Sr No. Title Page No.

1. Introduction 1

2. Scope 3

3. Terminologies 4

4. Requirements (Hardware, Software) 8

5. Implementation 9

6. Output 13

7. Conclusion 14
1. INTRODUCTION

Introduction:

Algorithm analysis is an important part of computational complexity theory, which provides theoretical
estimation for the required resources of an algorithm to solve a specific computational problem.
Analysis of algorithms is the determination of the amount of time and space resources required to
execute it.

Why Analysis of Algorithms is important?

• To predict the behavior of an algorithm without implementing it on a specific computer.


• It is much more convenient to have simple measures for the efficiency of an algorithm than to
implement the algorithm and test the efficiency every time a certain parameter in the underlying
computer system changes.
• It is impossible to predict the exact behavior of an algorithm. There are too many influencing
factors.
• The analysis is thus only an approximation; it is not perfect.
• More importantly, by analyzing different algorithms, we can compare them to determine the
best one for our purpose.

Types of Algorithm Analysis:

1. Best case: Best-case analysis, covers algorithm’s fastest time to complete any problem with n
elements as input. It covers the case, that an algorithm’s runtime for n is determined by its best growth
factor and that the growth factor is finite for input with n elements

2. Worst case: Worst-case analysis, covers the algorithm’s longest time to complete any problem with
n elements as input. It covers the case, where an algorithm’s runtime for n is determined by its worst
growth factor and that the growth factor is finite for input with n elements.

3. Average case: Average case analysis covers the average time for an algorithm to complete any
problem with n elements as input. It covers the case where an algorithm’s runtime for n lies in between
its own upper and lower boundary growth factors for any sizes of n. The average case depends on a
probability distribution for all input of a given size n.

1
An Algorithm is a sequence of steps to solve a problem. Design and Analysis of Algorithm is very
important for designing algorithm to solve different types of problems in the branch of computer science
and information technology. This mini project introduces the fundamental concepts of Designing
Strategies, Complexity analysis of Algorithms, followed Merge Sorting method.

1.2 Motivation:

The advancement in science and technology enhance the performance of processor, which
proportionally affect the characteristics of computer system, such as security, scalability and reusability.
Important problems such as sorting, searching, string processing, graph problems, Combinational
problems, numerical problems are basic motivations for designing algorithm.

1.3 Objectives:

The Basic objective of solving problem with multiple constraints such as problem size performance and
cost in terms of space and time. The goal is to design fast, efficient and effective solution to a problem
domain. Some problems are easy to solve and some are hard. Quite cleverness is required to design
solution with fast and better approach. Designing new system need a new technology and background
of the new technology is the enhancement of existing algorithm. The study of algorithm is to design
efficient algorithm not only limited in reducing cost and time but to enhance scalability, reliability and
availability.
In this work, merge sort and multi threaded merge sort algorithms are compared and analyzed in relation
to their sequential counterparts, using the Python platform. Through this, find the potential speedup for
multi threading and what factors affects the performance. In addition, provide source code for multi
threaded algorithms with proven time complexities.

2
2. SCOPE

Project Scope:

This mini project aims at introducing a new sorting algorithm which sorts the elements of an array In
Place. This algorithm has O(n) best case Time Complexity and O(n log n) average and worst case Time
Complexity. We achieve our goal using Recursive Partitioning combined with In Place merging to sort
a given array. A comparison is made between this particular idea and other popular implementations.
We finally draw out a conclusion and observe the cases where this outperforms other sorting algorithms.
We also look at its shortcomings and list the scope for future improvements that could be made.

The goal of multi threading merge sort is to describe in details of algorithm for the scope of parallel
implementation, we trigger two sub-calls to mergesort(): one to sort the first half of the array and another
one for the second half.Obviously, in this implementation, nothing is parallelized yet and one thread is
going to perform the whole sorting job.To implement a parallel solution, we have first to limit the
number of threads. As merge sort is solely CPU-bound, the most optimal number of threads to manage
the execution is something close to the number of CPU cores. Hence, we need a thread pool to manage
this execution.

3
3. TERMINOLOGIES

A lay man perceives that a computer perform anything and everything. It is very difficult to ensure that
it is not really the computer but the man behind computer who does the whole thing. For example users
just enter their queries and can get information as he/she desire. A common man rarely understands that
a man made procedure called search has done the entire task and the only support provided by the
computer is the execution speed and organized storage information.

‘Algorithm’ is defined after the name of Abu Ja’ far Muhammad ibn Musa Al-Khwarizmi, Ninth century
, al-jabr means “restoring” referring to the process of moving a subtracted quantity to other side of an
equation; al-muqabala is “comparing” and refers to subtracting equal quantities from both sides of an
equation.

Definitions of Algorithm

 An algorithm is a set of rules for carrying out calculation either by hand or on a machine.

 An algorithm is a sequence of computational steps that transform the input into the output.

 An algorithm is a sequence of operations performed on data that have to be organized in the data
structures.

 An algorithm is an abstraction of a program to be executed on a physical machine (model


computation).

 An algorithm is defined as set of instructions to perform a specific task within finite no. of steps.

 Algorithm is defined as a step by step procedure to perform a specific task within finite number of
steps.

4
Travelling Salesman Problem(TSP):

Given a set of cities and the distance between every pair of cities, the problem is to find the
shortest possible route that visits every city exactly once and returns to the starting point. Note
the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find
if there exists a tour that visits every city exactly once. Here we know that
Hamiltonian Tour exists (because the graph is complete) and in fact, many such tours exist,
the problem is to find a minimum weight Hamiltonian Cycle.

Naive Solution:

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

2) Generate all (n-1)! Permutations of cities.

3) Calculate the cost of every permutation and keep track of the minimum cost permutation.

4) Return the permutation with minimum cost.

Time Complexity: Θ(n!)

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

5
For maintaining the subsets we can use the bitmasks to represent the remaining nodes in our
subset. Since bits are faster to operate and there are only few nodes in graph, bitmasks is
better to use.

How is this algorithm 2-approximate? :-

The cost of the output produced by the above algorithm is never more than twice the cost of
best possible output. Let us see how is this guaranteed by the above algorithm.

Let us define a term full walk to understand this. A full walk is lists all vertices when they are
first visited in preorder, it also list vertices when they are returned after a subtree is visited in
preorder. The full walk of above tree would be 1-2-1-4-1-3-1.

Following are some important facts that prove the 2-approximateness.

The cost of best possible Travelling Salesman tour is never less than the cost of MST. (The
definition of MST says, it is a minimum cost tree that connects all vertices).

The total cost of full walk is at most twice the cost of MST (Every edge of MST is visited
atmost twice)

The output of the above algorithm is less than the cost of full walk. In above algorithm, we
print preorder walk as output. In preorder walk, two or more edges of full walk are replaced

6
with a single edge. For example, 2-1 and 1-4 are replaced by 1 edge 2-4. So if the graph
follows triangle inequality, then this is always true.

From the above three statements, we can conclude that the cost of output produced by the
approximate algorithm is never more than twice the cost of best possible solution.

Travelling SalesMan Time and Space Complexity:

We have recursive calls here as well as loops. In fact we have recursive call inside loops.

It can be derived that the space complexity will be O(V^2) where V is the number of nodes/cities
here.

For the time complexity we need to look into a little bit deeper.

As we can see we have a recurrance relation here in terms of recursion, which is a subproblem and
each subproblem takes linear time to get the output,i.e. O(V * 2^V).This recursive call happens inside
a loop havinbg runtime of O(V). Hence we have a total runtime of O(V^2 * 2^V), which is exponential.

7
4. REQUIREMENTS

Minimum Hardware and Software requirements:


Below is a list of the minimum Hardware and Software requirements to access the basic
website content.

Operating System:
• Windows 7, Windows 8 or Windows 10
• Mac OSX 10.8, 10.9, 10.10 or 10.11

Hardware:
• Processor (CPU) with 2 gigahertz (GHz) frequency or above
• A minimum of 2 GB of RAM
• Monitor Resolution 1024 X 768 or higher
• A minimum of 20 GB of available space on the hard disk
• Internet Connection Broadband (high-speed) Internet connection with a speed of 4 Mbps or
higher

Software Requirement:
VS Code
Python 3.9

8
5. IMPLEMENTATION
Source Code:-

1. Exact algorithm for Travelling Sales Person


from itertools import permutations

def tsp_brute_force(graph):
n = len(graph)
if n == 0:
return 0

min_cost = float('inf')
optimal_path = None

for perm in permutations(range(n)):


cost = 0
path = list(perm)
for i in range(n - 1):
cost += graph[path[i]][path[i + 1]]
cost += graph[path[-1]][path[0]]

if cost < min_cost:


min_cost = cost
optimal_path = path

return optimal_path, min_cost

# Usage:
graph = [[0, 29, 20, 21], [29, 0, 15, 18], [20, 15, 0, 28], [21, 18, 28, 0]]
optimal_path, min_cost = tsp_brute_force(graph)
print("Optimal Path:", optimal_path)
print("Minimum Cost:", min_cost)

9
Output

10
2. Approximation algorithm for Travelling Sales Person

import numpy as np

def tsp_nearest_neighbor(graph):

n = len(graph)

if n == 0:

return 0

visited = [False] * n

path = [0]

total_cost = 0

current_node = 0

for _ in range(n - 1):

nearest_neighbor = None

min_distance = float('inf')

for neighbor in range(n):

if not visited[neighbor] and graph[current_node][neighbor] < min_distance:

11
nearest_neighbor = neighbor

min_distance = graph[current_node][neighbor]

path.append(nearest_neighbor)

total_cost += min_distance

visited[nearest_neighbor] = True

current_node = nearest_neighbor

total_cost += graph[path[-1]][path[0]]

path.append(path[0])

return path, total_cost

# Usage:

graph = np.array([[0, 29, 20, 21], [29, 0, 15, 18], [20, 15, 0, 28], [21, 18, 28, 0]])

optimal_path, min_cost = tsp_nearest_neighbor(graph)

print("Optimal Path:", optimal_path)

print("Minimum Cost:", min_cost)

12
6. Output

13
7. Conclusion

Travelling Salesman Problem (TSP) is a problem that helps in a set of cities and distance between every
pair of cities, the problem is to find the shortest possible route that visits every city exactly once and
returns back to the starting point. It is useful to find the shortest path in a weighted graph using branch
and bound as well as backtracking algorithm

14

You might also like