diff --git a/src/GraphTheory.cpp b/src/GraphTheory.cpp index 228b23c..20f5a39 100644 --- a/src/GraphTheory.cpp +++ b/src/GraphTheory.cpp @@ -644,9 +644,8 @@ namespace raven return path; } - void components( + std::vector components( const cGraph &g, - std::string &results, bool fclique) { // working copy on input graph @@ -744,19 +743,21 @@ namespace raven vclique.push_back(clique); } - // Display results - std::stringstream ss; - std::string comp_clique = "component "; - if( fclique ) - comp_clique = "clique "; - for (auto &c : vclique) - { - ss << comp_clique; - for (int n : c) - ss << g.userName(n) << " "; - ss << "\n"; - } - results = ss.str(); + return vclique; + + // // Display results + // std::stringstream ss; + // std::string comp_clique = "component "; + // if( fclique ) + // comp_clique = "clique "; + // for (auto &c : vclique) + // { + // ss << comp_clique; + // for (int n : c) + // ss << g.userName(n) << " "; + // ss << "\n"; + // } + // results = ss.str(); } double diff --git a/src/GraphTheory.h b/src/GraphTheory.h index cae8e07..10e57c6 100644 --- a/src/GraphTheory.h +++ b/src/GraphTheory.h @@ -303,14 +303,14 @@ namespace raven /// @brief find components or cliques /// @param g - /// @param[out] results /// @param clique true for cliques, default false + /// @return vector of vectors of vertex indices in each component + /// /// the vertices in a component are all reachable from each other /// the vertices in a clique are all adjacent - void components( + std::vector components( const cGraph &g, - std::string &results, bool clique = false); diff --git a/src/cPathFinderGUI.cpp b/src/cPathFinderGUI.cpp index 2451c22..1a1ce86 100644 --- a/src/cPathFinderGUI.cpp +++ b/src/cPathFinderGUI.cpp @@ -311,16 +311,34 @@ void cGUI::calcSales() void cGUI::calcComponents() { - components( - myGraphData.g, - myResultText); + auto vclique = components( + myGraphData.g); + std::stringstream ss; + std::string comp_clique = "component "; + for (auto &c : vclique) + { + ss << comp_clique; + for (int n : c) + ss << myGraphData.g.userName(n) << " "; + ss << "\n"; + } + myResultText = ss.str(); } void cGUI::calcCliques() { - components( + auto vclique = components( myGraphData.g, - myResultText, true); + std::stringstream ss; + std::string comp_clique = "clique "; + for (auto &c : vclique) + { + ss << comp_clique; + for (int n : c) + ss << myGraphData.g.userName(n) << " "; + ss << "\n"; + } + myResultText = ss.str(); } void cGUI::calcFlows() diff --git a/src/test.cpp b/src/test.cpp index cab2697..5e47d7a 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -328,10 +328,23 @@ TEST(cliques) raven::graph::sGraphData gd; gd.fname = "../dat/cliques.txt"; readfile(gd); - std::string results; - cliques(gd.g, results,false); - std::string expected("clique: 1 5 3 7 \nclique: 2 8 6 4 \n"); - CHECK_EQUAL(expected, results); + auto vclique = components(gd.g, false); + CHECK_EQUAL(2, vclique.size()); + std::vector> exp{ + {"1", "5", "3", "7"}, + {"2", "8", "6", "4"}}; + auto svclique = gd.g.userName( vclique[0] ); + CHECK(std::equal( + exp[0].begin(), + exp[0].end(), + svclique.begin())); + svclique = gd.g.userName( vclique[1] ); + CHECK(std::equal( + exp[1].begin(), + exp[1].end(), + svclique.begin())); + // std::string expected("clique: 1 5 3 7 \nclique: 2 8 6 4 \n"); + // CHECK_EQUAL(expected, results); } // TEST(dfs_allpaths)