Skip to content

GH-111: Solve problem dsas_aog_week2_graph_decomposition2_3_intersection_reachability #119

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

Merged
merged 2 commits into from
Dec 30, 2022
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
14 changes: 14 additions & 0 deletions collections/datastructures-and-algorithms-specialization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@
<a href="../../problems/dsas_aog_week2_graph_decomposition2_1_cs_curriculum/src/main/acyclicity.cpp"><code>cpp🐀</code></a>
</td>
</tr>
<tr>
<td>2. Determining an Order of Courses</td>
<td><a href="../../problems/dsas_aog_week2_graph_decomposition2_2_order_of_courses/doc/week2_graph_decomposition2.pdf">Link</a></td>
<td>
<a href="../../problems/dsas_aog_week2_graph_decomposition2_2_order_of_courses/src/main/toposort.cpp"><code>cpp🐀</code></a>
</td>
</tr>
<tr>
<td>3. Intersection Reachability</td>
<td><a href="../../problems/dsas_aog_week2_graph_decomposition2_3_intersection_reachability/doc/week2_graph_decomposition2.pdf">Link</a></td>
<td>
<a href="../../problems/dsas_aog_week2_graph_decomposition2_3_intersection_reachability/src/main/strongly_connected.cpp"><code>cpp🐀</code></a>
</td>
</tr>
</tbody>
</table>

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
solution
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# DSAS Problem

## Usage

Run program with an example

```
bazel run src/main:strongly_connected < tests/data/1.in
```

Test program

```
# Run all tests
bazel test --test_output=all tests:solution_test
bazel test --test_output=all --cache_test_results=no tests:solution_test
# Run test with pecific test id
bazel test --test_output=all tests:solution_test --test_arg=1
```
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "strongly_connected",
srcs = ["strongly_connected.cpp"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while(tt--){
cout << tt << '\n';
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <algorithm>
#include <iostream>
#include <vector>

using std::pair;
using std::vector;

// function to fill the finishing times vector using DFS on the reverse graph
void dfs_reverse(int x, vector<vector<int>> &adj, vector<int> &used,
vector<int> &f) {
used[x] = 1;
for (int i = 0; i < adj[x].size(); i++) {
if (!used[adj[x][i]]) {
dfs_reverse(adj[x][i], adj, used, f);
}
}
f.push_back(x);
}

// function to count the number of SCCs using DFS on the original graph
void dfs(int x, vector<vector<int>> &adj, vector<int> &used, int &result) {
used[x] = 1;
for (int i = 0; i < adj[x].size(); i++) {
if (!used[adj[x][i]]) {
dfs(adj[x][i], adj, used, result);
}
}
}

int number_of_strongly_connected_components(vector<vector<int>> adj) {
int result = 0;
int n = adj.size();
vector<int> used(n, 0);
vector<int> f;

// perform DFS on the reverse graph to get the finishing times
for (int i = 0; i < n; i++) {
if (!used[i]) {
dfs_reverse(i, adj, used, f);
}
}

// reset the used vector
std::fill(used.begin(), used.end(), 0);

// build the reverse graph
vector<vector<int>> adj_reverse(n, vector<int>());
for (int i = 0; i < n; i++) {
for (int j = 0; j < adj[i].size(); j++) {
adj_reverse[adj[i][j]].push_back(i);
}
}

// peform DFS on the original graph using the finish times as the order in
// which the nodes should be visited
for (int i = f.size() - 1; i >= 0; i--) {
if (!used[f[i]]) {
result++;
dfs(f[i], adj_reverse, used, result);
}
}

return result;
}

int main() {
size_t n, m;
std::cin >> n >> m;
vector<vector<int>> adj(n, vector<int>());
for (size_t i = 0; i < m; i++) {
int x, y;
std::cin >> x >> y;
adj[x - 1].push_back(y - 1);
}
std::cout << number_of_strongly_connected_components(adj);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")

cc_test(
name = "solution_test",
srcs = ["solution_test.cpp"],
deps = [
"//problems/codeforcesAA/src/main:solution",
"@com_google_googletest//:gtest_main",
],
data = ["data"]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4 4
1 2
4 1
2 3
3 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
5 7
2 1
3 2
3 1
4 3
4 1
5 2
5 3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

#include "gtest/gtest.h"

namespace fs = std::filesystem;

using namespace std;

std::string TEST_ID = "";

vector<string> split(string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;

while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}

res.push_back(s.substr(pos_start));
return res;
}

std::string GetHello(std::string_view in) {
if (in.size() == 0) {
return std::string("hello, word");
} else {
return std::string("Hello, ") + in.data();
}
}

string ReadFile(const std::string &filename) {
std::ifstream f(filename);
string s((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
return s;
}

vector<string> RunProgram(string fileid) {
vector<string> v;
string program = "problems/codeforcesAA/src/main/solution";
string test_folder = "problems/codeforcesAA/tests/data/";
string command = program + " < " + test_folder + fileid + ".in > output.txt";
std::system(command.c_str());
string actual = ReadFile("output.txt");
string expectedOutputFile = test_folder + fileid + ".out";
string expected = ReadFile(expectedOutputFile);
v.push_back(actual);
v.push_back(expected);
return v;
}

TEST(RunTest, AllTestCases) {
if (TEST_ID != "") {
std::cout << "TEST_ID = " << TEST_ID << std::endl;
} else {
std::cout << "RUN ALL TESTS" << std::endl;
}

std::string test_data_folder = "problems/codeforcesAA/tests/data";
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
string filename = split(entry.path(), "/").back();
vector<string> v = split(filename, ".");
string fileid = v[0];
string extension = v[1];
if (extension != "in"){
continue;
}
if (TEST_ID != ""){
if (fileid != TEST_ID){
continue;
}
}
// Run Test Case
vector<string> output = RunProgram(fileid);
string actual = output[0];
string expected = output[1];
string message = "❌ FAIL CASE: " + fileid;
ASSERT_EQ(actual, expected) << message;
}
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
if (argc == 2) {
TEST_ID = argv[1];
}
return RUN_ALL_TESTS();
}