blob: b173b51c0a3a9e4e53a4ed42b66b8446c3dae629 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
}
|