DAA Lab Programs IA 1
DAA Lab Programs IA 1
Descriptions: Design, develop, and implement the specified algorithms for the following
problems using C/C++ Language under LINUX /Windows environment.
1 Write C/C++ programs to perform Linear and Binary search of an element from a set
of n elements. Run the program for varied values of n> 5000 and record the time taken
to sort. Plot a graph of the time taken versus non graph sheet. The elements can be read
from a file or can be generated using the random number generator. Demonstrate using
C/C++ how the divide-and conquer method works along with its time complexity
analysis: worst case, average case and best case. Compare the performance of both the
algorithms.
ANS
Program 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
low = 0;
high = n - 1;
1
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
if (arr[mid] == key) {
return mid;
}
else if (arr[mid] < key) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
int main() {
int n1 = 5000, choice, key, index, i, j, n2= 5000;
int arr[10000];
clock_t start, end;
double cpu_time_used= 0.0;
FILE *fp1, *fp2;
fp1 = fopen("lin_search.txt", "w");
fp2 = fopen("bin_search.txt", "w");
srand(time(0));
for(;;) {
printf("\n---- MENU-DRIVEN ----\n");
printf("1. Linear search\n");
printf("2. Binary search\n");
printf("3. Exit\n");
printf("---------------------");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
arr[i] = rand()%10000;
}
key = rand()%10000;
start = clock();
2
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
if (index != -1) {
printf("Number found at index %d\n", index);
}
else {
printf("Number not found\n");
}
end = clock();
cpu_time_used = ((double)(end - start))/CLOCKS_PER_SEC;
fprintf (fp1, "%d\t %lf \n", i, cpu_time_used);
printf("Time taken: %.6f seconds\n", cpu_time_used);
n1 = n1 + 500;
}
fclose(fp1);
break;
case 2:
arr[i] = rand()%10000;
}
key = rand()%10000;
start = clock();
index = binarySearch(arr, n2, key);
if (index != -1) {
printf("Number found at index %d\n", index);
} else {
printf("Number not found\n");
}
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
fprintf (fp2, "%d\t %lf \n", i, cpu_time_used);
printf("Time taken: %.6f seconds\n", cpu_time_used);
n2 = n2 + 500;
}
fclose(fp2);
break;
case 3:
exit(0);
break;
3
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
OUTPUT
lin_search.txt bin_search.txt
4
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
2. Write C/C++ programs to sort a given set of n integer elements using Quick Sort method
and compute its time Complexity. Run the program for varied values of n> 5000 and
record the time taken to sort. Plot a graph of the time taken versus non graph sheet.
The elements can be read from a file or can be generated using the random number
generator. Demonstrate using C/C++ how the divide-and-conquer method works along
with its time complexity analysis: worst case, average case and best case.
ANS Program 2
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
while (i < j) {
while (a[i] <= a[pivot] && i < last)
i++;
while (a[j] > a[pivot])
j--;
5
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
if (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[pivot];
a[pivot] = a[j];
a[j] = temp;
int main() {
int first, last, a[100000], i, j;
clock_t start, end;
double cpu_time_taken;
int n = 1000;
first = 0; last = n - 1;
start = clock();
quicksort(a, first, last);
end = clock();
cpu_time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
6
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
OUTPUT
quick_sort
1. Quick Sort
7
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
3 Write C/C++ programs to Sort a given set of n integer elements using Merge Sort
method and compute its time complexity. Run the program for varied values of n> 5000,
and record the time taken to sort. Plot a graph of the time taken versus non graph sheet.
The elements can be read from a file or can be generated using the random number
generator. Demonstrate using C/C++ how the divide-and-conquer method works along
with its time complexity analysis: worst case, average case and best case.
ANS Program 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int left[leftSize];
int right[rightSize];
int i, j, k;
i = 0;
j = 0;
k = low;
int main() {
int size, n = 1000, j;
int arr[100000];
double cpu_time_used;
clock_t start, end;
FILE *fp1 = fopen("merge_sort.txt", "w");
n = n + 1000;
}
fclose(fp1);
printf("\nMerge Sort results stored in 'merge_sort.txt' for X-graph.\n");
return 0;
}
OUTPUT
merge_sort.txt
1. Merge Sort
10
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
5. From a given vertex in a weighted connected graph, find shortest paths to other vertices
using Dijkstra's algorithm. Write the program in C/C++.
ANS
Program 4
#include<stdio.h>
#define INF 9999
#define MAX 10
while(count < n)
{
mindist = INF;
11
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
if(i != stnode)
{
int main()
{
int G[MAX][MAX], i, j, n ,u;
printf("\nEnter the Number of Vertices: ");
scanf("%d", &n);
printf("\nEnter the Adjacency Matrix: \n");
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d", &G[i][j]);
printf("\nEnter the Starting Node: \n");
scanf("%d", &u);
dijkstra(G, n, u);
return 0;
}
12
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
OUTPUT
Dijkstra’s Algorithm
6. Find Minimum Cost Spanning Tree of a given connected undirected graph using
Kruskal's algorithm. Use Union-Find algorithms in your program. Write the program
in C/C++.
ANS
Program 5
#include<stdio.h>
#include<stdlib.h>
int parent[9];
int find(int i)
{
while (parent[i])
i = parent[i];
return i;
}
13
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
{
parent[j] = i;
return 1;
}
return 0;
}
int main()
{
printf("Kruskal's Algorithm.\n");
int n, i,j;
printf("Enter the Number of Vertices: \n");
scanf("%d", &n);
int cost[9][9];
printf("\n Enter the Cost of Adjacancy Matrix: \n");
for(i=1; i<=n; i++)
14
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
{
for(j=1; j<=n; j++)
{
scanf("%d", &cost[i][j]);
if(cost[i][j] == 0)
cost[i][j] = 999;
}
}
kruskal(cost, n);
return 0;
}
OUTPUT
Kruskal's Algorithm.
minimum Cost = 13
7. Find Minimum Cost Spanning Tree of a given connected undirected graph using Prim's
algorithm. Write the program in C/C++.
ANS
Program 6
#include<stdio.h>
#include<stdlib.h>
int a,b,u,v,n,i,j,ne = 1;
int vis[10] = {0}, min, mincost = 0, cost[10][10];
void main()
{
printf("\n Enter the Number of Vertices: \n");
scanf("%d", &n);
printf("\n Enter the adjacency Matrix: \n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
15
Dept. CSE, BMSIT&M
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Course Code: 21CSEL48B
{
scanf("%d", &cost[i][j]);
if(cost[i][j] == 0)
cost [i][j] = 999;
}
vis[1] = 1;
printf("\n");
while(ne < n)
{
for(i=1, min=999; i<=n; i++)
for(j=1; j<=n; j++)
if(cost[i][j] < min)
if(vis[i] !=0)
{
min = cost[i][j];
a = u = i;
b = v = j;
}
if(vis[u] == 0 || vis[v] == 0)
{
printf("\n edge %d : (%d %d) cost : %d", ne++, a,b,min);
mincost += min;
vis[b] = 1;
}
cost[a][b] = cost[b][a] = 999;
}
edge 1 : (1 2) cost : 3
edge 2 : (2 4) cost : 2
edge 3 : (2 3) cost : 4
edge 4 : (4 5) cost : 4
Minimum cost = 13
16
Dept. CSE, BMSIT&M