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
36namespace ogdf {
37namespace internal {
38namespace gcm {
39namespace graph {
40template<typename Graph, typename Flags = datastructure::TimestampFlags>
41class Dijkstra {
42private:
43 using Node = typename Graph::Node;
44 using Edge = typename Graph::Edge;
46 using Element = typename Heap::Handle;
47
48 const Graph& graph;
51 std::vector<Element> reference;
52 std::vector<double> distances;
53
54public:
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
66 : graph(_graph)
68 , heap()
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,
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;
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,
121 return traverse(std::vector<Node>(1, source), weight, settle, expand, f_traverse);
122 }
123};
124
125}
126}
127}
128}
Priority queue interface wrapping various heaps.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:521
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition Graph_d.h:592
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.
bool traverse_single(const Node &source, FWeight &&weight, FSettle &&settle, FExpand &&expand, FTraverse &&f_traverse)
Definition Dijkstra.h:119
bool traverse(const Range &sources, FWeight &&weight, FSettle &&settle, FExpand &&expand, FTraverse &&f_traverse)
Definition Dijkstra.h:76
static void traverse_nothing(typename Graph::Node, typename Graph::Edge)
Definition Dijkstra.h:60
static bool expand_all(typename Graph::Node, typename Graph::Edge)
Definition Dijkstra.h:58
std::vector< double > distances
Definition Dijkstra.h:52
Dijkstra(const Graph &_graph)
Definition Dijkstra.h:65
static void settle_nothing(typename Graph::Node, double weight)
Definition Dijkstra.h:55
std::vector< Element > reference
Definition Dijkstra.h:51
typename Heap::Handle Element
Definition Dijkstra.h:46
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)
int r[]
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.