Files
ortools-clone/graph/dijkstra.cc
2010-09-21 14:57:35 +00:00

167 lines
4.7 KiB
C++

// Copyright 2010 Google
// 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 <hash_set>
#include <vector>
#include "base/callback.h"
#include "base/commandlineflags.h"
#include "base/integral_types.h"
#include "base/scoped_ptr.h"
#include "graph/shortestpaths.h"
#include "base/adjustable_priority_queue.h"
DECLARE_int32(shortestpaths_disconnected_distance);
namespace operations_research {
namespace {
// Priority queue element
class Element {
public:
Element() : heap_index_(-1), distance_(0), node_(-1) {}
bool operator <(const Element& other) const {
return distance_ > other.distance_;
}
void SetHeapIndex(int h) { heap_index_ = h; }
int GetHeapIndex() const { return heap_index_; }
void set_distance(int64 distance) { distance_ = distance; }
int64 distance() const { return distance_; }
void set_node(int node) { node_ = node; }
int node() const { return node_; }
private:
int heap_index_;
int64 distance_;
int node_;
};
} // namespace
class DijkstraSP {
public:
static const int64 kInfinity = kint64max / 2;
DijkstraSP(int node_count,
int start_node,
ResultCallback2<int64, int, int>* const graph)
: node_count_(node_count),
start_node_(start_node),
graph_(graph),
predecessor_(new int[node_count]),
elements_(node_count) {
graph->CheckIsRepeatable();
}
bool ShortestPath(int end_node, vector<int>* nodes);
private:
void Initialize();
int SelectClosestNode(int64* distance);
void Update(int label);
void FindPath(int dest, vector<int>* nodes);
const int node_count_;
const int start_node_;
scoped_ptr<ResultCallback2<int64, int, int> > graph_;
scoped_array<int> predecessor_;
AdjustablePriorityQueue<Element> frontier_;
vector<Element> elements_;
hash_set<int> not_visited_;
hash_set<int> added_to_the_frontier_;
};
void DijkstraSP::Initialize() {
for (int i = 0; i < node_count_; i++) {
elements_[i].set_node(i);
if (i == start_node_) {
predecessor_[i] = -1;
elements_[i].set_distance(0);
frontier_.Add(&elements_[i]);
} else {
elements_[i].set_distance(kInfinity);
predecessor_[i] = start_node_;
not_visited_.insert(i);
}
}
}
int DijkstraSP::SelectClosestNode(int64* distance) {
const int node = frontier_.Top()->node();
*distance = frontier_.Top()->distance();
frontier_.Pop();
not_visited_.erase(node);
added_to_the_frontier_.erase(node);
return node;
}
void DijkstraSP::Update(int node) {
for (hash_set<int>::const_iterator it = not_visited_.begin();
it != not_visited_.end();
++it) {
const int other_node = *it;
const int64 graph_node_i = graph_->Run(node, other_node);
if (graph_node_i != FLAGS_shortestpaths_disconnected_distance) {
if (added_to_the_frontier_.find(other_node) ==
added_to_the_frontier_.end()) {
frontier_.Add(&elements_[other_node]);
added_to_the_frontier_.insert(other_node);
}
const int64 other_distance = elements_[node].distance() + graph_node_i;
if (elements_[other_node].distance() > other_distance) {
elements_[other_node].set_distance(other_distance);
frontier_.NoteChangedPriority(&elements_[other_node]);
predecessor_[other_node] = node;
}
}
}
}
void DijkstraSP::FindPath(int dest, vector<int>* nodes) {
int j = dest;
nodes->push_back(j);
while (predecessor_[j] != -1) {
nodes->push_back(predecessor_[j]);
j = predecessor_[j];
}
}
bool DijkstraSP::ShortestPath(int end_node, vector<int>* nodes) {
Initialize();
bool found = false;
while (!frontier_.IsEmpty()) {
int64 distance;
int node = SelectClosestNode(&distance);
if (distance == kInfinity) {
found = false;
break;
} else if (node == end_node) {
found = true;
break;
}
Update(node);
}
if (found) {
FindPath(end_node, nodes);
}
return found;
}
bool DijkstraShortestPath(int node_count,
int start_node,
int end_node,
ResultCallback2<int64, int, int>* const graph,
vector<int>* nodes) {
DijkstraSP bf(node_count, start_node, graph);
return bf.ShortestPath(end_node, nodes);
}
} // namespace operations_research