Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changing output format of Kosaraju algorithm #365

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions include/CXXGraph/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3364,36 +3364,39 @@ SCCResult<T> Graph<T>::kosaraju() const {

visited.clear();

std::function<void(shared<const Node<T>>, std::vector<Node<T>> &)>
std::function<void(shared<const Node<T>>, SCCResult<T>, int)>
dfs_helper1 =
[this, &rev, &visited, &dfs_helper1](shared<const Node<T>> source,
std::vector<Node<T>> &comp) {
SCCResult<T> result, int sccLabel) {
// mark the vertex visited
visited[source->getId()] = true;
// Add the current vertex to the strongly connected
// component
comp.push_back(*source);
//comp.push_back(*source);
result.sccMap[source->getId()] = sccLabel;

// travel the neighbors
for (int i = 0; i < rev[source].size(); i++) {
shared<const Node<T>> neighbor = rev[source].at(i).first;
if (visited[neighbor->getId()] == false) {
// make recursive call from neighbor
dfs_helper1(neighbor, comp);
dfs_helper1(neighbor, result, sccLabel);
}
}
};

int sccLabel = 0;
while (st.size() != 0) {
auto rem = st.top();
st.pop();
if (visited[rem->getId()] == false) {
std::vector<Node<T>> comp;
dfs_helper1(rem, comp);
result.stronglyConnectedComps.push_back(comp);
//std::vector<Node<T>> comp;
dfs_helper1(rem, result, sccLabel);
sccLabel++;
//result.stronglyConnectedComps.push_back(comp);
}
}

result.noOfComponents = sccLabel;
result.success = true;
return result;
}
Expand Down
4 changes: 3 additions & 1 deletion include/CXXGraph/Utility/Typedef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ struct SCCResult_struct {
bool success =
false; // TRUE if the function does not return error, FALSE otherwise
std::string errorMessage = ""; // message of error
Components<T> stronglyConnectedComps;
int noOfComponents = 0;
std::unordered_map<size_t, int> sccMap;
//Components<T> stronglyConnectedComps;
};
template <typename T>
using SCCResult = SCCResult_struct<T>;
Expand Down
42 changes: 18 additions & 24 deletions test/KosarajuTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@ using std::make_shared;

// helper function to compare strongly connected components (SCC) as computed
// by the algorithm with expected (correct) SCC
void compareComponents(CXXGraph::Components<int>& comp1,
void compareComponents(CXXGraph::SCCResult<int> result,
CXXGraph::Components<int>& comp2) {
ASSERT_EQ(comp1.size(), comp2.size());
ASSERT_EQ(result.noOfComponents, comp2.size());

for (size_t i = 0; i < comp1.size(); ++i) {
std::sort(comp1[i].begin(), comp1[i].end());
std::sort(comp2[i].begin(), comp2[i].end());
for(int i=0;i<comp2.size();i++){
int curComp = result.sccMap[comp2[i][0].getId()];
for(int j=1;j<comp2[i].size();j++){
ASSERT_EQ(result.sccMap[comp2[i][j].getId()], curComp);
}
}

std::sort(comp1.begin(), comp1.end(),
[](auto& c1, auto& c2) { return c1.front() < c2.front(); });

std::sort(comp2.begin(), comp2.end(),
[](auto& c1, auto& c2) { return c1.front() < c2.front(); });

ASSERT_EQ(comp1, comp2);
}

// undirected graph
Expand All @@ -39,8 +33,8 @@ TEST(KosarajuTest, test_1) {
CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge));
CXXGraph::Graph<int> graph(edgeSet);
auto res = graph.kosaraju();
ASSERT_EQ(res.stronglyConnectedComps.size(), 0);
CXXGraph::SCCResult<int> res = graph.kosaraju();
ASSERT_EQ(res.noOfComponents, 0);
ASSERT_FALSE(res.success);
ASSERT_EQ(res.errorMessage, CXXGraph::ERR_UNDIR_GRAPH);
}
Expand All @@ -55,11 +49,11 @@ TEST(KosarajuTest, test_2) {
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge2));
CXXGraph::Graph<int> graph(edgeSet);
auto res = graph.kosaraju();
CXXGraph::SCCResult<int> res = graph.kosaraju();
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.stronglyConnectedComps.size(), 1);
ASSERT_EQ(res.stronglyConnectedComps[0].size(), 2);
ASSERT_EQ(res.noOfComponents, 1);
//ASSERT_EQ(res.stronglyConnectedComps[0].size(), 2);
}

// 1 comp, 2 strongly connected comp
Expand All @@ -75,12 +69,12 @@ TEST(KosarajuTest, test_3) {
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge3));
CXXGraph::Graph<int> graph(edgeSet);
auto res = graph.kosaraju();
CXXGraph::SCCResult<int> res = graph.kosaraju();
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");

CXXGraph::Components<int> expectedComponents = {{node1, node2}, {node3}};
compareComponents(res.stronglyConnectedComps, expectedComponents);
compareComponents(res, expectedComponents);
}

// 2 components, 2 strongly connected comps
Expand All @@ -99,12 +93,12 @@ TEST(KosarajuTest, test_4) {
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge4));
CXXGraph::Graph<int> graph(edgeSet);
auto res = graph.kosaraju();
CXXGraph::SCCResult<int> res = graph.kosaraju();
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
CXXGraph::Components<int> expectedComponents = {{node1, node2},
{node3, node4}};
compareComponents(res.stronglyConnectedComps, expectedComponents);
compareComponents(res, expectedComponents);
}

TEST(KosarajuTest, test_5) {
Expand Down Expand Up @@ -168,7 +162,7 @@ TEST(KosarajuTest, test_5) {
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge21));

CXXGraph::Graph graph(edgeSet);
auto res = graph.kosaraju();
CXXGraph::SCCResult<int> res = graph.kosaraju();
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");

Expand All @@ -179,5 +173,5 @@ TEST(KosarajuTest, test_5) {
{node8, node9},
{node10, node11, node12, node13}};

compareComponents(res.stronglyConnectedComps, expectedComponents);
compareComponents(res, expectedComponents);
}
Loading