BCSL404 ADA Lab Programs
BCSL404 ADA Lab Programs
INDEX
S.NO EXPERIMENTS
Introductory Programs
Design and implement C/C++ Program to sort a given set of n integer elements using Selec on
Sort method and compute its me complexity. Run the program for varied values of n> 5000 and
1.
record the me taken to sort. Plot a graph of the me taken versus n. The elements can be read
from a file or can be generated using the random number generator. (9th prog)
Design and implement C/C++ Program to sort a given set of n integer elements using Quick Sort
2.
method and compute its me complexity. Run the program for varied values of n> 5000 and
record the me taken to sort. Plot a graph of the me taken versus n. The elements can be read
from a file or can be generated using the random number generator. (10th prog)
Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort
method and compute its me complexity. Run the program for varied values of n> 5000, and
3.
record the me taken to sort. Plot a graph of the me taken versus n. The elements can be read
from a file or can be generated using the random number generator (11th prog)
4.
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Kruskal's algorithm.
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected
5.
undirected graph using Prim's algorithm
a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
6.
algorithm. b. Warshall’s Algorithm
Design and implement C/C++ Program to find shortest paths from a given vertex in a weighted
7.
connected graph to other ver ces using Dijkstra's algorithm.
Design and implement C/C++ Program to obtain the Topological ordering of ver ces in a given
8.
digraph.
9. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method
Design and implement C/C++ Program to solve discrete Knapsack and con nuous Knapsack
10.
problems using greedy approxima on method
Design and implement C/C++ Program to find a subset of a given set S = {sl , s2,.....,sn} of n
11.
posi ve integers whose sum is equal to a given posi ve integer d.
12 Design and implement C/C++ Program for N Queen's problem using Backtracking
SAMPLE VIVA QUESTIONS AND ANSWERS
1
Analysis & Design of Algorithms Lab (BCSL404)
Introductory Programs:
Euclid’s algorithm for compu ng GCD(m,n)
Program:
#include <stdio.h>
int main() {
int m, n;
prin ("Enter two non-nega ve integers m and n: ");
scanf("%d %d", &m, &n);
// Ensure both m and n are non-nega ve
if (m < 0 || n < 0) {
prin ("Error: Both integers must be non-nega ve.\n");
} else {
int result = euclid(m, n);
prin ("GCD of %d and %d is: %d\n", m, n, result);
}
return 0;
}
Program:
2
Analysis & Design of Algorithms Lab (BCSL404)
#include <stdio.h>
int main() {
int m, n;
prin ("Enter two non-nega ve integers m and n: ");
scanf("%d %d", &m, &n);
// Ensure both m and n are non-nega ve
if (m < 0 || n < 0) {
prin ("Error: Both integers must be non-nega ve.\n");
} else {
int result = consecu veIntegerGCD(m, n);
prin ("GCD of %d and %d is: %d\n", m, n, result);
}
return 0;
}
3
Analysis & Design of Algorithms Lab (BCSL404)
Lab Programs:
1. Design and implement C/C++ Program to sort a given set of n integer elements using Selection 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 n. The elements can be read from a file
or can be generated using the random number generator. (9 th prog)
Aim: Sort a given set of elements using Selection sort and determine the time required to sort elements.
Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a
graph of the time taken versus n.
Definition: selection sort is a sorting routine that scans a list of items repeatedly and, on each pass, selects the
item with the lowest value and places it in its final position. It is based on brute force approach.
Brute force is a straightforward approach to solving a problem, usually directly based on the problem
statement and definitions of the concepts involved. Selection Sort uses Brute Force Approach.
Algorithm start by scanning the entire given list to find its smallest element and exchange it with the
first element, putting the smallest element in its final position in the sorted list (outer loop).
ith pass through the list, i.e. from 0 to n − 2, the algorithm searches for the smallest item among the
last n − i elements and swaps it with Ai:
After n − 1 passes, the list is sorted.
Eg:
89 45 68 90 29 34 17
17 45 68 90 29 34 89
17 29 68 90 45 34 89
17 29 34 90 45 68 89
17 29 34 45 90 68 89
17 29 34 45 68 90 89
17 29 34 45 68 89 90
Code:
#include <stdio.h>
#include <stdlib.h>
4
Analysis & Design of Algorithms Lab (BCSL404)
#include <time.h>
// Function to perform Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[100000];
5
Analysis & Design of Algorithms Lab (BCSL404)
2. Design and implement C/C++ Program to sort a given set of n integer elements using Quick Sort
method and compute its me complexity. Run the program for varied values of n> 5000 and record
the me taken to sort. Plot a graph of the me taken versus n. The elements can be read from a file
or can be generated using the random number generator. (10th prog)
Aim: The aim of this program is to sort ‘n’ randomly generated elements using Quick sort and Plotting the
graph of the time taken to sort n elements versus n.
Definition: Quick sort is based on the Divide and conquer approach. Quick sort divides array according to their
value. Partition is the situation where all the elements before some position s are smaller than or equal to A[s]
and all the elements after position s are greater than or equal to A[s].
Code:
#include <stdio.h>
#include <stdlib.h>
#include < me.h>
6
Analysis & Design of Algorithms Lab (BCSL404)
do {
j--;
} while (arr[j] > pivot);
if (i >= j)
break;
return j;
}
int main() {
int n;
prin ("Enter the number of elements: ");
scanf("%d", &n);
7
Analysis & Design of Algorithms Lab (BCSL404)
int arr[n];
srand( me(NULL));
prin ("Array Generated: ");
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000;
prin ("%d ", arr[i]);
}
prin ("\n");
Output:
8
Analysis & Design of Algorithms Lab (BCSL404)
3. Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort
method and compute its me complexity. Run the program for varied values of n> 5000, and record
the me taken to sort. Plot a graph of the me taken versus n. The elements can be read from a file
or can be generated using the random number generator (11th prog)
Aim:
The aim of this program is to sort ‘n’ randomly generated elements using Merge sort and Plotting the graph
of the time taken to sort n elements versus n.
Definition: Merge sort is a sort algorithm based on divide and conquer technique. It divides the array element
based on the position in the array. The concept is that we first break the list into two smaller lists of roughly
the same size, and then use merge sort recursively on the subproblems, until they cannot subdivide anymore.
Then, we can merge by stepping through the lists in linear time. Its time efficiency is Θ(n log n).
9
Analysis & Design of Algorithms Lab (BCSL404)
Code:
#include <stdio.h>
#include <stdlib.h>
#include < me.h>
int i = 0, j = 0, k = low;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
10
Analysis & Design of Algorithms Lab (BCSL404)
int main() {
int n;
prin ("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
srand( me(NULL));
prin ("Array Generated: ");
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000;
prin ("%d ", arr[i]);
}
prin ("\n");
return 0;
}
Output:
11
Analysis & Design of Algorithms Lab (BCSL404)
4. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Kruskal's algorithm.
Aim : To apply Kruskal's Algorithm for computing the minimum spanning tree is directly based on the generic
MST algorithm. It builds the MST in forest.
Defini on: Kruskal’s algorithm is an algorithm in graph theory that finds a minimum spanning tree for a
connected weighted graph. This means it finds a subset of the edges that forms a tree that includes every
vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it
finds a minimum spanning forest .It is an example of a greedy algorithm.
Start with an empty set A, and select at every stage the shortest edge that has not been chosen or rejected,
regardless of where this edge is situated in graph.
If an edge (u, v) connects two different trees, then (u, v) is added to the set of edges of the MST, and
two trees connected by an edge (u, v) are merged into a single tree.
On the other hand, if an edge (u, v) connects two vertices in the same tree, then edge (u, v) is
discarded.
Code:
#include <stdio.h>
#define MAX_VERTICES 100
12
Analysis & Design of Algorithms Lab (BCSL404)
while (e != n - 1) {
min = 999;
//Selec ng edge from a given input graph in ascending order.
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (c[i][j] < min) {
min = c[i][j];
a = u = i;
b = v = j;
}
}
}
if (u != v) {
prin ("%d --> %d = %d\n", a, b, min);
p[v] = u;
e = e + 1;
cost = cost + min;
}
c[a][b] = c[b][a] = 999;
}
prin ("\nMinimum cost of a spanning tree is %d units\n", cost);
}
int main() {
int i, j, n;
prin ("Enter the number of ver ces: ");
scanf("%d", &n);
int c[MAX_VERTICES][MAX_VERTICES];
prin ("\nMatrix:\n");
for (i = 0; i < n; i++) {
13
Analysis & Design of Algorithms Lab (BCSL404)
kruskalApproach(c, n);
return 0;
}
Output:
14
Analysis & Design of Algorithms Lab (BCSL404)
5. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Prim's algorithm
Aim:To find minimum spanning tree of a given graph using prim’s algorithm
Defini on: Prim’s is an algorithm that finds a minimum spanning tree for a connected weighted undirected
graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total
weight of all edges in the tree is minimized. Prim’s algorithm is an example of a greedy algorithm.
15
Analysis & Design of Algorithms Lab (BCSL404)
Code:
#include <stdio.h>
16
Analysis & Design of Algorithms Lab (BCSL404)
}
}
return sum;
}
int main() {
int i, j, n;
prin ("Enter the number of ver ces: ");
scanf("%d", &n);
int a[MAX_VERTICES][MAX_VERTICES];
prin ("\nMatrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
prin ("%d\t", a[i][j]);
prin ("\n");
}
int source;
prin ("\nEnter the source vertex: ");
scanf("%d", &source);
return 0;
}
Output:
17
Analysis & Design of Algorithms Lab (BCSL404)
18
Analysis & Design of Algorithms Lab (BCSL404)
6. a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.
Code:
#include <stdio.h>
19
Analysis & Design of Algorithms Lab (BCSL404)
}
}
}
}
int main() {
int n;
prin ("Enter the number of ver ces: ");
scanf("%d", &n);
int a[MAX_VERTICES][MAX_VERTICES];
prin ("Enter the weighted graph in form of adjacency matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
if (a[i][j] == 0 && i != j) {
a[i][j] = INF;
}
}
}
prin ("\nMatrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
prin ("%d\t", a[i][j]);
}
prin ("\n");
}
floydAlgorithm(a, n);
return 0;
}
Output:
20
Analysis & Design of Algorithms Lab (BCSL404)
b. Design and implement C/C++ Program to find the transi ve closure using Warshal's algorithm.
Code:
#include <stdio.h>
// Ini alize transi ve closure matrix with the given adjacency matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
transi veClosure[i][j] = a[i][j];
}
}
21
Analysis & Design of Algorithms Lab (BCSL404)
int main() {
int n;
prin ("Enter the number of ver ces: ");
scanf("%d", &n);
int a[MAX_VERTICES][MAX_VERTICES];
prin ("Enter the directed graph in form of adjacency matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
prin ("\nMatrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
prin ("%d\t", a[i][j]);
}
prin ("\n");
}
warshallAlgorithm(a, n);
return 0;
}
Output:
22
Analysis & Design of Algorithms Lab (BCSL404)
23
Analysis & Design of Algorithms Lab (BCSL404)
7. Design and implement C/C++ Program to find shortest paths from a given vertex in a weighted
connected graph to other ver ces using Dijkstra's algorithm.
Code:
#include <stdio.h>
#include <stdbool.h>
24
Analysis & Design of Algorithms Lab (BCSL404)
// Find the vertex with the minimum distance from the source vertex
for (int v = 0; v < n; v++) {
if (!s[v] && d[v] < min) {
min = d[v];
u = v;
}
}
int main() {
int n;
prin ("Enter the number of ver ces: ");
scanf("%d", &n);
int a[MAX_VERTICES][MAX_VERTICES];
prin ("Enter the weighted graph in form of adjacency matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
prin ("\nMatrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
prin ("%d\t", a[i][j]);
25
Analysis & Design of Algorithms Lab (BCSL404)
}
prin ("\n");
}
int source;
prin ("\nEnter the source vertex: ");
scanf("%d", &source);
dijkstraAlgorithm(a, n, source);
return 0;
}
Output:
26
Analysis & Design of Algorithms Lab (BCSL404)
8. Design and implement C/C++ Program to obtain the Topological ordering of ver ces in a given
digraph.
Code:
#include <stdio.h>
#include <stdlib.h>
return graph;
}
27
Analysis & Design of Algorithms Lab (BCSL404)
stack[(*index)++] = vertex;
}
int main() {
int numVer ces, numEdges;
prin ("Enter the number of ver ces: ");
scanf("%d", &numVer ces);
prin ("Enter the number of edges: ");
scanf("%d", &numEdges);
28
Analysis & Design of Algorithms Lab (BCSL404)
topologicalSort(graph);
return 0;
}
Output:
29
Analysis & Design of Algorithms Lab (BCSL404)
9. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic Programming
method
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ITEMS 50
#define max(a, b) ((a > b) ? a : b)
30
Analysis & Design of Algorithms Lab (BCSL404)
int main() {
int n, m;
prin ("Enter the number of items: ");
scanf("%d", &n);
int w[MAX_ITEMS], p[MAX_ITEMS];
prin ("Enter the capacity of knapsack: ");
scanf("%d", &m);
prin ("Enter the weight of items:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &w[i]);
}
prin ("Enter the profit of items:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
knapsack(n, m, w, p);
return 0;
}
Output:
31
Analysis & Design of Algorithms Lab (BCSL404)
32
Analysis & Design of Algorithms Lab (BCSL404)
10. Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack
problems using greedy approximation method
This program first calculates the profit-to-weight ra o for each item, then sorts the items based on this ra o in
non-increasing order. It then fills the knapsack greedily by selec ng items with the highest ra o un l the
knapsack is full. If there's space le in the knapsack a er selec ng whole items, it adds frac onal parts of the
next item. Finally, it prints the op mal solu on and the solu on vector.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ITEMS 50
33
Analysis & Design of Algorithms Lab (BCSL404)
}
return totalProfit;
}
int main() {
int n, capacity;
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Enter the capacity of knapsack: ");
scanf("%d", &capacity);
Item items[MAX_ITEMS];
printf("Enter the weight and profit of each item:\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &items[i].weight, &items[i].profit);
items[i].ratio = (float)items[i].profit / items[i].weight;
}
// Discrete Knapsack
int discreteProfit = discreteKnapsack(n, capacity, items);
printf("Discrete Knapsack - Maximum Profit: %d\n", discreteProfit);
// Continuous Knapsack
float continuousProfit = continuousKnapsack(n, capacity, items);
printf("Continuous Knapsack - Maximum Profit: %.2f\n", continuousProfit);
34
Analysis & Design of Algorithms Lab (BCSL404)
return 0;
}
Output:
35
Analysis & Design of Algorithms Lab (BCSL404)
11. Design and implement C/C++ Program to find a subset of a given set S = {sl , s2,.....,sn} of n positive
integers whose sum is equal to a given positive integer d.
AIM: An instance of the Subset Sum problem is a pair (S, t), where S = {x1, x2, ..., xn} is a set of positive
integers and t (the target) is a positive integer. The decision problem asks for a subset of S whose sum is as
large as possible, but not larger than t.
Code:
#include <stdio.h>
#include <stdbool.h>
int main() {
int n, d;
printf("Enter the number of elements: ");
scanf("%d", &n);
int set[MAX_SIZE];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &set[i]);
36
Analysis & Design of Algorithms Lab (BCSL404)
Output:
37
Analysis & Design of Algorithms Lab (BCSL404)
12. Design and implement C/C++ Program for N Queen's problem using Backtracking
38
Analysis & Design of Algorithms Lab (BCSL404)
// Ini alize board with no queens placed (-1 represents empty cell)
for (int i = 0; i < N; i++) {
board[i] = -1;
}
solveNQueensU l(board, 0, N);
free(board);
}
int main() {
int N;
prin ("Enter the number of queens (N): ");
scanf("%d", &N);
if (N <= 0) {
39
Analysis & Design of Algorithms Lab (BCSL404)
Output:
40
Analysis & Design of Algorithms Lab (BCSL404)
void solveNQueens(int N) {
41
Analysis & Design of Algorithms Lab (BCSL404)
int board[N];
for (int i = 0; i < N; i++) {
board[i] = -1;
}
if (!solveNQueensU l(board, 0, N)) {
prin ("No solu on exists for N = %d.\n", N);
}
}
int main() {
int N;
prin ("Enter the number of queens (N): ");
scanf("%d", &N);
if (N <= 0) {
prin ("Invalid input! Please enter a posi ve integer.\n");
return 1;
}
solveNQueens(N);
return 0;
}
Output:
42
Analysis & Design of Algorithms Lab (BCSL404)
An algorithm is a well-defined computational procedure that take some value as input and generate some
value as output. In simple words, it’s a sequence of computational steps that converts input into the output.
Time complexity of an algorithm indicates the total time needed by the program to run to completion. It is
usually expressed by using the big O notation.
6) The number of arithmetic and the operations that are required to run the program
9) The complexity of three algorithms is given as: O(n), O(n2) and O(n3). Which should execute slowest for
large value of n?
All will execute in same time.
10) In quick sort, the number of partitions into which the file of size n is divided by a selected record is 2.
The three factors contributing to the sort efficiency considerations are the efficiency in coding, machine run
time and the space requirement for running the procedure.
11) How many passes are required to sort a file of size n by bubble sort method?
N-1
12) How many number of comparisons are required in insertion sort to sort a file if the file is sorted in
reverse order?
A. N2
43
Analysis & Design of Algorithms Lab (BCSL404)
13) How many number of comparisons are required in insertion sort to sort a file if the file is already sorted?
N-1
14) In quick sort, the number of partitions into which the file of size n is divided by a selected record is 2
18)The algorithm like Quick sort does not require extra memory for carrying out the sorting procedure. This
technique is called __________.in-place
20) The time factor when determining the efficiency of algorithm is measured by Counting the number of
key operations
23) A sort which compares adjacent elements in a list and switches where necessary is ____.
A. insertion sort
24) The correct order of the efficiency of the following sorting algorithms according to their overall running
time comparison is
bubble>selection>insertion
25) The total number of comparisons made in quick sort for sorting a file of size n, is
A. O(n log n)
27)For the improvement of efficiency of quick sort the pivot can be ______________.
“the mean element”
44
Analysis & Design of Algorithms Lab (BCSL404)
31). What are important problem types? (or) Enumerate some important types of problems.
1. Sorting 2. Searching
3. Numerical problems 4. Geometric problems
5. Combinatorial Problems 6. Graph Problems
7. String processing Problems
38). Specify the algorithms used for constructing Minimum cost spanning tree.
a) Prim’s Algorithm
b) Kruskal’s Algorithm
45
Analysis & Design of Algorithms Lab (BCSL404)
46