0% found this document useful (0 votes)
25 views

20CSI42 - DAA Lab Manual

The document describes a laboratory assignment on algorithms for a computer science course. It includes 10 problems covering sorting, graph, and optimization algorithms like quicksort, mergesort, Dijkstra's algorithm, knapsack problem, Prim's algorithm, and more. Students are asked to write C programs to implement the algorithms for different problems like sorting files by ID, finding shortest paths between cities, minimum spanning tree, and allocating patients to rooms.

Uploaded by

Swathi. N.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

20CSI42 - DAA Lab Manual

The document describes a laboratory assignment on algorithms for a computer science course. It includes 10 problems covering sorting, graph, and optimization algorithms like quicksort, mergesort, Dijkstra's algorithm, knapsack problem, Prim's algorithm, and more. Students are asked to write C programs to implement the algorithms for different problems like sorting files by ID, finding shortest paths between cities, minimum spanning tree, and allocating patients to rooms.

Uploaded by

Swathi. N.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

DESIGN AND ANALYSIS OF

ALGORITHMS
LABORATORY (20CSI42)
For IV SEMESTER

Department of Computer Science & Engineering

By
Prof. SWATHI S & Prof. RAGHAVENDRA B
Assistant Professor, Department of CSE
INDEX
Sl. Page
Program Name
No. No.
Consider a list of ‘n’ files numbered using ID’s. Write a C
1 2-4
program to sort files based on its ID using Quick Sort method.
Consider a list of ‘n’ books numbered using Book_ID’s. Write a
2 C program to sort files based on its Book_ID using Merge Sort 5-6
method.
Suppose a travel agent is interested in finding shortest path from
3 a single city to all the other cities in a network of ‘n’ cities. Write 7-8
a C program to implement this using Djikstra’s algorithm.
Implement 0/1 Knapsack problem using Dynamic
4 9
Programming.
Consider a Electrical layout where ‘n’ houses are connected by
5 electrical wires. Design a ‘C’ program using Prim’s algorithm 10-11
to output a connection with minimum cost.
Consider a network of ‘n’ systems represented as a Graph. Write
6 a ‘C’ program to find the transitive closure of such a network 12-13
using Warshall’s algorithm.
Suppose in a network of cities, you are interested in finding
7 shortest paths between all cities. Design a ‘C’ program to 14-15
implement this using Floyd’s algorithm.
Print all the nodes reachable from a given starting node in a
8 16-17
digraph using BFS method.
Consider a network having ‘n’ systems. Design a DFS based
9 program in ‘C’ which outputs all systems reachable from a given 18-19
system.
Consider ‘N’ patients and ‘N X N’ small rooms. Design a C
program to allot the patients to these rooms using N-queen’s
10 20-21
method such that no two patients are allotted rooms in same
row, column or diagonal.
1. Consider a list of ‘n’ files numbered using ID’s. Write a C program to sort files based on
its ID using Quick Sort method.

