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 
38 namespace ogdf {
39 
41 
53 template<typename T>
54 class AStarSearch {
55 private:
57 
58  bool m_directed;
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 
69 public:
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) {
81  OGDF_ASSERT(m_et.geq(maxGap, 1.0));
82  }
83 
98  T call(
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);
112  m_heuristic = heuristic;
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 {
131  investigateNode(v);
132  }
133  }
134 
135  OGDF_ASSERT((*m_predecessor)[target] == nullptr || validatePath(source, target));
136 
137  return m_distance[target];
138  }
139 
140 private:
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]))) {
173  m_distance[w] = distanceW;
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 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
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:41
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::AStarSearch::m_folded
NodeArray< bool > m_folded
Definition: AStarSearch.h:62
ogdf::AStarSearch::NodeQueue
PrioritizedMapQueue< node, T > NodeQueue
Definition: AStarSearch.h:56
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:331
ogdf::AStarSearch::m_maxGap
double m_maxGap
Definition: AStarSearch.h:59
ogdf::AStarSearch::m_directed
bool m_directed
Definition: AStarSearch.h:58
ogdf::pq_internal::PrioritizedArrayQueueBase::contains
bool contains(const E &element) const
Returns whether this queue contains that key.
Definition: PriorityQueue.h:316
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
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:57
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:209
ogdf::AStarSearch::m_distance
NodeArray< T > m_distance
Definition: AStarSearch.h:66
ogdf::AdjElement::succ
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:211
ogdf::PrioritizedMapQueue
Prioritized queue interface wrapper for heaps.
Definition: PriorityQueue.h:409
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::AStarSearch::m_cost
const EdgeArray< T > * m_cost
Definition: AStarSearch.h:63
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:78
ogdf::AStarSearch::investigateNode
void investigateNode(const node v)
Definition: AStarSearch.h:166
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:109
ogdf::AStarSearch::m_predecessor
NodeArray< edge > * m_predecessor
Definition: AStarSearch.h:65
ogdf::AStarSearch
A-Star informed search algorithm.
Definition: AStarSearch.h:54
ogdf::AStarSearch::m_heuristic
std::function< T(node)> m_heuristic
Definition: AStarSearch.h:64
ogdf::pq_internal::PrioritizedArrayQueueBase::decrease
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
Definition: PriorityQueue.h:349
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:98
ogdf::NodeElement::firstAdj
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:279
ogdf::EdgeElement::opposite
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:406
ogdf::AStarSearch::validatePath
bool validatePath(const node source, const node target) const
Definition: AStarSearch.h:142
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::AStarSearch::m_et
EpsilonTest m_et
Definition: AStarSearch.h:60
ogdf::AStarSearch::m_queue
NodeQueue * m_queue
Definition: AStarSearch.h:67
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:135
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:394
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709