-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathPrim.cpp
121 lines (110 loc) · 2.56 KB
/
Prim.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
// declare a class for graph
struct Edge{
int src,dest,weight;
};
class Graph{
int V,E;
vector<Edge> edge; // an array of all the edges
public:
Graph(int v, int e);
void addEdge(int src, int dest, int weight);
void mst();
};
Graph::Graph(int v, int e){
V=v;
E=e;
}
void Graph::addEdge(int src, int dest, int weight){
Edge *e= new Edge;
e->src= src;
e->dest= dest;
e->weight= weight;
edge.push_back(*e);
}
bool comp(Edge e1, Edge e2){
return e1.weight<e2.weight ;
}
struct subset{
int parent;
int rank;
};
// A utility function to find set of an element i
// (uses path compression technique)
int find(struct subset subsets[], int i){
// find root and make root as parent of i (path compression)
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
// A function that does union of two sets of x and y
// (uses union by rank)
void Union(struct subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
// Attach smaller rank tree under root of high rank tree
// (Union by Rank)
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else
{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
void Graph::mst(){
sort(edge.begin(), edge.end(), comp);
//create disjoint set for all vertices
struct subset *subsets = new subset[V];
// Create V subsets with single elements
for(int i=0;i<V;i++){
subsets[i].parent = i;
subsets[i].rank = 0;
}
vector<Edge> result;
// Number of edges to be taken is equal to V-1
for(auto i=edge.begin(); i!=edge.end();i++){
int src= i->src;
int dest=i->dest;
//if not same root
if(find(subsets, src)!= find(subsets, dest)){
result.push_back(*i);
Union(subsets, src, dest);
}
}
printf("The edges of the MST are\n");
for(auto i=result.begin(); i!=result.end(); i++)
cout<<i->src<<"--"<<i->dest<<"="<<i->weight<<endl;
return;
}
int main(){
int V; // Number of vertices in graph
int E;
printf("Enter no of vertices and edges\n");
scanf("%d%d",&V,&E); // Number of edges in graph
Graph g(V,E);
int i,u,v,x;
printf("Enter\nsrc dest weight\n");
for(i=0;i<E;i++)
{
scanf("%d%d%d",&u,&v,&x);
g.addEdge(u,v,x);
}
// add edge 0-1
/*g.addEdge(0,3,1);
g.addEdge(4,5,2);
g.addEdge(2,4,5);
g.addEdge(3,4,6);
g.addEdge(0,1,3);
g.addEdge(1,2,1);
g.addEdge(2,3,1);
g.addEdge(1,3,3);
g.addEdge(2,5,4);*/
g.mst();
}