0% found this document useful (0 votes)
4 views11 pages

Share C Lab - 4-6

The document contains multiple C/C++ programs implementing various algorithms including Floyd's algorithm for All-Pairs Shortest Paths, Warshall's algorithm for transitive closure, Dijkstra's algorithm for shortest paths from a given vertex, topological sorting for directed graphs, and a dynamic programming solution for the 0/1 Knapsack problem. Each program includes input prompts and outputs the results in a matrix format or as optimal solutions. The document provides code snippets and example outputs for each algorithm.

Uploaded by

naykarshruti58
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)
4 views11 pages

Share C Lab - 4-6

The document contains multiple C/C++ programs implementing various algorithms including Floyd's algorithm for All-Pairs Shortest Paths, Warshall's algorithm for transitive closure, Dijkstra's algorithm for shortest paths from a given vertex, topological sorting for directed graphs, and a dynamic programming solution for the 0/1 Knapsack problem. Each program includes input prompts and outputs the results in a matrix format or as optimal solutions. The document provides code snippets and example outputs for each algorithm.

Uploaded by

naykarshruti58
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/ 11

3.

a Design and implement C/C++ Program to solve All-Pairs Shortest


Paths problem using Floyd's algorithm.

#include<stdio.h>
#define INF 999
int min(int a,int b) {
return(a<b)?a:b;
}
void floyd(int p[][10],int n) {
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
void main() {
int a[10][10],n,i,j;
printf("\nEnter the 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",&a[i][j]);
floyd(a,n);
printf("\nShortest path matrix\n");
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
OUTPUT -3A

Enter the n value:4


Enter the graph data:

0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0

Shortest path matrix


0 10 3 4
2056
7701
6 16 9 0
3b. Design and implement C/C++ Program to find the transitive closure using
Warshal's algorithm.

#include<stdio.h>
void warsh(int p[][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=p[i][j] || p[i][k] && p[k][j];
}
int main()
{
int a[10][10],n,i,j;
printf("\nEnter the 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",&a[i][j]);
warsh(a,n);
printf("\nResultant path matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-3B

Enter the n value:4


Enter the graph data:
0100
0001
0000
1010
Resultant path matrix
1111
1111
0000
1111
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>
voiddij(int,int[20][20],int[20],int[20],int);
void main() {
inti,j,n,visited[20],source,cost[20][20],d[20];
printf("Enter no. of vertices: ");
scanf("%d",&n);
printf("Enter the cost adjacency matrix\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d",&cost[i][j]);
}
}
printf("\n Enter the source node:");
scanf("%d", &source);
dij(source,cost,visited,d,n);
for (i = 1; i <= n; i++) {
if(i!= source)
printf("\n Shortest path from %d to %d is %d", source, i, d[i]);
}
}
voiddij(int source, int cost[20][20], int visited[20],int d[20],int n)
{
int i, j, min, u, w;
for(i=1;i<=n;i++)
{
visited[i] = 0;
d[i]=cost[source][i];
}
visited[source]=1;
d[source]=0;
for(j=2;j<=n;j++)
{
min = 999;
for(i=1;i<=n;i++)
{
if (!visited[i])
{
if(d[i]<min)
{
min = d[i];
u=i;
}
}
}
visited[u]=1;
for(w =1; w<=n; w++) { if(cost[u][w]!=999&&visited[w]==0){ if (d[w] > cost[u][w] +
d[u])
d[w]=cost[u][w]+d[u];
}
}
}
}
OUTPUT
5. Design and implement C/C++ Program to obtain the Topological ordering
of vertices in a given digraph.
#include<stdio.h>
void find in degree (int[10]
[10],int[10],int);
void topological(int,int [10][10]);
void main()
{
int a[10][10],i,j,n;
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i]
[j]);
printf("\nThe adjacency matirxis:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
topological(n,a);
}
void find in degree(int a[10][10],int in degree[10],int n)
{
int i,j,sum;
for(j=1;j<=n;j++)
{
sum=0;
for(i=1;i<=n;i+
+)
{
sum=sum+a[i][j];
}
in degree[j]=sum;
}
}
voidtopological(intn,int a[10][10])
{
intk,top,t[100],i,stack[20],u,v,indegre
e[20]; k=1;
top=-1;
findindegree(a,indegree,n);
for(i=1;i<=n;i++)
{
if(indegree[i]==0)
{
stack[++top]=i;
}
}
while(top!=-1)
{
u=stack[top--];
t[k++]=u;
for(v=1;v<=n;v+
+)
{
if(a[u][v]==1)
{
in degree[v]--;
if(in degree[v]==0)
{
stack[++top]=v;
}}
}}
printf("\n Topological sequenceis\n");
for(i=1;i<=n;i++)
printf("%d\t",t[i]);
}

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

#include<stdio.h>
#define MAX 50
int p[MAX],w[MAX],n;
int knapsack(int,int);
in tmax(int,int);
void main()
{
int m,i,optsoln;
printf("Enter no.of objects:");
scanf("%d",&n);
printf("\nEnter the weights:\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("\nEnter the profits:\n");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("\nEnter the knapsack capacity:");
scanf("%d",&m);
optsoln=knapsack(1,m);
printf("\nThe optimalsoluntionis:%d",optsoln);
}
intknapsack(inti,intm)
{
if(i==n)
return(w[n]>m)?0:p[n]; if(w[i]>m)
return knapsack(i+1,m);
return max(knapsack(i+1,m),knapsack(i+1,m-w[i])+p[i]);
}
int max(inta, intb)
{
if(a>b) return a;
else
return b;
}

OUTPUT

You might also like