Professional Documents
Culture Documents
A.SAHITHYA
AP21110011110
#include <bits/stdc++.h>
#include<iostream>
#include<time.h>
int sum = 0;
int counter = 0;
int j = 0, i = 0;
visitedRouteList[0] = 1;
int route[tsp.size()];
// matrix tsp[][]
break;
{
if (tsp[i][j] < min)
min = tsp[i][j];
route[counter] = j + 1;
j++;
if (j == tsp[i].size())
sum += min;
min = INT_MAX;
visitedRouteList[route[counter] - 1] = 1;
j = 0;
i = route[counter] - 1;
counter++;
min = tsp[i][j];
route[counter] = j + 1;
sum += min;
// we finished as well.
// Driver Code
int main()
{
int start_t;
// Input Matrix
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
};
// Function Call
findMinRoute(tsp);
#include <iostream>
const int n = 4;
// give appropriate maximum to avoid overflow
{ 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
};
// base case
// memoization
if (memo[i][mask] != 0)
return memo[i][mask];
+ dist[j][i]);
int main()
{
int ans = MAX;
double total_t;
start_t = clock();
// shortest route to 1
+ dist[i][1]);
end_t = clock();
return 0;
#include <bits/stdc++.h>
#include<time.h>
#define V 4
// Function to find the minimum weight Hamiltonian Cycle
return;
// BACKTRACKING STEP
v[i] = true;
tsp(graph, v, i, n, count + 1,
v[i] = false;
};
// Driver code
int main()
int n = 4;
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 }
};
vector<bool> v(n);
v[i] = false;
v[0] = true;
tsp(graph, v, 0, n, 1, 0, ans);
end_t = clock();
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
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.