Skip to content

Commit

Permalink
Recalculation if graph was changed
Browse files Browse the repository at this point in the history
  • Loading branch information
bonewell committed Jun 27, 2020
1 parent ebb1dba commit 8f2f707
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
6 changes: 5 additions & 1 deletion graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void Graph::calculate(Vertex& from) {
updateNeighbors(current);
markAsVisited(current);
} while (!isFinished());
dirty_ = false;
}

void Graph::init() {
Expand All @@ -83,6 +84,7 @@ Id Graph::addVertex() {
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 {
Expand All @@ -98,7 +100,7 @@ std::list<Id> Graph::path(Vertex const& to) const {
std::list<Id> Graph::path(Id from, Id to, bool force) {
auto& source = *at(from);
auto const& target = *at(to);
if (force || !source.isSource()) {
if (force || dirty_ || !source.isSource()) {
calculate(source);
}
return path(target);
Expand All @@ -114,6 +116,7 @@ Vertex* Graph::at(Id id) {

void Graph::removeEdge(Id from, Id to) {
at(from)->neighbors.erase(at(to));
dirty_ = true;
}

void Graph::removeVertex(Id id) {
Expand All @@ -122,6 +125,7 @@ void Graph::removeVertex(Id id) {
p.second.neighbors.erase(v);
});
vertexes_.erase(v->id);
dirty_ = true;
}

void Graph::checkDistance(Distance distance) const {
Expand Down
1 change: 1 addition & 0 deletions graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class Graph {
Id id_{0};
std::unordered_map<Id, Vertex> vertexes_;
std::set<Vertex*, LessDistance> unvisited_{LessDistance{}};
bool dirty_{true};
};

#endif /* GRAPH_H_ */
48 changes: 48 additions & 0 deletions graph_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,51 @@ TEST(SPF, CalculatedTwoDifferentPathes) {

EXPECT_THAT(g.path(0, 4), ContainerEq(std::list<Id>{0, 2, 5, 4}));
}

TEST(SPF, RecalculateIfEdgeRemoved) {
Graph g;
g.addVertex(); g.addVertex(); g.addVertex(); g.addVertex();
g.addVertex(); g.addVertex();
g.setEdge(0, 1, 7); g.setEdge(0, 2, 9); g.setEdge(0, 5, 14);
g.setEdge(1, 0, 7); g.setEdge(1, 2, 10); g.setEdge(1, 3, 15);
g.setEdge(2, 0, 9); g.setEdge(2, 1, 10); g.setEdge(2, 5, 2);
g.setEdge(3, 1, 15); g.setEdge(3, 2, 11); g.setEdge(3, 4, 6);
g.setEdge(4, 3, 6); g.setEdge(4, 5, 9);
g.setEdge(5, 0, 14); g.setEdge(5, 2, 2); g.setEdge(5, 4, 9);
g.path(0, 4);
g.removeEdge(2, 5);

EXPECT_THAT(g.path(0, 4), ContainerEq(std::list<Id>{0, 5, 4}));
}

TEST(SPF, RecalculateIfWeightUpdated) {
Graph g;
g.addVertex(); g.addVertex(); g.addVertex(); g.addVertex();
g.addVertex(); g.addVertex();
g.setEdge(0, 1, 7); g.setEdge(0, 2, 9); g.setEdge(0, 5, 14);
g.setEdge(1, 0, 7); g.setEdge(1, 2, 10); g.setEdge(1, 3, 15);
g.setEdge(2, 0, 9); g.setEdge(2, 1, 10); g.setEdge(2, 5, 2);
g.setEdge(3, 1, 15); g.setEdge(3, 2, 11); g.setEdge(3, 4, 6);
g.setEdge(4, 3, 6); g.setEdge(4, 5, 9);
g.setEdge(5, 0, 14); g.setEdge(5, 2, 2); g.setEdge(5, 4, 9);
g.path(1, 5);
g.setEdge(2, 5, 100);

EXPECT_THAT(g.path(0, 4), ContainerEq(std::list<Id>{0, 5, 4}));
}

TEST(SPF, RecalculateIfVertexRemoved) {
Graph g;
g.addVertex(); g.addVertex(); g.addVertex(); g.addVertex();
g.addVertex(); g.addVertex();
g.setEdge(0, 1, 7); g.setEdge(0, 2, 9); g.setEdge(0, 5, 14);
g.setEdge(1, 0, 7); g.setEdge(1, 2, 10); g.setEdge(1, 3, 15);
g.setEdge(2, 0, 9); g.setEdge(2, 1, 10); g.setEdge(2, 5, 2);
g.setEdge(3, 1, 15); g.setEdge(3, 2, 11); g.setEdge(3, 4, 6);
g.setEdge(4, 3, 6); g.setEdge(4, 5, 9);
g.setEdge(5, 0, 14); g.setEdge(5, 2, 2); g.setEdge(5, 4, 9);
g.path(1, 5);
g.removeVertex(2);

EXPECT_THAT(g.path(0, 4), ContainerEq(std::list<Id>{0, 5, 4}));
}

0 comments on commit 8f2f707

Please sign in to comment.