ADA LAB MANAUL 2024 updated
ADA LAB MANAUL 2024 updated
TECHNOLOGY
(AICTE Approved, VTU Affiliated and NAAC 'A' Accredited)
UG Programs - CSE, ECE, ISE, EIE and EEE have been Accredited by NBA up to 30.06.2025
DR. VISHNUVARDHAN ROAD, CHANNASANDRA, RR NAGAR POST, BENGALURU – 560 098
NAME: .......................................................................................................................................................................................................................................................................................................................................................................................................................................
BRANCH: ..............................................................................................................................................................................................................................................................................................................................................................................................................................
SECTION : ..................................................................................................................................................................................................................................................................................................................................................................................................................................
USN : ..........................................................................................................................................................................................................................................................................................................................................................................................................................................
VISION AND MISSION OF INSTITUTION
Vision
Building RNSIT into a World Class Institution
Mission
To impart high quality education in Engineering, Technology and Management with
a Difference, Enabling Students to Excel in their Career by
1. Attracting quality Students and preparing them with a strong foundation in fundamentals so
as to achieve distinctions in various walks of life leading to outstanding contributions
2. Imparting value based, need based, choice based and skill based professional education to the
aspiring youth and carving them into disciplined, World class Professionals with social
responsibility
3. Promoting excellence in Teaching, Research and Consultancy that galvanizes academic
consciousness among Faculty and Students
4. Exposing Students to emerging frontiers of knowledge in various domains and make them
suitable for Industry, Entrepreneurship, Higher studies, and Research & Development
5. Providing freedom of action and choice for all the Stake holders with better visibility
VISION AND MISSION OF DEPARTMENT
Vision
Mission
The Department of CSE will make every effort to promote an intellectual and ethical
environment by
1. Imparting solid foundations and applied aspects in both Computer Science Theory and
Programming practices
2. Providing training and encouraging R&D and Consultancy Services in frontier areas of
Computer Science and Engineering with a Global outlook
3. Fostering the highest ideals of ethics, values and creating awareness of the role of
Computing in Global Environment
4. Educating and preparing the graduates, highly sought after, productive, and well-
respected for their work culture.
5. Supporting and inducing lifelong learning
ANALYSIS & DESIGN OF ALGORITHMS
LABORATORY-BCSL404
INTERNAL EVALUATION SHEET
Sl.
Parameters Excellent Good Needs
No.
Improvement
a. Design, Program follows syntax Program has few logical Syntax and
and
implementation, and semantics of C programming errors, semantics of C
moderatel programming is not
y clear (1)
demonstration language. Demonstrates demonstrates all
the possible
(5 marks) complete knowledge of concepts implemented in
the program programs (3)
written (5)
b. Result and All test cases are successful, Moderately debugs the Test cases are not
documentation all errors are debugged with programs , few test case taken care , unable to
(5 marks) own practical knowledge and are unsuccessful and debug the errors and
clear documentation Partial documentation no proper
according to the guidelines (3) documentation (1)
(5)
R4: RECORD EVALUATION RUBRIC (MAX MARKS
20)
Sl. Needs
No. Parameter Excellent Good Average Improvement
a. Documentation Meticulous record Write up contains Write up Program written
(20 marks) writing including program and test contains only with few
program, comments cases, but comments program (15) mistakes (10)
and test cases as per are not included
the guidelines (18)
mentioned (20)
TEST-1
TEST-2 𝟒𝟎 2𝟎
10
11
12
Total
Marks 𝟑𝟔𝟎 𝟑𝟎 𝟏𝟎
Final Marks obtained from Lab in charge:
A. Test (20) +
B. Regular Evaluation (10) + 5𝟎 HOD:
C. Record (20)
We have developed this comprehensive laboratory manual on Analysis & Design of Algorithms Lab
with two primary objectives: To make the students comfortable with various data structures for solving
the problems and to train them in choosing efficient algorithmic techniques as an efficient C programmer
by strengthening their programming abilities.
This material provides the students an exposure to problem solving approaches and solutions to
prescribed problems using C programming language. The problems discussed in this manual comprise
of a programming solution, viva questions and practicing programming problems constitute an
indispensable part of this material.
Our profound and sincere efforts will be fruitful only when students acquire extensive knowledge by
reading this manual and apply the concepts learnt apart from the requirements specified in Analysis &
Design of Algorithms Laboratory as prescribed by VTU, Belagavi.
Department of CSE
Analysis & Design of Algorithms Lab (BCSL404)
TABLE OF
CONTENTS
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
@# 16032024
Template for Practical Course and if AEC is a practical Course Annexure-V
@# 16032024
Template for Practical Course and if AEC is a practical Course Annexure-V
● SEE shall be conducted jointly by the two examiners of the same institute, examiners are
appointed by the Head of the Institute.
● The examination schedule and names of examiners are informed to the university before the
conduction of the examination. These practical examinations are to be conducted between the
schedule mentioned in the academic calendar of the University.
● All laboratory experiments are to be included for practical examination.
● (Rubrics) Breakup of marks and the instructions printed on the cover page of the answer script
to be strictly adhered to by the examiners. OR based on the course requirement evaluation
rubrics shall be decided jointly by examiners.
● Students can pick one question (experiment) from the questions lot prepared by the examiners
jointly.
● Evaluation of test write-up/ conduction procedure and result/viva will be conducted jointly by
examiners.
● General rubrics suggested for SEE are mentioned here, writeup-20%, Conduction procedure and
result in -60%, Viva-voce 20% of maximum marks. SEE for practical shall be evaluated for 100 marks
and scored marks shall be scaled down to 50 marks (however, based on course type, rubrics shall be
decided by the examiners)
● Change of experiment is allowed only once and 15% of Marks allotted to the procedure part are to be
made zero.
The minimum duration of SEE is 02 hours
@# 16032024
1.Design and implement C Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm
#include <stdio.h>
#include <stdlib.h>
if (x != y) {
mst[e++] = next_edge;
Union(subsets, x, y);
}
}
int main() {
int V, E;
printf("Enter number of vertices and edges: ");
scanf("%d %d", &V, &E);
kruskalMST(graph);
return 0;
}
OUTPUT:
2.Design and implement C Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm
#include <stdio.h>
#include <limits.h>
// Function to find the vertex with the minimum key value, from the set of vertices not yet included
in the MST
int minKey(int key[], int mstSet[], int V) {
int min = INT_MAX, min_index;
return min_index;
}
// Function to construct and print MST for a graph represented using adjacency matrix
representation
void primMST(int graph[][V_MAX], int V) {
int parent[V_MAX]; // Array to store constructed MST
int key[V_MAX]; // Key values used to pick minimum weight edge in cut
int mstSet[V_MAX]; // To represent set of vertices not yet included in MST
// Always include first 1st vertex in MST. Make key 0 so that this vertex is picked as the first
vertex
key[0] = 0;
parent[0] = -1; // First node is always the root of MST
int main() {
int V, E;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &V, &E);
// Prompt the user to enter the source vertex, destination vertex, and weight for each edge
printf("Enter the source vertex, destination vertex, and weight for each edge:\n");
for (int i = 0; i < E; i++) {
int source, dest, weight;
scanf("%d %d %d", &source, &dest, &weight);
graph[source][dest] = weight;
graph[dest][source] = weight; // Since the graph is undirected
}
return 0;
}
OUTPUT:
3.a. Design and implement C Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.
#include<stdio.h>
int min(int,int);
void floyds(int p[10][10],int n) {
int i,j,k;
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if(i==j)
p[i][j]=0; else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b) {
if(a<b)
return(a); else
return(b);
}
void main() {
int p[10][10],w,n,e,u,v,i,j;
OUTPUT:
Transitive closure:
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
<1,2>=10
<1,3>=3
<1,4>=4
<2,1>=2
<2,3>=5
<2,4>=6
<3,1>=7
<3,2>=7
<3,4>=1
<4,1>=6
<4,2>=16
3b.Design and implement C Program to find the transitive closure using Warshal's algorithm.
#include<stdio.h>
#include<math.h>
int i, j, k;
if (a > b)
return (a);
else
return (b);
void main() {
int p[10][10] = { 0 }, n, e, u, v, i, j;
scanf("%d", &e);
p[u][v] = 1;
printf("%d\t", p[i][j]);
printf("\n");
warshal(p, n);
printf("%d\t", p[i][j]);
printf("\n");
}
OUTPUT:
Transitive closure:
1 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 1 0 1 0
0 1 1 1 1
4.Design and implement C Program to find shortest paths from a given vertex in a weighted
connected graph to other vertices using Dijkstra's algorithm
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
// A function to find the vertex with the minimum distance value, from the set of vertices not yet
included in the shortest path tree
int minDistance(int dist[], bool sptSet[], int V) {
int min = INF, min_index;
return min_index;
}
dist[src] = 0;
// Driver code
int main() {
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
printf("Enter the number of edges: ");
scanf("%d", &E);
printf("Enter the source vertex, destination vertex, and weight for each edge:\n");
for (int i = 0; i < E; i++) {
int source, dest, weight;
scanf("%d %d %d", &source, &dest, &weight);
graph[source][dest] = weight;
graph[dest][source] = weight; // Assuming undirected graph
}
dijkstra(graph, 0, V);
return 0;
}
OUTPUT:
5.Design and implement C Program to obtain the Topological ordering of vertices in a given
digraph.
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
Graph* graph = createGraph(V);
printf("Enter the number of edges: ");
scanf("%d", &E);
printf("Enter the edges (source vertex, destination vertex):\n");
for (int i = 0, src, dest; i < E; i++) {
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
topologicalSort(graph);
return 0;
}
OUTPUT:
6.Design and implement C Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
#include <stdio.h>
// K[n][W] contains the maximum value that can be put in a knapsack of capacity W
return K[n][W];
}
int main() {
int val[100], wt[100]; // Arrays to store values and weights
int W, n; // Knapsack capacity and number of items
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Maximum value that can be obtained: %d\n", knapsack(W, wt, val, n));
return 0;
}
OUTPUT:
7.Design and implement C Program to solve discrete Knapsack and continuous Knapsack
problems using greedy approximation method.
#include<stdio.h>
int main()
{
float weight[50],profit[50],ratio[50],Totalvalue,temp,capacity,amount;
int n,i,j;
printf("Enter the number of items :");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter Weight and Profit for item[%d] :\n",i);
scanf("%f %f", &weight[i], &profit[i]);
}
printf("Enter the capacity of knapsack :\n");
scanf("%f",&capacity);
for(i=0;i<n;i++)
ratio[i]=profit[i]/weight[i];
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
OUTPUT:
8.Design and implement 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.
#include <stdio.h>
#include <stdbool.h>
int main() {
int set[MAX_SIZE];
int subset[MAX_SIZE];
int n, sum;
return 0;
}
OUTPUT:
9.Design and implement 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.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
srand(time(0)); // Seed for random number generation
start = clock();
selectionSort(arr, n);
end = clock();
return 0;
}
OUTPUT:
10.Design and implement C Program 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 n. The elements can be
read from a file or can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
FILE *fp;
fp = fopen("numbers.txt", "w");
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
srand(time(NULL));
for (int i = 0; i < n; i++) {
int num = rand() % 10000;
fprintf(fp, "%d ", num);
}
fclose(fp);
int arr[n];
fp = fopen("numbers.txt", "r");
for (int i = 0; i < n; i++) {
fscanf(fp, "%d", &arr[i]);
}
fclose(fp);
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
return 0;
}
OUTPUT:
11.Design and implement C Program 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 n. The elements can
be read from a file or can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
FILE *fp;
fp = fopen("numbers.txt", "w");
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
srand(time(NULL));
for (int i = 0; i < n; i++) {
int num = rand() % 10000;
fprintf(fp, "%d ", num);
}
fclose(fp);
int arr[n];
fp = fopen("numbers.txt", "r");
for (int i = 0; i < n; i++) {
fscanf(fp, "%d", &arr[i]);
}
fclose(fp);
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
return 0;
}
OUTPUT:
12.Design and implement C Program for N Queen's problem using Backtracking
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
OUTPUT:
Analysis & Design of Algorithms Lab
Appendix-A
This section lists the Viva questions for each lab session:
SELECTION SORT
1. As the input size increases, the performance of selection sort decreases.(T/F)
2. What is the best case, worst case, average case time complexity for quick sort
algorithm?
3. Explain working of selection sort
QUICK SORT
MERGE SORT
1. What is the best case, worst case, average case time complexity for merge sort
algorithm?
2. What is the output of merge sort after the 1st pass given the following sequence of
numbers?
25 57 48 37 12 92 86 33
3. What is the running time of merge sort?
4. What technique is used to sort elements in merge sort?
5. Is merge sort in place sorting algorithm?
6. Define stable sort algorithm. Is merge sort stable?
WARSHALL’S ALGORITHM
1. Define transitive closure.
2. Define topological sequence.
3. What is the time complexity of Warshall's algorithm?
KNAPSACK PROBLEM
1. Define knapsack problem.
2. Define principle of optimality.
3. What is the optimal solution for knapsack problem?
4. What is the time complexity of knapsack problem?
5. Provide a use case where greedy knapsack cannot yield optimal solution.
Analysis & Design of Algorithms Lab
GRAPH TRAVERSALS
1. Define graph, connected graph.
2. List the different graph traversals.
3. Explain DFS traversal.
4. Explain BFS traversal.
5. What are the time complexities of BFS and DFS algorithms?
6. What are the data structures used to implement DFS and BFS traversals?
BACKTRACKING