OR-Tools  9.2
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 <cstdint>
15 #include <functional>
16 #include <limits>
17 #include <memory>
18 #include <utility>
19 #include <vector>
20 
23 
24 namespace operations_research {
25 class BellmanFord {
26  public:
27  static constexpr int64_t kInfinity = std::numeric_limits<int64_t>::max() / 2;
28 
29  BellmanFord(int node_count, int start_node,
30  std::function<int64_t(int, int)> graph,
31  int64_t disconnected_distance)
32  : node_count_(node_count),
33  start_node_(start_node),
34  graph_(std::move(graph)),
35  disconnected_distance_(disconnected_distance),
36  distance_(new int64_t[node_count_]),
37  predecessor_(new int[node_count_]) {}
38  bool ShortestPath(int end_node, std::vector<int>* nodes);
39 
40  private:
41  void Initialize();
42  void Update();
43  bool Check() const;
44  void FindPath(int dest, std::vector<int>* nodes) const;
45 
46  const int node_count_;
47  const int start_node_;
48  std::function<int64_t(int, int)> graph_;
49  const int64_t disconnected_distance_;
50  std::unique_ptr<int64_t[]> distance_;
51  std::unique_ptr<int[]> predecessor_;
52 };
53 
54 void BellmanFord::Initialize() {
55  for (int i = 0; i < node_count_; i++) {
56  distance_[i] = std::numeric_limits<int64_t>::max() / 2;
57  predecessor_[i] = -1;
58  }
59  distance_[start_node_] = 0;
60 }
61 
62 void BellmanFord::Update() {
63  for (int i = 0; i < node_count_ - 1; i++) {
64  for (int u = 0; u < node_count_; u++) {
65  for (int v = 0; v < node_count_; v++) {
66  const int64_t graph_u_v = graph_(u, v);
67  if (graph_u_v != disconnected_distance_) {
68  const int64_t other_distance = distance_[u] + graph_u_v;
69  if (distance_[v] > other_distance) {
70  distance_[v] = other_distance;
71  predecessor_[v] = u;
72  }
73  }
74  }
75  }
76  }
77 }
78 
79 bool BellmanFord::Check() const {
80  for (int u = 0; u < node_count_; u++) {
81  for (int v = 0; v < node_count_; v++) {
82  const int graph_u_v = graph_(u, v);
83  if (graph_u_v != disconnected_distance_) {
84  if (distance_[v] > distance_[u] + graph_u_v) {
85  return false;
86  }
87  }
88  }
89  }
90  return true;
91 }
92 
93 void BellmanFord::FindPath(int dest, std::vector<int>* nodes) const {
94  int j = dest;
95  nodes->push_back(j);
96  while (predecessor_[j] != -1) {
97  nodes->push_back(predecessor_[j]);
98  j = predecessor_[j];
99  }
100 }
101 
102 bool BellmanFord::ShortestPath(int end_node, std::vector<int>* nodes) {
103  Initialize();
104  Update();
105  if (distance_[end_node] == kInfinity) {
106  return false;
107  }
108  if (!Check()) {
109  return false;
110  }
111  FindPath(end_node, nodes);
112  return true;
113 }
114 
115 bool BellmanFordShortestPath(int node_count, int start_node, int end_node,
116  std::function<int64_t(int, int)> graph,
117  int64_t disconnected_distance,
118  std::vector<int>* nodes) {
119  BellmanFord bf(node_count, start_node, std::move(graph),
120  disconnected_distance);
121  return bf.ShortestPath(end_node, nodes);
122 }
123 } // namespace operations_research
static constexpr int64_t kInfinity
Definition: bellman_ford.cc:27
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)
int64_t max
Definition: alldiff_cst.cc:140
BellmanFord(int node_count, int start_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance)
Definition: bellman_ford.cc:29
Collection of objects used to extend the Constraint Solver library.
int nodes
bool ShortestPath(int end_node, std::vector< int > *nodes)