NEP ADA LAB
NEP ADA LAB
LABORATORY MANUAL
PREPARED BY
Prof. Rashmi P C
Assistant Professor, Dept. of BCA
Brindavan College
Department of BCA
IV Semester
ACADEMIC YEAR
2024 – 2025
BATCH :
PREPARED BY
LABORATORY CERTIFICATE
This is to certify that Mr. /Ms. ____
bearing USN___ of __ branch
and ____ semester has satisfactorily completed the course of experiments in Design
and Analysis of Algorithms Lab, code CA-C17P prescribed by the Bengaluru City
University, Bengaluru of this Institute for the academic year 2024 – 2025.
MARKS
Date:
Brindavan College
Dwarakanagar, Bagalur Main Road, Yelahanka, Bengaluru - 560063
Approved by AICTE, Recognized by Government & UGC
Affiliated to Bengaluru City University, Karnataka, India
Accredited at the “B”+ Level by NAAC
Department of BCA
DEPARTMENT VISION
DEPARTMENT MISSION
Department of BCA
DOs
➢ Be on time and students should carry observation and completed records in all
aspects.
➢ Dress code & wearing ID card is compulsory.
➢ Electronic gadgets are not allowed inside the lab.
➢ Students should be at their concerned desktop.
➢ After execution the students should get it verified by the concerned faculty.
➢ The executed results should be noted in their observations and get it verified by the
concerned faculty.
➢ Observe good housekeeping practices. Keep the equipment’s in proper place after the
conduction.
➢ Students must ensure that all the switches are in the OFF position; desktop is
shutdown properly after completion of the assignments.
DON’Ts
Course objectives:
• Understand the fundamental concepts of algorithms and their design principles.
• Analyze the time and space complexity of algorithms using Big O, Omega, and Theta notations.
• Apply sorting and searching algorithms to solve computational problems.
• Design and implement algorithms using techniques like divide-and-conquer, dynamic
programming, and greedy strategies
Laboratory Experiments
SI.NO. Experiments
1 Write a program to implement linear search algorithm Repeat the
experiment for different values of n, the number of elements in the list to be
searched and plot a graph of the time taken versus n.
2 Write a program to implement binary search algorithm. Repeat the
experiment for different values of n, the number of elements in the list to be
searched and plot a graph of the time taken versus n.
3 Write a program to solve towers of honai problem and execute it for
different number of disks.
Write a program to find the minimum spanning tree of a given graph using
13
Prim’s algorithm
14 Write a Program to obtain the topological ordering of vertices in a given
digraph. Compute the transitive closure of a given directed graph using
Warshall's algorithm.
15 Write a Program to Find a subset of a given set S = {s1,s2, ,sn} of n positive
integers whose ..... sum is equal to a given positive integer d. For example, if
S= {1, 2, 5, 6, 8} and d = 9 there are two solutions {1,2,6} and {1,8}.A suitable
message is to be displayed if the given problem instance doesn't have a
solution
Course outcomes:
At the end of the course the student will be able to:
int main() {
int n, i, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Repeat the search operation multiple times to amplify the time taken
int repeat = 1000000;
int result;
if (result != -1) {
printf("\nKey %d Found at Position %d", key, result);
} else {
printf("\nKey %d Not Found", key);
}
return 0;
}
OUTPUT
Run 1:
Enter the number of elements: 5
Enter the elements of an array: 10 20 30 40 50
Enter the key element to be searched: 50
Key 50 found at 4
Time taken to search a key element = 64.000000 milliseconds
Run 2:
Enter the number of elements: 10
Enter the elements of an array: 10 20 30 40 50 100 90 80 70 60
Enter the key element to be searched: 99
Key 99 not found
Time taken to search a key element = 134.000000 milliseconds.
Run 3:
Enter the number of elements: 20
Enter the elements of an array: 10 20 30 30 40 50 60 70 80 90 100 110 120 130 140
150 160 170 180 200
Enter the key element to be searched: 200
Key 200 found at position 19
Time taken to search a key element = 404.000000 milliseconds.
OUTPUT
int main() {
int n, i, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Repeat the search operation multiple times to amplify the time taken
int repeat = 1000000;
int result;
if (result != -1) {
printf("\nKey %d Found at Position %d", key, result);
} else {
printf("\nKey %d Not Found", key);
}
free(arr);
return 0;
}
OUTPUT
Run 1:
Enter the number of elements: 5
Enter the elements of an array in ascending order: 10 20 30 40 50
Enter the key element to be searched: 50
Key 50 Found at Position 4
Time taken to search a key element = 83.000000 milliseconds
Run 2:
Enter the number of elements: 10
Enter the elements of an array in ascending order: 10 20 30 40 50 60 70 80 90
100
Enter the key element to be searched: 120
Key 120 Not Found
OUTPUT
Time taken to search a key element = 127.000000 milliseconds
Run :
Enter the number of elements: 20
Enter the elements of an array in ascending order: 10 20 30 40 50 60 70 80 90
100 110 120 130 140 150 160 170 180 190 200
Enter the key element to be searched: 200
Key 200 Found at position 19
Time taken to search a key element = 156.000000 milliseconds
OUTPUT
int main() {
char source = 'S', temp = 'T', dest = 'D';
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
toh(n, source, dest, temp);
printf("The number of moves: %d", count);
return 0;
}
Run 1
Run 2
OUTPUT
OUTPUT
Run 1:
Enter the number of elements: 500
Populating the array with random numbers...
Time taken to sort the array = 1.000000 ms
Run 2:
Enter the number of elements: 1000
Populating the array with random numbers...
Time taken to sort the array = 8.000000 ms
Run 3:
Enter the number of elements: 1500
Populating the array with random numbers...
Time taken to sort the array = 16.000000 ms
Run 3:
Enter the number of elements: 2000
Populating the array with random numbers...
Time taken to sort the array = 29.000000 ms
OUTPUT
#include<stdio.h>
int main() {
int a, n;
printf("Enter the value of a and n: ");
scanf("%d %d", &a, &n);
return 0;
}
OUTPUT
OUTPUT
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
OUTPUT
Run 1:
Enter the number of elements: 1000
Populating the array with random numbers...
Time taken to sort the array = 0.000000 milli seconds
Run 2:
Enter the number of elements: 5000
Populating the array with random numbers...
Time taken to sort the array = 0.000000 milli seconds
Run 3:
Enter the number of elements: 10000
Populating the array with random numbers...
Time taken to sort the array = 7.000000 milli seconds
Run 4:
Enter the number of elements: 15000
Populating the array with random numbers...
Time taken to sort the array = 10.000000 milli seconds
Run 5:
Enter the number of elements: 20000
Populating the array with random numbers...
Time taken to sort the array = 15.000000 milli seconds
OUTPUT
int main() {
int n, k;
printf("Enter the values of n and k: ");
scanf("%d %d", &n, &k);
return 0;
}
OUTPUT
OUTPUT
#include<stdio.h>
#include<limits.h>
#define INF 99999
int i, j;
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (D[i][j] == INF)
printf("%s", "INF");
else
printf("%d ", D[i][j]);
}
printf("\n");
}
}
// Implementing Floyd Warshall Algorithm
void floyd(int V, int **C) {
int i, j, k;
int *D = (int *)malloc(V * sizeof(int *));
for (i = 0; i < V; i++)
D[i] = (int *)malloc(V * sizeof(int));
printSolution(V, D);
}
int main() {
int i, j, V;
printf("Enter the number of vertices: ");
scanf("%d", &V);
floyd(V, C);
return 0;
OUTPUT
OUTPUT
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main() {
int i, n;
printf("Enter the degree of the polynomial: ");
scanf("%d", &n);
double x;
printf("Enter the value of x: ");
scanf("%lf", &x);
start = clock();
double brute_force_result = bruteForce(coef, n, x);
end = clock();
time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Brute force result: %.2f, time used: %f seconds\n", brute_force_result,
time_used);
start = clock();
double horners_rule_result = hornersRule(coef, n, x);
end = clock();
time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Horner rule result: %.2f, time used: %f seconds\n", horners_rule_result,
time_used);
free(coef);
return 0;
}
OUTPUT
[Let us evaluate value of 2x^3 - 6x^2 + 2x - 1 for x = 3, co-efficients are {2, -6, 2, -1}]
Enter the degree of the polynomial: 3
Enter the coefficients from highest degree to lowest:
2
-6
2
-1
Enter the value of x:3
Brute force result :5, time used : 0.000000 seconds
Honer rule result :5, time used : 0.000000 seconds
OUTPUT
int badchar[MAX_CHARS];
if (j < 0) {
printf("\nPattern occurs at position = %d", s);
s += (s + m < n) ? m - badchar[(int)text[s + m]] : 1;
} else
s += max(1, j - badchar[(int)text[s + j]]);
}
}
int main() {
char text[100];
pat[100];
patternSearch(text, pat);
return 0;
}
OUTPUT
OUTPUT
// Create lps[] that will hold the longest prefix suffix values for pattern
int lps[M];
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
int j = 0; // index for pat[]
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
printf("Found pattern at index %d \n", i - j);
j = lps[j - 1];
} else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1; }
}
}
// Fills lps[] for given pattern 'pat' of length 'M'
void computeLPSArray(char* pat, int M, int* lps) {
int len = 0; // length of the previous longest prefix suffix
lps[0] = 0; // lps[0] is always 0
KMPSearch(pat, txt);
return 0;
OUTPUT
Enter the text: SKYWARDPUBLISHERS
Enter the pattern: PUB
Found Pattern at index 7
OUTPUT
int C[MAX][MAX];
int visited[MAX];
int queue[MAX];
int n; // Number of vertices
void BFS(int V) {
int front = 0, rear = -1;
visited[V] = 1;
queue[++rear] = V;
int main() {
int i, j, V;
return 0;
}
OUTPUT
RUN 1
011000
101100
110110
011011
001101
000110
• Starting vertex: 1
• BFS Traversal Output: 1 2 3 4 5 6
OUTPUT
#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
return min_index;
}
int i, count, u, v;
for (i = 0; i < n; i++) {
key[i] = INT_MAX, mstSet[i] = 0;
}
key[0] = 0;
parent[0] = -1;
int main() {
int n, i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
primMST(c, n);
return 0;
}
OUTPUT
02060
20385
03007
68009
05790
Edge Weight
1-2 2
2-3 3
1-4 6
2-5 5
OUTPUT
int main()
{
int n, count = 0, c[10][10], indeg[10], flag[10], i, j, k;
while(count < n)
{
for(k = 0; k < n; k++)
{
if((indeg[k] == 0) && (flag[k] == 0))
{
printf("%d ", (k + 1));
flag[k] = 1;
}
return 0;
}
OUTPUT
warshallsc(c, n);
return 0;
}
OUTPUT
OUTPUT
#include<stdio.h>
int main() {
int sum = 0, k;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
OUTPUT
Subset 1 = 7 11 13
Subset 2 = 7 24