OR-Tools  9.0
dijkstra.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 <set>
17 #include <vector>
18 
19 #include "absl/container/flat_hash_set.h"
23 
24 namespace operations_research {
25 namespace {
26 
27 // Priority queue element
28 class Element {
29  public:
30  bool operator<(const Element& other) const {
31  return distance_ != other.distance_ ? distance_ > other.distance_
32  : node_ > other.node_;
33  }
34  void SetHeapIndex(int h) { heap_index_ = h; }
35  int GetHeapIndex() const { return heap_index_; }
36  void set_distance(int64_t distance) { distance_ = distance; }
37  int64_t distance() const { return distance_; }
38  void set_node(int node) { node_ = node; }
39  int node() const { return node_; }
40 
41  private:
42  int64_t distance_ = 0;
43  int heap_index_ = -1;
44  int node_ = -1;
45 };
46 } // namespace
47 
48 template <class S>
49 class DijkstraSP {
50  public:
51  static constexpr int64_t kInfinity = kint64max / 2;
52 
53  DijkstraSP(int node_count, int start_node,
54  std::function<int64_t(int, int)> graph,
55  int64_t disconnected_distance)
56  : node_count_(node_count),
57  start_node_(start_node),
58  graph_(std::move(graph)),
59  disconnected_distance_(disconnected_distance),
60  predecessor_(new int[node_count]),
61  elements_(node_count) {}
62 
63  bool ShortestPath(int end_node, std::vector<int>* nodes) {
64  Initialize();
65  bool found = false;
66  while (!frontier_.IsEmpty()) {
67  int64_t distance;
68  int node = SelectClosestNode(&distance);
69  if (distance == kInfinity) {
70  found = false;
71  break;
72  } else if (node == end_node) {
73  found = true;
74  break;
75  }
76  Update(node);
77  }
78  if (found) {
79  FindPath(end_node, nodes);
80  }
81  return found;
82  }
83 
84  private:
85  void Initialize() {
86  for (int i = 0; i < node_count_; i++) {
87  elements_[i].set_node(i);
88  if (i == start_node_) {
89  predecessor_[i] = -1;
90  elements_[i].set_distance(0);
91  frontier_.Add(&elements_[i]);
92  } else {
93  elements_[i].set_distance(kInfinity);
94  predecessor_[i] = start_node_;
95  not_visited_.insert(i);
96  }
97  }
98  }
99 
100  int SelectClosestNode(int64_t* distance) {
101  const int node = frontier_.Top()->node();
102  *distance = frontier_.Top()->distance();
103  frontier_.Pop();
104  not_visited_.erase(node);
105  added_to_the_frontier_.erase(node);
106  return node;
107  }
108 
109  void Update(int node) {
110  for (const auto& other_node : not_visited_) {
111  const int64_t graph_node_i = graph_(node, other_node);
112  if (graph_node_i != disconnected_distance_) {
113  if (added_to_the_frontier_.find(other_node) ==
114  added_to_the_frontier_.end()) {
115  frontier_.Add(&elements_[other_node]);
116  added_to_the_frontier_.insert(other_node);
117  }
118  const int64_t other_distance =
119  elements_[node].distance() + graph_node_i;
120  if (elements_[other_node].distance() > other_distance) {
121  elements_[other_node].set_distance(other_distance);
122  frontier_.NoteChangedPriority(&elements_[other_node]);
123  predecessor_[other_node] = node;
124  }
125  }
126  }
127  }
128 
129  void FindPath(int dest, std::vector<int>* nodes) {
130  int j = dest;
131  nodes->push_back(j);
132  while (predecessor_[j] != -1) {
133  nodes->push_back(predecessor_[j]);
134  j = predecessor_[j];
135  }
136  }
137 
138  const int node_count_;
139  const int start_node_;
140  std::function<int64_t(int, int)> graph_;
141  const int64_t disconnected_distance_;
142  std::unique_ptr<int[]> predecessor_;
144  std::vector<Element> elements_;
145  S not_visited_;
146  S added_to_the_frontier_;
147 };
148 
149 bool DijkstraShortestPath(int node_count, int start_node, int end_node,
150  std::function<int64_t(int, int)> graph,
151  int64_t disconnected_distance,
152  std::vector<int>* nodes) {
154  node_count, start_node, std::move(graph), disconnected_distance);
155  return bf.ShortestPath(end_node, nodes);
156 }
157 
158 bool StableDijkstraShortestPath(int node_count, int start_node, int end_node,
159  std::function<int64_t(int, int)> graph,
160  int64_t disconnected_distance,
161  std::vector<int>* nodes) {
162  DijkstraSP<std::set<int>> bf(node_count, start_node, std::move(graph),
163  disconnected_distance);
164  return bf.ShortestPath(end_node, nodes);
165 }
166 } // namespace operations_research
bool ShortestPath(int end_node, std::vector< int > *nodes)
Definition: dijkstra.cc:63
DijkstraSP(int node_count, int start_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance)
Definition: dijkstra.cc:53
static constexpr int64_t kInfinity
Definition: dijkstra.cc:51
static const int64_t kint64max
Collection of objects used to extend the Constraint Solver library.
bool DijkstraShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance, std::vector< int > *nodes)
Definition: dijkstra.cc:149
bool StableDijkstraShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance, std::vector< int > *nodes)
Definition: dijkstra.cc:158
int nodes
double distance