-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellman_ford.cpp
More file actions
39 lines (35 loc) · 1.04 KB
/
bellman_ford.cpp
File metadata and controls
39 lines (35 loc) · 1.04 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
#include "bellman_ford.h"
#include <stdexcept>
void BellmanFord::Graph::BellmanFordAlgorithm(Vertex& source)
{
for (int i = 0; i < static_cast<int>(vertices.size()) - 1; ++i)
{
for (auto& [u, v] : adjacency_list)
{
// Relaxation
const auto weight_uv = weight_list.at(std::make_pair(u->id, v->id));
if (v->distance > (u->distance + weight_uv))
{
v->distance = u->distance + weight_uv;
v->predecessor = u;
}
}
}
for (auto& [u, v] : adjacency_list)
{
if (v->distance > (u->distance + weight_list.at(std::make_pair(u->id, v->id))))
{
throw std::runtime_error("Graph has a negative cycle");
}
}
}
void BellmanFord::Graph::AddVertex(Vertex& v)
{
vertices.push_back(&v);
}
void BellmanFord::Graph::AddEdge(Vertex& u, Vertex& v, int weight)
{
adjacency_list.emplace_back(&u, &v);
u.neighbors.insert(&v);
weight_list.insert(std::make_pair(std::make_pair(u.id, v.id), weight));
}