MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / kruskal

Function kruskal

graph/code15.cpp:65–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63}
64
65void kruskal(vector<pair<int, int>> vec[], int v)
66{
67 vector<int> arr(v), rank(v, 0);
68 for (int i = 0; i < v; i++)
69 {
70 arr[i] = i;
71 }
72 vector<node> nodes;
73 for (int i = 0; i < v; i++)
74 {
75 int curr = i;
76 for (int j = 0; j < vec[curr].size(); j++)
77 {
78 int adj = vec[curr][j].first;
79 int adj_weight = vec[curr][j].second;
80
81 nodes.push_back({curr, adj, adj_weight});
82 }
83 }
84 sort(nodes.begin(), nodes.end(), mycomp);
85
86 int mini_weight = 0;
87
88 for (int i = 0; i < nodes.size(); i++)
89 {
90 node curr = nodes[i];
91 int find_first = find(arr, curr.u);
92 int find_sec = find(arr, curr.v);
93
94 if (find_first == find_sec)
95 {
96 continue;
97 }
98 else
99 {
100 make_union(arr, rank, curr.u, curr.v);
101 cout << "Connected Edge: " << curr.u << " " << curr.v << " " << curr.weight << endl;
102 mini_weight += curr.weight;
103 }
104 }
105
106 cout << "Minimum Spanning Tree weight: " << mini_weight << endl;
107}
108
109int main()
110{

Callers 1

mainFunction · 0.85

Calls 4

make_unionFunction · 0.85
push_backMethod · 0.80
findFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected