RNSIT BCSL404 - ADA Lab Manual
RNSIT BCSL404 - ADA Lab Manual
RNSIT BCSL404 - ADA Lab Manual
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, and 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.
Page No. 2
PROGRAM EDUCATIONAL OBJECTIVES (PEOs)
ISE Graduates within four years of graduation will have
• PEO1: Acquired the fundamentals of computers and applied knowledge of Information
Science & Engineering and continue to develop their technical competencies by problem
solving using programming.
• PEO2: Ability to formulate problems, attained the Proficiency to develop
system/application software in a scalable and robust manner with various platforms, tools
and frameworks to provide cost effective solutions.
• PEO3: Obtained the capacity to investigate the necessities of the software Product, adapt
to technological advancement, promote collaboration and interdisciplinary activities,
Protecting Environment and developing Comprehensive leadership.
• PEO4: Enabled to be employed and provide innovative solutions to real-world problems
across different domains.
• PEO5: Possessed communication skills, ability to work in teams, professional ethics,
social responsibility, entrepreneur and management, to achieve higher career goals, and
pursue higher studies.
PROGRAM OUTCOMES(POs)
Page No. 4
RNS INSTITUTE OF TECHNOLOGY
Dr. VISHNUVARDHAN ROAD, CHANNASANDRA, BENGALURU -560 098
Course Outcomes
At the end of the course the student will be able to:
Develop programs to solve computational problems using suitable algorithm design
CO1
strategy.
Compare algorithm design strategies by developing equivalent programs and observing
CO2
running times for analysis (Empirical).
CO3 Make use of suitable integrated development tools to develop programs.
Choose appropriate algorithm design techniques to develop solution to the computational
CO4
and complex problems.
Demonstrate and present the development of program, its execution and running time(s)
CO5
and record the results/inferences.
CO mapping to PO/PSOs
CO /
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
PO & PSO
CO 1 2 3 3 2 3 1 3 2
CO2 3 3 3 2 3 2 3 3
CO3 3 3 3 2 3 2 3 3
CO4 3 3 3 3 3 2 2 3
CO5 3 3 3 3 3 1 2 3
Page No. 5
Data Structures Laboratory
Evaluation Rubrics
Page No. 7
1. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm.
#include<stdio.h>
int cost[10][10],n;
void kruskal()
{
int par[n];
int a=0,b=0,u=0,v=0,min, mincost = 0, ne = 0;
for(int i=0;i<n;i++)
par[i]=-1;
while(par[v]!=-1)
v=par[v];
if(u!=v)
{
printf("From vertex %d to vertex %d and the cost = %d\n",a,b,min);
mincost+=min;
par[v]=u;
ne++;
}
//edge included in MST should not be considered for next iteration
cost[a][b]=cost[b][a]=999;
}
printf("Cost of MST = %d", mincost);
}
void main()
{
printf("Enter the no. of vertices:");
scanf("%d",&n);
printf("Enter the cost matrix\n");
Page No. 8
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&cost[i][j]);
kruskal();
}
Page No. 9
2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
#include<stdio.h>
int cost[10][10],n;
void prim()
{
int vt[10]={0};
int a=0,b=0,min, mincost = 0, ne = 0;
//start from the first vertex
vt[0] = 1;
while(ne < n-1)
{
//Find the nearest neighbour
min = 999;
for (int i = 0; i<n; i++)
{
if(vt[i]==1)
for(int j = 0;j <n; j++)
if(cost[i][j] < min && vt[j]==0)
{
min = cost[i][j];
a = i;
b = j;
}
}
//Include nearest neighbour 'b' into MST
printf("Edge from vertex %d to vertex %d and the cost %d\n",a,b,min);
vt[b] = 1;
ne++;
mincost += min;
cost[a][b] = cost[b][a] = 999;
}
printf("minimum spanning tree cost is %d",mincost);
}
void main()
{
printf("Enter the no. of vertices: ");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&cost[i][j]);
prim();
}
Sample Input and Output:
Page No. 10
Enter the no. of vertices: 4
Enter the cost matrix
999 2 3 1
2 999 4 999
3 4 999 5
1 999 5 999
Edge from vertex 0 to vertex 3 and the cost 1
Edge from vertex 0 to vertex 1 and the cost 2
Edge from vertex 0 to vertex 2 and the cost 3
minimum spanning tree cost is 6
Page No. 11
3a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
#include<stdio.h>
int main()
{
int n, cost[10][10];
printf("Enter no. of Vertices: ");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
floyd(cost,n);
Page No. 13
3b. Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.
#include<stdio.h>
void main()
{
int n, adj[10][10];
printf("Enter no. of Vertices: ");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
warshal(adj,n);
#include<stdio.h>
int cost[10][10],n,dist[10];
for(int i=0;i<n;i++)
dist[i]=cost[source][i];
int main()
{
Page No. 15
int source;
Page No. 16
5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in
a given digraph.
int cost[10][10],n,colsum[10];
void cal_colsum()
{
for(int j=0;j<n;j++)
{
colsum[j]=0;
for(int i=0;i<n;i++)
colsum[j]+=cost[i][j];
}
}
void source_removal()
{
int i,j,k,select[10]={0};
printf("Topological ordering is:");
for(i=0;i<n;i++)
{
//Calculate the outdegree for each vertices
cal_colsum();
for(j=0;j<n;j++)
{
if(select[j]==0 && colsum[j]==0)//source vertex
break;
}
printf("%d ",j);
select[j]=1;
//Remove source vertex j from cost matrix
for(k=0;k<n;k++)
cost[j][k]=0;
}
}
void main()
{
printf("Enter no. of Vertices: ");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&cost[i][j]);
source_removal();
}
Page No. 17
Sample Input and Output:
Page No. 18
6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
#include<stdio.h>
int n,m,p[10],w[10];
void knapsack_DP()
{
int V[10][10],i,j;
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
if(i==0 || j==0)
V[i][j]=0;
else if(j<w[i])//weight of the item is larger than capacity
V[i][j]=V[i-1][j];
else
V[i][j]=max(V[i-1][j],p[i]+V[i-1][j-w[i]]);//maximization
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
printf("%d ",V[i][j]);
printf("\n");
}
/* tracking back the optimal solution vector */
printf("Items included are:");
while(n > 0)
{
if(V[n][m] != V[n-1][m])
{
printf("%d ",n);
m = m - w[n];
}
n--;
}
}
int main()
{
int i;
printf("Enter the no. of items: ");
scanf("%d",&n);
printf("Enter the weights of n items: ");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("Enter the prices of n items: ");
for(i=1;i<=n;i++)
Page No. 19
scanf("%d",&p[i]);
printf("Enter the capacity of Knapsack: ");
scanf("%d",&m);
knapsack_DP();
}
Page No. 20
7. Design and implement C/C++ Program to solve discrete Knapsack and continuous
Knapsack problems using greedy approximation method.
#include<stdio.h>
int n,m,p[10],w[10];
void greedy_knapsack()
{
float max, profit=0;
int k=0,i,j;
printf("item included is :");
for(i=0;i<n;i++)
{
max=0;
//choose the item which has highest price to weight ratio
for(j=0;j<n;j++)
{
if(((float)p[j])/w[j] > max)
{
k=j;
max=((float)p[j])/w[j];
}
}
//kth element has highest price to weight ratio
if(w[k] <= m )
{
printf("%d",k);
m = m - w[k];
profit=profit+p[k];
p[k]=0;
}
else
break;//unable fit item k into knapsack
}
printf("Discrete Knapsack profit = %f\n",profit);
printf("Continuous Knapsack also includes item %d with portion: %f\n", k, (float)m)/w[k]);
profit = profit + ((float)m)/w[k] * p[k];
printf("Continuous Knapsack profit = %f\n",profit);
}
int main()
{
int i;
printf("Enter the no. of items: ");
scanf("%d",&n);
printf("Enter the weights of n items: ");
for(i=0;i<n;i++)
scanf("%d",&w[i]);
printf("Enter the prices of n items: ");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter the capacity of Knapsack: ");
Page No. 21
scanf("%d",&m);
greedy_knapsack();
}
Sample Input and Output:
Enter the no. of items: 4
Enter the weights of n items: 2 1 3 2
Enter the prices of n items: 12 10 20 15
Enter the capacity of Knapsack: 5
item included is :1 3
Discrete Knapsack profit = 25.000000
Continuous Knapsack also includes item 2 with portion: 0.666667
Continuous Knapsack profit = 38.333332
Page No. 22
8. 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.
#include<stdio.h>
int main()
{
int sum = 0,n;
printf("enter no of elements:");
scanf("%d",&n);
printf("enter the elements in increasing order:");
for( int i = 0; i < n ; i++)
{
scanf("%d",&w[i]);
sum=sum+w[i];
}
printf("eneter the sum:");
scanf("%d",&d);
Page No. 23
1 2 3 4
subset = 2
1 4 5
subset = 3
2 3 5
Page No. 24
9. 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.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a[10000],n,count;
void selection_sort()
{
for(int i=0;i<n-1;i++)
{
int min = i;
for(int j=i+1;j<n;j++)
{
count++;
if(a[j]<a[min])
min=j;
}
int temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
printf("Enter the number of elements in an array:");
scanf("%d",&n);
printf("All the elements:");
srand(time(0));
for(int i=0;i<n;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
selection_sort();
printf("\nAfter sorting\n");
for(int i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}
Sample Input and Output:
Enter the number of elements in an array:5
All the elements:
24152 32742 28304 4804 22274
After sorting
4804 22274 24152 28304 32742
Page No. 25
Number of basic operations = 10
Page No. 26
10. Design and implement C/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 count=0;
int partition(int a[], int low,int high)
{
int pivot=a[low],temp,i=low+1,j=high;
while(1)
{
//Traverse i from left to right, segregating element of left group
while(i<=high && a[i]<=pivot)//a[i]<=pivot used for avoiding multiple duplicates
{
i++; count++;
}
//Traverse j from right to left, segregating element of right group
while(j>0 && a[j]>pivot)
{
j--; count++;
}
count+=2;
//If grouping is incomplete
if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] =temp;
}
else if(i>j)//If grouping is completed
{
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
else //Duplicate of Pivot found
return j;
}
}
Page No. 27
quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}
int main()
{
int a[10000],n;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
printf("All the elements:");
srand(time(0));
for(int i=0;i<n;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
quicksort(a,0,n-1);
printf("\nAfter sorting\n");
for(int i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}
Sample Input and Output:
Enter the number of elements in an array:5
All the elements:
24442 6310 12583 16519 22767
After sorting
6310 12583 16519 22767 24442
Number of basic operations = 18
Page No. 28
11. Design and implement C/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 count=0;
void merge(int a[], int low,int mid,int high)
{
int i,j,k,c[10000];
int main()
{
int a[10000],n,i;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
printf("All the elements:");
Page No. 29
srand(time(0));
for(i=0;i<n;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
merge_sort(a,0,n-1);
printf("\nAfter sorting\n");
for(i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}
Page No. 30
12. Design and implement C/C++ Program for N Queen's problem using Backtracking.
#include<stdio.h>
#include<math.h> //for abs() function
int nqueens(int n)
{
int x[10], k, count=0;
if(x[k] <= n)
{
if(k == n) // all queens are placed
{
printf("\nSolution %d\n",++count);
for(int i=1;i <= n;i++)
{
for(int j=1;j <= n;j++)
printf("%c ",j==x[i]?'Q':'X');
printf("\n");
}
}
else
{
++k; //select the next queen
x[k]=0; // start from the next column
}
}
else
k--; // backtrack
}
return count;
}
void main()
Page No. 31
{
int n;
printf("Enter the size of chessboard: ");
scanf("%d",&n);
printf("\nThe number of possibilities are %d",nqueens(n));
}
Sample Input and Output:
1. Enter the size of chessboard: 4
Solution 1
XQXX
XXXQ
QXXX
XXQX
Solution 2
XXQX
QXXX
XXXQ
XQXX
Solution 1
Q
Page No. 32