-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
45 lines (43 loc) · 722 Bytes
/
prim.cpp
File metadata and controls
45 lines (43 loc) · 722 Bytes
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
#include<iostream>
using namespace std;
int cost[10][10],i,j,k,n,u,v,a,visited[10]={0};
int main()
{
int c,min;
cout<<"Enter the number of vertices\n";
cin>>n;
cout<<"Enter the 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<<"Order of visited vertices\n";
i=1;
v=1;
visited[v]=1;
while(i<n)
{
min=999;
for(k=1;k<=n;k++)
{
if(visited[k]==1)
{
for(j=1;j<=n;j++)
if(cost[k][j]<min && visited[j]==0)
{
min=cost[k][j];
u=j;
a=k;
}
}
}
cost[a][u]=cost[u][a]=999;
cout<<a<<" -> "<<u<<"\t"<<min<<"\n";
i++;
visited[u]=1;
}
return 0;
}