Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

SC205 DISCRETE MATHEMATICS

PROJECT

July 9, 2022

Professors:
Prof. Manish Gupta & Prof. Manoj Raut

Made by:
Nishith Parekh(202101449)
Abhinav Agarwal(202101040)
Priya Tank(202101463)
Dhruv Lad(202101497)
Keyur Govrani(202101498)
Vraj Patel(202101060)

1
1 INTRODUCTION
→In today’s world, everyone wishes not only to complete the work but to
complete the work in minimum amount of time and with minimum resources.
For example: A truck delivering goods to various shops or a postman deliv-
ering posts to various homes. The truck driver wishes to spend less on petrol
and the same applies for the postman.

→So we will discuss about this problem and the possible solutions to it
and which solution is better. The problem is called ’The travelling salesman
problem’.

2 EXPLANATION OF PROBLEM
→The Travelling Salesman Problem (also known as Travelling Salesperson
Problem or TSP) is a renowned NP-hard problem as no ‘quick’ solution has
been found till now.

→The travelling salesman problem is about starting from a point and pass-
ing through all the points (destinations) exactly once and returning back to
the starting point by covering minimum distance.

→The Travelling Salesman Problem is a problem aimed at minimizing the


amount of energy expenditure. In this problem, there are a set of nodes
(or cities), and a salesperson, starting from a city (say hometown), travels
through each one of them exactly once to advertise his product and return
to his hometown in the end. The salesman completes the job by covering the
least distance to stay economical.

→So, one has the adjacency matrix of a particular quantity between the
pairs of cities (or nodes) that could have everything that has to be mini-
mized. The adjacency matrix may contain the cost of traveling between all
the possible pairs of cities, the distance between every pair of cities, or any
such constraint.
→ Interaction of TSP with some real-life problems:[1]

→The TSP is a problem in which we will be searching for the minima of


the function provided. So, we can say that it is a problem in combinatorial
optimization.

→This problem has many real-life applications. It finds applications in the


scenarios involving routing, planning, logistics, scheduling, etc.

→GPS System: The GPS is an application of the TSP. Google Maps would
provide the traveler with the shortest possible route to reach the destination.
It re-routes to the best way if the traveler changes his mind.

→Airline crew scheduling: Airlines also assign flights to the crew members
in a way that they return to their hometowns.

→Routing long-distance trains: Long-distance trains are routed in the best


way to conserve fuel and energy.

→In this way, TSP finds its application in various discrete fields.

→There are various solutions possible for it , here we will be discussing


about three of the solutions and deciding which solution is the best.

3 SOLUTIONS
[2]
→Let the hometown of the salesman be node 1:

→ Let the adjacency matrix shown be the cost required for traveling from
one node to another.

→In the adjacency matrix, the column numbers determine the start point of
the salesman at a particular instance & the row numbers represent the final
point of the salesman with that traversal.

→ The three algorithms that will be talked about here are:

1) Greedy algorithm
2) Brute Force Approach or Naive Approach
3) Dynamic Programming

→The Greedy algorithm is:

→There are various approaches and algorithms to solve the Travelling Sales-
man Problem.

→One approach is the use of Greedy Algorithm. As per the Greedy Al-
gorithm, the salesman initially goes to the point which consumes the least
energy.

→So, as per the above example, the salesman will go to the second city
first. Then, the algorithm will again search for the best location. Thus, the
salesman will go to city 4.

→Here, the algorithm won’t allow the salesman to return to the first city,
though it is cheaper than travelling to the fourth city because doing so will
complete the loop without covering all the cities, leading to an overall in-
crease in the cost of covering all the cities just once.

→With the use of the Greedy Algorithm, the path of traversal would be:
1 → 2 → 4 → 3 → 1.

→The cost calculated on the basis of the above traversal would be 10 +


10 + 20 + 15 which equals 55.

→The answer obtained based on the Greedy Algorithm is correct but we


can’t comment on the optimization of this solution.

→So, it can be concluded that this algorithm is unconcerned with whether


the most excellent outcome at the moment will produce the final best result.

→The time complexity while using Greedy algorithm is O(n2 *log2 n).

→The Brute Force algorithm is:

→Another such approach is Brute Force Approach or Naive Approach. This


approach lists down all the possible paths. On calculating out the cost for
each path, this approach provides us with the most optimized path out of all
the possible paths.

→For the above given 4 node TSP:

→It can be observed that the most optimal path is


1 → 3 → 4 → 2 → 1. The cost for this path is 15 + 5 + 10 + 5 which equals
35.

→This solution is not at all optimized one as the approach will check each
and every traversal completely calculating the cost, thus increasing the time
complexity.

→The time complexity while using Brute Force Approach or Naive Approach
is O(n!).

→On generalizing, for n nodes, there is a total of (n-1)! possible paths.


So, calculating the cost for each path will give the time complexity of O(n!)
which equals O(nn ).

