Skip to content
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ cmake_minimum_required(VERSION 3.14)
project(graphs)

find_package(Boost REQUIRED)
find_package(GTest REQUIRED)

include(CTest)
include(GoogleTest)

add_library(graphs INTERFACE)
target_include_directories(graphs INTERFACE include)
Expand Down
2 changes: 2 additions & 0 deletions conan.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"requires": [
"zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76%1733936244.862",
"libbacktrace/cci.20210118#a7691bfccd8caaf66309df196790a5a1%1722218217.276",
"gtest/1.17.0#5224b3b3ff3b4ce1133cbdd27d53ee7d%1755784855.585",
"bzip2/1.0.8#d00dac990f08d991998d624be81a9526%1724661266.998",
"boost/1.86.0#0675eb54da69d8eed264eb22203a7117%1761059017.02"
],
"build_requires": [
"cmake/4.2.0#ae0a44f44a1ef9ab68fd4b3e9a1f8671%1764093466.37",
"b2/5.2.1#91bc73931a0acb655947a81569ed8b80%1718416612.241"
],
"python_requires": [],
Expand Down
1 change: 1 addition & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[requires]
boost/1.86.0
gtest/1.17.0

[generators]
CMakeDeps
Expand Down
127 changes: 114 additions & 13 deletions include/graphs/graph.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <filesystem>
#include <format>
#include <queue>
#include <unordered_map>
#include <vector>

Expand All @@ -12,42 +11,144 @@ namespace graph {
template <typename T>
class Graph final {
public:
using AdjMap = std::unordered_map<T, std::vector<T>>;

Graph() noexcept = default;

Graph(
std::initializer_list<std::pair<T, std::initializer_list<T>>> adj_list) {
for (const auto& [v, a] : adj_list) {
insert(v, std::vector(a));
}
}

bool insert(const T& v, const std::vector<T>& adj) {
vertices_.insert(v);
auto it = adj_list_.find(v);
if (it != adj_list_.end()) {
return false;
}

for (const auto& a : adj) {
vertices_.insert(a);
auto it = adj_list_.find(a);
++in_deg_[a];
out_deg_.emplace(a, 0);
}

return adj_list_.insert({v, adj}).second;
in_deg_.emplace(v, 0);
out_deg_[v] = adj.size();
adj_list_.emplace(v, adj);
return true;
}

void dump() const {
std::vector<T> topologicalSort() const {
std::vector<T> order;
std::queue<T> q;

for (auto& [u, cnt] : in_deg_) {
if (cnt == 0) {
q.push(u);
}
}

order.reserve(in_deg_.size());
auto id = in_deg_;

while (!q.empty()) {
T u = q.front();
q.pop();

order.push_back(u);

auto it = adj_list_.find(u);

if (it != adj_list_.end()) {
for (auto& v : it->second) {
if (--id[v] == 0) {
q.push(v);
}
}
}
}

if (order.size() != in_deg_.size()) {
throw std::runtime_error("Graph has cycle");
}
return order;
}

void dump(std::ostream& os) const {
using namespace boost;

using OutGraph = adjacency_list<vecS, vecS, directedS,
property<vertex_name_t, std::string>,
property<edge_name_t, std::string>>;

OutGraph g;

auto start = add_vertex(g);
auto end = add_vertex(g);
put(vertex_name, g, start, "Start");
put(vertex_name, g, end, "End");

std::unordered_map<T, OutGraph::vertex_descriptor> vd;
vd.reserve(adj_list_.size());
for (const auto& v : vertices_) {
vd.reserve(in_deg_.size());
for (const auto& [v, _] : in_deg_) {
auto d = add_vertex(g);
put(vertex_name, g, d, std::to_string(v));
vd.insert({v, d});
vd.emplace(v, d);
}

for (const auto& [v, cnt] : in_deg_) {
if (cnt == 0) {
add_edge(start, vd[v], g);
}
}

for (auto& [v, adj] : adj_list_) {
for (const auto& [v, cnt] : out_deg_) {
if (cnt == 0) {
add_edge(vd[v], end, g);
}
}

for (const auto& [v, adj] : adj_list_) {
for (const auto& a : adj) {
add_edge(vd[v], vd[a], g);
}
}

write_graphviz(std::cout, g, make_label_writer(get(vertex_name, g)));
write_graphviz(os, g, make_label_writer(get(vertex_name, g)));
}

private:
std::unordered_map<T, std::vector<T>> adj_list_;
std::unordered_set<T> vertices_;
AdjMap adj_list_;

using DegreeMap = std::unordered_map<T, std::size_t>;
DegreeMap in_deg_;
DegreeMap out_deg_;
};

template <typename T>
inline Graph<T> readGraph(std::istream& is) {
graph::Graph<T> g;

std::string line;
while (std::getline(is, line)) {
if (line.empty()) {
break;
}

std::istringstream ss(line);
T u;
ss >> u;

std::vector<T> adj;
T v;
while (ss >> v) {
adj.push_back(v);
}
g.insert(u, adj);
}

return g;
}
} // namespace graph
24 changes: 2 additions & 22 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,8 @@ int main(int argc, char** argv) try {
return 0;
}

graph::Graph<std::size_t> g;

std::string line;
while (std::getline(std::cin, line)) {
if (line.empty()) {
break;
}

std::istringstream ss(line);
std::size_t u;
ss >> u;

std::vector<std::size_t> adj;
std::size_t v;
while (ss >> v) {
adj.push_back(v);
}

g.insert(u, adj);
}

g.dump();
auto g = graph::readGraph<std::size_t>(std::cin);
g.dump(std::cout);
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
Expand Down
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
add_test(NAME GraphvizOutputTest
COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/run_tests.py --executable $<TARGET_FILE:graphs_main>)

add_executable(unit_test unit_tests.cc)
target_link_libraries(unit_test gtest::gtest graphs)

gtest_discover_tests(unit_test)
24 changes: 24 additions & 0 deletions tests/in/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
1 2 6
2 3 7
3 4 8
4 5 9
5 10
6 7 11
7 8 12
8 9 13
9 10 14
10 15
11 12 16
12 13 17
13 14 18
14 15 19
15 20
16 17 21
17 18 22
18 19 23
19 20 24
20 25
21 22
22 23
23 24
24 25
7 changes: 7 additions & 0 deletions tests/in/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1 2 3
2 4 5
3 6 7
4 8 9
5 10 11
6 12 13
7 14 15
19 changes: 19 additions & 0 deletions tests/in/4.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1 2 11
2 3 12
3 4 13
4 5 14
5 6 15
6 7 16
7 8 17
8 9 18
9 10 19
10 20
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
Loading