kruskals in c 2b 2b

Solutions on MaxInterview for kruskals in c 2b 2b by the best coders in the world

showing results for - "kruskals in c 2b 2b"
Jesús
23 Nov 2016
1#include<bits/stdc++.h> 
2using namespace std; 
3  
4
5typedef  pair<int, int> iPair; 
6  
7
8struct Graph 
9{ 
10    int V, E; 
11    vector< pair<int, iPair> > edges; 
12  
13   
14    Graph(int V, int E) 
15    { 
16        this->V = V; 
17        this->E = E; 
18    } 
19  
20    void addEdge(int u, int v, int w) 
21    { 
22        edges.push_back({w, {u, v}}); 
23    } 
24 
25  
26    int kruskalMST(); 
27}; 
28  
29
30struct DisjointSets 
31{ 
32    int *parent, *rnk; 
33    int n; 
34  
35
36    DisjointSets(int n) 
37    { 
38    
39        this->n = n; 
40        parent = new int[n+1]; 
41        rnk = new int[n+1]; 
42  
43    
44        for (int i = 0; i <= n; i++) 
45        { 
46            rnk[i] = 0; 
47  
48      
49            parent[i] = i; 
50        } 
51    } 
52 
53    int find(int u) 
54    { 
55     
56        if (u != parent[u]) 
57            parent[u] = find(parent[u]); 
58        return parent[u]; 
59    } 
60  
61 
62    void merge(int x, int y) 
63    { 
64        x = find(x), y = find(y); 
65  
66       
67        if (rnk[x] > rnk[y]) 
68            parent[y] = x; 
69        else 
70            parent[x] = y; 
71  
72        if (rnk[x] == rnk[y]) 
73            rnk[y]++; 
74    } 
75}; 
76  
77
78  
79int Graph::kruskalMST() 
80{ 
81    int mst_wt = 0; 
82  
83    sort(edges.begin(), edges.end()); 
84  
85
86    DisjointSets ds(V); 
87  
88
89    vector< pair<int, iPair> >::iterator it; 
90    for (it=edges.begin(); it!=edges.end(); it++) 
91    { 
92        int u = it->second.first; 
93        int v = it->second.second; 
94  
95        int set_u = ds.find(u); 
96        int set_v = ds.find(v); 
97  
98      
99        if (set_u != set_v) 
100        { 
101          
102            cout << u << " - " << v << endl; 
103  
104      
105            mst_wt += it->first; 
106  
107        
108            ds.merge(set_u, set_v); 
109        } 
110    } 
111  
112    return mst_wt; 
113} 
114  
115
116int main() 
117{ 
118   
119    int V = 9, E = 14; 
120    Graph g(V, E); 
121  
122   
123    g.addEdge(0, 1, 4); 
124    g.addEdge(0, 7, 8); 
125    g.addEdge(1, 2, 8); 
126    g.addEdge(1, 7, 11); 
127    g.addEdge(2, 3, 7); 
128    g.addEdge(2, 8, 2); 
129    g.addEdge(2, 5, 4); 
130    g.addEdge(3, 4, 9); 
131    g.addEdge(3, 5, 14); 
132    g.addEdge(4, 5, 10); 
133    g.addEdge(5, 6, 2); 
134    g.addEdge(6, 7, 1); 
135    g.addEdge(6, 8, 6); 
136    g.addEdge(7, 8, 7); 
137  
138    cout << "Edges of MST are \n"; 
139    int mst_wt = g.kruskalMST(); 
140  
141    cout << "\nWeight of MST is " << mst_wt; 
142  
143    return 0; 
144} 
queries leading to this page
minimum spanning tree gfg c 2b 2bstlkruskals in c 2b 2bsimple c 2b 2b program for kruskal 27s algorithmkruskals algorithmkruskal 27s algorithm in cppkruskal algorithmkruskal algorithm using stlkruskals minimum spanning tree cppkruskal 27s algorithm implementation c 2b 2bimplementation of kruskal 27s algorithm in c 2b 2bkruskal codeminimum cost spanning tree using kruskal 27s algorithm in c 2b 2b optimisedmst krusgal c 2b 2b codekruskal e2 80 99s algorithm using c 2b 2bwrite a program to implement kruskal algorithm cppsimple program for kruskal 27s algorithm in c 2b 2bkruskal algorithm in ckruskal 27s algorithm examplekruskals program in cc 2b 2b code implementation of krushkals algorithmkruskal cp algorithmkruskal 27s algorithmkruskal 27s algorithm c 2b 2bkruskal algorithm in c 2b 2bminimum cost spanning tree using kruskal 27s algorithm in c 2b 2bmst powerline problem c 2b 2bkruskal implementation c 2b 2bkruskal algorithm for mst c 2b 2bkruskal algorithm in cppkruskal 27s algorithm in c 2b 2b using arraykruskal 27s algorithm program in c 2b 2b kruskal cpp implementationkruskal algorithm implementation in c 2b 2bkruskal algorithm c 2b 2bkruskal algorithm cppstandard minimum spanning tree algorithm c 2b 2bkruskal 27s algorithm in c 2b 2bkruskal 27s algorithm simple program in c 2b 2bkruskal 27s algorithm followskruskal 27s algorithm cpminimum spanning tree using c 2b 2b stlkruskal cpp competitive programmingkruskal algorithm with array implementation in c 2b 2bhow to implement kruskal algorithm in cpppoints as vertices in a graph using kruskal algorithm in c 2b 2bkruskal 27s algorithm cppgraph implementation in cpp using kruskal 27s algorithmkruskals in c 2b 2b