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

Ada Lab

Lab documents

Uploaded by

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

Ada Lab

Lab documents

Uploaded by

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

1 ) Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected

undirected graph using Kruskal’s algorithm.

#define INF 999

#define MAX 100

int p[MAX], c[MAX][MAX], t[MAX][2];

int find(int v)

while (p[v])

v = p[v];

return v;

void union1(int i, int j)

p[j] = i;

void kruskal(int n)

int i, j, k, u, v, min, res1, res2, sum = 0;

for (k = 1; k < n; k++)

min = INF;

for (i = 1; i < n - 1; i++)

for (j = 1; j <= n; j++)

if (i == j) continue;

if (c[i][j] < min)

u = find(i);

v = find(j);

if (u != v)
{

res1 = i;

res2 = j;

min = c[i][j];

union1(res1, find(res2)); t[k]

[1] = res1;

t[k][2] = res2;

sum = sum + min;

printf("\nCost of spanning tree is=%d", sum);

printf("\nEdgesof spanning tree are:\n");

for (i = 1; i < n; i++)

printf("%d -> %d\n", t[i][1], t[i][2]);

int main()

int i, j, n;

printf("\nEnter the n value:");

scanf("%d", & n);

for (i = 1; i <= n; i++)

p[i] = 0;

printf("\nEnter the graph data:\n");

for (i = 1; i <= n; i++)

for (j = 1; j <= n; j++)

scanf("%d", & c[i][j]);

kruskal(n);

return 0;
}

OUTPUT

Enter the n value:5

Enter the graph

data: 1 3 4 6 2

17693

5 2 8 99 45

1 44 66 33 6

12 4 3 2 0

Cost of spanning tree is=11

Edgesof spanning tree are:

2 -> 1

1 -> 5

3 -> 2

1 -> 4
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>

#define INF 999

int prim(int c[10][10],int n,int s)

int v[10],i,j,sum=0,ver[10],d[10],min,u;

for(i=1; i<=n; i++)

ver[i]=s;

d[i]=c[s][i];

v[i]=0;

v[s]=1;

for(i=1; i<=n-1; i++)

min=INF;

for(j=1; j<=n; j++)

if(v[j]==0 && d[j]<min)

min=d[j];

u=j;

v[u]=1;

sum=sum+d[u];

printf("\n%d -> %d sum=%d",ver[u],u,sum);

for(j=1; j<=n; j++)

if(v[j]==0 && c[u][j]<d[j])

d[j]=c[u][j];

ver[j]=u;
}

return sum;

void main()

int c[10][10],i,j,res,s,n;

printf("\nEnter n value:");

scanf("%d",&n);

printf("\nEnter the graph data:\n");

for(i=1; i<=n; i++)

for(j=1; j<=n; j++)

scanf("%d",&c[i][j]);

printf("\nEnter the souce node:");

scanf("%d",&s);

res=prim(c,n,s);

printf("\nCost=%d",res);

getch();

OUTPUT

Enter n value:4

Enter the graph data:

4521

7592

1769

0285

Enter the souce node:4

4 -> 1 sum=0
4 -> 2 sum=2

1 -> 3 sum=4

Cost=4
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>

#define INF 999

void dijkstra(int c[10][10],int n,int s,int d[10])

int v[10],min,u,i,j;

for(i=1; i<=n; i++)

d[i]=c[s][i];

v[i]=0;

v[s]=1;

for(i=1; i<=n; i++)

min=INF;

for(j=1; j<=n; j++)

if(v[j]==0 && d[j]<min)

min=d[j];

u=j;

v[u]=1;

for(j=1; j<=n; j++)

if(v[j]==0 && (d[u]+c[u][j])<d[j]) d[j]=d[u]

+c[u][j];

int main()

int c[10][10],d[10],i,j,s,sum,n;
printf("\nEnter n value:");

scanf("%d",&n);

printf("\nEnter the graph data:\n");

for(i=1; i<=n; i++)

for(j=1; j<=n; j++)

scanf("%d",&c[i][j]);

printf("\nEnter the souce node:");

scanf("%d",&s);

dijkstra(c,n,s,d);

for(i=1; i<=n; i++)

printf("\nShortest distance from %d to %d is %d",s,i,d[i]);

return 0;

OUTPUT

Enter n value:4

Enter the graph data:

444 767 987 12

999 87 56 45

1 0 999 678

444 678 235 0

Enter the souce node:1

Shortest distance from 1 to 1 is 444

Shortest distance from 1 to 2 is 247

Shortest distance from 1 to 3 is 247

Shortest distance from 1 to 4 is 12


5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.

#include<stdio.h>

#include<conio.h>

int temp[10],k=0;

void sort(int a[][10],int id[],int n)

int i,j;

for(i=1; i<=n; i++)

if(id[i]==0)

id[i]=-1;

temp[++k]=i;

for(j=1; j<=n; j++)

if(a[i][j]==1 && id[j]!=-1)

id[j]--;

i=0;

void main()

int a[10][10],id[10],n,i,j;

printf("\nEnter the n value:");

scanf("%d",&n);

for(i=1; i<=n; i++)

id[i]=0;

printf("\nEnter the graph data:\n");


for(i=1; i<=n; i++)

for(j=1; j<=n; j++)

scanf("%d",&a[i][j]);

if(a[i][j]==1) id[j]+

+;

sort(a,id,n);

if(k!=n)

printf("\nTopological ordering not possible");

else

printf("\nTopological ordering is:");

for(i=1; i<=k; i++)

printf("%d ",temp[i]);

getch();

OUTPUT

****************************************OUTPU1***********************************
*****

Enter the n value:6

Enter the graph data:

001100

000110

000101

000001

000001

000000
Topological ordering is: 1 2 3 4 5 6

****************************************OUTPU2***********************************
*****

Enter the n value:4

Enter the graph data:

1432

5421

5342

4123

Topological ordering not possible


6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.

#include<stdio.h>

int w[10],p[10],n;

int max(int a,int

b)

return a>b?a:b;

int knap(int i,int m)

if(i==n) return w[i]>m?0:p[i];

if(w[i]>m) return knap(i+1,m);

return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);

int main()

int m,i,max_profit;

printf("\nEnter the no. of objects:");

scanf("%d",&n);

printf("\nEnter the knapsack capacity:");

scanf("%d",&m);

printf("\nEnter profit followed by weight:\n");

for(i=1; i<=n; i++)

scanf("%d %d",&p[i],&w[i]);

max_profit=knap(1,m);

printf("\nMax profit=%d",max_profit);

return 0;

OUTPUT

Enter the no. of objects:4


Enter the knapsack capacity:5

Enter profit followed by weight:

12 3

43 5

45 2

55 3

Max profit=100

You might also like