DAA LAB
DAA LAB
Exp 1:
Aim:
Write a C program to Obtain The Topological Ordering of Vertices in a Given
Digraph
Program:
#include<stdio.h>
int temp[10],k=0;
void topo(int n,int indegree[10],int a[10][10])
{
int i,j;
for(i=1;i<=n;i++)
{
if(indegree[i]==0)
{
indegree[i]=1;
temp[++k]=i;
for(j=1;j<=n;j++)
{
if(a[i][j]==1&&indegree[j]!=-1)
indegree[j]--;
}
i=0;
}
}
}
void main()
{
int i,j,n,indegree[10],a[10][10];
printf("enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
indegree[i]=0;
printf("\n enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==1)
indegree[j]++;
}
topo(n,indegree,a);
if(k!=n)
printf("topological ordering is not possible\n");
else
{
printf("\n topological ordering is :\n");
for(i=1;i<=k;i++)
printf("v%d\t",temp[i]);
}
}
Input/Output:
Result:
Program:
#include<stdio.h>
int i,visit[20],n,adj[20][20],s,topo_order[10];
void dfs(int v)
{
int w;
visit[v]=1;
for(w=1;w<=n;w++)
if((adj[v][w]==1) && (visit[w]==0))
dfs(w);
topo_order[i--]=v;
}
void main ()
{
int v,w;
printf("Enter the number of vertices:\n");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(v=1;v<=n;v++)
for(w=1;w<=n;w++)
scanf("%d",&adj[v][w]);
for(v=1;v<=n;v++)
visit[v]=0;
i=n;
for(v=1;v<=n;v++)
{
if(visit[v]==0)
dfs(v);
}
printf("\nTopological sorting is:");
for(v=1;v<=n;v++)
printf("v%d ",topo_order[v]);
}
Input/Output:
Result:
Exp 2:
Aim:
To Sort a given set of elements using the Quick sort method and determine the
time required to sort the elements.
Program:
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<utime.h>
void quicksort(int[],int, int);
int partition (int[],int,int);
void main()
{
int i,n,a[20],ch=1;
clock_t start,end;
clrscr();
while(ch)
{
printf("\n enter the number of elements\n");
scanf("%d",&n);
printf("\n enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
start =clock();
quicksort(a,0,n-1);
end=clock();
printf("\n\nthe sorted array elements are\n\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
printf("\nTime taken=%lf",(end-start)/CLK_TCK);
printf("\n\n do u wish to continue (0/1)\n");
scanf("%d",&ch);
}
getch();
}
void quicksort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=partition(a,low,high);
quicksort(a,low,mid-1);
quicksort(a,mid+1,high);
}
}
int partition(int a[],int low,int high)
{
int key,i,j,temp,k;
key=a[low];
i=low+1;
j=high;
while(i<=j)
{
while(i<=high && key>=a[i])
i=i+1;
while(key<a[j])
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
k=a[j];
a[j]=a[low];
a[low]=k;
}
}
return j;
}
Input/Output:
Result:
Exp 3:
Aim:
To Sort a given set of elements using the Merge sort method and determine the
time required to sort the elements.
Program:
#include<stdio.h>
#include<conio.h>
#include<time.h>
#define max 20
void mergesort(int a[],int low,int high);
void merge(int a[],int low,int mid,int high);
void main()
{
int n,i,a[max],ch=1;
clock_t start,end;
clrscr();
while(ch)
{
printf("\n\t enter the number of elements\n");
scanf("%d",&n);
printf("\n\t enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
start= clock();
mergesort(a,0,n-1);
end=clock();
printf("\nthe sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("\n\ntime taken=%lf",(end-start)/CLK_TCK);
printf("\n\ndo u wish to continue(0/1) \n");
scanf("%d",&ch);
}
getch();
}
void mergesort(int a[],int low,int high) {
int mid;
delay(100);
if(low<high) {
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[],int low,int mid,int high) {
int i,j,k,t[max];
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
if(a[i]<=a[j])
t[k++]=a[i++];
else
t[k++]=a[j++];
while(i<=mid)
t[k++]=a[i++];
while(j<=high)
t[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=t[i];
}
Input/Output:
Result:
Exp 4:
Aim:
To Check whether a given graph is connected or not using DFS method.
Program:
#include<stdio.h>
int visit[20],n,adj[20][20],s,count=0;
void dfs(int v)
{
int w;
visit[v]=1;
count++;
for(w=1;w<=n;w++)
if((adj[v][w]==1) && (visit[w]==0))
dfs(w);
}
void main()
{
int v,w;
printf("Enter the no.of vertices:");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(v=1;v<=n;v++)
for(w=1;w<=n;w++)
scanf("%d",&adj[v][w]);
for(v=1;v<=n;v++)
visit[v]=0;
dfs(1);
if(count==n)
printf("\nThe graph is connected");
else
printf("The graph is not connected");
}
Input/Output:
Result:
Exp 5:
Aim:
To Simulate Bankers Algorithm for deadlock prevention.
Program:
#include<stdio.h>
#define size 20
#define true 1
#define false 0
int queue[size],visit[20],rear=-1,front=0;
int n,s,adj[20][20],flag=0;
void insertq(int v)
{
queue[++rear]=v;
}
int deleteq()
{
return(queue[front++]);
}
int qempty()
{
if(rear<front)
return 1;
else
return 0;
}
void bfs(int v)
{
int w;
visit[v]=1;
insertq(v);
while(!qempty())
{
v=deleteq();
for(w=1;w<=n;w++)
if((adj[v][w]==1) && (visit[w]==0))
{
visit[w]=1;
flag=1;
printf("v%d\t",w);
insertq(w);
}
}
}
void main()
{
int v,w;
printf("\n Enter the no.of vertices:\n");
scanf("%d",&n);
printf("Enter adjacency matrix:");
for(v=1;v<=n;v++)
{
for(w=1;w<=n;w++)
scanf("%d",&adj[v][w]);
}
printf("Enter the start vertex:");
scanf("%d",&s);
printf("Reachability of vertex %d\n",s);
for(v=1;v<=n;v++)
visit[v]=0;
bfs(s);
if(flag==0)
{
printf("No path found!!\n");
}
}
Input/Output:
Result:
Exp 6:
Aim:
To Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.
Program:
# include <stdio.h>
int Prim (int g[20][20], int n, int t[20][20])
{
int u,v, min, mincost;
int visited[20];
int i,j,k;
visited[1] = 1;
for(k=2; k<=n; k++)
visited[k] = 0 ;
mincost = 0;
for(k=1; k<=n-1; k++)
{
min= 99;
u=1;
v=1;
for(i=1; i<=n; i++)
if(visited[i]==1)
for(j=1; j<=n; j++)
if( g[i][j] < min )
{
min = g[i][j];
u = i;
v = j;
}
t[u][v] = t[v][u] = g[u][v] ;
mincost = mincost + g[u][v] ;
visited[v] = 1;
printf("\n (%d, %d) = %d", u, v, t[u][v]);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if( visited[i] && visited[j] )
g[i][j] = g[j][i] = 99;
}
return(mincost);
}
void main()
{
int n, cost[20][20], t[20][20];
int mincost,i,j;
printf("\nEnter the no of nodes: ");
scanf("%d",&n);
printf("Enter the cost matrix:\n");
for(i=1; i<=n; i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=99;
}
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
t[i][j] = 99;
printf("\nThe order of Insertion of edges:");
mincost = Prim (cost,n,t);
printf("\nMinimum cost = %d\n\n", mincost);
}
Input/Output:
Result:
Exp 7:
Aim:
To Find Minimum Cost Spanning Tree of a given undirected graph using
Kruskal's algorithm, Implement 0/1 Knapsack problem using Dynamic
Programming.
Program:
#include<stdio.h>
int parent[20],min,mincost=0,ne=1,n,cost[20][20];
int a,b,i,j,u,v;
void main()
{
printf("Enter the no of nodes\n");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=99;
}
while(ne <n)
{
for(i=1,min=99;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
printf("%d\t edge \t (%d,%d)=%d\n",ne++,a,b,min);
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=99;
}
printf("The minimum cost=%d\n",mincost);
}
Input/Output:
Result:
Program:
#include<stdio.h>
void kpsk(int ,int ,int[],int[],int[][100]);
void opt(int,int,int[],int[][100]);
int max(int,int);
void main()
{
int w[20],p[20],n,m,i,v[10][100];
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the capacity of knapsack\n");
scanf("%d",&m);
printf("Enter the weight of elements\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
Input/Output:
Result:
Exp 8:
Aim:
To Write a program to implement Travelling Sales Person problem using
Dynamic programming.
Program:
#include<stdio.h>
#include<conio.h>
int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf("Enter No. of Cities: ");
scanf("%d",&n);
printf("\nEnter Cost Matrix\n");
for( i=0;i <n;i++)
{
printf("\nEnter Elements of Row # : %d\n",i+1);
for( j=0;j <n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
}
printf("\n\nThe cost list is:\n\n");
for( i=0;i <n;i++)
{
printf("\n\n");
for(j=0;j <n;j++)
printf("\t%d",a[i][j]);
}
}
void mincost(int city)
{
int i,ncity;
visited[city]=1;
printf("%d -->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=a[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i <n;i++)
{
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i] < min)
{
min=a[i][0]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
void put()
{
printf("\n\nMinimum cost:");
printf("%d",cost);
}
void main()
{
clrscr();
get();
printf("\n\nThe Path is:\n\n");
mincost(0);
put();
getch();
}
Input/Output:
Result:
Exp 9:
Aim:
To From a given vertex in a weighted connected graph, find shortest paths to
other vertices using Dijkstra's algorithm.
Program:
#include<stdio.h>
void dij(int,int);
int min(int);
int a[20][20],dis[20],s[20],i,j;
void main()
{
int v,n;
printf("\n Enter the no of nodes\n");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("Enter the vertex\n");
scanf("%d",&v);
dij(v,n);
for(i=1;i<=n;i++)
printf("From %d to %d is %d\n",v,i,dis[i]);
}
void dij(int v,int n)
{
int w,u,k;
for(i=1;i<=n;i++)
{
s[i]=0;
dis[i]=a[v][i];
}
s[v]=1;
dis[v]=0;
for(i=2;i<=n;i++)
{
u=min(n);
s[u]=1;
for(w=1;w<=n;w++)
{
if(dis[w] > dis[u]+a[u][w])
dis[w]=dis[u]+a[u][w];
else
dis[w]=dis[w];
}
}
}
int min(int n)
{
int i,p,min=99;
for(i=1;i<=n;i++)
{
if(min > dis[i] && s[i]==0)
{
min=dis[i];
p=i;
}
}
return p;
}
Input/Output:
Result:
Exp 10:
Aim:
To Design and implement the presence of Hamiltonian Cycle in an undirected
Graph G of n vertices.
Program:
#include<stdio.h>
#include<conio.h>
#define V 5
Input/Output:
Result: