Вы находитесь на странице: 1из 17

Name:-Santosh kumar Das

Regd No:-1806151022
ADA Lab Assignment

Semester:- 4th sem


1.Aim of the program:-
Program to implement Floyd warshall algorithm.
Program:-
#include<iostream>
#include<conio.h>
using namespace std;
void floyds(int b[][7])
{
int i,j,k;
for(k=0;k<7;k++)
{
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if((b[i][k]*b[k][j]!=0)&&(i!=j))
{
if((b[i][k]+b[k][j]<b[i][j])||(b[i][j]==0))
{
b[i][j]=b[i][k]+b[k][j];
}
}
}
}
}
for(i=0;i<7;i++)
{
cout<<"\nMinimum cost with respect to Node:"<<i<<endl;
for(j=0;j<7;j++)
{
cout<<b[i][j]<<"\t";
}
}
}
int main()
{
int b[7][7];
cout<<"ENTER VALUES PF ADJACENCY MATRIX\n\n";
for(int i=0;i<7;i++)
{
cout<<"enter values for"<<(i+1)<<"row"<<endl;
for(int j=0;j<7;j++)
{
cin>>b[i][j];
}
}
floyds(b);
getch();
}
2.Aim of the program:-
Program to implement dijkstra algorithm.

Program:-
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define inf 9999
#define max 5
using namespace std;
void dijkstra(int G[max][max],int n,int startnode);
int main()
{
Int G[max][max]={{0,1,0,3,10},{1,0,5,0,0},{0,5,0,2,1},{3,0,2,0,6},
{10,0,1,6,0}};
int n=5; int u=0;
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[max][max],int n,int startnode)
{
int cost[max][max],distance[max],pred[max];
int visited[max],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=inf;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode; visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=inf;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i]; nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
cout<<"\nDistance of node"<<i<<"="<<distance[i];
cout<<"\npath="<<i; j=i;
do
{
j=pred[j];
cout<<"<-"<<j;
}while(j!=startnode);
}
}

3.Aim of the program:-


Program to implement kruskal’s algorithm.

Program:-
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int i,j,k,a,b,u,v,n,ne=1;
int mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
int main()
{
int min;
cout<<"\n\tImplementation of Kruskal's algorithm\n";
cout<<"\nEnter the no. of vertices:";
cin>>n;
cout<<"\nEnter the cost adjacency matrix:\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
}
cout<<"The edges of Minimum Cost Spanning Tree are\n";
while(ne < 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;
}
}
}
u=find(u); v=find(v);
if(uni(u,v))
{
cout<<" edge ="<<ne++,a,b,min;
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n\tMinimum cost ="<<mincost;
return 0;
}
int find(int i)
{
while(parent[i])
i=parent[i]; return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i; return 1;
}
return 0;
}
4.Aim of the program:-
Program to implement prims algorithm.

Program:-
#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define V 5
bool createsMST(int u, int v, vector<bool> inMST){
if (u == v)
return false;
if (inMST[u] == false && inMST[v] == false)
return false;
else if (inMST[u] == true && inMST[v] == true)
return false;
return true;
}
void printMinSpanningTree(int cost[][V]){
vector<bool> inMST(V, false);
inMST[0] = true;
int edgeNo = 0, MSTcost = 0;
while (edgeNo < V - 1) {
int min = INT_MAX, a = -1, b = -1;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (cost[i][j] < min) {
if (createsMST(i, j, inMST)) {
min = cost[i][j];
a = i;
b = j;
}
}
}
}
if (a != -1 && b != -1) {
cout<<"Edge "<<edgeNo++<<" : ("<<a<<" , "<<b<<" ) : cost =
"<<min<<endl;
MSTcost += min;
inMST[b] = inMST[a] = true;
}
}
cout<<"Cost of Minimum spanning tree ="<<MSTcost;
}
int main() {
int cost[][V] = {
{ INT_MAX, 12, INT_MAX, 25, INT_MAX },
{ 12, INT_MAX, 11, 8, 12 },
{ INT_MAX, 11, INT_MAX, INT_MAX, 17 },
{ 25, 8, INT_MAX, INT_MAX, 15 },
{ INT_MAX, 12, 17, 15, INT_MAX },
};
cout<<"The Minimum spanning tree for the given tree is :\n";
printMinSpanningTree(cost);
return 0;
}

5.Aim of the program:-


Program to implement to Bellman Ford algorithm.

Program:-
#include<iostream>
#define MAX 10
using namespace std;
typedef struct edge
{
int src;
int dest;
int wt;
}edge;
void bellman_ford(int nv,edge e[],int src_graph,int ne)
{
int u,v,weight,i,j=0;
int dis[MAX];
for(i=0;i<nv;i++)
{
dis[i]=999;
}
dis[src_graph]=0;
for(i=0;i<nv-1;i++)
{
for(j=0;j<ne;j++)
{
u=e[j].src;
v=e[j].dest;
weight=e[j].wt;

if(dis[u]!=999 && dis[u]+weight < dis[v])


{
dis[v]=dis[u]+weight;
}
}

}
for(j=0;j<ne;j++)
{
u=e[j].src;
v=e[j].dest;
weight=e[j].wt;

if(dis[u]+weight < dis[v])


{
cout<<"\n\nNEGATIVE CYCLE PRESENT..!!\n";
return;
}
}
cout<<"\nVertex"<<" Distance from source";
for(i=1;i<=nv;i++)
{
cout<<"\n"<<i<<"\t"<<dis[i];
}
}
int main()
{
int nv,ne,src_graph;
edge e[MAX];
cout<<"Enter the number of vertices: ";
cin>>nv;
printf("Enter the source vertex of the graph: ");
cin>>src_graph;
cout<<"\nEnter no. of edges: ";
cin>>ne;
for(int i=0;i<ne;i++)
{
cout<<"\nFor edge "<<i+1<<"=>";
cout<<"\nEnter source vertex :";
cin>>e[i].src;
cout<<"Enter destination vertex :";
cin>>e[i].dest;
cout<<"Enter weight :";
cin>>e[i].wt;
}
bellman_ford(nv,e,src_graph,ne);
return 0;
}
Name:-Santosh Kumar Das
Regd No:-1806151022

Вам также может понравиться