Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
AStarSearch.h
Go to the documentation of this file.
1
32#pragma once
33
35#include <ogdf/basic/Graph.h>
37#include <ogdf/basic/basic.h>
38
39#include <functional>
40
41namespace ogdf {
42
44
56template<typename T>
58private:
60
62 double m_maxGap;
64
66 const EdgeArray<T>* m_cost = nullptr;
67 std::function<T(node)> m_heuristic;
70 NodeQueue* m_queue = nullptr;
71
72public:
81 explicit AStarSearch(const bool directed = false, const double maxGap = 1,
82 const EpsilonTest& et = EpsilonTest())
83 : m_directed(directed), m_maxGap(maxGap), m_et(et) {
84 OGDF_ASSERT(m_et.geq(maxGap, 1.0));
85 }
86
102 const Graph& graph, const EdgeArray<T>& cost, const node source, const node target,
103 NodeArray<edge>& predecessor, std::function<T(node)> heuristic = [](node) {
104#ifdef _MSC_VER
105 return 0;
106#else
107 return T(0);
108#endif
109 }) {
110 // initialize auxiliary structures
111 m_cost = &cost;
112 m_distance.init(graph);
113 m_predecessor = &predecessor;
114 m_predecessor->init(graph);
115 m_heuristic = heuristic;
116
117 m_folded.init(graph, false);
118 NodeQueue queue(graph);
119 m_queue = &queue;
120
121 m_distance[source] = 0;
122 (*m_predecessor)[target] = nullptr;
123 m_queue->push(source, 0);
124
125 // investigate each node
126 while (!m_queue->empty()) {
127 node v = queue.topElement();
128 queue.pop();
129 m_folded[v] = true;
130
131 if (v == target) {
132 queue.clear();
133 } else {
135 }
136 }
137
138 OGDF_ASSERT((*m_predecessor)[target] == nullptr || validatePath(source, target));
139
140 return m_distance[target];
141 }
142
143private:
144#ifdef OGDF_DEBUG
145 bool validatePath(const node source, const node target) const {
146 NodeArray<bool> visited(*m_predecessor->graphOf(), false);
147
148 OGDF_ASSERT(m_et.equal(m_distance[source], T(0)));
149
150 for (node v = target; v != source;) {
151 OGDF_ASSERT(!visited[v]);
152
153 visited[v] = true;
154 edge e = (*m_predecessor)[v];
155
156 OGDF_ASSERT(e != nullptr);
157
158 node w = e->opposite(v);
159
161
162 v = w;
163 }
164
165 return true;
166 }
167#endif
168
169 void investigateNode(const node v) {
170 for (adjEntry adj = v->firstAdj(); adj != nullptr; adj = adj->succ()) {
171 edge e = adj->theEdge();
172 if (!m_directed || e->target() != v) {
173 node w = e->opposite(v);
174 T distanceW = m_distance[v] + (*m_cost)[e];
175 if (!m_folded(w) && (!m_queue->contains(w) || m_et.less(distanceW, m_distance[w]))) {
176 m_distance[w] = distanceW;
177 (*m_predecessor)[w] = e;
178 T priority = (T)(m_distance[w] + m_maxGap * m_heuristic(w));
179
180 if (!m_queue->contains(w)) {
181 m_queue->push(w, priority);
182 } else {
183 m_queue->decrease(w, priority);
184 }
185 }
186 }
187 }
188 }
189};
190
191}
Compare floating point numbers with epsilons and integral numbers with normal compare operators.
Includes declaration of graph class.
Priority queue interface wrapping various heaps.
Basic declarations, included by all source files.
A-Star informed search algorithm.
Definition AStarSearch.h:57
PrioritizedMapQueue< node, T > NodeQueue
Definition AStarSearch.h:59
void investigateNode(const node v)
NodeQueue * m_queue
Definition AStarSearch.h:70
NodeArray< edge > * m_predecessor
Definition AStarSearch.h:68
const EdgeArray< T > * m_cost
Definition AStarSearch.h:66
NodeArray< bool > m_folded
Definition AStarSearch.h:65
AStarSearch(const bool directed=false, const double maxGap=1, const EpsilonTest &et=EpsilonTest())
Initializes a new A* search algorithm.
Definition AStarSearch.h:81
EpsilonTest m_et
Definition AStarSearch.h:63
bool validatePath(const node source, const node target) const
NodeArray< T > m_distance
Definition AStarSearch.h:69
std::function< T(node)> m_heuristic
Definition AStarSearch.h:67
T call(const Graph &graph, const EdgeArray< T > &cost, const node source, const node target, NodeArray< edge > &predecessor, std::function< T(node)> heuristic=[](node) { return T(0);})
Computes the shortests path between source and target.
Class for adjacency list elements.
Definition Graph_d.h:143
Class for the representation of edges.
Definition Graph_d.h:364
node opposite(node v) const
Returns the adjacent node different from v.
Definition Graph_d.h:414
node target() const
Returns the target node of the edge.
Definition Graph_d.h:402
std::enable_if< std::is_integral< T >::value, bool >::type geq(const T &x, const T &y) const
Compare if x is GEQ to y for integral types.
std::enable_if< std::is_integral< T >::value, bool >::type less(const T &x, const T &y) const
Compare if x is LESS than y for integral types.
Definition EpsilonTest.h:55
std::enable_if< std::is_integral< T >::value, bool >::type equal(const T &x, const T &y) const
Compare if x is EQUAL to y for integral types.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
Class for the representation of nodes.
Definition Graph_d.h:241
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition Graph_d.h:287
Prioritized queue interface wrapper for heaps.
bool empty() const
Checks whether the queue is empty.
void init(const Base *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
bool contains(const E &element) const
Returns whether this queue contains that key.
void push(const E &element, const P &priority)
Adds a new element to the queue.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.