MANAV RACHNA UNIVERSITY, FARIDABAD
Department of Computer Science
Course: B. Tech (CSE) Semester: 6th Session: 2025
Practical File
6th Semester (Academic Year: 2025)
Subject Name: - Analysis & Design of Algorithms Lab
Subject Code : CSH204B-P
Submitted to: Dr. Gunjan Chandwani
Submitted by: - Shivam Sehrawat
Roll No: - 2K22CSUN01079
Branch: - CSE – 6B
Department: - B.
Tech
[Link] LAB DATE REMARKS
INDEX
Lab : 01
Q.1 Write a Program to Sort a given set of elements using
bubble sort method and determine the time required to sort
the elements.
#include <bits/stdc++.h>
using namespace std; using
namespace chrono;
void bubbleSort(int arr[], int n)
{ bool swapped;
// Run the steps n-1 times
for (int i = 0; i < n; i++) {
swapped = false;
for (int j = 1; j < n - i; j++) {
if (arr[j] < arr[j - 1]) {
// Swap elements if they are in the wrong order
swap(arr[j], arr[j - 1]);
swapped = true;
}
}
// If no elements were swapped, break the loop early (optimized)
if (!swapped) {
break;
}
}
}
int main() {
// Test cases: Sorted, Reverse-sorted, and Random
array int sortedArr[] = {1, 2, 3, 4, 5};
int reverseArr[] = {5, 4, 3, 2, 1};
bubbleSort(sortedArr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start); cout
<< "Sorted Array: ";
for (int i = 0; i < n; i++) cout << sortedArr[i] << " ";
cout << "\nExecution Time for Sorted Array: " << [Link]() << "
microseconds\n";
// Measure execution time for Reverse Sorted Array
start = high_resolution_clock::now();
bubbleSort(reverseArr, n);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
cout << "Reverse Sorted Array: ";
for (int i = 0; i < n; i++) cout << reverseArr[i] << " ";
cout << "\nExecution Time for Reverse Sorted Array: " <<
[Link]() << " microseconds\n";
// Measure execution time for Random Array
start = high_resolution_clock::now();
bubbleSort(randomArr, n);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
cout << "Random Array: ";
for (int i = 0; i < n; i++) cout << randomArr[i] << " ";
cout << "\nExecution Time for Random Array: " << [Link]()
<< " microseconds\n";
return 0;
}
Output :
Q. Derive the time complexity of Bubble Sort in the following cases:
Best case (already sorted list).
Worst case (list sorted in reverse order).
Average case.
1. Best Case (Already Sorted List)
Explanation:
In the best case, the array is already sorted.
On the first pass, the algorithm checks all elements but makes no swaps.
After completing one pass, the swapped flag will remain false,
indicating that no elements were swapped, and the algorithm
terminates early. Time Complexity:
Outer loop: Runs nnn times (the number of elements in the array).
Inner loop: Runs n−1n - 1n−1 times on the first pass but no
further iterations happen due to the swapped flag being false.
Since the inner loop doesn't perform swaps in the best case, the total
work done is proportional to nnn.
Thus, the Best Case Time Complexity is O(n)O(n)O(n).
2. Worst Case (List Sorted in Reverse
Order) Explanation:
In the worst case, the array is sorted in reverse order, meaning
the algorithm needs to make the maximum number of swaps.
The algorithm will perform n−1n-1n−1 comparisons and swaps in the first
pass, n−2n-2n−2 comparisons in the second pass, and so on until it
reaches the last pass.
Time Complexity:
Outer loop: Runs nnn times.
Inner loop: For each pass, the number of comparisons is n−1n - 1n−1, n−2n
- 2n−2, ..., 2, 1. The total number of comparisons is the sum of the
first n−1n-1n−1 integers, which is O(n2)O(n^2)O(n2).
In the worst case, every comparison results in a swap.
Thus, the Worst Case Time Complexity is O(n2)O(n^2)O(n2).
3. Average Case (Randomly Shuffled
Array) Explanation:
In the average case, the array is randomly shufled.
The algorithm will make several passes through the array, and the number
of comparisons and swaps will be approximately half of the worst case,
but still proportional to n2n^2n2.
Time Complexity:
Outer loop: Runs nnn times.
Inner loop: In the average case, the algorithm still makes about
n2n^2n2 comparisons and swaps, but not all comparisons result in
swaps.
Thus, the Average Case Time Complexity is O(n2)O(n^2)O(n2).
Measure Execution Time A sorted array.
A reverse-sorted array.
A randomly shuffled
array. #include <bits/stdc+
+.h> using namespace std;
using namespace chrono;
void bubbleSort(int arr[], int n)
{ bool swapped;
for (int i = 0; i < n; i++)
{ swapped = false;
for (int j = 1; j < n - i; j++)
{ if (arr[j] < arr[j - 1]) {
swap(arr[j], arr[j - 1]);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
void printArray(int arr[], int n)
{ for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{ int n = 5;
int sortedArr[] = {1, 2, 3, 4, 5};
int reverseArr[] = {5, 4, 3, 2, 1};
int randomArr[] = {5, 3, 4, 1, 2};
// Measure execution time for Sorted Array
auto start = high_resolution_clock::now();
bubbleSort(sortedArr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Sorted Array: ";
printArray(sortedArr, n);
cout << "Execution Time for Sorted Array: " << [Link]() << "
microseconds\n";
// Measure execution time for Reverse Sorted Array
start = high_resolution_clock::now();
bubbleSort(reverseArr, n);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
cout << "Reverse Sorted Array: ";
printArray(reverseArr, n);
cout << "Execution Time for Reverse Sorted Array: " << [Link]()
<< " microseconds\n";
// Measure execution time for Random Array
start = high_resolution_clock::now();
bubbleSort(randomArr, n);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
cout << "Random Array: ";
printArray(randomArr, n);
cout << "Execution Time for Random Array: " << [Link]() <<
" microseconds\n";
return 0;
}
Output :
2. Write a C++ program to calculate the nthn^{th}nth Fibonacci number
using both the Recursive and Iterative approaches. Measure and
compare the execution time for both approaches.
#include <bits/stdc+
+.h> using namespace
std;
using namespace chrono;
// Recursive Fibonacci Function
long long fibonacciRecursive(int n)
{
if (n <= 1) return n;
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
// Iterative Fibonacci Function
long long fibonacciIterative(int n)
{
if (n <= 1) return n;
long long a = 0, b = 1,
c;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
int main() {
int n = 40; // Fibonacci term (adjust n as needed)
// Measure execution time for Recursive Fibonacci
auto start = high_resolution_clock::now();
long long resultRecursive = fibonacciRecursive(n);
auto stop = high_resolution_clock::now();
auto durationRecursive = duration_cast<microseconds>(stop - start);
// Output for Recursive Fibonacci
cout << "Recursive Fibonacci of " << n << ": " << resultRecursive <<
endl;
cout << "Execution Time (Recursive): " << [Link]()
<< " microseconds" << endl;
// Measure execution time for Iterative Fibonacci
start = high_resolution_clock::now();
long long resultIterative = fibonacciIterative(n);
stop = high_resolution_clock::now();
auto durationIterative = duration_cast<microseconds>(stop - start);
// Output for Iterative Fibonacci
cout << "Iterative Fibonacci of " << n << ": " << resultIterative << endl;
cout << "Execution Time (Iterative): " << [Link]() << "
microseconds" << endl;
return 0;
}
Output
Lab 02
1. You are given a sorted array and an unsorted array. Implement
and compare two search algorithms: Binary Search and Linear
Search. Measure and print the execution time for both
algorithms #include <bits/stdc++.h>
using namespace std;
using namespace chrono;
// Function for Binary Search
int binarySearch(vector<int>& arr, int target) {
int left = 0, right = [Link]() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
return -1; // Element not found
}
// Function for Linear Search
int linearSearch(vector<int>& arr, int target)
{ for (int i = 0; i < [Link](); ++i) {
if (arr[i] == target) return i;
}
return -1; // Element not found
}
int main() {
vector<int> arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};int target = 7;
// Measure execution time for Binary
Search auto start =
high_resolution_clock::now(); int
result_binary = binarySearch(arr, target); if
(result_binary != -1)
cout << "Binary Search: Element found at index: " <<
result_binary << endl;
else
cout << "Binary Search: Element not found" << endl;
auto stop_binary = high_resolution_clock::now();
auto duration_binary =
duration_cast<microseconds>(stop_binary - start);
cout << "Binary Search Execution Time: " <<
duration_binary.count() << " microseconds" << endl;
// Measure execution time for Linear Search
start = high_resolution_clock::now();
int result_linear = linearSearch(arr, target); if
(result_linear != -1)
cout << "Linear Search: Element found at index: " <<
result_linear << endl;
else
cout << "Linear Search: Element not found" << endl;
auto stop_linear = high_resolution_clock::now();
auto duration_linear = duration_cast<microseconds>(stop_linear
- start);
cout << "Linear Search Execution Time: " <<
duration_linear.count() << " microseconds" << endl;
return 0;
Lab : 03
1. Implement a Disjoint Set data structure with union by rank and
path compression. Perform union and find operations and
measure the time taken for each.
#include <bits/stdc+
+.h> using namespace
std;
using namespace chrono;
class DisjointSet {
public:
vector<int> parent, rank;
DisjointSet(int n) {
[Link](n);
[Link](n, 0);
for (int i = 0; i < n; ++i) parent[i] = i;
}
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]); // Path compression
return parent[x];
}
void unionSets(int x, int y)
{ int rootX = find(x);
int rootY = find(y);
if (rootX != rootY) {
if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else {
parent[rootY] =
rootX; rank[rootX]++;
}
}
}
};
int main() {
int n = 100000;
DisjointSet
ds(n);
auto start = high_resolution_clock::now();
[Link](1, 2);
[Link](2, 3);
cout << "Find 3: " << [Link](3) << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" << endl;
return 0;
}
Output :
LAB : 04
1. Implement the Merge Sort algorithm and measure its execution time.
#include <bits/stdc++.h>
using namespace std; using
namespace chrono;
void merge(vector<int>& arr, int left, int mid, int right) { int
n1 = mid - left + 1;
int n2 = right - mid;
vector<int> leftArr(n1), rightArr(n2);
for (int i = 0; i < n1; ++i) leftArr[i] = arr[left + i];
for (int i = 0; i < n2; ++i) rightArr[i] = arr[mid + 1 + i]; int
i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) arr[k++] = leftArr[i++]; else
arr[k++] = rightArr[j++];
}
while (i < n1) arr[k++] = leftArr[i++]; while
(j < n2) arr[k++] = rightArr[j++];
}
void mergeSort(vector<int>& arr, int left, int right) { if
(left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
vector<int> arr = {12, 11, 13, 5, 6, 7};
auto start = high_resolution_clock::now();
mergeSort(arr, 0, [Link]() - 1);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" <<
endl;
return 0;
}
Output :
LAB : 05
1. Implement a Max-Heap and measure the execution time of insertion
and extraction operations.
#include <bits/stdc+
+.h> using namespace
std;
using namespace chrono;
class MaxHeap {
public:
vector<int> heap;
void insert(int val) {
heap.push_back(val);
int i = [Link]() - 1;
while (i > 0 && heap[(i - 1) / 2] < heap[i])
{ swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
int extractMax() {
if ([Link]() == 0) return -
1; int maxVal = heap[0];
heap[0] = [Link]();
heap.pop_back();
maxHeapify(0);
return maxVal;
}
void maxHeapify(int i) {
int left = 2 * i + 1, right = 2 * i + 2, largest = i;
if (left < [Link]() && heap[left] > heap[largest]) largest = left;
if (right < [Link]() && heap[right] > heap[largest]) largest =
right; if (largest != i) {
swap(heap[i], heap[largest]);
maxHeapify(largest);
}
}
};
int main()
{ MaxHeap mh;
auto start = high_resolution_clock::now();
[Link](10);
[Link](20);
[Link](5);
cout << "Max Element: " << [Link]() << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" <<
endl;
return 0;
}
Output :
Lab : 6
1. Implement Strassen's Algorithm for matrix multiplication and measure its execution time.
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
void add(vector<vector<int>>& A, vector<vector<int>>& B,
vector<vector<int>>& result) {
int n = [Link]();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
result[i][j] = A[i][j] + B[i][j];
}
void subtract(vector<vector<int>>& A, vector<vector<int>>& B,
vector<vector<int>>& result) {
int n = [Link]();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
result[i][j] = A[i][j] - B[i][j];
}
void strassen(vector<vector<int>>& A, vector<vector<int>>& B,
vector<vector<int>>& result) {
int n = [Link]();
if (n == 1) {
result[0][0] = A[0][0] * B[0][0];
return;
}
int new_size = n / 2;
vector<vector<int>> A11(new_size, vector<int>(new_size)),
A12(new_size, vector<int>(new_size)),
A21(new_size, vector<int>(new_size)),
A22(new_size, vector<int>(new_size)),
B11(new_size,
vector<int>(new_size)),
B12(new_size,
vector<int>(new_size)),
B21(new_size,
vector<int>(new_size)),
B22(new_size,
vector<int>(new_size)), M1(new_size,
vector<int>(new_size)), M2(new_size,
vector<int>(new_size)), M3(new_size,
vector<int>(new_size)), M4(new_size,
vector<int>(new_size)), M5(new_size,
vector<int>(new_size)), M6(new_size,
vector<int>(new_size)), M7(new_size,
vector<int>(new_size)),
temp1(new_size, vector<int>(new_size)),
temp2(new_size, vector<int>(new_size));
for (int i = 0; i < new_size; ++i)
for (int j = 0; j < new_size; ++j)
{ A11[i][j] = A[i][j];
A12[i][j] = A[i][j + new_size];
A21[i][j] = A[i + new_size][j];
A22[i][j] = A[i + new_size][j + new_size];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + new_size];
B21[i][j] = B[i + new_size][j];
B22[i][j] = B[i + new_size][j + new_size];
}
add(A11, A22, temp1);
add(B11, B22, temp2);
strassen(temp1, temp2, M1);
add(A21, A22, temp1);
strassen(temp1, B11, M2);
subtract(B12, B22, temp2);
strassen(A11, temp2, M3);
subtract(B21, B11, temp2);
strassen(A22, temp2, M4);
add(A11, A12, temp1);
strassen(temp1, B22, M5);
subtract(A21, A11,
temp1); add(B11, B12,
temp2);
strassen(temp1, temp2, M6);
subtract(A12, A22,
temp1); add(B21, B22,
temp2);
strassen(temp1, temp2, M7);
add(M1, M4, temp1);
subtract(M1, M2, temp2);
add(M7, M5, temp2);
subtract(M3, M6, temp2); add(temp1, temp2, result);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
result[i][j] = result[i][j];
}
int main() {
vector<vector<int>> A = {{1, 2}, {3, 4}};
vector<vector<int>> B = {{5, 6}, {7, 8}};
vector<vector<int>> result(2, vector<int>(2));
auto start = high_resolution_clock::now();
strassen(A, B, result);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << "
microseconds" << endl;
return 0;
}
Output :
Lab : 07
1. Solve the 0/1 Knapsack problem using dynamic programming and measure its
execution time.
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
int knapsack(int W, vector<int>& wt, vector<int>& val, int n)
{ vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
for (int i = 1; i <= n; ++i) {
for (int w = 1; w <= W; ++w) { if
(wt[i - 1] <= w)
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - wt[i - 1]] + val[i - 1]); else
dp[i][w] = dp[i - 1][w];
}
}
return dp[n][W];
}
int main() {
vector<int> val = {60, 100, 120};
vector<int> wt = {10, 20, 30}; int W
= 50, n = [Link]();
auto start = high_resolution_clock::now();
cout << "Max Value: " << knapsack(W, wt, val, n) << endl; auto
stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" << endl;
return 0;
}
Output :
Lab : 08
1. Find the length of the longest common substring between two strings using dynamic
programming and measure its execution time.
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
int longestCommonSubstring(string X, string Y) { int m =
[Link](), n = [Link]();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); int
length = 0;
for (int i = 1; i <= m; ++i) { for
(int j = 1; j <= n; ++j) {
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1; length
= max(length, dp[i][j]);
} else {
dp[i][j] = 0;
}
}
}
return length;
}
int main() {
string X = "abcdef", Y = "zcdemf";
auto start = high_resolution_clock::now();
cout << "Longest Common Substring Length: " << longestCommonSubstring(X,
Y) << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" << endl;
return 0;
}
Output :
Lab : 09
1. Implement the Optimal Binary Search Tree (OBST) algorithm and measure its execution
time.
#include <bits/stdc++.h> using
namespace std;
using namespace chrono;
int optimalBST(int keys[], int freq[], int n) {
vector<vector<int>> dp(n, vector<int>(n, 0)); for (int i = 0; i
< n; ++i) dp[i][i] = freq[i];
for (int len = 2; len <= n; ++len) { for (int i =
0; i < n - len + 1; ++i) {
int j = i + len - 1; dp[i][j] =
INT_MAX;
for (int r = i; r <= j; ++r) {
int cost = (r > i ? dp[i][r - 1] : 0) + (r < j ? dp[r + 1][j] : 0);
dp[i][j] = min(dp[i][j], cost + accumulate(freq + i, freq + j + 1, 0));
}
}
}
return dp[0][n - 1];
}
int main() {
int keys[] = {10, 12, 20};
int freq[] = {34, 8, 50};
int n = sizeof(keys) / sizeof(keys[0]);
auto start = high_resolution_clock::now();
cout << "Optimal BST Cost: " << optimalBST(keys, freq, n) << endl; auto stop =
high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" << endl;
return 0;
}
Output :
Lab : 10
1. Find the length of the longest common subsequence between two strings
using dynamic programming and measure its execution time.
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
int lcs(string X, string Y) {
int m = [Link](), n = [Link]();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); for
(int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (X[i - 1] == Y[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[m][n];
}
int main() {
string X = "AGGTAB", Y = "GXTXAYB";
auto start = high_resolution_clock::now();
cout << "Longest Common Subsequence Length: " << lcs(X, Y) << endl; auto
stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" <<
endl;
return 0;
}
Output :
Lab : 11
1. Implement Prim's algorithm to find the Minimum Spanning Tree
(MST) of a graph and measure its execution time.
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
const int INF = INT_MAX;
int primMST(int V, vector<vector<int>>& graph)
{ vector<int> key(V, INF);
vector<bool> mstSet(V, false);
key[0] = 0;
int totalWeight = 0;
for (int count = 0; count < V - 1; ++count)
{ int u = -1;
for (int v = 0; v < V; ++v) {
if (!mstSet[v] && (u == -1 || key[v] < key[u])) u = v;
}
mstSet[u] = true;
totalWeight += key[u];
for (int v = 0; v < V; ++v) {
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v])
{ key[v] = graph[u][v];
}
}
}
return totalWeight;
}
int main() {
int V = 5;
vector<vector<int>> graph = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
auto start = high_resolution_clock::now();
cout << "Weight of MST: " << primMST(V, graph) << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << "
microseconds" << endl;
return 0;
}
Lab : 12
[Link] the 0/1 Knapsack problem using dynamic programming and measure its
execution time.
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
int knapsack(int W, vector<int>& wt, vector<int>& val, int n)
{ vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
for (int i = 1; i <= n; ++i) {
for (int w = 1; w <= W; ++w) { if
(wt[i - 1] <= w)
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - wt[i - 1]] + val[i - 1]); else
dp[i][w] = dp[i - 1][w];
}
}
return dp[n][W];
}
int main() {
vector<int> val = {60, 100, 120};
vector<int> wt = {10, 20, 30}; int
W = 50, n = [Link]();
auto start = high_resolution_clock::now();
cout << "Max Value: " << knapsack(W, wt, val, n) << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" <<
endl;
return 0;
}
Output :
Lab : 13
1. Solve the Travelling Salesman Problem (TSP) using dynamic
programming and measure its execution time.
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
const int INF = INT_MAX;
int tsp(int n, vector<vector<int>>& dist, vector<vector<int>>& dp, int mask,
int pos) {
if (mask == (1 << n) - 1) return dist[pos][0];
if (dp[mask][pos] != -1) return dp[mask][pos];
int ans = INF;
for (int city = 0; city < n; ++city) {
if ((mask & (1 << city)) == 0) {
int newAns = dist[pos][city] + tsp(n, dist, dp, mask | (1 << city), city);
ans = min(ans, newAns);
}
}
return dp[mask][pos] = ans;
}
int main()
{ int n = 4;
vector<vector<int>> dist = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
vector<vector<int>> dp(1 << n, vector<int>(n, -1));
auto start = high_resolution_clock::now();
cout << "Minimum cost of TSP: " << tsp(n, dist, dp, 1, 0) << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Execution Time: " << [Link]() << " microseconds" <<
endl;
return 0;
}
Output :