Skip to content

Commit c0cc854

Browse files
authored
Add Minimum spanning tree in c-plus-plus (#4497)
Finished minimum spanning tree
1 parent 787bd96 commit c0cc854

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <string>
5+
#include <cmath>
6+
#include <algorithm>
7+
8+
using namespace std;
9+
10+
struct Edge {
11+
int src, dest, weight;
12+
};
13+
14+
int find(vector<int>& parent, int i) {
15+
if (parent[i] != i)
16+
parent[i] = find(parent, parent[i]);
17+
return parent[i];
18+
}
19+
20+
void unionp(vector<int>& parent, vector<int>& rank, int x, int y) {
21+
int xroot = find(parent, x);
22+
int yroot = find(parent, y);
23+
24+
if (rank[xroot] < rank[yroot])
25+
parent[xroot] = yroot;
26+
else if (rank[xroot] > rank[yroot])
27+
parent[yroot] = xroot;
28+
else {
29+
parent[yroot] = xroot;
30+
rank[xroot]++;
31+
}
32+
}
33+
34+
int main(int argc, char* argv[]) {
35+
if (argc < 2) {
36+
cout << "Usage: please provide a comma-separated list of integers";
37+
return 0;
38+
}
39+
40+
string input = argv[1];
41+
if (input.empty()) {
42+
cout << "Usage: please provide a comma-separated list of integers";
43+
return 0;
44+
}
45+
46+
vector<int> values;
47+
istringstream iss(input);
48+
string token;
49+
while (getline(iss, token, ',')) {
50+
// Trim spaces from token.
51+
size_t start = token.find_first_not_of(" \t");
52+
size_t end = token.find_last_not_of(" \t");
53+
if (start == string::npos) token = "";
54+
else token = token.substr(start, end - start + 1);
55+
56+
try {
57+
int num = stoi(token);
58+
values.push_back(num);
59+
} catch (...) {
60+
cout << "Usage: please provide a comma-separated list of integers";
61+
return 0;
62+
}
63+
}
64+
65+
int n = values.size();
66+
int side = static_cast<int>(sqrt(n));
67+
if (side * side != n) {
68+
cout << "Usage: please provide a comma-separated list of integers";
69+
return 0;
70+
}
71+
72+
vector<vector<int>> matrix(side, vector<int>(side, 0));
73+
int index = 0;
74+
for (int i = 0; i < side; i++) {
75+
for (int j = 0; j < side; j++) {
76+
matrix[i][j] = values[index++];
77+
}
78+
}
79+
80+
vector<Edge> edges;
81+
int V = side;
82+
for (int i = 0; i < V; i++) {
83+
for (int j = i + 1; j < V; j++) {
84+
int weight = matrix[i][j];
85+
if (weight != 0) {
86+
edges.push_back({i, j, weight});
87+
}
88+
}
89+
}
90+
91+
vector<int> parent(V), rank(V, 0);
92+
for (int i = 0; i < V; i++) {
93+
parent[i] = i;
94+
}
95+
96+
sort(edges.begin(), edges.end(), [](const Edge &a, const Edge &b) {
97+
return a.weight < b.weight;
98+
});
99+
100+
int totalCost = 0;
101+
int countEdges = 0;
102+
103+
for (auto &e : edges) {
104+
int setU = find(parent, e.src);
105+
int setV = find(parent, e.dest);
106+
107+
if (setU != setV) {
108+
totalCost += e.weight;
109+
unionp(parent, rank, setU, setV);
110+
countEdges++;
111+
}
112+
if (countEdges == V - 1)
113+
break;
114+
}
115+
116+
cout << totalCost;
117+
return 0;
118+
}

0 commit comments

Comments
 (0)