-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKRUSKAL.cpp
More file actions
85 lines (79 loc) · 2.1 KB
/
Copy pathKRUSKAL.cpp
File metadata and controls
85 lines (79 loc) · 2.1 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
#include<bits/stdc++.h>
using namespace std;
class edge{//EVERY EDGE HAS THESE 3 THINGS
public:
int src;
int dest;
int weight;
};
bool compare(edge e1,edge e2)//SORT ACCORDING TO WEIGHT
{
return e1.weight<e2.weight;
}
int getparent(int v,int* parent)//RECURSIVE FIND THE PARENT OF A VERTEX
{
if(parent[v]==v)//STOP WHEN ANY VERTEX HAS SAME VERTEX AS PARENT MEANS THAT'S THE ROOT
{
return v;
}
return getparent(parent[v],parent);
}
edge* kruskals(edge* edges,int n ,int e)//RETURNS THE OUTPUT ARRAY OF EDGE TYPE ...WITH NO CYCLES AND MINIMUM SPANNING TREE EDGES
{
//sorted the edges array in increasing order
sort(edges,edges+e,compare);
edge* output=new edge[n-1];//Make OUTPUT ARRAY OF EDGE TYPE
int* parent=new int[n];
for(int i=0;i<n;i++)
{
parent[i]=i;
}
int count=0;
int i=0;
while(count<(n-1))//JABTAK EDGES N-1 NA HO JAI TA TAK KEEP ON SEARCHING
{
//check the topmost parent of v1 and v2.
//v1 and v2 are vertices of current edge
edge currentedge=edges[i];//EDGE VARIABLE HOLDING EDGE
int srcparent=getparent(currentedge.src,parent);
int destparent=getparent(currentedge.dest,parent);
if(srcparent != destparent)
{
output[count]=currentedge;
count+=1;
parent[srcparent]=destparent;
}
i+=1;
}
return output;
}
int main()
{
int n,e;
cin>>n>>e;
edge* edges=new edge[e];
for(int i=0;i<e;i++)
{
int s,d,w;
cin>>s>>d>>w;
edges[i].src=s;
edges[i].dest=d;
edges[i].weight=w;
}
edge* output=kruskals(edges,n,e);
long int sum=0;
for(int i=0;i<n-1;i++)
{
/* if(output[i].src<output[i].dest)//EDGE PRINTING AS SMALL LARGE WEIGHT
{
cout<<output[i].src<<" "<<output[i].dest<<" " <<output[i].weight<<endl;
}
else{
cout<<output[i].dest<< " "<<output[i].src<<" "<<output[i].weight<<endl;
}
sum+=output[i].weight;*/
sum+=output[i].weight;
}
cout<<sum<<endl;
return 0;
}