OR-Tools  9.0
bellman_ford.cc
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include <functional>
15 #include <memory>
16 #include <utility>
17 #include <vector>
18 
21 
22 namespace operations_research {
23 class BellmanFord {
24  public:
25  static constexpr int64_t kInfinity = kint64max / 2;
26 
27  BellmanFord(int node_count, int start_node,
28  std::function<int64_t(int, int)> graph,
29  int64_t disconnected_distance)
30  : node_count_(node_count),
31  start_node_(start_node),
32  graph_(std::move(graph)),
33  disconnected_distance_(disconnected_distance),
34  distance_(new int64_t[node_count_]),
35  predecessor_(new int[node_count_]) {}
36  bool ShortestPath(int end_node, std::vector<int>* nodes);
37 
38  private:
39  void Initialize();
40  void Update();
41  bool Check() const;
42  void FindPath(int dest, std::vector<int>* nodes) const;
43 
44  const int node_count_;
45  const int start_node_;
46  std::function<int64_t(int, int)> graph_;
47  const int64_t disconnected_distance_;
48  std::unique_ptr<int64_t[]> distance_;
49  std::unique_ptr<int[]> predecessor_;
50 };
51 
52 void BellmanFord::Initialize() {
53  for (int i = 0; i < node_count_; i++) {
54  distance_[i] = kint64max / 2;
55  predecessor_[i] = -1;
56  }
57  distance_[start_node_] = 0;
58 }
59 
60 void BellmanFord::Update() {
61  for (int i = 0; i < node_count_ - 1; i++) {
62  for (int u = 0; u < node_count_; u++) {
63  for (int v = 0; v < node_count_; v++) {
64  const int64_t graph_u_v = graph_(u, v);
65  if (graph_u_v != disconnected_distance_) {
66  const int64_t other_distance = distance_[u] + graph_u_v;
67  if (distance_[v] > other_distance) {
68  distance_[v] = other_distance;
69  predecessor_[v] = u;
70  }
71  }
72  }
73  }
74  }
75 }
76 
77 bool BellmanFord::Check() const {
78  for (int u = 0; u < node_count_; u++) {
79  for (int v = 0; v < node_count_; v++) {
80  const int graph_u_v = graph_(u, v);
81  if (graph_u_v != disconnected_distance_) {
82  if (distance_[v] > distance_[u] + graph_u_v) {
83  return false;
84  }
85  }
86  }
87  }
88  return true;
89 }
90 
91 void BellmanFord::FindPath(int dest, std::vector<int>* nodes) const {
92  int j = dest;
93  nodes->push_back(j);
94  while (predecessor_[j] != -1) {
95  nodes->push_back(predecessor_[j]);
96  j = predecessor_[j];
97  }
98 }
99 
100 bool BellmanFord::ShortestPath(int end_node, std::vector<int>* nodes) {
101  Initialize();
102  Update();
103  if (distance_[end_node] == kInfinity) {
104  return false;
105  }
106  if (!Check()) {
107  return false;
108  }
109  FindPath(end_node, nodes);
110  return true;
111 }
112 
113 bool BellmanFordShortestPath(int node_count, int start_node, int end_node,
114  std::function<int64_t(int, int)> graph,
115  int64_t disconnected_distance,
116  std::vector<int>* nodes) {
117  BellmanFord bf(node_count, start_node, std::move(graph),
118  disconnected_distance);
119  return bf.ShortestPath(end_node, nodes);
120 }
121 } // namespace operations_research
bool ShortestPath(int end_node, std::vector< int > *nodes)
BellmanFord(int node_count, int start_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance)
Definition: bellman_ford.cc:27
static constexpr int64_t kInfinity
Definition: bellman_ford.cc:25
static const int64_t kint64max
Collection of objects used to extend the Constraint Solver library.
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)
int nodes