From 128d2442b5e67b513d9dc751a88826b2af02e239 Mon Sep 17 00:00:00 2001 From: Isabella Inuso Date: Fri, 18 Oct 2024 19:32:21 +0200 Subject: [PATCH] added docs --- Graph.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++-------- Progetto.cpp | 7 +++++- aux.cpp | 19 +++++++++++++++-- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/Graph.cpp b/Graph.cpp index 3fdc8a4..6703920 100644 --- a/Graph.cpp +++ b/Graph.cpp @@ -12,6 +12,14 @@ using namespace std; +/** + * @brief Struct that represents an edge in the graph + * + * @param from: the starting node of the edge + * @param to: the ending node of the edge + * @param weight: the weight of the edge + * + */ struct Edge { string from, to; int weight; @@ -20,11 +28,25 @@ struct Edge { // } }; - +/** + * @brief Function that compares two edges based on their weight + * + * @param e1: the first edge + * @param e2: the second edge + * @return true if the weight of e1 is greater than the weight of e2, false otherwise + */ bool compareEdge(Edge e1, Edge e2) { return (e1.weight > e2.weight); } +/** + * @brief Class that represents an undirected weighted graph + * + * @param AdjList: the adjacency list of the graph + * + * @details The class provides methods to add edges to the graph, to check if a combination of edges is valid and to find a solution to the problem + * + */ class UndirectedWeightedGraph { private: void addWeightedEdge(string from, string to, int weight) { @@ -57,6 +79,15 @@ class UndirectedWeightedGraph { // } // } + /** + * @brief Function that checks if a combination of edges is valid + * + * @param star: the combination of edges to check + * @return true if the combination is valid, false otherwise + * + * @details The function checks if the intersection of the sets of nodes reachable from the nodes of the edges in the combination is a singleton + * + */ bool checkComb(vector star) { auto it = star.begin(); auto v = AdjList[(*it).to]; @@ -79,28 +110,39 @@ class UndirectedWeightedGraph { return acc.size() == 1; } - vector> findSol(int c, int k) { + /** + * @brief Function that finds a solution to the problem + * + * @param c: the number of solutions to find + * @param k: the number of edges in the solution + * @param seed: the seed for the random number generator + * @return a vector of vectors of edges, each vector of edges represents a solution + * + * @details The function iterates over the keys of the adjacency list, shuffles them, and, for each key, it sorts the edges by weight and tries all the combinations of k edges. + * If a combination is valid, it is added to the solution vector + */ + vector> findSol(int c, int k, long seed) { vector> Sol; - vector keys = getShuffleKeys(AdjList); + vector keys = getShuffleKeys(AdjList, seed); for (auto& key : keys) { - auto node = AdjList[key]; + auto edges = AdjList[key]; if (c == 0) break; - if (node.size() >= k) { + if (edges.size() >= k) { - sort(node.begin(), node.end(), compareEdge); + sort(edges.begin(), edges.end(), compareEdge); vector indices(k); - iota(indices.begin(), indices.end(), 0); + iota(indices.begin(), indices.end(), 0); // Fill indices with 0, 1, 2, ..., k - 1 do { vector subV; for (int i : indices) - subV.push_back(node[i]); + subV.push_back(edges[i]); if (checkComb(subV)) { c--; @@ -108,7 +150,7 @@ class UndirectedWeightedGraph { break; } - } while(!nextComb(indices, node.size())); + } while(!nextComb(indices, edges.size())); } } diff --git a/Progetto.cpp b/Progetto.cpp index 38b5f34..69899a5 100644 --- a/Progetto.cpp +++ b/Progetto.cpp @@ -9,6 +9,7 @@ using namespace std; int main(int argc,char const *argv[]) { int k = 5; int count = 1; + long seed = -1; vector Files; @@ -25,6 +26,10 @@ int main(int argc,char const *argv[]) { i++; k = atoi(argv[i]); } + else if (arg == "-s") { + i++; + seed = atol(argv[i]); + } else { Files.push_back(arg); } @@ -46,7 +51,7 @@ int main(int argc,char const *argv[]) { populateGraph(&graph, phrases); } - vector> Sol = graph.findSol(count, k); + vector> Sol = graph.findSol(count, k, seed); graph.printSol(Sol); return 0; diff --git a/aux.cpp b/aux.cpp index 363c653..fb5c9af 100644 --- a/aux.cpp +++ b/aux.cpp @@ -10,6 +10,9 @@ using namespace std; +/** + * @brief Function that generates the next combination of k elements from a set of n elements + */ bool nextComb(vector &indices, int n) { int k = indices.size(); int i = n - 1; @@ -27,6 +30,9 @@ bool nextComb(vector &indices, int n) { return 0; } +/** + * @brief Function that returns the intersection of two sets + */ template unordered_set intersectSets(unordered_set s1, unordered_set s2) { unordered_set s; @@ -47,9 +53,18 @@ unordered_set intersectSets(unordered_set s1, unordered_set s2) { return s; } +/** + * @brief Function that shuffles the keys of a map and returns them in a vector + * + * @param map: the map to shuffle + * @param seed: the seed for the random number generator + */ template -vector getShuffleKeys(unordered_map map) { - srand(time(NULL)); +vector getShuffleKeys(unordered_map map, long seed) { + if (seed != -1) + srand(seed); + else + srand(time(NULL)); vector output; for (auto& pair : map) { output.push_back(pair.first);