Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

AStarSearch.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
35 #include <ogdf/basic/EpsilonTest.h>
37 
38 namespace ogdf {
39 
41 
51 template<typename T>
52 class AStarSearch {
53 private:
55 
56  bool m_directed;
57  double m_maxGap;
59 
62  std::function<T(node)> m_heuristic;
66 
67 public:
68 
77  explicit AStarSearch(const bool directed = false, const double maxGap = 1, const EpsilonTest &et = EpsilonTest())
78  : m_directed(directed), m_maxGap(maxGap), m_et(et) {
79  OGDF_ASSERT(m_et.geq(maxGap, 1.0));
80  }
81 
96  T call(const Graph &graph,
97  const EdgeArray<T> &cost,
98  const node source,
99  const node target,
100  NodeArray<edge> &predecessor,
101  std::function<T(node)> heuristic = [](node) {
102 #ifdef _MSC_VER
103  return 0;
104 #else
105  return T(0);
106 #endif
107  })
108  {
109  // initialize auxiliary structures
110  m_cost = &cost;
111  m_distance.init(graph);
112  m_predecessor = &predecessor;
113  m_predecessor->init(graph);
114  m_heuristic = heuristic;
115 
116  m_folded.init(graph, false);
117  NodeQueue queue(graph);
118  m_queue = &queue;
119 
120  m_distance[source] = 0;
121  (*m_predecessor)[target] = nullptr;
122  m_queue->push(source, 0);
123 
124  // investigate each node
125  while(!m_queue->empty()) {
126  node v = queue.topElement();
127  queue.pop();
128  m_folded[v] = true;
129 
130  if(v == target) {
131  queue.clear();
132  } else {
133  investigateNode(v);
134  }
135  }
136 
137  OGDF_ASSERT((*m_predecessor)[target] == nullptr || validatePath(source, target));
138 
139  return m_distance[target];
140  }
141 
142 private:
143 
144 #ifdef OGDF_DEBUG
145  bool validatePath(const node source, const node target) {
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 
160  OGDF_ASSERT(m_et.equal(m_distance[w] + (*m_cost)[e], m_distance[v]));
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 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
void pop()
Removes the topmost element from the queue.
NodeArray< T > m_distance
Definition: AStarSearch.h:64
void investigateNode(const node v)
Definition: AStarSearch.h:169
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:113
NodeArray< edge > * m_predecessor
Definition: AStarSearch.h:63
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
void push(const E &element, const P &priority)
Adds a new element to the queue.
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
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:96
AStarSearch(const bool directed=false, const double maxGap=1, const EpsilonTest &et=EpsilonTest())
Initializes a new A* search algorithm.
Definition: AStarSearch.h:77
const EdgeArray< T > * m_cost
Definition: AStarSearch.h:61
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
void clear()
Removes all elements from this queue.
Prioritized queue interface wrapper for heaps.
Compare floating point numbers with epsilons and integral numbers with normal compare operators...
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::function< T(node)> m_heuristic
Definition: AStarSearch.h:62
const E & topElement() const
Returns the topmost element in the queue.
EpsilonTest m_et
Definition: AStarSearch.h:58
NodeQueue * m_queue
Definition: AStarSearch.h:65
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
Class for the representation of edges.
Definition: Graph_d.h:291
Class for the representation of nodes.
Definition: Graph_d.h:167
Priority queue interface wrapping various heaps.
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:335
NodeArray< bool > m_folded
Definition: AStarSearch.h:60
Includes declaration of graph class.
bool empty() const
Checks whether the queue is empty.
A-Star informed search algorithm.
Definition: AStarSearch.h:52
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:145
const Graph * graphOf() const
Returns a pointer to the associated graph.
Definition: NodeArray.h:160
bool contains(const E &element) const
Returns whether this queue contains that key.
The namespace for all OGDF objects.
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:141
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:211
Class for adjacency list elements.
Definition: Graph_d.h:78
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41