-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
135 lines (115 loc) · 2.99 KB
/
graph.cpp
File metadata and controls
135 lines (115 loc) · 2.99 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "graph.h"
#include <algorithm>
#include <iterator>
bool Graph::LessDistance::operator()(Vertex const* lhs, Vertex const* rhs) const {
return (lhs->info.distance < rhs->info.distance)
|| (lhs->info.distance == rhs->info.distance && lhs->id < rhs->id);
}
void Vertex::setDistance(Vertex const& a) {
if (info.visited) return;
auto d = a.info.distance + a.neighbors.at(this);
if (d < info.distance) {
info.distance = d;
info.previous = &a;
}
}
bool Vertex::isInfinity() const {
return info.distance == std::numeric_limits<Distance>::infinity();
}
bool Vertex::isSource() const {
return info.visited && !info.previous;
}
void Graph::updateNeighbors(Vertex const& a) {
std::for_each(begin(a.neighbors), end(a.neighbors),
[this, &a](auto const& p) {
auto v = p.first;
if (!v->info.visited) {
unvisited_.erase(v);
v->setDistance(a);
unvisited_.insert(v);
}
});
}
void Graph::markAsVisited(Vertex& a) {
a.info.visited = true;
unvisited_.erase(&a);
}
bool Graph::isFinished() const {
return unvisited_.empty() || (*begin(unvisited_))->isInfinity();
}
Vertex& Graph::next() {
return **begin(unvisited_);
}
void Graph::calculate(Vertex& from) {
init();
setSource(from);
do {
auto& current = next();
updateNeighbors(current);
markAsVisited(current);
} while (!isFinished());
dirty_ = false;
}
void Graph::init() {
std::for_each(begin(vertexes_), end(vertexes_),
[] (auto& p) { p.second.info = {}; });
unvisited_.clear();
std::transform(begin(vertexes_), end(vertexes_),
std::inserter(unvisited_, unvisited_.end()),
[] (auto& p) -> Vertex* { return &p.second; });
}
void Graph::setSource(Vertex& v) {
unvisited_.erase(&v);
v.info.distance = 0;
unvisited_.insert(&v);
}
Id Graph::addVertex() {
vertexes_.emplace(id_, Vertex{id_});
return id_++;
}
void Graph::setEdge(Id from, Id to, Distance distance) {
checkDistance(distance);
at(from)->neighbors[at(to)] = distance;
dirty_ = true;
}
std::list<Id> Graph::path(Vertex const& to) const {
std::list<Id> p{to.id};
auto prev = to.info.previous;
while (prev) {
p.push_front(prev->id);
prev = prev->info.previous;
}
return p;
}
std::list<Id> Graph::path(Id from, Id to, bool force) {
auto& source = *at(from);
auto const& target = *at(to);
if (force || dirty_ || !source.isSource()) {
calculate(source);
}
return path(target);
}
Vertex* Graph::at(Id id) {
auto it = vertexes_.find(id);
if (it == end(vertexes_)) {
throw std::invalid_argument{"Wrong vertex ID"};
}
return &it->second;
}
void Graph::removeEdge(Id from, Id to) {
at(from)->neighbors.erase(at(to));
dirty_ = true;
}
void Graph::removeVertex(Id id) {
auto const v = at(id);
std::for_each(begin(vertexes_), end(vertexes_), [v](auto& p) {
p.second.neighbors.erase(v);
});
vertexes_.erase(v->id);
dirty_ = true;
}
void Graph::checkDistance(Distance distance) const {
if (distance < 0) {
throw std::invalid_argument{"Negative weight"};
}
}