#include<stdio.h>
#include<conio.h>
void qsort(int low,int high,int a[ ]);
int partition(int low,int high,int a[ ]);
void main()
{
int i,n,a[10],low,high;
clrscr();
printf("enter the no.of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("elments before sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
qsort(0,n-1,a);
printf("\nelements after sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
void qsort(int low,int high,int a[])
{
int mid;
if(low<high)
{
mid=partition(low,high,a);
qsort(low,mid-1,a);
qsort(mid+1,high,a);
}
}

int partition(int low,int high,int a[ ])


{
int key,i,j,temp;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i<=high&&a[i]<key)
i++;
while(a[j]>key)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}

OUTPUT

enter the no.of elements


5
elments before sorting
5 4 0 2 1
elements after sorting
0 1 2 4 5
2. Consider a list of ‘n’ books numbered using Book_ID’s. Write a C program to sort files
based on its Book_ID using Merge Sort method.
#include<stdio.h>
#include<stdlib.h>
void simple_merge(int a[],int low,int mid ,int high)
{
int i=low;
int j=mid+1;
int k=low;
int c[10];
while(i<=mid&&j<=high)
{
if(a[i]<a[j])
{
c[k]=a[i];
i++;
k++;
}
else
{
c[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
c[k++]=a[i++];
}
while(j<=high)
{
c[k++]=a[j++];
}
for(i=low;i<=high;i++)
{
a[i]=c[i];
}
}
void merge_sort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(a,low,mid);
merge_sort(a,mid+1,high);
simple_merge(a,low,mid,high);
}
}
void main()
{
int a[10],n,i;
printf("enter no of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("elements before sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
merge_sort(a,0,n-1);
printf("sorted elements\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

OUTPUT

Enter number of Elements: 5


elements before sorting : 4 5 3 2 1
sorted elements : 1 2 3 4 5
3. Suppose a travel agent is interested in finding shortest path from a single city to all the
other cities in a network of ‘n’ cities. Write a C program to implement this using Djikstra’s
algorithm.

#include<stdio.h>
#include<conio.h>
int minm(int m,int n)
{
return ((m>n)?n:m);
}
void sssp(int cost[20][20],int n,int source,int dest[20])
{
int s[20],min,w,i,j,v;
for(i=1;i<=n;i++)
{
s[i]=0;
dest[i]=cost[source][i];
}
s[source]=1;
for(i=1;i<=n-1;i++)
{
min=999;
for(j=1;j<=n;j++)
if((s[j]==0) && (min>dest[j]))
{
min=dest[j];
w=j;
}
s[w]=1;
for(v=1;v<=n;v++)
if(s[v]==0)
{
dest[v]=minm(dest[v],(dest[w]+cost[w][v]));
}
}
}
void main()
{
int source,i,j,n;
int cost[20][20],dest[20];
clrscr();
printf("enter no of vertices\n");
scanf("%d",&n);
printf("enter source vertex\n");
scanf("%d",&source);
printf("enter cost matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
sssp(cost,n,source,dest);
printf(":the shortest distance is \n");
for(i=1;i<=n;i++)
printf("the cost from %d to %d is %d\t", source,i,dest[i]);
getch();
}

OUTPUT 1
1 2
enter no of vertices
10
5
enter source vertex
1 3 5 5
enter cost matrix 6

0 1 999 3 10 1
999 0 5 999 999
4 3
999 999 0 999 1
999 999 2 0 6 2
999 999 999 999 0

the shortest distance is


the cost from 1 to 1 is 0
the cost from 1 to 2 is 1
the cost from 1to 3 is 5
the cost from 1 to 4 is 3
the cost from 1 to 5 is 6
4. Implement 0/1 Knapsack problem using Dynamic Programming.

#include<stdio.h>
#include<conio.h>
int p[10],w[10],n;
int g(int i,int m)
{
if((i==n)&&(w[i]<=m))
return p[i];
if((i==n)&&(w[i]>m))
return 0;
if(w[i]>m)
return g(i+1,m);
return max(g(i+1,m),g(i+1,m-w[i])+p[i]);
}
int max(int a,int b)
{
return(a>b)?a:b;
}
void main()
{
int i,m,profit;
clrscr();
printf("Enter number of objects\n");
scanf("%d",&n);
printf("Enter weight & profit\n");
for(i=1;i<=n;i++)
{
scanf("%d%d",&w[i],&p[i]);
}
printf("Enter Knapsack capacity\n");
scanf("%d",&m);
profit=g(1,m);
printf("Maximum profit earned = %d\n",profit);
getch();
}

OUTPUT

Enter number of objects


3
Enter weight & profit
100 20
14 18
10 15
Enter Knapsack capacity
116
Maximum profit earned = 38
5. Consider a Electrical layout where ‘n’ houses are connected by electrical wires. Design a ‘C’
program using Prim’s algorithm to output a connection with minimum cost.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int n,c[20][20],i,j,visited[20];
void prims();
void prims()
{
int min,a,b,k,count=0,cost=0;
visited[1]=1;
while(count<n-1)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if((visited[i]) && (!visited[j]) && (min>c[i][j]))
{
min=c[i][j];
a=i;
b=j;
}
}
}
printf("%d---> %d=%d\n",a,b,c[a][b]);
cost+=c[a][b];
visited[b]=1;
count++;
}
printf("total cost is %d",cost);
}
void main()
{
clrscr();
printf("enter the number of nodes in the graph\n");
scanf("%d",&n);
printf("entr the cost adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&c[i][j]);
visited[i]=0;
}
prims();
getch();
}

OUTPUT
enter the number of nodes in the graph
6
entr the cost adjacency matrix
3 1
999 3 999 999 6 5 1 2 3
3 999 1 6 999 4
999 1 999 6 999 4 6
999 6 6 999 8 5 4
6
6 999 999 8 999 2 5 4
5 4 4 5 2 999 6

1--->2=3 6 5 4
2--->3=1 2 8
2--->6=4 5
6--->5=2
6--->4=5
total cost is 15
6. Consider a network of ‘n’ systems represented as a Graph. Write a ‘C’ program to find the
transitive closure of such a network using Warshall’s algorithm.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],p[10][10],n,i;
clrscr();
printf("\n\n....... Warshall Algorithm ........\n");
printf("Enter number of nodes\n");
scanf("%d",&n);
printf("Enter the matrix\n");
read_adj(n,a);
warshall(n,a);
printf("\n Path Matrix\n");
write_matrix(n,a);
getch();
}
read_adj(int n,int a[10][10])
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
return;
}
write_matrix(int n,int a[10][10])
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return;
}
warshall(int n,int a[10][10])
{
int i,j,k;
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i][j]==0)
{
if((a[i][k]==1) && (a[k][j]==1))
{
a[i][j]=1;
}
}
}
}
}
return;
}

OUTPUT

....... Warshall Algorithm ........


Enter number of nodes
4
1 2
Enter the matrix
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
4 3
Path Matrix
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
7. Suppose in a network of cities, you are interested in finding shortest paths between all
cities. Design a ‘C’ program to implement this using Floyd’s algorithm.

