You are on page 1of 26

BHAGWAN MAHAVIR COLLEGE OF

ENGINEERING & TECHNOLOGY

SAURABH UPADHYAY (180060107060)


ASHISH SINGH
(180060107055)
DHRUV PATIL
(180060107044)
IRFAN BENGALI
(180060107007)

PRESENTED TO:
Mrs. Twinkle Ankleshwaria
Content

The class of P and Np

Travelling Salesman problems


CLASS OF P AND NP

 There are two groups in which problems can be classified .

1.) The first group consists of those problems that can be


solved in polynomial time.

 For example :- ~ Searching of a elements from the list


O(Logn).

~ Sorting of elements O(logn).


2.) The second group consists of problems that can be solved in non-deterministic polynomial
time.

~ Non-deterministic :-
 An algorithm is said to be non-deterministic when there are more than one paths that algorithm
can follow.
 Due to which one cannot determine which path is to be followed.
~DETERMINISTIC ~ NON-DETERMINISTIC
1 B 0 B
A A
C C
0 0
 For example :- ~ Knapsack problem O(2^n/2).

~ Travelling salesperson problem


O(n^2 2^n).
Defination of P

Problems that can be solved in polynomial time.(P stands for polynomial)


i.e. these problems can be solved in time O(nk) in worst-case, where k is
constant.
Formally, an algorithm is polynomial time algorithm, if there exists a
polynomial p(n) such that the algorithm can solve any instance of size n in a
time O(p(n)).
For understanding :- X  A [O(n^2)] {X=problem , A=algorithm}

-Where n is the size of input.


-So the problem X is solve in polynomial time.

 For Example :- Kruskal’s algorithm Searching of element , sorting of


element , etc.
Example of P class :- Kruskal’s algorithm

In Kruskal’s algorithm the minimum weight is


obtain.
In this algorithm also the circuit not be formed.
Each time the edge of minimum weight has to be
selected , from graph.
It is not necessary in this algorithm to have edges of
minimum weight to be adjacent.
Let solve one example by this method :-
5 BD=2
7 4 DE=2A B

3F 6 2 AC=3 E

8 2 CD=3C D

3 BE=4
AB=5
BC=6
AF=7
FC=8
BD=2
DE=2
A B
7 AC=3
F 3 2 CD=3 E
3C 2 D BE=4

AB=5
BC=6
AF=7
FC=8
Defination of NP

The problems which can be verified in polynomial time.


The definition of NP involves the idea of a non-deterministic algorithm.
For understanding :- There is a problem X and the solution S is given;

XS(A) { X=problem , s= solution , a=algorithm}

Then algorithm A in polynomial time verifies that give solution S of


problem X is correct or not.
So , therefore X is a NP problem.
For example :- Travelling salespersons problem , knapsack problem , etc.
~ NOTE :- All P problems are NP problems therefore P problems
are subset of NP problems.
 The NP problems can be further categorized into two
problems :-

Computational complexity problems

P - class NP - class

NP - complete NP - hard
IMPLEMENTATION

Algorithm Non_determin()
// A[1:n] is a set of elements
// We have to determine the index I of A at which element X is located.
{
// The following for loop is the guessing stage
for i=1 to n do
A[i] := choose(i);
// Next is the verification (deterministic) stage
if (A[i] = x) then
{
write(i);
success();
}
write(0);
fail();
}
Travelling Salesman Problem

The Travelling Salesman Problem (TSP) is


the challenge of finding the shortest yet most efficient route
for a person to take given a list of specific destinations. ...
The problem can be solved by analyzing every round-trip route
to determine the shortest one.

TSP has commanded so much attention because it’s so easy to


describe yet so difficult to solve. In fact, TSP belongs to the class
of combinatorial optimization problems known as NP-complete.
This means that TSP is classified as NP-hard because it has no
“quick” solution and the complexity of calculating the best route
will increase when you add more destinations to the problem. 
Popular Travelling Salesman Problem Solutions

The Brute-Force Approach

The Brute Force approach, also known as the Naive


