-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim's Algorithm.CPP
More file actions
107 lines (97 loc) · 1.43 KB
/
Prim's Algorithm.CPP
File metadata and controls
107 lines (97 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream.h>
#include <conio.h>
class prims
{
int graph[11][11],a[10],mincost,t[11][3],n;
public:
prims();
int cost(int,int);
void spantree();
void getdata();
int min();
};
prims :: prims()
{
mincost=0;
cout<<"\nEnter the no of vertices";
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
graph[i][j]=t[i][1]=t[i][2]=100;
a[i]=1;
}
}
void prims :: getdata()
{
int **a;
a=new int*[n+1];
for(int i=1;i<=n;i++)
a[i]=new int[n+1];
cout<<"\nEnter the matrix";
for(i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]!=0)
graph[i][j]=a[i][j];
}
}
int prims :: cost(int i,int j)
{
return(graph[i][j]);
}
int prims :: min()
{
int m=100,j=-1;
for(int i=2;i<=n;i++)
{ if(a[i]!=0)
{
if(cost(i,a[i])<m)
{
m=cost(i,a[i]);
j=i;
}
}
}
return(j);
}
void prims :: spantree()
{
int i,j,k;
cout<<"\nSpanning tree";
a[1]=0;
for(i=1;i<n;i++)
{
j=min();
if(j!=-1)
{
t[i][1]=j;
t[i][2]=a[j];
mincost+=cost(j,a[j]);
a[j]=0;
for(k=1;k<=n;k++)
{
if(a[k]!=0)
if(cost(k,a[k])>cost(k,j))
a[k]=j;
}
}
else
{
cout<<"Disconnected";
return;
}
}
for(i=1;i<n;i++)
cout<<endl<<t[i][2]<<"->"<<t[i][1]<<"\n";
cout<<"\nminimum cost is"<<mincost;
}
void main()
{
clrscr();
prims p;
p.getdata();
p.spantree();
getch();
}