-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellman_ford.cpp
More file actions
58 lines (50 loc) · 1.67 KB
/
bellman_ford.cpp
File metadata and controls
58 lines (50 loc) · 1.67 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
// Banu Miruns Elena, 321CA, November 2019
#include "algo.h"
#include <iostream>
#include <queue>
std::vector<int> computeBellmanFord(const std::vector<std::vector<edge>>& graph, int node) {
std::queue<int> q;
std::vector<bool> isEnqueued(graph.size(), false);
std::vector<int> distance(graph.size(), INF);
std::vector<unsigned int> count(graph.size(), 0);
distance[node] = 0;
q.push(node);
isEnqueued[node] = true;
count[node]++;
while (!q.empty()) {
int u = q.front();
q.pop();
isEnqueued[u] = false;
for (unsigned int i = 0; i < graph[u].size(); ++i) {
int v = graph[u][i].first;
int w = graph[u][i].second;
if ((w + distance[u] < distance[v] || distance[v] == INF) && distance[u] != INF) {
distance[v] = w + distance[u];
if (!isEnqueued[v]) {
q.push(v);
isEnqueued[v] = true;
}
count[v]++;
if (count[v] > graph.size()) {
return std::vector<int>();
}
}
}
}
return distance;
}
namespace BellmanFord {
std::vector<std::vector<int>> shortest_path(const std::vector<std::vector<edge>>& graph) {
std::vector<std::vector<int>> distanceMatrix;
std::vector<int> nodeArray;
for (unsigned int i = 0; i < graph.size(); ++i) {
nodeArray = computeBellmanFord(graph, i);
if (!nodeArray.size()) {
return std::vector<std::vector<int>>();
} else {
distanceMatrix.push_back(nodeArray);
}
}
return distanceMatrix;
}
}