#include<stdio.h>
int n,c[10][10],i,j,k,d[10][10];
void read_data();
void write_data();
void floyd();
void main()
{
printf("enter no.of nodes\n");
scanf("%d",&n);
printf("enter the cost adjacency matrix\n");
read_data();
floyd(c,n);
write_data();
}
void read_data()
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&c[i][j]);
}
}
}
void floyd(int c[10][10],int n)
{
int min(int,int);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[i][j]=c[i][j];
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
}
void write_data()
{
printf("the distance matrix is shown below\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
}
int min(int a,int b)
{
return a<b?a:b;
}

OUTPUT

enter the number of nodes 5


4 1 2
enter the cost adjacency matrix
0 5 999 2 1 6
999 0 6 999 2
999 999 0 3
999 1 999 0
4 3
the distance matrix is shown below 3

0 392
999 0 6 9
999 4 0 3
999 1 7 0
8. Print all the nodes reachable from a given starting node in a digraph using BFS method.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int n,a[10][10],src,i,s[10];
clrscr();
printf("______________BFS________________\n");
printf("enter the no.of vertices in the graph\n");
scanf("%d",&n);
printf("enter the adjacency matrix\n");
read_adj(n,a);
for(i=1;i<=n;i++)
s[i]=0;
printf("enter the source vertex\n");
scanf("%d",&src);
bfs(n,a,src,s);
for(i=1;i<=n;i++)
{
if(s[i]==1)
printf("\n %d is reachable\n",i);
else
printf("\n %d is not reachable\n",i);
}
getch();
}

read_adj(int n,int a[10][10])


{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
return;
}

int bfs(int n,int a[][10],int src,int s[])


{
int u,v,f,r,q[10];
q[f=r=0]=src;

while(f<=r)
{
u=q[f++];
for(v=1;v<=n;v++)
{
if((a[u][v]==1)&&(s[v]==0))
{
q[++r]=v;
s[v]=1;
}
}
}
return;
}

OUTPUT
______________BFS________________

enter the no.of vertices in the graph


4
enter the adjacency matrix
0 1 0 0 1 2
0 0 0 0
1 0 0 1
0 1 0 0
enter the source vertex
1
4 3
1 is not reachable
2 is reachable
3 is not reachable
4 is not reachable
9. Consider a network having ‘n’ systems. Design a DFS based program in ‘C’ which outputs
all systems reachable from a given system.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void dfs(int u,int n,int a[][10],int s[]);
void read_data(int n,int a[][10]);
int checkon(int n,int a[][10]);
void read_data(int n,int a[][10])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void dfs(int u,int n,int a[][10],int s[])
{
int v;
s[u]=1;
for(v=0;v<n;v++)
{
if((a[u][v]==1)&&(s[v]==0))
{
dfs(v,n,a,s);
}
}
}
int checkon(int n,int a[][10])
{
int i,j,flag,s[10],sol[10][10];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
s[j]=0;
}
dfs(i,n,a,s);
for(j=0;j<n;j++)
sol[i][j]=s[j];
}
flag=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((sol[i][j]==0) && (sol[j][i]==0))
{
flag=0;
}
}
}
return flag;
}
void main()
{
int result,n,a[10][10];;
clrscr();
printf("enter the number of nodes\n");
scanf("%d",&n);
printf("enter the adjacency matrix\n");
read_data(n,a);
result=checkon(n,a);
if(result==1)
printf("the graph is connected\n");
else
printf("the graph is not connected\n");
getch();
}

OUTPUT 1: OUTPUT 2:

enter the number of nodes 1 2 enter the number of nodes 1 2


4 4
enter the adjacency matrix enter the adjacency matrix
0 1 0 1 0 1 1 0
1 0 1 0 1 0 1 0
0 1 0 1 1 1 0 0 4 3
1 0 1 0 4 3 0 0 0 0
the graph is connected the graph is not connected
10. Consider ‘N’ patients and ‘N X N’ small rooms. Design a C program to allot the patients
to these rooms using N-queen’s method such that no two patients are allotted rooms in
same row, column or diagonal.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
void print_solution(int n,int x[])
{
int c[10][10],i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
c[i][j]='X';

for(i=1;i<=n;i++)
{
c[i][x[i]]='Q';
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%c",c[i][j]);
}
printf("\n\n");
}
}

int place(int x[],int k)


{
int i;
for(i=1;i<k;i++)
{
if(x[i]==x[k]||(i-x[i]==k-x[k])||(i+x[i])==(k+x[k]))
return FALSE;
}
return TRUE;
}
void nqueens(int n)
{
int x[10],count=0,k=1;
x[k]=0;
while(k!=0)
{
x[k]+=1;
while((x[k]<=n)&&(!place(x,k)))
x[k]+=1;
if(x[k]<=n)
{
if(k==n)
{
count++;
printf("solution=%d\n",count);
print_solution(n,x);
}
else
{
k++;
x[k]=0;
}
}
else
k--;
}
}
void main()
{
int n;
clrscr();
printf("enter the no.of queens\n");
scanf("%d",&n);
nqueens(n);
getch();
}
OUTPUT
enter the number of queens
4
Solution: 1 Solution: 2
XQXX XXQX
XXXQ QXXX
QXXX XXXQ
XXQX XQXX

You might also like