Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
SpannerModule.h
Go to the documentation of this file.
1
35#pragma once
36
38#include <ogdf/basic/Graph.h>
42#include <ogdf/basic/Module.h>
44#include <ogdf/basic/basic.h>
46
47#include <cstdint>
48#include <string>
49
50namespace ogdf {
51
62template<typename TWeight>
63class SpannerModule : public Module {
64public:
66 SpannerModule() : m_GA(nullptr) { }
67
68 // destruction
69 virtual ~SpannerModule() { }
70
92 virtual ReturnType call(const GraphAttributes& GA, double stretch, GraphCopySimple& spanner,
93 EdgeArray<bool>& inSpanner) {
94 init(GA, stretch, spanner, inSpanner);
95#ifdef OGDF_DEBUG
96 std::string error;
97 OGDF_ASSERT(preconditionsOk(GA, stretch, error));
98#endif
99
100 if (isTimelimitEnabled()) {
101 m_watch.start(true);
102 }
103 ReturnType result;
104 try {
105 result = execute();
106 } catch (const TimeoutException&) {
108 }
109 if (isTimelimitEnabled()) {
110 m_watch.stop();
111 }
112 return result;
113 }
114
119 virtual bool preconditionsOk(const GraphAttributes& GA, double stretch, std::string& error) = 0;
120
126 void setTimelimit(int64_t milliseconds) { m_timelimit = milliseconds; }
127
131 int64_t getTimeNeeded() { return m_watch.milliSeconds(); }
132
133protected:
135 double m_stretch;
138
142 virtual void init(const GraphAttributes& GA, double stretch, GraphCopySimple& spanner,
143 EdgeArray<bool>& inSpanner) {
144 m_GA = &GA;
145 m_stretch = stretch;
146 m_spanner = &spanner;
147 m_inSpanner = &inSpanner;
148
149 const Graph& G = GA.constGraph();
150 m_spanner->clear();
152 for (node n : G.nodes) {
153 m_spanner->newNode(n);
154 }
155 m_inSpanner->init(G, false);
156 }
157
162 virtual ReturnType execute() = 0;
163
167 bool isTimelimitEnabled() { return m_timelimit != -1; }
168
172 int64_t getTimeLeft() { return m_timelimit - getTimeNeeded(); }
173
179 if (isTimelimitEnabled() && getTimeLeft() <= 0) {
180 throw TimeoutException();
181 }
182 }
183
184private:
185 int64_t m_timelimit = -1;
187
188protected:
190 };
191
192public:
198 static bool isMultiplicativeSpanner(const GraphAttributes& GA, const GraphCopySimple& spanner,
199 double stretch);
200
204 static void apspSpanner(const GraphAttributes& GA, const GraphCopySimple& spanner,
205 NodeArray<NodeArray<TWeight>>& shortestPathMatrix);
206
207protected:
211 static TWeight getWeight(const GraphAttributes& GA, edge e);
212};
213
214template<typename TWeight>
216 NodeArray<NodeArray<TWeight>>& shortestPathMatrix) {
217 EdgeArray<TWeight> weights(spanner);
218 for (edge e : spanner.edges) {
219 weights[e] = getWeight(GA, spanner.original(e));
220 }
221 dijkstra_SPAP(spanner, shortestPathMatrix, weights);
222}
223
224template<typename TWeight>
226 const GraphCopySimple& spanner, double stretch) {
227 const Graph& G = GA.constGraph();
228 OGDF_ASSERT(&(spanner.original()) == &G);
229
230 if (G.numberOfNodes() != spanner.numberOfNodes()) {
231 return false;
232 }
233
234 NodeArray<NodeArray<TWeight>> originalDistances(G);
235 EdgeArray<TWeight> weights(G);
236 for (edge e : G.edges) {
237 weights[e] = getWeight(GA, e);
238 }
239 dijkstra_SPAP(G, originalDistances, weights);
240
241 NodeArray<NodeArray<TWeight>> newDistances(spanner);
242 apspSpanner(GA, spanner, newDistances);
243
244 EpsilonTest m_eps;
245 for (edge e : G.edges) {
246 node u = e->source();
247 node v = e->target();
248 TWeight originalDistance = originalDistances[u][v];
249 TWeight newDistance = newDistances[spanner.copy(u)][spanner.copy(v)];
250 if (m_eps.greater(static_cast<double>(newDistance), stretch * originalDistance)) {
251 return false;
252 }
253 }
254
255 return true;
256}
257
258template<>
261 return GA.intWeight(e);
262 } else {
263 return 1;
264 }
265}
266
267template<>
270 return GA.doubleWeight(e);
271 } else {
272 return 1.0;
273 }
274}
275
276}
Compare floating point numbers with epsilons and integral numbers with normal compare operators.
Includes declaration of graph class.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Declaration of graph copy classes.
Decralation of GraphElement and GraphList classes.
Declares base class for all module types.
Declaration of several shortest path algorithms.
Declaration of stopwatch classes.
Basic declarations, included by all source files.
Class for the representation of edges.
Definition Graph_d.h:364
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.
Stores additional attributes of a graph (like layout information).
int intWeight(edge e) const
Returns the (integer) weight of edge e.
const Graph & constGraph() const
Returns a reference to the associated graph.
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
static const long edgeDoubleWeight
Corresponds to edge attribute doubleWeight(edge).
bool has(long attr) const
Returns true iff all attributes in attr are available.
static const long edgeIntWeight
Corresponds to edge attribute intWeight(edge).
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition GraphCopy.h:191
const Graph & original() const
Returns a reference to the original graph.
Definition GraphCopy.h:104
Copies of graphs with mapping between nodes and edges.
Definition GraphCopy.h:260
void setOriginalGraph(const Graph *G) override
Re-initializes the copy using G (which might be null), but does not create any nodes or edges.
void clear() override
Removes all nodes and edges from this copy but does not break the link with the original graph.
edge copy(edge e) const override
Returns the edge in the graph copy corresponding to e.
Definition GraphCopy.h:294
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition Graph_d.h:979
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition Graph_d.h:932
Base class for modules.
Definition Module.h:49
ReturnType
The return type of a module.
Definition Module.h:52
@ TimeoutInfeasible
The solution is not feasible due to a timeout.
Class for the representation of nodes.
Definition Graph_d.h:241
Interface for spanner algorithms.
static void apspSpanner(const GraphAttributes &GA, const GraphCopySimple &spanner, NodeArray< NodeArray< TWeight > > &shortestPathMatrix)
Calculates an all-pair shortest-path on spanner with the weights given by GA.
virtual ReturnType execute()=0
Executes the core algorithm.
StopwatchCPU m_watch
Used for keeping track of time.
void assertTimeLeft()
Assert, that time is left.
virtual ReturnType call(const GraphAttributes &GA, double stretch, GraphCopySimple &spanner, EdgeArray< bool > &inSpanner)
Executes the algorithm.
SpannerModule()
Initializes a spanner module.
int64_t m_timelimit
Timelimit in milliseconds.
virtual void init(const GraphAttributes &GA, double stretch, GraphCopySimple &spanner, EdgeArray< bool > &inSpanner)
Initializes members and create an empty spanner.
const GraphAttributes * m_GA
EdgeArray< bool > * m_inSpanner
static bool isMultiplicativeSpanner(const GraphAttributes &GA, const GraphCopySimple &spanner, double stretch)
Validates a spanner.
void setTimelimit(int64_t milliseconds)
Sets the timelimit for the algorithm in milliseconds.
virtual bool preconditionsOk(const GraphAttributes &GA, double stretch, std::string &error)=0
static TWeight getWeight(const GraphAttributes &GA, edge e)
GraphCopySimple * m_spanner
Implements a stopwatch measuring CPU time.
Definition Stopwatch.h:157
void start(bool reset=false)
Starts the stopwatch.
void stop()
Stops the stopwatch and adds the difference between the current time and the starting time to the tot...
int64_t milliSeconds() const
Returns the currently elapsed time in milliseconds.
Definition Stopwatch.h:97
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
double dijkstra_SPAP(const GraphAttributes &GA, NodeArray< NodeArray< TCost > > &shortestPathMatrix)
Computes all-pairs shortest paths in GA using Dijkstra's algorithm.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
TWeight getWeight(edge e, const EdgeArray< TWeight > &weights)
Helper function to get the edge weight of e from the EdgeArray weights.
Definition utils.h:65
The namespace for all OGDF objects.
A simple exception used to exit from the execution, if the timelimit is reached.