-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSetGraph.cpp
48 lines (41 loc) · 1.31 KB
/
CSetGraph.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
#include <vector>
#include <unordered_set>
#include "CSetGraph.h"
CSetGraph::CSetGraph(int vertexNum) {
vertexCount = vertexNum;
adjLists = new std::unordered_set<int>[vertexNum];
adjListsReverse = new std::unordered_set<int>[vertexNum];
}
CSetGraph::CSetGraph(const IGraph* graph): CSetGraph(graph->VerticesCount()) {
std::vector<int> temp;
for (int i = 0; i < vertexCount; i++) {
graph->GetNextVertices(i, temp);
for (int j = 0; j < temp.size(); j++)
this->AddEdge(i, temp[j]);
temp.clear();
}
}
CSetGraph::~CSetGraph() {
delete[] adjLists;
delete[] adjListsReverse;
}
int CSetGraph::VerticesCount() const {
return vertexCount;
}
bool CSetGraph::HasEdge(int from, int to) {
return !(adjLists[from].find(to) == adjLists[from].end());
}
void CSetGraph::AddEdge(int from, int to) {
adjLists[from].insert(to);
adjListsReverse[to].insert(from);
}
void CSetGraph::GetNextVertices(int vertex, std::vector<int>& vertices) const {
for (std::unordered_set<int>::const_iterator it = adjLists[vertex].begin(); it != adjLists[vertex].end(); it++) {
vertices.push_back(*it);
}
}
void CSetGraph::GetPrevVertices(int vertex, std::vector<int>& vertices) const {
for (std::unordered_set<int>::const_iterator it = adjListsReverse[vertex].begin(); it != adjListsReverse[vertex].end(); it++) {
vertices.push_back(*it);
}
}