Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Dijkstra.h
Go to the documentation of this file.
1 
31 #pragma once
32 
35 
36 namespace ogdf {
37 namespace internal {
38 namespace gcm {
39 namespace graph {
40 template<typename Graph, typename Flags = datastructure::TimestampFlags>
41 class Dijkstra {
42 private:
43  using Node = typename Graph::Node;
44  using Edge = typename Graph::Edge;
46  using Element = typename Heap::Handle;
47 
48  const Graph& graph;
49  Flags visited;
51  std::vector<Element> reference;
52  std::vector<double> distances;
53 
54 public:
55  static void settle_nothing(typename Graph::Node, double weight) { /* nothing to do*/
56  }
57 
58  static bool expand_all(typename Graph::Node, typename Graph::Edge) { return true; }
59 
60  static void traverse_nothing(typename Graph::Node, typename Graph::Edge) { /* nothing to do*/
61  }
62 
63  Node cycle_vertex = nullptr;
64 
65  Dijkstra(const Graph& _graph)
66  : graph(_graph)
67  , visited(graph.max_node_index() + 1)
68  , heap()
69  , reference(graph.max_node_index() + 1, nullptr)
70  , distances(graph.max_node_index() + 1, 0) {
71  // nothing to do
72  }
73 
74  //return true on success, false on negative cycle
75  template<typename Range, typename FWeight, typename FSettle, typename FExpand, typename FTraverse>
76  bool traverse(const Range& sources, FWeight&& weight, FSettle&& settle, FExpand&& expand,
77  FTraverse&& f_traverse) {
78  visited.clear();
79  heap.clear();
80 
81  for (auto v : sources) {
82  reference[v->index()] = heap.push(v, 0);
83  distances[v->index()] = 0;
84  visited.set(v->index());
85  }
86 
87  while (!heap.empty()) {
88  const Node v = heap.topElement();
89  const double current_weight = distances[v->index()];
90  heap.pop();
91  reference[v->index()] = nullptr;
92  settle(v, current_weight);
93  for (const Edge e : graph.edges(v)) {
94  const Node w = e->opposite(v);
95  if (expand(v, e)) {
96  if (!visited.is_set(w->index())) {
97  f_traverse(v, e);
98  distances[w->index()] = current_weight + weight(v, e);
99  reference[w->index()] = heap.push(w, current_weight + weight(v, e));
100  visited.set(w->index());
101  } else if (current_weight + weight(v, e) < distances[w->index()]) {
102  distances[w->index()] = current_weight + weight(v, e);
103  f_traverse(v, e);
104  if (!reference[w->index()]) {
105  cycle_vertex = w;
106  return false;
107  }
108 
109  heap.decrease(reference[w->index()], distances[w->index()]);
110  }
111  auto r = reference[w->index()];
112  }
113  }
114  }
115  return true;
116  }
117 
118  template<typename FWeight, typename FSettle, typename FExpand, typename FTraverse>
119  bool traverse_single(const Node& source, FWeight&& weight, FSettle&& settle, FExpand&& expand,
120  FTraverse&& f_traverse) {
121  return traverse(std::vector<Node>(1, source), weight, settle, expand, f_traverse);
122  }
123 };
124 
125 }
126 }
127 }
128 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::internal::gcm::graph::Dijkstra::settle_nothing
static void settle_nothing(typename Graph::Node, double weight)
Definition: Dijkstra.h:55
PriorityQueue.h
Priority queue interface wrapping various heaps.
ogdf::internal::gcm::graph::Dijkstra::graph
const Graph & graph
Definition: Dijkstra.h:48
ogdf::internal::gcm::graph::Dijkstra::traverse_single
bool traverse_single(const Node &source, FWeight &&weight, FSettle &&settle, FExpand &&expand, FTraverse &&f_traverse)
Definition: Dijkstra.h:119
ogdf::internal::gcm::graph::Dijkstra::visited
Flags visited
Definition: Dijkstra.h:49
ogdf::pq_internal::PrioritizedQueue::decrease
void decrease(Handle pos, const P &priority)
Definition: PriorityQueue.h:301
ogdf::pq_internal::PrioritizedQueue
Defines a queue for handling prioritized elements.
Definition: PriorityQueue.h:269
ogdf::PriorityQueue::clear
void clear()
Removes all the entries from the queue.
Definition: PriorityQueue.h:203
ogdf::internal::gcm::graph::Dijkstra::Element
typename Heap::Handle Element
Definition: Dijkstra.h:46
ogdf::PriorityQueue::pop
void pop()
Removes the top element from the heap.
Definition: PriorityQueue.h:175
ogdf::internal::gcm::graph::Dijkstra::expand_all
static bool expand_all(typename Graph::Node, typename Graph::Edge)
Definition: Dijkstra.h:58
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:209
r
int r[]
Definition: hierarchical-ranking.cpp:8
ogdf::pq_internal::PrioritizedQueue::push
Handle push(const E &element, const P &priority)
Pushes a new element with the respective priority to the queue.
Definition: PriorityQueue.h:295
ogdf::internal::gcm::graph::Dijkstra::traverse_nothing
static void traverse_nothing(typename Graph::Node, typename Graph::Edge)
Definition: Dijkstra.h:60
ogdf::internal::gcm::graph::Dijkstra::Edge
typename Graph::Edge Edge
Definition: Dijkstra.h:44
ogdf::internal::gcm::graph::Dijkstra::traverse
bool traverse(const Range &sources, FWeight &&weight, FSettle &&settle, FExpand &&expand, FTraverse &&f_traverse)
Definition: Dijkstra.h:76
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::Range
Simple before-C++20 version for std::ranges::ref_view.
Definition: Iterators.h:42
ogdf::pq_internal::PrioritizedQueue::topElement
const E & topElement() const
Returns the topmost element in the queue.
Definition: PriorityQueue.h:284
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:927
ogdf::internal::gcm::graph::Dijkstra::Dijkstra
Dijkstra(const Graph &_graph)
Definition: Dijkstra.h:65
ogdf::internal::gcm::graph::Dijkstra::Node
typename Graph::Node Node
Definition: Dijkstra.h:43
ogdf::pq_internal::PrioritizedQueue::Handle
typename SuperQueue::handle Handle
The type of handle for accessing the elements of this queue.
Definition: PriorityQueue.h:278
ogdf::internal::gcm::graph::Dijkstra::reference
std::vector< Element > reference
Definition: Dijkstra.h:51
ogdf::internal::gcm::graph::Dijkstra::cycle_vertex
Node cycle_vertex
Definition: Dijkstra.h:63
ogdf::internal::gcm::graph::Dijkstra::distances
std::vector< double > distances
Definition: Dijkstra.h:52
TimestampFlags.h
ogdf::internal::gcm::graph::Dijkstra::heap
Heap heap
Definition: Dijkstra.h:50
ogdf::internal::gcm::graph::Dijkstra
Definition: Dijkstra.h:41