0% found this document useful (0 votes)
10 views13 pages

Daa Lab Manual (Part B)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

Daa Lab Manual (Part B)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PART – B

1. Write a program to implement BFS and DFS algorithm for a graph.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 100
int c[MAX][MAX];
int visited[MAX];
int queue[MAX];
int n;
void BFS(int v)
{
int front=0,rear=-1,i;
visited[v]=1;
queue[++rear]=v;
while(front<=rear)
{
v=queue[front++];
printf("%d",v);
for(i=1;i<=n;i++)
{
if(c[v][i]==1&&visited[i]==0)
{
queue[++rear]=i;
visited[i]=1;
}
}
}
printf("\n");
}
void DFS(int v)
{
int i;
visited[v]=1;
printf("%d",v);
for(i=1;i<=n;i++)
{
if(c[v][i]==1&&visited[i]==0)
{
DFS(i);
}
}
}
int main()
{
int i,j,v;
clrscr();
printf("Enter the number of verticesin the graph; ");
scanf("%d",&n);
printf("Enter the cost matrix of the graph: \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&c[i][j]);
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter the starting vertex for BFS: ");
scanf("%d",&v);
printf("BFS traversal of the graph is: ");
BFS(v);
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter the starting vertex for DFS: ");
scanf("%d",&v);
printf("DFS traversal of the graph is: ");
DFS(v);
printf("\n");
getch();
return 0;
}

OUTPUT:
2. Write C program that accepts vertices and edges for a graph and stores it in an adjacency
matrix.

#include<stdio.h>
#indlude<conio.h>
#define v 4
void init(int arr[][v])
{
int i,j;
for(i=0;i<v;i++)
for(j=0;j<v;j++)
arr[i][j]=0;
}
void addEdge(int arr[][v],int i,int j)
{
arr[i][j]=1;
arr[j][i]=1;
}
void printAdjMatrix(int arr[][v])
{
int i,j;
for(i=0;i<v;i++)
{
printf("%d",i);
for(j=0;j<v;j++)
{
printf("%d",arr[i][j]);
}
printf("\n");
}
}
void main()
{
int adjMatrix[v][v];
init(adjMatrix);
addEdge(adjMatrix,0,1);
addEdge(adjMatrix,0,2);
addEdge(adjMatrix,1,2);
addEdge(adjMatrix,0,3);
addEdge(adjMatrix,2,3);
printAdjMatrix(adjMatrix);
getch();
}
OUTPUT:

3. Write a program that implements Kruskal algorithms to generate minimum cost spanning
Tree.

#include<stdio.h>
#include<conio.h>
int parent[10],min,edges=1,mincost=0,cost[10][10];
int a,b,i,j,u,v,n;
void main()
{
printf("Enter the number of vertices:");
scanf("%d",&n);
printf("Enter the adjacency 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]=999;
}
while(edges<n)
{
for(i=1,min=999;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("\n%d\\t Edge\t(%d%d)=%d",edges,a,b,min);
mincost=mincost+min;
parent[v]=u;
edges++;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\t Minimum Cost=%d",mincost);
getch();
}

OUTPUT:

4. 4. Write a program that implements Prim’s algorithms to generate minimum cost spanning
Tree.

#include<stdio.h>
int a,b,u,v,n,i,j,edges=1;
int visited[10],min,mincost=0,cost[10][10];
void main()
{
printf("Enter the no of vertices:");
scanf("%d",&n);
printf("Enter the adjacency 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]=999;
}
for(i=2;i<=n;i++)
visited[i]=0;
printf("Edges of spaning tree are:\n");
visited[1]=1;
while(edges<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0||visited[v]==0)
{
printf("\n%d\tEdge\t(%d,%d)=%d",edges,a,b,min);
mincost=mincost+min;
visited[b]=1;
edges++;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum Cost=%d\n",mincost);
getch();
}

OUTPUT:
5. Write a program to implement backtracking algorithm for solving problems like N queens.

#include<stdio.h>
#include<conio.h>
int s[50][50];
void display(int m[],int n)
{
register int i,j;
for(i=0;i<n;i++)
s[i][m[i]]=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(s[i][j])
printf("Q");
else
printf("X");
printf("\n");
}
exit(0);
}
int place(int m[],int k)
{
int i;
for(i=0;i<k;i++)
if(m[i]==m[k] || (abs(m[i]-m[k])==abs(i-k))
return 0;
return 1;
}
void main()
{
int m[25],n,k;
printf("Enter the no of queens:");
scanf("%d",&n);
printf("Solution to queens problem is \n");
n--;
for(m[0]=0,k=0;k>=0;m[k]+=1)
{
while(m[k]<=n && !place(m,k))
m[k]+=1;
if(m[k]<=n)
if(k==n)
display(m,n+1);
else
k++,m[k]=-1;
else
k--;
}
getch();
}

OUTPUT:

6. Write a program to implement function to print In-Degree, Out-Degree and to display that
adjacency matrix

int main()
{
int graph[50][50];
int i,j,edge,vertex,p,x,y,indegree1[90]={0},outdegree1[90]={0},a,q,w;
printf("Enter the Number of Vertex : ");
scanf("%d",&vertex);
printf("\n");
printf("Enter the Number of Edge : ");
scanf("%d",&edge);
printf("\n");
for(w=1;w<=vertex;w++)
for(q=1;q<=vertex;q++)
graph[w][q]=0;
for(i=1;i<=edge;i++)
{
scanf("%d%d",&x,&y);
graph[x][y]=1;
}
printf("\n");
for(i=1;i<=vertex;i++)
{
for(j=1;j<=vertex;j++)
{
if(graph[i][j]==1)
{
outdegree1[i]++;
indegree1[j]++;
}
}

}
printf("Indegree\n");
for(p=1;p<=vertex;p++)
{
printf("%d : %d\n",p,indegree1[p]);
}
printf("Outdegree\n");
for(a=1;a<=vertex;a++)
{
printf("%d : %d\n",a,outdegree1[a]);
}

return 0;

OUTPUT:

7. Write a program to implement greedy algorithm for job sequencing with deadlines.

#include<stdio.h>
#include<conio.h>
void job(int *,int *,int,int);
void main()
{
int profit[20],dead[20],n,i,j,maxdead;
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter profit in descending order");
for(i=0;i<n;i++)
scanf("%d",&profit[i]);
printf("Enter deadline according to profit value");
for(i=0;i<n;i++)
scanf("%d",&dead[i]);
printf("Enter Maximum deadline");
scanf("%d",&maxdead);
job(profit,dead,n,maxdead);
getch();
}
void job(int profit[],int dead[],int n, int maxdead)
{
int result[20],total=0,i,j;
for(int i=0;i<maxdead;i++)
{
result[i]=0;
}
for(i=0;i<n;i++)
{
if(result[dead[i]-1]==0)
{
result[dead[i]-1]=profit[i];
total=total+profit[i];
}
else
{
for(j=dead[i]-1;j>=0;j--)
{
if(result[j]==0)
{
result[j]=profit[i];
total=total+profit[i];
}
}
}
}
printf("Total profit is %d",total);
printf("Output Array is\n");
for(i=0;i<maxdead;i++)
{
printf("At Index %d is profit %d\n",i+1,result[i]);
}
}
OUTPUT:

8. Write a program to implement the backtracking algorithm for the sum of subsets
problem

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
void sumset(int,int,int);
int inc[50],w[50],sum,n;
int promising(int i,int wt,int total)
{
return(((wt + total)>=sum) && ((wt==sum) || (wt + w[i+1]<=sum)));
}
void main()
{
int i,j,n,temp,total=0;
printf("Enter how many numbers:");
scanf("%d",&n);
printf("Enter the numbers to the set");
for(1=0;i<n;i++)
{
scanf("%d",&w[i]);
total=total+w[i];
}
printf("Input the sum value to create sub set:\n");
scanf("%d",&sum);
for(i=0;i<=n;i++)
for(j=0;j<n-1;j++)
if(w[j]>e[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("Given Numbers in ascending order:\n");
for(i=0;i<n;i++)
printf("%d \t",w[i]);
if(total,sum)
printf("Subset construction is not possible");
else
for(i=0;i<n;i++)
inc[i]=0;
printf("\nSolution using backtrackin is:\n");
sumset(-1,0,total);
}
}
void sumset(int i,int wt,int total)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("\n{\t");
for(j=0;j<=i;j++)
if(inc[j])
printf("%d\t"w[j]);
printf("}\n");
}
else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
OUTPUT:

You might also like