Approach, calculates and compares all possible
permutations of routes or paths to determine the
shortest unique solution. To solve the TSP using the
Brute-Force approach, you must calculate the total
number of routes and then draw and list all the possible
routes. Calculate the distance of each route and then
choose the shortest one—this is the optimal solution. 
The Branch and Bound Method

This method breaks a problem to be solved into several sub-problems. It’s


a system for solving a series of sub-problems, each of which may have
several possible solutions and where the solution selected for one problem
may have an effect on the possible solutions of subsequent sub-problems.
To solve the TSP using the Branch and Bound method, you must choose a
start node and then set bound to a very large value (let’s say infinity).
Select the cheapest arch between the unvisited and current node and then
add the distance to the current distance. Repeat the process while the
current distance is less then the bound. If the current distance is less than
the bound, you’re done. You may now add up the distance so that the
bound will be equal to the current distance. Repeat this process until all
the arcs have been covered.
As you see in branch and bound
method there is a problem which is
solved by dividing this problem
into serveral sub problems
The Nearest Neighbor Method

This is perhaps the simplest TSP heuristic. The key


to this method is to always visit the nearest
destination and then go back to the first city when all
other cities are visited. To solve the TSP using this
method, choose a random city and then look for the
closest unvisited city and go there. Once you have
visited all cities, you must return to the first city.
One of the simplest algorithms for approximately
solving the STSP is the nearest neighbor method,
where the salesman always visits the nearest city.
The process is as follows:
Select a starting city.
Find the nearest city to your current one and go
there.
If there are still cities not yet visited, repeat step 2.
Else, return to the starting city.
Example of Travelling Salesman Problem

The Hamiltoninan cycle


For example, consider the graph shown in figure on
right side. A TSP tour in the graph is 1-2-4-3-1. The
cost of the tour is 10+25+30+15 which is 80.
Consider city 1 as the starting and ending point. Since the
route is cyclic, we can consider any point as a starting
point.
Generate all (n-1)! permutations of cities.
Calculate the cost of every permutation and keep track of
minimum cost permutation.
Return the permutation with minimum cost.
Implementation

Given a list of cities and the distances between each


pair of cities, what is the shortest possible route that
visits each city exactly once and returns to the origin
city?.

Solution : To compute a minimum distance tour, use


the final equation to generate the 1st node, and
repeat for the other nodes. For this problem, we
cannot know which subproblems we need to solve, so
we solve them all.
#include <stdio.h>
 int matrix[25][25], visited_cities[10], limit, cost = 0;
 int tsp(int c)
 { int count, nearest_city = 999;
 int minimum = 999, temp;
 for(count = 0; count < limit; count++)
{
 if((matrix[c][count] != 0) && (visited_cities[count] == 0))
{
if(matrix[c][count] < minimum)
{
minimum = matrix[count][0] + matrix[c][count];
}
 temp = matrix[c][count]; nearest_city = count;
}
}
 if(minimum != 999) { cost = cost + temp;
}
 return nearest_city;
}
 void minimum_cost(int city)
{
 int nearest_city; visited_cities[city] = 1;
 printf("%d ", city + 1); nearest_city = tsp(city);
 if(nearest_city == 999)
{
 nearest_city = 0;
 printf("%d", nearest_city + 1);
 cost = cost + matrix[city][nearest_city]; return;
}
 minimum_cost(nearest_city);
}

int main()
{
 int i, j;
 printf("Enter Total Number of Cities:\t");
 scanf("%d", &limit);
 printf("\nEnter Cost Matrix\n");
 for(i = 0;i < limit;i++)
{
 printf("\nEnter %d Elements in Row[%d]\n", limit, i + 1);
 for(j = 0; j < limit; j++)
{
 scanf("%d", &matrix[i][j]);
}
 visited_cities[i] = 0;
}
 printf("\nEntered Cost Matrix\n");
 for(i = 0; i < limit; i++)
{
 printf("\n");
 for(j = 0; j < limit; j++)
{
 printf("%d ", matrix[i][j]);
}
}
 printf("\n\nPath:\t");
 minimum_cost(0); printf("\n\nMinimum Cost: \t"); printf("%d\n", cost);
 return 0;
}
Output

You might also like