18 KiB
18 KiB
<html lang="en">
<head>
</head>
</html>
LCOV - code coverage report | ||||||||||||||||||||||
![]() | ||||||||||||||||||||||
|
||||||||||||||||||||||
![]() |
Line data Source code 1 : // *************************************************************** 2 : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez 3 : // SPDX-FileType: SOURCE 4 : // SPDX-License-Identifier: MIT 5 : // *************************************************************** 6 : 7 : #include <sstream> 8 : #include <vector> 9 : #include <list> 10 : #include "Mst.h" 11 : /* 12 : Based on the code from https://www.softwaretestinghelp.com/minimum-spanning-tree-tutorial/ 13 : 14 : */ 15 : 16 : namespace bayesnet { 17 58 : Graph::Graph(int V) : V(V), parent(std::vector<int>(V)) 18 : { 19 217 : for (int i = 0; i < V; i++) 20 188 : parent[i] = i; 21 29 : G.clear(); 22 29 : T.clear(); 23 29 : } 24 576 : void Graph::addEdge(int u, int v, float wt) 25 : { 26 576 : G.push_back({ wt, { u, v } }); 27 576 : } 28 2663 : int Graph::find_set(int i) 29 : { 30 : // If i is the parent of itself 31 2663 : if (i == parent[i]) 32 1152 : return i; 33 : else 34 : //else recursively find the parent of i 35 1511 : return find_set(parent[i]); 36 : } 37 159 : void Graph::union_set(int u, int v) 38 : { 39 159 : parent[u] = parent[v]; 40 159 : } 41 29 : void Graph::kruskal_algorithm() 42 : { 43 : // sort the edges ordered on decreasing weight 44 2242 : stable_sort(G.begin(), G.end(), [](const auto& left, const auto& right) {return left.first > right.first;}); 45 605 : for (int i = 0; i < G.size(); i++) { 46 : int uSt, vEd; 47 576 : uSt = find_set(G[i].second.first); 48 576 : vEd = find_set(G[i].second.second); 49 576 : if (uSt != vEd) { 50 159 : T.push_back(G[i]); // add to mst std::vector 51 159 : union_set(uSt, vEd); 52 : } 53 : } 54 29 : } 55 : 56 159 : void insertElement(std::list<int>& variables, int variable) 57 : { 58 159 : if (std::find(variables.begin(), variables.end(), variable) == variables.end()) { 59 159 : variables.push_front(variable); 60 : } 61 159 : } 62 : 63 29 : std::vector<std::pair<int, int>> reorder(std::vector<std::pair<float, std::pair<int, int>>> T, int root_original) 64 : { 65 : // Create the edges of a DAG from the MST 66 : // replacing unordered_set with list because unordered_set cannot guarantee the order of the elements inserted 67 29 : auto result = std::vector<std::pair<int, int>>(); 68 29 : auto visited = std::vector<int>(); 69 29 : auto nextVariables = std::list<int>(); 70 29 : nextVariables.push_front(root_original); 71 217 : while (nextVariables.size() > 0) { 72 188 : int root = nextVariables.front(); 73 188 : nextVariables.pop_front(); 74 664 : for (int i = 0; i < T.size(); ++i) { 75 476 : auto [weight, edge] = T[i]; 76 476 : auto [from, to] = edge; 77 476 : if (from == root || to == root) { 78 159 : visited.insert(visited.begin(), i); 79 159 : if (from == root) { 80 106 : result.push_back({ from, to }); 81 106 : insertElement(nextVariables, to); 82 : } else { 83 53 : result.push_back({ to, from }); 84 53 : insertElement(nextVariables, from); 85 : } 86 : } 87 : } 88 : // Remove visited 89 347 : for (int i = 0; i < visited.size(); ++i) { 90 159 : T.erase(T.begin() + visited[i]); 91 : } 92 188 : visited.clear(); 93 : } 94 29 : if (T.size() > 0) { 95 0 : for (int i = 0; i < T.size(); ++i) { 96 0 : auto [weight, edge] = T[i]; 97 0 : auto [from, to] = edge; 98 0 : result.push_back({ from, to }); 99 : } 100 : } 101 58 : return result; 102 29 : } 103 : 104 29 : MST::MST(const std::vector<std::string>& features, const torch::Tensor& weights, const int root) : features(features), weights(weights), root(root) {} 105 29 : std::vector<std::pair<int, int>> MST::maximumSpanningTree() 106 : { 107 29 : auto num_features = features.size(); 108 29 : Graph g(num_features); 109 : // Make a complete graph 110 188 : for (int i = 0; i < num_features - 1; ++i) { 111 735 : for (int j = i + 1; j < num_features; ++j) { 112 576 : g.addEdge(i, j, weights[i][j].item<float>()); 113 : } 114 : } 115 29 : g.kruskal_algorithm(); 116 29 : auto mst = g.get_mst(); 117 58 : return reorder(mst, root); 118 29 : } 119 : 120 : } |
![]() |
Generated by: LCOV version 2.0-1 |
</html>