Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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