Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar12 / src / graph_impl / graph
diff options
context:
space:
mode:
Commit00cd4d0b3541c1026a4c0f8d66d953690299178a (patch)
Author
 Stoycho Kyosev <skyosev@192.168.100.17>
2025-01-10 07:51:36 -0500
Committer
 Stoycho Kyosev <skyosev@192.168.100.17>
2025-01-10 07:51:36 -0500
Filesf5ff87b8fec65e5fe38bad2b2186d41c3a4cd0e8 /Seminar12/src/graph_impl/graph
Data-structures-and-algorithms-00cd4d0b3541c1026a4c0f8d66d953690299178a.tar
Data-structures-and-algorithms-00cd4d0b3541c1026a4c0f8d66d953690299178a.tar.gz
Data-structures-and-algorithms-00cd4d0b3541c1026a4c0f8d66d953690299178a.zip
Parent90b32d10c043f97b364537758e4edf610acd6bd1 (diff)
Add code for sem12
2 files changed, 76 insertions, 0 deletions
-rw-r--r--Seminar12/src/graph_impl/graph/graph.cpp53
-rw-r--r--Seminar12/src/graph_impl/graph/graph.h23
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