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
38namespace ogdf {
39
41
53template<typename T>
55private:
57
59 double m_maxGap;
61
63 const EdgeArray<T>* m_cost = nullptr;
64 std::function<T(node)> m_heuristic;
67 NodeQueue* m_queue = nullptr;
68
69public:
78 explicit AStarSearch(const bool directed = false, const double maxGap = 1,
79 const EpsilonTest& et = EpsilonTest())
80 : m_directed(directed), m_maxGap(maxGap), m_et(et) {
82 }
83
99 const Graph& graph, const EdgeArray<T>& cost, const node source, const node target,
100 NodeArray<edge>& predecessor, std::function<T(node)> heuristic = [](node) {
101#ifdef _MSC_VER
102 return 0;
103#else
104 return T(0);
105#endif
106 }) {
107 // initialize auxiliary structures
108 m_cost = &cost;
109 m_distance.init(graph);
110 m_predecessor = &predecessor;
111 m_predecessor->init(graph);
113
114 m_folded.init(graph, false);
115 NodeQueue queue(graph);
116 m_queue = &queue;
117
118 m_distance[source] = 0;
119 (*m_predecessor)[target] = nullptr;
120 m_queue->push(source, 0);
121
122 // investigate each node
123 while (!m_queue->empty()) {
124 node v = queue.topElement();
125 queue.pop();
126 m_folded[v] = true;
127
128 if (v == target) {
129 queue.clear();
130 } else {
132 }
133 }
134
135 OGDF_ASSERT((*m_predecessor)[target] == nullptr || validatePath(source, target));
136
137 return m_distance[target];
138 }
139
140private:
141#ifdef OGDF_DEBUG
142 bool validatePath(const node source, const node target) const {
143 NodeArray<bool> visited(*m_predecessor->graphOf(), false);
144
145 OGDF_ASSERT(m_et.equal(m_distance[source], T(0)));
146
147 for (node v = target; v != source;) {
148 OGDF_ASSERT(!visited[v]);
149
150 visited[v] = true;
151 edge e = (*m_predecessor)[v];
152
153 OGDF_ASSERT(e != nullptr);
154
155 node w = e->opposite(v);
156
158
159 v = w;
160 }
161
162 return true;
163 }
164#endif
165
166 void investigateNode(const node v) {
167 for (adjEntry adj = v->firstAdj(); adj != nullptr; adj = adj->succ()) {
168 edge e = adj->theEdge();
169 if (!m_directed || e->target() != v) {
170 node w = e->opposite(v);
171 T distanceW = m_distance[v] + (*m_cost)[e];
172 if (!m_folded(w) && (!m_queue->contains(w) || m_et.less(distanceW, m_distance[w]))) {
174 (*m_predecessor)[w] = e;
175 T priority = (T)(m_distance[w] + m_maxGap * m_heuristic(w));
176
177 if (!m_queue->contains(w)) {
178 m_queue->push(w, priority);
179 } else {
180 m_queue->decrease(w, priority);
181 }
182 }
183 }
184 }
185 }
186};
187
188}
Compare floating point numbers with epsilons and integral numbers with normal compare operators.
Includes declaration of graph class.
Priority queue interface wrapping various heaps.
A-Star informed search algorithm.
Definition AStarSearch.h:54
PrioritizedMapQueue< node, T > NodeQueue
Definition AStarSearch.h:56
void investigateNode(const node v)
NodeQueue * m_queue
Definition AStarSearch.h:67
NodeArray< edge > * m_predecessor
Definition AStarSearch.h:65
const EdgeArray< T > * m_cost
Definition AStarSearch.h:63
NodeArray< bool > m_folded
Definition AStarSearch.h:62
AStarSearch(const bool directed=false, const double maxGap=1, const EpsilonTest &et=EpsilonTest())
Initializes a new A* search algorithm.
Definition AStarSearch.h:78
EpsilonTest m_et
Definition AStarSearch.h:60
NodeArray< T > m_distance
Definition AStarSearch.h:66
std::function< T(node)> m_heuristic
Definition AStarSearch.h:64
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.
Definition AStarSearch.h:98
Class for adjacency list elements.
Definition Graph_d.h:79
Dynamic arrays indexed with edges.
Definition EdgeArray.h:125
void init()
Reinitializes the array. Associates the array with no graph.
Definition EdgeArray.h:292
Class for the representation of edges.
Definition Graph_d.h:300
node opposite(node v) const
Returns the adjacent node different from v.
Definition Graph_d.h:350
node target() const
Returns the target node of the edge.
Definition Graph_d.h:338
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:57
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:521
Dynamic arrays indexed with nodes.
Definition NodeArray.h:125
void init()
Reinitializes the array. Associates the array with no graph.
Definition NodeArray.h:266
Class for the representation of nodes.
Definition Graph_d.h:177
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition Graph_d.h:223
Prioritized queue interface wrapper for heaps.
bool empty() const
Checks whether the queue is empty.
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.
NodeElement * node
The type of nodes.
Definition Graph_d.h:64
EdgeElement * edge
The type of edges.
Definition Graph_d.h:68
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.