→This obtained time complexity is the biggest time complexity one can
ever get. So, this is the least optimized solution for the Travelling Salesman
Problem.

→One can also use the Nearest Neighbour Method to solve the Travelling
Salesman Problem.

→In this method, the salesman will travel from the city where he is rest-
ing currently to the city which is at his nearest distance. The salesman
continues this traversal until all the cities are not visited.

→The most efficient method to solve the TSP is Dynamic Programming.


This method gives the guaranteed solution. Though the time complexity
for solving the TSP remains exponential, it reduces the time complexity as
compared to other methods.[3]

→In this method, the problem is solved in a Bottom-up approach. The


Dynamic Programming is beneficial:

→only when the problem is getting divided into smaller sub-problems (the
solution of the problem is obtained by combining the solutions of the sub-
problems in which it is divided)

→there is repetition/overlapping in the sub-problems (the benefit of this


is that we can once store the value of the repetitive portion and use it as and
when required)

→The time complexity while using Dynamic Programming is O(n2 *2n ).

→So, the best solution after taking into account both the results and
time complexity turns out to be Dynamic Programming.

→The method which would definitely obtain the optimal solution of TSP
is the method of exhaustive enumeration and evaluation.

→Solutions:

1)Hybrid Genetic Algorithm


2)Dynamic Programming
3)Greedy Algorithm
4)Brute force method/ Naive method
5)The Nearest Neighbour Method

→The answer obtained when the problem is solved using Greedy algorithm
is not so accurate so it is not the most optimal solution. The time complexity
for solving the travelling salesman problem using Dynamic Programming is
exponential , i.e., O(n2 *2n ) but still it is lesser than O(n!) which is the time
complexity for Brute Force Approach.

→The code for travelling salesman problem using dynamic programming


is:

#include <iostream>

using namespace std;

int maximum = 1000000; // Here we have assumed a maximum value to prevent


//overflow in calculating the minimum cost

int steps [24][24];


int cache [24][1 << (24)];

int solve (int k, int num, int box)


{
// This if condition is for the base case
// When only 1st bit and kth bit are set in our box, it implies that we
//have visited all the other nodes before
if (box == ((1 << k) | 3))
return steps [1][k];

if (cache [k][box] != 0)
return cache [k][box];

int answer = maximum; // The minimum cost for traversing all the nodes

/* In order to find the minimum cost, we have to traverse


all the p nodes in box and end our path at kth node.
Hence, with the help of dynamic programming,
we are recursively calling solve function
to calculate cost of traversing
all nodes in box except k. Then we will traverse back from
node p to k by taking the path that has minimum cost of all
the available paths.
*/

for (int p = 1; p <= num ; p++)


if ((box & (1 << p)) && p != k && p != 1)
answer = min(answer, solve(p, num, box & (~(1 << k))) + steps [p][k]);

return cache [k][box] = answer;


}

//This is the driver code to find minimum cost


//using the above defined solve function

int main()
{
int num, x, y;

cout << "Enter the no. nodes : ";


cin >> num;
cout << endl;

cout << "Now let’s make a matrix of cost of going from one node
to another node" << endl << endl;

// steps [x][y] represents the minimum steps or cost required to go from a


//node x to node y

for (x = 0; x < num+1; x++)


{
for (y = 0; y < num+1; y++)
{
if (x == y || x == 0 || y==0)
{
steps [x][y] = 0;
continue;
}

cout <<"Enter the cost of going from node "<< x << " to node "<<y<<" : ";
cin >> steps [x][y];
cout << endl;
}
}

cout << "The matrix of cost that you have


entered is as follows : "<<endl<<endl;

cout << " ";

for (x = 0; x < num+1; x++)


cout << x << "\t";
cout << endl << endl;

for (x = 0; x < num+1; x++)


{
cout << x << " ";

for (y = 0; y < num+1; y++)


{
cout << steps[x][y] << "\t";
}

cout << endl << endl;


}

int sol = maximum;

for (int q = 1; q <= num; q++)


/*
Here we are starting from node 1 and will keep traversing upto q.
After reaching at q, we will traverse back to node 1 by following
the path that has minimum cost
of all available paths.
*/
sol = min(sol, solve(q, num, (1 << (num + 1)) - 1) + steps [q][1]);

cout << "The minimum cost/steps required to traverse


through all the nodes = " << sol << endl << endl;
return 0;
}

References
[1] In: (). url: https : / / aip . scitation . org / doi / pdf / 10 . 1063 / 1 .
4976899#:~:text=The%20traveling%20salesman%20problem%20.
[2] In: (). url: https://www.tutorialspoint.com/design_and_analysis_
of_algorithms/design_and_analysis_of_algorithms_travelling_
salesman_problem.htm.
[3] In: (). url: https://en.wikipedia.org/wiki/Dynamic_programming.

You might also like