Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Dijkstra.h
Go to the documentation of this file.
1
31#pragma once
32
35
36#include <vector>
37
38namespace ogdf {
39namespace internal {
40namespace gcm {
41namespace graph {
42template<typename Graph, typename Flags = datastructure::TimestampFlags>
43class Dijkstra {
44private:
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
56public:
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}
Priority queue interface wrapping various heaps.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition Graph_d.h:932
void pop()
Removes the top element from the heap.
void clear()
Removes all the entries from the queue.
bool empty() const
Checks whether the queue is empty.
Simple before-C++20 version for std::ranges::ref_view.
Definition Iterators.h:41
bool traverse_single(const Node &source, FWeight &&weight, FSettle &&settle, FExpand &&expand, FTraverse &&f_traverse)
Definition Dijkstra.h:121
bool traverse(const Range &sources, FWeight &&weight, FSettle &&settle, FExpand &&expand, FTraverse &&f_traverse)
Definition Dijkstra.h:78
static void traverse_nothing(typename Graph::Node, typename Graph::Edge)
Definition Dijkstra.h:62
static bool expand_all(typename Graph::Node, typename Graph::Edge)
Definition Dijkstra.h:60
std::vector< double > distances
Definition Dijkstra.h:54
Dijkstra(const Graph &_graph)
Definition Dijkstra.h:67
static void settle_nothing(typename Graph::Node, double weight)
Definition Dijkstra.h:57
std::vector< Element > reference
Definition Dijkstra.h:53
typename Heap::Handle Element
Definition Dijkstra.h:48
Defines a queue for handling prioritized elements.
const E & topElement() const
Returns the topmost element in the queue.
Handle push(const E &element, const P &priority)
Pushes a new element with the respective priority to the queue.
typename SuperQueue::handle Handle
The type of handle for accessing the elements of this queue.
void decrease(Handle pos, const P &priority)
The namespace for all OGDF objects.