diff options
Add code for sem12
2 files changed, 76 insertions, 0 deletions
| -rw-r--r-- | Seminar12/src/graph_impl/graph/graph.cpp | 53 | ||||
| -rw-r--r-- | Seminar12/src/graph_impl/graph/graph.h | 23 |
diff --git a/Seminar12/src/graph_impl/graph/graph.cpp b/Seminar12/src/graph_impl/graph/graph.cpp new file mode 100644 index 0000000..b173b51 --- /dev/null +++ b/Seminar12/src/graph_impl/graph/graph.cpp @@ -0,0 +1,53 @@ +#include "graph.h" +#include <stdexcept> + +graph::graph(size_t vertex_count, bool is_oriented) : + _adjacent(vertex_count), + _oriented(is_oriented) {} + +void graph::add_edge(vertex_t from, vertex_t to) +{ + if(from >= _adjacent.size() || to >= _adjacent.size()) + throw std::runtime_error("Invalid vertex passed"); + + _adjacent[from].push_back(to); + if(!_oriented) + _adjacent[to].push_back(from); +} + +void graph::add_vertex() +{ + _adjacent.emplace_back(); +} + +size_t graph::vertex_count() const +{ + return _adjacent.size(); +} + +bool graph::is_oriented() const +{ + return _oriented; +} + +const std::vector<graph::vertex_t>& graph::adjacent(vertex_t vertex) const +{ + if(vertex >= _adjacent.size()) + throw std::runtime_error("Invalid vertex passed"); + + return _adjacent[vertex]; +} + +bool graph::are_adjacent(vertex_t v1, vertex_t v2) const +{ + if(v1 >= _adjacent.size() || v2 >= _adjacent.size()) + throw std::runtime_error("Invalid vertex passed"); + + for (const auto& v : _adjacent[v1]) + { + if(v == v2) + return true; + } + + return false; +}
\ No newline at end of file diff --git a/Seminar12/src/graph_impl/graph/graph.h b/Seminar12/src/graph_impl/graph/graph.h new file mode 100644 index 0000000..fc1eb31 --- /dev/null +++ b/Seminar12/src/graph_impl/graph/graph.h @@ -0,0 +1,23 @@ +#pragma once +#include <vector> + +class graph +{ +public: + using vertex_t = int; +private: + std::vector<std::vector<vertex_t>> _adjacent; + bool _oriented; + +public: + graph(size_t vertex_count, bool is_oriented); + + void add_edge(vertex_t from, vertex_t to); + void add_vertex(); + + size_t vertex_count() const; + bool is_oriented() const; + + const std::vector<vertex_t>& adjacent(vertex_t vertex) const; + bool are_adjacent(vertex_t v1, vertex_t v2) const; +};
\ No newline at end of file |
