DAA Practical
DAA Practical
Specifications -
Processor: AMD Ryzen 7 4800HS with Radeon Graphics 2.90GHz
System Type: 64-bit operating system, x64-based processor
RAM: 16.0 GB (15.4 GB usable)
OS Build: 22621.2134
b. Language – C++
C++ is the most used and most popular programming language developed by Bjarne
Stroustrup. C++ is a high-level and object-oriented programming language. This
language allows developers to write clean and efficient code for large applications and
software development, game development, and operating system programming. It is
an expansion of the C programming language to include Object Oriented
Programming(OOPs) and is used to develop programs for computers. This C++
Tutorial will cover all the basic to advanced topics of C++ like C++ basics, C++
functions, C++ classes, OOPs and STL concepts.
Features of C++ :
1. Object-Oriented Programming
2. Machine Independent
3. Simple
4. High-Level Language
5. Popular
6. Case-sensitive
7. Compiler Based
8. Dynamic Memory Allocation
9. Memory Management
10. Multi-threading
c. Platform – VS Code:
Visual Studio Code is a lightweight, yet powerful source code editor developed by
Microsoft. It offers a rich set of features, including smart code completion, built-in
Git integration, debugging support, and a vast library of extensions. Its cross-
platform availability makes it a go-to choose for developers working on various
operating systems. With its fast performance and highly customizable interface, VS
Code has garnered a large and active community of users, making it one of the most
popular code editors for a wide range of programming languages and development
tasks.
Features :
1. Lightweight and Fast
2. Language Support
3. Integrated Development Environment (IDE) Features:
4. Integrated Terminal
PRACTICAL – 2
AIM: Write a program to sort a given set of elements and determine the time required
to sort the elements. Repeat the exp for different values of n, the number of elements in
list to be sorted and plot a graph of the time v/s n elements.
Libraries Used: -
1) Random No Generator:
Library used for generating random number:
< cstdlib>,<time.h>
Function Invoked:
rand()
2) Time of Execution:
Library used for checking time of execution for each run:
<chrono>
Function Invoked:
chrono::high_resolution_clock::now();
3) Dynamic Array:
Library used for checking time of execution for each run:
<vector>
#include <iostream>
#include <chrono>
#include <vector>
{
for (int j = 0; j < n - i - 1; j++)
int main()
int n;
cin >> n;
arr[i] = rand();
// ascending_sort(arr, n);
descending_sort(arr,n);
start = std::chrono::system_clock::now();
bubbleSort(arr, n);
cout << '\n';
end = chrono::system_clock::now();
cout << "finished computation at " << std::ctime(&end_time) << "Elapsed time: " <<
elapsed_seconds.count() << "s\n";
delete[] arr;
return 0;
OUTPUT: -
GRAPH: -
Bubble Sort Time Graph
2.75
2.25
1.75
TIme
1.25
0.75
0.25
1 10 100 500 1000 2000 5000 10000
Best Case 0.001 0.003121 0.018973 0.094824 0.210166 0.40591 1.01975 2.13994
(sec)
Average Case 0.001 0.002996 0.019299 0.096289 0.193636 0.396648 1.07751 2.2574
(sec)
Worst Case 0.001 0.002504 0.020566 0.092271 0.197371 0.406942 1.15113 2.43272
(sec)
PRACTICAL – 3
Libraries Used: -
1) Random No Generator:
Library used for generating random number:
< cstdlib>,<time.h>
Function Invoked:
rand()
2) Time of Execution:
Library used for checking time of execution for each run:
<chrono>
Function Invoked:
chrono::high_resolution_clock::now();
3) Dynamic Array:
Library used for checking time of execution for each run:
<vector>
Written Code: -
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
if (arr[i] == key)
break;
}
return time;
int main()
int n;
cin >> n;
vector<int> arr;
arr.push_back(rand());
int best_key = arr[0]; // Best case: the key is found in the first element
int worst_key = rand(); // Worst case: the key is not in the array, so it's a random value
int avg_key = arr[rand() % n]; // Average case: a random key from the array
cout << "Time taken for best case: " << (best_time.count()) << " microseconds" << endl;
auto worst_time = linear_search(arr, n, worst_key);
cout << "Time taken for worst case: " << (worst_time.count()) << " microseconds" <<
endl;
cout << "Time taken for average case: " << (avg_time.count()) << " microseconds" <<
endl;
return 0;
OUTPUT: -
GRAPH: -
Linear SearchTime Graph
65
55
45
TIme
35
25
15
5
1 10 100 500 1000 2000 5000 10000
Best Case (μ sec) 0 0 0 0 0 0 0 NaN
Average Case (μ sec) 0 0 1 3 4 11 30 NaN
Worst Case (μ sec) 0 0 2 6 8 23 59 NaN
3.2 BINARY SEARCH
Libraries Used: -
1) Random No Generator:
Library used for generating random number:
< cstdlib>,<time.h>
Function Invoked:
rand()
2) Time of Execution:
Library used for checking time of execution for each run:
<chrono>
Function Invoked:
chrono::high_resolution_clock::now();
3) Dynamic Array:
Library used for checking time of execution for each run:
<vector>
sort(arr.begin(), arr.end());
auto start = std::chrono::high_resolution_clock::now();
int left = 0;
int right = n - 1;
int key = arr[(left + right) / 2];
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] == key)
{
break;
}
else if (arr[mid] > key)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto time = end - start;
return time;
}
auto worst_binary_search(vector<int> arr, int n)
{
int key = arr[n - 1];
sort(arr.begin(), arr.end());
auto start = std::chrono::high_resolution_clock::now();
int left = 0;
int right = n - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] == key)
{
break;
}
else if (arr[mid] > key)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto time = end - start;
return time;
}
int main()
{
int T;
cout << "Enter the number of values of n:";
cin >> T;
for (int i = 0; i < T; i++)
{
int n;
cout << "Enter the value of n:";
cin >> n;
vector<int> arr;
for (int i = 0; i < n; i++)
{
arr.push_back(rand());
}
auto best_time = best_binary_search(arr, n);
cout << "Time taken for best case=" << (best_time / std::chrono::nanoseconds(1)) <<
"nanoseconds" << endl;
auto average_time = average_binary_search(arr, n);
cout << "Time taken for average case=" << (average_time /
std::chrono::nanoseconds(1)) << "microseconds" << endl;
auto worst_time = worst_binary_search(arr, n);
cout << "Time taken for average case=" << (worst_time / std::chrono::nanoseconds(1))
<< "microseconds" << endl;
}
return 0;
}
OUTPUT: -
GRAPH: -
1100
900
700
500
300
100
10 100 500 1000 5000 10000
Best Case (μ sec) 1 8 57 131 772 1559
Average Case (μ sec) 1 11 54 138 742 1628
Worst Case (μ sec) 1 12 55 168 838 1848
3.3 MERGE SORT
Libraries Used: -
1) Random No Generator:
Library used for generating random number:
< cstdlib>,<time.h>
Function Invoked:
rand()
2) Time of Execution:
Library used for checking time of execution for each run:
<chrono>
Function Invoked:
chrono::high_resolution_clock::now();
3) Dynamic Array:
Library used for checking time of execution for each run:
<vector>
int main()
{
int n;
cout << "Enter the value of n: ";
cin >> n;
int lb = 0;
int rb = 100;
vector<int> arr;
for (int i = 0; i < n; i++)
{
arr.push_back(rand() % (rb - lb + 1));
}
cout << endl;
auto best_time = best_merge_sort(n);
cout << "Time Taken for best case = " << (best_time / std::chrono::microseconds(1)) << "
microseconds" << endl;
auto average_time = average_merge_sort(arr, n);
cout << "Time Taken for average case = " << (average_time /
std::chrono::microseconds(1)) << " microseconds" << endl;
auto worst_time = worst_merge_sort(n);
cout << "Time Taken for worst case = " << (worst_time / std::chrono::microseconds(1)) <<
" microseconds" << endl;
cout << endl;
return 0;
}
OUTPUT: -
GRAPH: -
2250
1750
Time
1250
750
250
AIM: Program of Merge Sort, Heap Sort, Selection Sort, Radix Sort.
Libraries Used: -
1) Random No Generator:
Library used for generating random number:
< cstdlib>,<time.h>
Function Invoked:
rand()
2) Time of Execution:
Library used for checking time of execution for each run:
<chrono>
Function Invoked:
chrono::high_resolution_clock::now();
3) Dynamic Array:
Library used for checking time of execution for each run:
<vector>
auto worst_heap_sort(int n)
{
vector<int> arr;
for (int i = 0; i < n; i++)
{
arr.push_back(i);
}
auto start = std::chrono::high_resolution_clock::now();
heap_Sort(arr, n);
auto finish = std::chrono::high_resolution_clock::now();
auto time = finish - start;
return time;
}
auto best_heap_sort(int n)
{
vector<int> arr;
for (int i = n; i > 0; i--)
{
arr.push_back(i);
}
auto start = std::chrono::high_resolution_clock::now();
heap_Sort(arr, n);
auto finish = std::chrono::high_resolution_clock::now();
auto time = finish - start;
return time;
}
int main()
{
int n;
cout << "Enter the value of n: ";
cin >> n;
int lb = 0;
int rb = 100;
vector<int> arr;
for (int i = 0; i < n; i++)
{
arr.push_back(rand() % (rb - lb + 1));
}
cout << endl;
return 0;
}
OUTPUT: -
GRAPH: -
22500
17500
12500
7500
2500
10 200 500 1000 5000 100000
Best Case (μ sec) 1 44 131 314 1833 31513
Average Case (μ sec) 1 49 152 321 2102 40011
Worst Case (μ sec) 1 50 191 366 2311 33028
4.2 SELECTION SORT
Libraries Used: -
1) Random No Generator:
Library used for generating random number:
< cstdlib>,<time.h>
Function Invoked:
rand()
2) Time of Execution:
Library used for checking time of execution for each run:
<chrono>
Function Invoked:
chrono::high_resolution_clock::now();
3) Dynamic Array:
Library used for checking time of execution for each run:
<vector>
vector<int> arr;
for (int i = 0; i < n; i++)
{
arr.push_back(i);
}
auto start = std::chrono::high_resolution_clock::now();
selection_Sort(arr, n);
auto finish = std::chrono::high_resolution_clock::now();
auto time = finish - start;
return time;
}
auto worst_selection_sort(int n)
{
vector<int> arr;
for (int i = n; i > 0; i--)
{
arr.push_back(i);
}
auto start = std::chrono::high_resolution_clock::now();
selection_Sort(arr, n);
auto finish = std::chrono::high_resolution_clock::now();
auto time = finish - start;
return time;
}
int main()
{
int n;
cout << "Enter the value of n: ";
cin >> n;
vector<int> arr;
for (int i = 0; i < n; i++)
{
arr.push_back(rand());
}
cout << "Time Taken for best case : " << (best_time / std::chrono::microseconds(1)) << "
microseconds" << endl;
auto average_time = average_selection_sort(arr, n);
cout << "Time Taken for average case : " << (average_time / std::chrono::microseconds(1))
<< " microseconds" << endl;
return 0;
}
OUTPUT: -
GRAPH: -
4500
3500
2500
1500
500
10 200 500 1000 2000
Libraries Used: -
1) Random No Generator:
Library used for generating random number:
< cstdlib>,<time.h>
Function Invoked:
rand()
2) Time of Execution:
Library used for checking time of execution for each run:
<chrono>
Function Invoked:
chrono::high_resolution_clock::now();
3) Dynamic Array:
Library used for checking time of execution for each run:
<vector>
#include <iostream>
#include <chrono>
#include <vector>
vector<int> arr;
for (int i = n; i > 0; i--)
{
arr.push_back(i);
}
auto start = std::chrono::high_resolution_clock::now();
radix_Sort(arr, n);
auto finish = std::chrono::high_resolution_clock::now();
auto time = finish - start;
return time;
}
int main()
{
int n;
cout << "Enter the value of n: ";
cin >> n;
int lb = 0;
int rb = 100;
vector<int> arr;
for (int i = 0; i < n; i++)
{
arr.push_back(rand() % (lb + rb + 1));
}
return 0;
}
OUTPUT: -
GRAPH: -
1100
900
700
500
300
100
10 100 500 1000 5000 10000
Best Case (μ sec) 1 8 57 131 772 1559
Average Case (μ sec) 1 11 54 138 742 1628
Worst Case (μ sec) 1 12 55 168 838 1848
PRACTICAL-5
Knapsack Problem:-
Knapsack is like a container or a bag. If we have given some items with weight and profit
then we have to put items in knapsack in such a way that total value produces maximum
profit.
In fractional Knapsack problem we can divide the problem. Fractional knapsack problem can
be solved by greedy approach. The basic idea is calculating profit/weight ratio of each item
and then sort it according to that. Take item having highest ratio and add it as much as can.
For example:-
Consider arr[] = {{100,20},{60,10},{120,30}} and W=50
First of all calculate profit/weight ratio of each item.
100/20 = 5
60/10 = 6
120/30 = 4
Now sort them: {{60,10},{100,20},{120,30}}
Iteration:-
For i = 0 weight=10 <50 so add it in the knapsack. W becomes 40
For i = 1 weight=20<40 so add it in the knapsack. W becomes 20
For i = 2 weight=30>20 fraction of 30 will be added that is 20/30 = 2/3 fraction of item in the
knapsack.
Calculating profit: 60 + 100+ 2/3*120 = 240 which is the maximum profit.
Algorithm:-
Begin
Take an array of structure item
Declare profit, weight, knapsack weight and density
Calculate density = profit/weight for each item
Sort the items of array in decreasing order
Add weight of items of array in knapsack until it is full (Total value <=W)
End
CODE:-
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
int v;
int w;
float d;
} Item;
int main()
{
int W, n;
cout << "enter n: ";
cin >> n;
Item items[n];
input(items, n);
Output :
b. AIM: Program to find Minimum Cost Spanning tree using prims algorithm.
Prim’s Algorithm:-
Prim’s algorithm is the minimum spanning tree algorithm that takes graph as input and find
subset of edges of that graph which forms tree and includes every vertex, has the minimum
sum of weights among all the trees formed from graph.
Algorithm:-
Select a starting vertex
Repeat steps 3 and 4 until there are fringe vertices
Select an edge ‘e’ connecting the tree vertex and fringe vertex that has minimum weight.
Add the selected edge and the vertex to the minimum spanning tree T
Exit
CODE:-
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int spanningTree(int V, vector<vector<int>> adj[])
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> vis(V, 0);
pq.push({0, 0});
int sum = 0;
while (!pq.empty())
{
auto it = pq.top();
pq.pop();
int node = it.second;
int wt = it.first;
if (vis[node] == 1)
continue;
vis[node] = 1;
sum += wt;
for (auto it : adj[node])
{
int adjNode = it[0];
int edW = it[1];
if (!vis[adjNode])
{
pq.push({edW, adjNode});
}
}
}
return sum;
}
};
int main()
{
int V = 5;
vector<vector<int>> edges = {{0, 1, 2}, {0, 2, 1}, {1, 2, 1}, {2, 3, 2}, {3, 4, 1}, {4, 2, 2}};
vector<vector<int>> adj[V];
Solution obj;
int sum = obj.spanningTree(V, adj);
cout << "The sum of all the edge weights: " << sum << endl;
return 0;
}
Output:-
PRACTICAL-6
a. AIM: Program to find minimum cost Spanning tree using Kruskal Algorithm.
In Kruskal’s algorithm sort all edges of given graph in increasing order. Then keeps on
adding new edges and nodes in minimum spanning tree if newly added doesn’t form cycle. It
picks the minimum weighted edge at first and the maximum weighted edge at last.
STEPS:
Sort all the edges in non-decreasing order of their weight.
Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If the
cycle is not formed, include this edge. Else, discard it.
Repeat step#2 until there are (V-1) edges in the spanning tree.
CODE:-
#include <iostream>
#include <algorithm>
using namespace std;
class Edge
{
public:
int source;
int dest;
int weight;
};
int count = 0;
int i = 0;
while (count != n - 1)
{
Edge currentedge = input[i];
int sourceparent = findparent(currentedge.source, parent);
int destparent = findparent(currentedge.dest, parent);
if (sourceparent != destparent)
{
output[count] = currentedge;
count++;
parent[sourceparent] = destparent;
}
i++;
}
cout << "Result is:" << endl;
int main()
{
int n, E;
cout << "Enter number of vertices and edges:";
cin >> n >> E;
Edge input[E];
for (int i = 0; i < E; i++)
{
cout << "Enter " << i + 1 << " connection:";
cin >> input[i].source >> input[i].dest >> input[i].weight;
}
kruskals(input, n, E);
return 0;
}
Output :-
b. AIM: Program to find Minimum Cost Spanning tree using prims algorithm.
Djikstra Algorithm:-
Program to implement Djikstra Algorithm
The idea is to generate a SPT (shortest path tree) with a given source as a root. Maintain an
Adjacency Matrix with two sets,
one set contains vertices included in the shortest-path tree,
other set includes vertices not yet included in the shortest-path tree.
At every step of the algorithm, find a vertex that is in the other set (set not yet included)
and has a minimum distance from the source.
CODE:-
#include <iostream>
#include <limits.h>
using namespace std;
#define V 9
int minDistance(int dist[], bool sptset[])
{
int min = INT_MAX, min_index;
dist[src] = 0;
for (int count = 0; count < V - 1; count++)
{
int u = minDistance(dist, sptset);
sptset[u] = true;
for (int v = 0; v < V; v++)
{
if (!sptset[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] <
dist[v])
dist[v] = dist[u] + graph[u][v];
}
}
printSolution(dist);
}
int main()
{
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0,
2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0,
0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0}};
dijkstra(graph, 0);
return 0;
}
Output:-
PRACTICAL-7
a. AIM: Write a program to implement 0/1 Knapsack Problem.
CODE:-
#include <iostream>
using namespace std;
int max(int x, int y)
{
return (x > y) ? x : y;
}
int knapSack(int W, int w[], int v[], int n)
{
int i, wt;
int K[n + 1][W + 1];
for (i = 0; i <= n; i++)
{
for (wt = 0; wt <= W; wt++)
{
if (i == 0 || wt == 0)
K[i][wt] = 0;
else if (w[i - 1] <= wt)
K[i][wt] = max(v[i - 1] + K[i - 1][wt - w[i - 1]], K[i - 1][wt]);
else
K[i][wt] = K[i - 1][wt];
}
}
return K[n][W];
}
int main()
{
cout << "Enter the number of items in a Knapsack:";
int n, W;
cin >> n;
int v[n], w[n];
for (int i = 0; i < n; i++)
{
cout << "Enter value and weight for item " << i << ":";
cin >> v[i];
cin >> w[i];
}
cout << "Enter the capacity of knapsack: ";
cin >> W;
cout << knapSack(W, w, v, n);
return 0;
}
Output:-
b. AIM: Write a program to solve All pairs shortest path problem using Floyd-
Warshall Algorithm.
CODE:-
#include <iostream>
#include <cstdlib>
#define max 10
#define infi 999
using namespace std;
int p[max][max];
void allpairshort(int a[max][max], int n)
{
int k, i, j;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (a[i][k] + a[k][j] < a[i][j])
{
a[i][j] = a[i][k] + a[k][j];
p[i][j] = k;
}
}
}
}
}
Output:-
PRACTICAL-8
a. AIM: Write a program to solve 8-queen problem using backtracking
approach
CODE:-
#include <bits/stdc++.h>
using namespace std;
int countt = 0;
void print(int board[][4])
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
cout << "-----------------\n";
}
bool isValid(int board[][4], int row, int col)
{
board[i][currentColumn] = 0;
}
}
}
int main()
{
Output:-
b. AIM: Write a program to solve Sum of Subsets problem using backtracking
approach.
CODE:-
#include <iostream>
#include <vector>
using namespace std;
bool isSubsetSum(vector<int> &set, int n, int targetSum, vector<int> &subset)
{
if (targetSum == 0)
{
for (int i = 0; i < subset.size(); i++)
{
cout << subset[i] << " ";
}
cout << endl;
return true;
}
if (n == 0 || targetSum < 0)
{
return false;
}
if (isSubsetSum(set, n - 1, targetSum, subset))
{
return true;
}
subset.push_back(set[n - 1]);
if (isSubsetSum(set, n - 1, targetSum - set[n - 1], subset))
{
return true;
}
subset.pop_back();
return false;
}
int main()
{
vector<int> set = {3, 34, 4, 12, 5, 2};
int targetSum;
cout << "Enter the targetSum: ";
cin >> targetSum;
vector<int> subset;
return 0;
}
Output:-