forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bellman_ford.cc
123 lines (111 loc) · 3.58 KB
/
bellman_ford.cc
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
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <utility>
#include <vector>
#include "ortools/base/integral_types.h"
#include "ortools/graph/shortestpaths.h"
namespace operations_research {
class BellmanFord {
public:
static constexpr int64_t kInfinity = std::numeric_limits<int64_t>::max() / 2;
BellmanFord(int node_count, int start_node,
std::function<int64_t(int, int)> graph,
int64_t disconnected_distance)
: node_count_(node_count),
start_node_(start_node),
graph_(std::move(graph)),
disconnected_distance_(disconnected_distance),
distance_(new int64_t[node_count_]),
predecessor_(new int[node_count_]) {}
bool ShortestPath(int end_node, std::vector<int>* nodes);
private:
void Initialize();
void Update();
bool Check() const;
void FindPath(int dest, std::vector<int>* nodes) const;
const int node_count_;
const int start_node_;
std::function<int64_t(int, int)> graph_;
const int64_t disconnected_distance_;
std::unique_ptr<int64_t[]> distance_;
std::unique_ptr<int[]> predecessor_;
};
void BellmanFord::Initialize() {
for (int i = 0; i < node_count_; i++) {
distance_[i] = std::numeric_limits<int64_t>::max() / 2;
predecessor_[i] = -1;
}
distance_[start_node_] = 0;
}
void BellmanFord::Update() {
for (int i = 0; i < node_count_ - 1; i++) {
for (int u = 0; u < node_count_; u++) {
for (int v = 0; v < node_count_; v++) {
const int64_t graph_u_v = graph_(u, v);
if (graph_u_v != disconnected_distance_) {
const int64_t other_distance = distance_[u] + graph_u_v;
if (distance_[v] > other_distance) {
distance_[v] = other_distance;
predecessor_[v] = u;
}
}
}
}
}
}
bool BellmanFord::Check() const {
for (int u = 0; u < node_count_; u++) {
for (int v = 0; v < node_count_; v++) {
const int graph_u_v = graph_(u, v);
if (graph_u_v != disconnected_distance_) {
if (distance_[v] > distance_[u] + graph_u_v) {
return false;
}
}
}
}
return true;
}
void BellmanFord::FindPath(int dest, std::vector<int>* nodes) const {
int j = dest;
nodes->push_back(j);
while (predecessor_[j] != -1) {
nodes->push_back(predecessor_[j]);
j = predecessor_[j];
}
}
bool BellmanFord::ShortestPath(int end_node, std::vector<int>* nodes) {
Initialize();
Update();
if (distance_[end_node] == kInfinity) {
return false;
}
if (!Check()) {
return false;
}
FindPath(end_node, nodes);
return true;
}
bool BellmanFordShortestPath(int node_count, int start_node, int end_node,
std::function<int64_t(int, int)> graph,
int64_t disconnected_distance,
std::vector<int>* nodes) {
BellmanFord bf(node_count, start_node, std::move(graph),
disconnected_distance);
return bf.ShortestPath(end_node, nodes);
}
} // namespace operations_research