Skip to content

Commit

Permalink
Fix Strongly Connected Components Search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
vadyushkins committed Dec 25, 2023
1 parent 2d7db89 commit 0115996
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
14 changes: 11 additions & 3 deletions algorithms/sccs/sccs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ std::vector<std::vector<int64_t>> strongly_connected_components_search(
}
}

std::vector<std::vector<int64_t>> transposed_graph(n);
for (int64_t i = 0; i < n; ++i) {
for (int64_t j : graph[i]) {
transposed_graph[j].push_back(i);
}
}

visited.assign(n, false);

std::vector<int64_t> strongly_connected_component;
Expand All @@ -34,7 +41,7 @@ std::vector<std::vector<int64_t>> strongly_connected_components_search(
visited[node] = true;
strongly_connected_component.push_back(node);

for (int64_t neighbour : graph[node]) {
for (int64_t neighbour : transposed_graph[node]) {
if (false == visited[neighbour]) {
compute_strongly_connected_component(neighbour);
}
Expand All @@ -43,8 +50,9 @@ std::vector<std::vector<int64_t>> strongly_connected_components_search(

std::vector<std::vector<int64_t>> result;
for (int64_t i = n - 1; i >= 0; --i) {
if (false == visited[i]) {
compute_strongly_connected_component(i);
int64_t node = condensation_order[i];
if (false == visited[node]) {
compute_strongly_connected_component(node);
result.emplace_back(strongly_connected_component);
strongly_connected_component.clear();
}
Expand Down
30 changes: 24 additions & 6 deletions algorithms/sccs/test_sccs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ TEST(StrongleConnectedComponents, Test0) {
std::vector<std::vector<int64_t>> strongly_connected_components = strongly_connected_components_search(graph);

std::vector<std::vector<int64_t>> expected_strongly_connected_components = {
{3, 2},
{1, 0},
{0, 1},
{2, 3},
};

ASSERT_EQ(strongly_connected_components, expected_strongly_connected_components);
Expand All @@ -33,8 +33,8 @@ TEST(StrongleConnectedComponents, Test1) {
std::vector<std::vector<int64_t>> strongly_connected_components = strongly_connected_components_search(graph);

std::vector<std::vector<int64_t>> expected_strongly_connected_components = {
{5, 3, 4},
{2, 1, 0},
{0, 1, 2},
{3, 5, 4},
};

ASSERT_EQ(strongly_connected_components, expected_strongly_connected_components);
Expand All @@ -49,8 +49,8 @@ TEST(StrongleConnectedComponents, Test2) {
std::vector<std::vector<int64_t>> strongly_connected_components = strongly_connected_components_search(graph);

std::vector<std::vector<int64_t>> expected_strongly_connected_components = {
{1},
{0},
{1},
};

ASSERT_EQ(strongly_connected_components, expected_strongly_connected_components);
Expand All @@ -68,7 +68,25 @@ TEST(StrongleConnectedComponents, Test3) {
std::vector<std::vector<int64_t>> strongly_connected_components = strongly_connected_components_search(graph);

std::vector<std::vector<int64_t>> expected_strongly_connected_components = {
{4, 0, 1, 2, 3},
{0, 4, 3, 2, 1},
};

ASSERT_EQ(strongly_connected_components, expected_strongly_connected_components);
}

TEST(StrongleConnectedComponents, Test4) {
std::vector<std::vector<int64_t>> graph = {
{},
{},
{},
};

std::vector<std::vector<int64_t>> strongly_connected_components = strongly_connected_components_search(graph);

std::vector<std::vector<int64_t>> expected_strongly_connected_components = {
{2},
{1},
{0},
};

ASSERT_EQ(strongly_connected_components, expected_strongly_connected_components);
Expand Down

0 comments on commit 0115996

Please sign in to comment.