-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicKrushkalsalgorithm.cpp
100 lines (100 loc) · 1.7 KB
/
DynamicKrushkalsalgorithm.cpp
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
/*Shaik Abbu*/
#include<iostream>
#define I 32467
using namespace std;
int res[2][10];
int edges[20][20];
void display(int V)
{
cout<<"The Minimum Cost Spanning Tree is : ";
for(int i=0;i<V-1;i++)
{
cout<<"("<<res[0][i]<<" "<<res[1][i]<<")"<<"\n";
}
}
void unioun(int u,int v,int set[])
{
if(set[u]<set[v])
{
set[u]+=set[v];
set[v]=u;
}
else
{
set[v]+=set[u];
set[u]=v;
}
}
int find(int u,int set[])
{
int x=u;
int v=0;
while(set[x]>0)
{
x=set[x];
}
while(u!=x)
{
v=set[u];
set[u]=x;
u=v;
}
return x;
}
// 7 vertices and 9 edges
void Krushkals(int ver,int edg)
{
int set[ver+1]={-1,-1,-1,-1,-1,-1,-1,-1};
int included[edg] {0};
int i=0,j,u,v,min=I,k;
while(i<ver-1)
{
min=I;
u=0,v=0,k=0;
for(int i=0;i<ver;i++)
{
for(int j=0;j<edg;j++)
{
if(included[j]==0 && edges[i][j]<min)
{
min=edges[i][j];
u=i;
v=j;
k=j;
}
}
}
if(find(u,set)!=find(v,set))
{
res[0][i]=u;
res[1][i]=v;
unioun(find(u,set),find(v,set),set);
i++;
}
included[k]=1;
}
}
int main()
{
int v,u;
cout<<"Enter no.of vertices & edges : ) : ";
cin>>v>>u;
for(int i=0;i<=v;i++)
{
for(int j=0;j<u;j++)
{
cin>>edges[i][j];
}
}
for(int i=0;i<=v;i++)
{
for(int j=0;j<u;j++)
{
if(edges[i][j]==0)
edges[i][j]=I;
}
}
Krushkals(v,u);
display(v);
return 0;
}