-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixGraph.cpp
49 lines (38 loc) · 1.26 KB
/
MatrixGraph.cpp
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
#include "MatrixGraph.h"
MatrixGraph::MatrixGraph(int vertices_count) : vertices_count(vertices_count),
matrix(vertices_count * vertices_count, false) {
}
MatrixGraph::MatrixGraph(const IGraph &graph) : vertices_count(graph.VerticesCount()),
matrix(vertices_count * vertices_count, false) {
for (int i = 0; i < graph.VerticesCount(); ++i) {
for (auto j: graph.GetNextVertices(i)) {
matrix[i * vertices_count + j] = true;
}
}
}
MatrixGraph::~MatrixGraph() {
}
void MatrixGraph::AddEdge(int from, int to) {
matrix[from * vertices_count + to] = true;
}
int MatrixGraph::VerticesCount() const {
return vertices_count;
}
std::vector<int> MatrixGraph::GetNextVertices(int vertex) const {
std::vector<int> result;
for (int j = 0; j < vertices_count; ++j) {
if (matrix[vertex * vertices_count + j]) {
result.push_back(j);
}
}
return result;
}
std::vector<int> MatrixGraph::GetPrevVertices(int vertex) const {
std::vector<int> result;
for (int i = 0; i < vertices_count; ++i) {
if (matrix[i * vertices_count + vertex]) {
result.push_back(i);
}
}
return result;
}