Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Dijkstra.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 
39 namespace ogdf {
40 
51 template<typename T, template<typename P, class C> class H = PairingHeap>
52 class Dijkstra {
53 protected:
55 
56 public:
61  void call(const Graph &G,
62  const EdgeArray<T> &weight,
63  const List<node> &sources,
64  NodeArray<edge> &predecessor,
65  NodeArray<T> &distance,
66  bool directed = false)
67  {
69  distance.init(G, numeric_limits<T>::max());
70  predecessor.init(G, nullptr);
71 
72  // initialization
73  for (node v : G.nodes) {
74  queue.push(v, distance[v]);
75  }
76  for (node s : sources) {
77  queue.decrease(s, (distance[s] = 0));
78  }
79 
80 #ifdef OGDF_DEBUG
81  for (edge de : G.edges) {
82  OGDF_ASSERT(weight[de] >= 0);
83  }
84 #endif
85 
86  while (!queue.empty()) {
87  node v = queue.topElement();
88  queue.pop();
89  if (!predecessor[v]
90  && m_eps.greater(distance[v], static_cast<T>(0))) { // v is unreachable, ignore
91  continue;
92  }
93  for(adjEntry adj : v->adjEntries) {
94  edge e = adj->theEdge();
95  node w = adj->twinNode();
96  if (directed && e->target() == v) { // edge is in wrong direction
97  continue;
98  }
99  if (m_eps.greater(distance[w], distance[v] + weight[e])) {
100  OGDF_ASSERT(numeric_limits<T>::max() - weight[e] >= distance[v]);
101  queue.decrease(w, (distance[w] = distance[v] + weight[e]));
102  predecessor[w] = e;
103  }
104  }
105  }
106  }
107 
112  void call(const Graph &G,
113  const EdgeArray<T> &weight,
114  node s,
115  NodeArray<edge> &predecessor,
116  NodeArray<T> &distance,
117  bool directed = false)
118  {
119  List<node> sources;
120  sources.pushBack(s);
121  call(G, weight, sources, predecessor, distance, directed);
122  }
123 };
124 
125 } // end namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:95
Dijkstra&#39;s single source shortest path algorithm.
Definition: Dijkstra.h:52
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
void call(const Graph &G, const EdgeArray< T > &weight, const List< node > &sources, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false)
Calculates, based on the graph G with corresponding edge costs and source nodes, the shortest paths a...
Definition: Dijkstra.h:61
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
Prioritized queue interface wrapper for heaps.
Compare floating point numbers with epsilons and integral numbers with normal compare operators...
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:548
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
const E & topElement() const
Returns the topmost element in the queue.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:105
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.
std::enable_if< std::is_integral< T >::value, bool >::type greater(const T &x, const T &y) const
Compare if x is GREATER than y for integral types.
Definition: EpsilonTest.h:169
void call(const Graph &G, const EdgeArray< T > &weight, node s, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false)
Calculates, based on the graph G with corresponding edge costs and a source node s, the shortest paths and distances to all other nodes by Dijkstra&#39;s algorithm.
Definition: Dijkstra.h:112
Includes declaration of graph class.
bool empty() const
Checks whether the queue is empty.
EpsilonTest m_eps
For floating point comparisons (if floating point is used)
Definition: Dijkstra.h:54
The namespace for all OGDF objects.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
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