Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

AStarSearch.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/EpsilonTest.h>
35 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/basic.h>
38 
39 #include <functional>
40 
41 namespace ogdf {
42 
44 
56 template<typename T>
57 class AStarSearch {
58 private:
60 
61  bool m_directed;
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 
72 public:
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 
101  T call(
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 {
134  investigateNode(v);
135  }
136  }
137 
138  OGDF_ASSERT((*m_predecessor)[target] == nullptr || validatePath(source, target));
139 
140  return m_distance[target];
141  }
142 
143 private:
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 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
EpsilonTest.h
Compare floating point numbers with epsilons and integral numbers with normal compare operators.
Graph.h
Includes declaration of graph class.
ogdf::EpsilonTest
Definition: EpsilonTest.h:39
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::AStarSearch::m_folded
NodeArray< bool > m_folded
Definition: AStarSearch.h:65
ogdf::AStarSearch::NodeQueue
PrioritizedMapQueue< node, T > NodeQueue
Definition: AStarSearch.h:59
PriorityQueue.h
Priority queue interface wrapping various heaps.
ogdf::pq_internal::PrioritizedArrayQueueBase::push
void push(const E &element, const P &priority)
Adds a new element to the queue.
Definition: PriorityQueue.h:333
ogdf::AStarSearch::m_maxGap
double m_maxGap
Definition: AStarSearch.h:62
ogdf::AStarSearch::m_directed
bool m_directed
Definition: AStarSearch.h:61
ogdf::pq_internal::PrioritizedArrayQueueBase::contains
bool contains(const E &element) const
Returns whether this queue contains that key.
Definition: PriorityQueue.h:318
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::EpsilonTest::less
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
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:211
ogdf::AStarSearch::m_distance
NodeArray< T > m_distance
Definition: AStarSearch.h:69
ogdf::AdjElement::succ
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:218
ogdf::PrioritizedMapQueue
Prioritized queue interface wrapper for heaps.
Definition: PriorityQueue.h:411
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::AStarSearch::m_cost
const EdgeArray< T > * m_cost
Definition: AStarSearch.h:66
ogdf::AStarSearch::AStarSearch
AStarSearch(const bool directed=false, const double maxGap=1, const EpsilonTest &et=EpsilonTest())
Initializes a new A* search algorithm.
Definition: AStarSearch.h:81
ogdf::AStarSearch::investigateNode
void investigateNode(const node v)
Definition: AStarSearch.h:169
ogdf::EpsilonTest::equal
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.
Definition: EpsilonTest.h:107
ogdf::AStarSearch::m_predecessor
NodeArray< edge > * m_predecessor
Definition: AStarSearch.h:68
ogdf::AStarSearch
A-Star informed search algorithm.
Definition: AStarSearch.h:57
ogdf::AStarSearch::m_heuristic
std::function< T(node)> m_heuristic
Definition: AStarSearch.h:67
ogdf::pq_internal::PrioritizedArrayQueueBase::decrease
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
Definition: PriorityQueue.h:351
ogdf::AStarSearch::call
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:101
basic.h
Basic declarations, included by all source files.
ogdf::NodeElement::firstAdj
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:286
ogdf::EdgeElement::opposite
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:413
ogdf::AStarSearch::validatePath
bool validatePath(const node source, const node target) const
Definition: AStarSearch.h:145
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::AStarSearch::m_et
EpsilonTest m_et
Definition: AStarSearch.h:63
ogdf::AStarSearch::m_queue
NodeQueue * m_queue
Definition: AStarSearch.h:70
ogdf::EpsilonTest::geq
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.
Definition: EpsilonTest.h:133
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716