You are on page 1of 13

DAA LAB ASSIGNMENT

A.SAHITHYA
AP21110011110

.1. Implement a solution for the traveling salesperson problem, which


accepts the road network containing city names with distances in terms of a
weighted graph. Now, find out the shortest tour from the given city to
cover all given cities and return to the same city.

1. Use the greedy method to find the shortest tour:

#include <bits/stdc++.h>

#include<iostream>

#include<time.h>

using namespace std;

// Function to find the minimum

// cost path for all the paths

void findMinRoute(vector<vector<int> > tsp)

int sum = 0;

int counter = 0;

int j = 0, i = 0;

int min = INT_MAX;


map<int, int> visitedRouteList;

// Starting from the 0th indexed

// city i.e., the first city

visitedRouteList[0] = 1;

int route[tsp.size()];

// Traverse the adjacency

// matrix tsp[][]

while (i < tsp.size() && j < tsp[i].size())

// Corner of the Matrix

if (counter >= tsp[i].size() - 1)

break;

// If this path is unvisited then

// and if the cost is less then

// update the cost

if (j != i && (visitedRouteList[j] == 0))

{
if (tsp[i][j] < min)

min = tsp[i][j];

route[counter] = j + 1;

j++;

// Check all paths from the

// ith indexed city

if (j == tsp[i].size())

sum += min;

min = INT_MAX;

visitedRouteList[route[counter] - 1] = 1;

j = 0;

i = route[counter] - 1;

counter++;

// Update the ending city in array

// from city which was last visited


i = route[counter - 1] - 1;

for (j = 0; j < tsp.size(); j++)

if ((i != j) && tsp[i][j] < min)

min = tsp[i][j];

route[counter] = j + 1;

sum += min;

// Started from the node where

// we finished as well.

cout << ("Minimum Cost is : ");

cout << (sum)<<endl;

// Driver Code

int main()

{
int start_t;

// Input Matrix

vector<vector<int> > tsp = { { 0, 10, 15, 20 },

{ 10, 0, 35, 25 },

{ 15, 35, 0, 30 },

{ 20, 25, 30, 0 }

};

// Function Call

findMinRoute(tsp);

int end_t = clock();

double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;

printf("Total time taken by CPU: %f\n", total_t );

2. Use dynamic programming to find the shortest tour.

#include <iostream>

using namespace std;

// there are four nodes in example graph (graph is 1-based)

const int n = 4;
// give appropriate maximum to avoid overflow

const int MAX = 1000000;

// dist[i][j] represents shortest distance to go from i to j

// this matrix can be calculated for any given graph using

// all-pair shortest path algorithms

int dist[n + 1][n + 1] = {

{ 0, 10, 15, 20 },

{ 10, 0, 35, 25 },

{ 15, 35, 0, 30 },

{ 20, 25, 30, 0 }

};

// memoization for top down recursion

int memo[n + 1][1 << (n + 1)];

int fun(int i, int mask)

// base case

// if only ith bit and 1st bit is set in our mask,

// it implies we have visited all other nodes already

if (mask == ((1 << i) | 3))


return dist[1][i];

// memoization

if (memo[i][mask] != 0)

return memo[i][mask];

int res = MAX; // result of this sub-problem

// we have to travel all nodes j in mask and end the

// path at ith node so for every node j in mask,

// recursively calculate cost of travelling all nodes in

// mask except i and then travel back from node j to

// node i taking the shortest path take the minimum of

// all possible j nodes

for (int j = 1; j <= n; j++)

if ((mask & (1 << j)) && j != i && j != 1)

res = std::min(res, fun(j, mask & (~(1 << i)))

+ dist[j][i]);

return memo[i][mask] = res;

// Driver program to test above logic

int main()

{
int ans = MAX;

clock_t start_t, end_t;

double total_t;

start_t = clock();

for (int i = 1; i <= n; i++)

// try to go from node 1 visiting all nodes in

// between to i then return from i taking the

// shortest route to 1

ans = std::min(ans, fun(i, (1 << (n + 1)) - 1)

+ dist[i][1]);

printf("The cost of most efficient tour = %d\n", ans);

end_t = clock();

total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;

printf("Total time taken by CPU: %f\n", total_t );

return 0;

3. Use backtracking to find the shortest tour.

#include <bits/stdc++.h>

#include<time.h>

using namespace std;

#define V 4
// Function to find the minimum weight Hamiltonian Cycle

void tsp(int graph[][V], vector<bool>& v, int currPos,

int n, int count, int cost, int& ans)

// If last node is reached and it has a link

// to the starting node i.e the source then

// keep the minimum value out of the total cost

// of traversal and "ans"

// Finally return to check for more possible values

if (count == n && graph[currPos][0]) {

ans = min(ans, cost + graph[currPos][0]);

return;

// BACKTRACKING STEP

// Loop to traverse the adjacency list

// of currPos node and increasing the count

// by 1 and cost by graph[currPos][i] value

for (int i = 0; i < n; i++) {

if (!v[i] && graph[currPos][i]) {


// Mark as visited

v[i] = true;

tsp(graph, v, i, n, count + 1,

cost + graph[currPos][i], ans);

// Mark ith node as unvisited

v[i] = false;

};

// Driver code

int main()

// n is the number of nodes i.e. V

int n = 4;

clock_t start_t, end_t;

double total_t;

start_t = clock();

int graph[][V] = {

{ 0, 10, 15, 20 },

{ 10, 0, 35, 25 },

{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 }

};

// Boolean array to check if a node

// has been visited or not

vector<bool> v(n);

for (int i = 0; i < n; i++)

v[i] = false;

// Mark 0th node as visited

v[0] = true;

int ans = INT_MAX;

// Find the minimum weight Hamiltonian Cycle

tsp(graph, v, 0, n, 1, 0, ans);

// ans is the minimum weight Hamiltonian Cycle

cout << ans<<endl;

end_t = clock();

total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;

printf("Total time taken by CPU: %f\n", total_t );

return 0;

}
4. Print the total execution time taken by the program using the greedy method.

5. Print the total execution time the dynamic programming takes for the same.
6. Print the total execution time backtracking takes for the same.

7. Compare the observations found in all three implementations in terms of time and space
complexity

a. According to the time given by the compile to run the code are in the order

Greedy Approach: 0.001829

:Dynamic Programming: 0.000029

Back Tracking: 0.000062

According to the results, the best approach is solving the Travelling Salesman
Problem using dynamic programming as it got the shortest path in 0.000029. This
result can change according to the graph but here, it is clearly seen that dynamic
programming is the best approach.

You might also like