39 using id_type = G::id_type;
40 using edge_weight_type =
typename G::edge_type::weight_type;
44 for (
const auto&
id : graph.get_nodes()) {
45 mst.add_node_with_id(
id, graph.get_node_data(
id));
49 std::map<id_type, id_type> parent;
50 for (
const auto&
id : graph.get_nodes()) {
56 std::function<id_type(id_type)> find_set = [&parent, &find_set](id_type node) {
57 if (parent[node] != node) {
58 parent[node] = find_set(parent[node]);
64 auto union_sets = [&find_set, &parent](id_type a, id_type b) { parent[find_set(a)] = find_set(b); };
67 std::vector<std::tuple<edge_weight_type, id_type, id_type>> edges;
68 for (
const auto&
id : graph.get_nodes()) {
69 for (
const auto& neighbor : graph.get_neighbors(
id)) {
71 auto edge = graph.get_edge(
id, neighbor);
72 edges.emplace_back(edge.weight,
id, neighbor);
78 std::sort(edges.begin(), edges.end());
81 for (
const auto& [weight, u, v] : edges) {
82 if (find_set(u) != find_set(v)) {
83 mst.add_edge(u, v, weight);