Sie sind auf Seite 1von 4

#include<stdio.

h>
#include<conio.h>
#define MAXNODES 50
#define MAX1 150
#define INFINITY 5000

using namespace std;

int weight[MAXNODES][MAXNODES],i,j,distance[MAXNODES],visit[MAXNODES];
int precede[MAXNODES],final=0;
int path[MAX1];
int smalldist,newdist,k,s,d,current,n,distcurr;

void Display_Result()
{
i=d;
path[final]=d;
final++;
while(precede[i]!=s)
{
j=precede[i];
i=j;
path[final]=i;
final++;
}
path[final]=s;
printf("\nThe shortest path followed is :\n\n");
for(i=final;i>0;i--)
printf("\t\t(%d -> %d) with cost = %d\n\n",path[i],path[i-1],weight[path[i]]
[path[i-1]]);
printf("\nFor total cost = %d",distance[d]);
}

main()
{
printf("\nEnter the number of nodes(Less than 50)in the matrix : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&weight[i][j]);
printf("\nEnter the source node (0 to %d) : ",n-1);
scanf("%d",&s);
printf("\nEnter the destination node (0 to %d) : ",n-1);
scanf("%d",&d);
for(i=0;i<n;i++)
{
distance[i]=INFINITY;
precede[i]=INFINITY;
}
distance[s]=0;
current=s;
visit[current]=1;
while(current!=d)
{
distcurr=distance[current];
smalldist=INFINITY;
for(i=0;i<n;i++)
if(visit[i]==0)
{
newdist=distcurr+weight[current][i];
if(newdist<distance[i])
{
distance[i]=newdist;
precede[i]=current;
}
if(distance[i]<smalldist)
{
smalldist=distance[i];
k=i;
}
}
current=k;
visit[current]=1;
}
Display_Result();
getch();
}

/*----------------OUTPUT----------------*/
/*
Enter the number of nodes(Less than 50)in the matrix:6

Enter the cost matrix:

5000 6 3 5000 5000 5000


6 5000 2 5 5000 5000
3 2 5000 3 4 5000
5000 5 3 5000 2 3
5000 5000 4 2 5000 5
5000 5000 5000 3 5 5000

Enter the source code:0

Enter the destination code:5

The shortest path followed is:

(0 -> 2)with cost=3

(2 -> 3)with cost=3

(3 -> 5)with cost=3

For total cost=9


*/
-----------------------------------------------------------------------------------
-------------------------
PRIM'S ALGORITHM
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int weight[20][20],visited[20],d[20],p[20];
int v,e;

void creategraph()
{
int i,j,a,b,w;
cout<<"\nEnter number of vertices";
cin>>v;
cout<<"\nEnter number of edges";
cin>>e;
for(i=1;i<=v;i++)
for(j=1;j<=v;j++)
weight[i][j]=0;
for(i=1;i<=v;i++)
{
p[i]=visited[i]=0;
d[i]=32767;
}
for(i=1;i<=e;i++)
{
cout<<"\nEnter edge a,b and weight w:";
cin>>a>>b>>w;
weight[a][b]=weight[b][a]=w;
}
}

void prim()
{
int current,totalvisited,mincost,i;
current=1;d[current]=0;
totalvisited=1;
visited[current]=1;
while(totalvisited!=v)
{
for(i=1;i<=v;i++)
{
if(weight[current][i]!=0)
if(visited[i]==0)
if(d[i]>weight[current][i])
{
d[i]=weight[current][i];
p[i]=current;
}
}
mincost=32767;
for(i=1;i<=v;i++)
{
if(visited[i]==0)
if(d[i]<mincost)
{
mincost=d[i];
current=i;
}
}
visited[current]=1;
totalvisited++;
}
mincost=0;
for(i=1;i<=v;i++)
mincost+=d[i];
cout<<"\nMinimum cost="<<mincost;
cout<<"\nMinimum span tree is";
for(i=1;i<=v;i++)
cout<<"\nVertex "<<i<<" is connected to"<<p[i];
}

main()
{
clrscr();
creategraph();
prim();
getch();
}

/*-------------------OUTPUT---------------------*/

/*
Enter number of vertices6

Enter number of edges9

Enter edge a,b and weight w:1 2 6

Enter edge a,b and weight w:1 3 3

Enter edge a,b and weight w:2 3 2

Enter edge a,b and weight w:2 4 5

Enter edge a,b and weight w:3 5 4

Enter edge a,b and weight w:3 4 3

Enter edge a,b and weight w:4 5 2

Enter edge a,b and weight w:4 6 3

Enter edge a,b and weight w:5 6 5

Minimum cost=13
Minimum span tree is
Vertex 1 is connected to 0
Vertex 2 is connected to 3
Vertex 3 is connected to 1
Vertex 4 is connected to 3
Vertex 5 is connected to 4
Vertex 6 is connected to 4
*/