This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
87 lines (66 loc) · 1.94 KB
/
graph.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "graph.h"
#include <vector>
using std::vector;
Graph::Graph() {
n = 0;
}
Graph::Graph(int n) {
Graph::n = n;
}
void Graph::AddEdge(int a, int b) {
edges.insert(std::make_pair(a, b));
edgesReverse.insert(std::make_pair(b, a));
}
void Graph::AddEdgeBidirectional(int a, int b) {
AddEdge(a, b);
AddEdge(b, a);
}
void Graph::RemoveEdge(int a, int b) {
edges.erase(std::make_pair(a, b));
edgesReverse.erase(std::make_pair(b, a));
}
void Graph::RemoveEdgeBidirectional(int a, int b) {
RemoveEdge(a, b);
RemoveEdge(b, a);
}
// void RemoveVertex(int a) {
// vector< std::pair<int, int> > toRemove;
// for (auto it = edges.lower_bound(std::make_pair(a, 0));
// it != edges.cend() && it->first == a; ++it)
// {
// toRemove.push_back(it->first);
// }
// for (auto it = edgesReverse.lower_bound(std::make_pair(a, 0));
// it != edgesReverse.cend() && it->first == a; ++it)
// {
// toRemove.push_back(it->first);
// }
// for (int32_t i = 0; i < toRemove.size(); ++i) {
// RemoveEdge(toRemove[i].first, toRemove[i].second);
// }
// }
bool Graph::EdgeExists(int a, int b) const {
return (edges.find(std::make_pair(a, b)) != edges.cend());
}
bool Graph::EdgeExistsBidirectional(int a, int b) const {
return EdgeExists(a, b) && EdgeExists(b, a);
}
vector<int> Graph::GetDirectSuccessors(int node) const {
vector<int> res;
for (auto it = edges.lower_bound(std::make_pair(node, 0));
it != edges.cend() && it->first == node; ++it) {
res.push_back(it->second);
}
return res;
}
vector<int> Graph::GetDirectPredecessors(int node) const {
vector<int> res;
for (auto it = edgesReverse.lower_bound(std::make_pair(node, 0));
it != edgesReverse.cend() && it->first == node; ++it) {
res.push_back(it->second);
}
return res;
}
bool Graph::Symmetric() const {
return (edges == edgesReverse);
}