Ada Lab Manual-2024
Ada Lab Manual-2024
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: [email protected]
Affiliated to VTU, Belagavi| Approved by AICTE, New Delhi| NAAC& NBA Accredited| UGC 2(f) Certified| Recognized
by Govt. of Karnataka
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & MACHINE
LEARNING ENGINEERING 2024
ANALYSIS AND DESIGN OF ALGORITHMS
LAB MANUAL
(BCSL404)
Compiled by:
Vision
To become a premier institute transforming our students to be global
professionals.
Mission
M1: Develop competent Human Resources, and create state-of-the-art
infrastructure to impart quality education and to support research.
M3: Nurture and train students to develop the qualities of global professionals.
CAMBRIDGE INSTITUTE OF TECHNOLOGY
Vision
To enrich the academic acumen in the field of artificial intelligence and machine
learning coupled with providing sustainable solutions to global challenges.
Mission
M2: Design and develop adequate infrastructure for patentable technologies resulting in
useful products
M3: Equip the students in fulfilling duty of serving the nation by ensuring self-reliance in
technological prowess
CAMBRIDGE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING
PEO1: To provide students learn skills in the domain of artificial intelligence and machine learning.
PEO2: To strengthen students’ knowledge for higher studies in premier institutes in India and abroad.
PEO4: To augment new frontiers of research in artificial intelligence and machine learning
throughsystematic process.
PEO5: To enable the academic community to render its service to the nation by reducing technological
barriers in the domain.
PSO1: To gain fundamental knowledge of computing paradigms with the intricacies through laboratory
experiments
PSO2: To design and develop artificial intelligence systems such as mobile robots, drones and intelligence
software systems
PSO3: To use intelligence system with machine learning to solve challenging problems in defence, healthcare
and industrial applications.
TABLE OF CONTENTS:
S.NO EXPERIMENTS
1 Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm.
2 Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
3 a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.
b. Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.
4 Design and implement C/C++ Program to find shortest paths from a given vertex in a weighted
connected graph to other vertices using Dijkstra's algorithm.
5 Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given
digraph.
6 Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
7 Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack
problems using greedy approximation method.
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.
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.
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.
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.
12 Design and implement C/C++ Program for N Queen's problem using Backtracking
#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");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&cost[i][j]);
kruskal();
}
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:
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);
3b. Design and implement C/C++ Program to find the transitive closure using
Warshal's algorithm.
#include<stdio.h>
void warshal(int A[][10],int n)
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
A[i][j]=A[i][j] || (A[i][k] && A[k][j]);
}
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);
4.Design and implement C/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>
int cost[10][10],n,dist[10];
int minm(int m, int n)
{
return(( m < n) ? m: n);
}
for(int i=0;i<n;i++)
dist[i]=cost[source][i];
int main()
{
int source;
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();
}
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++)
scanf("%d",&p[i]);
printf("Enter the capacity of Knapsack: ");
scanf("%d",&m);
knapsack_DP();
}
#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: ");
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
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 x[10], w[10], count, d;
void sum_of_subsets(int s, int k, int rem)
{
x[k] = 1;
if( s + w[k] == d)
{
//if subset found
printf("subset = %d\n", ++count);
for(int i=0 ; i <= k ; i++)
if ( x[i] == 1)
printf("%d ",w[i]);
printf("\n");
}
else if ( s + w[k] + w[k+1] <= d )//left tree evaluation
sum_of_subsets(s+w[k], k+1, rem-w[k]);
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);
enter no of elements:5
enter the elements in increasing order:1 2 3 4 5
enter the sum:10
subset = 1
1 2 3 4
subset = 2
1 4 5
subset = 3
2 3 5
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
Number of basic operations = 10
Enter the number of elements in an array:10 All
the elements:
24243 6017 4212 23217 16170 24802 1085 24280 9847 6392
After sorting
1085 4212 6017 6392 9847 16170 23217 24243 24280 24802
Number of basic operations = 45
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;
}
}
void quicksort(int a[],int low, int high)
{
int s;
if(low<high)
{
//partition to place pivot element in between left and right group
s = partition(a,low,high);
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
#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];
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()
{
int n;
printf("Enter the size of chessboard: ");
scanf("%d",&n);
printf("\nThe number of possibilities are %d",nqueens(n));
}
Solution 1
XQXX
XXXQ
QXXX
XXQX
Solution 2
XXQX
QXXX
XXXQ
XQXX
The number of possibilities are 2
1. Enter the size of chessboard: 3
Solution 1
Q