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
39#include <ogdf/basic/Module.h>
43
44namespace ogdf {
45
56template<typename TWeight>
57class SpannerModule : public Module {
58public:
61
62 // destruction
63 virtual ~SpannerModule() { }
64
89#ifdef OGDF_DEBUG
90 std::string error;
92#endif
93
94 if (isTimelimitEnabled()) {
95 m_watch.start(true);
96 }
97 ReturnType result;
98 try {
99 result = execute();
100 } catch (const TimeoutException&) {
102 }
103 if (isTimelimitEnabled()) {
104 m_watch.stop();
105 }
106 return result;
107 }
108
113 virtual bool preconditionsOk(const GraphAttributes& GA, double stretch, std::string& error) = 0;
114
121
126
127protected:
129 double m_stretch;
132
136 virtual void init(const GraphAttributes& GA, double stretch, GraphCopySimple& spanner,
138 m_GA = &GA;
142
143 const Graph& G = GA.constGraph();
144 m_spanner->clear();
146 for (node n : G.nodes) {
147 m_spanner->newNode(n);
148 }
149 m_inSpanner->init(G, false);
150 }
151
156 virtual ReturnType execute() = 0;
157
161 bool isTimelimitEnabled() { return m_timelimit != -1; }
162
167
173 if (isTimelimitEnabled() && getTimeLeft() <= 0) {
174 throw TimeoutException();
175 }
176 }
177
178private:
181
182protected:
184 };
185
186public:
193 double stretch);
194
198 static void apspSpanner(const GraphAttributes& GA, const GraphCopySimple& spanner,
200
201protected:
206};
207
208template<typename TWeight>
217
218template<typename TWeight>
220 const GraphCopySimple& spanner, double stretch) {
221 const Graph& G = GA.constGraph();
222 OGDF_ASSERT(&(spanner.original()) == &G);
223
224 if (G.numberOfNodes() != spanner.numberOfNodes()) {
225 return false;
226 }
227
230 for (edge e : G.edges) {
231 weights[e] = getWeight(GA, e);
232 }
234
236 apspSpanner(GA, spanner, newDistances);
237
238 EpsilonTest m_eps;
239 for (edge e : G.edges) {
240 node u = e->source();
241 node v = e->target();
243 TWeight newDistance = newDistances[spanner.copy(u)][spanner.copy(v)];
244 if (m_eps.greater(static_cast<double>(newDistance), stretch * originalDistance)) {
245 return false;
246 }
247 }
248
249 return true;
250}
251
252template<>
255 return GA.intWeight(e);
256 } else {
257 return 1;
258 }
259}
260
261template<>
264 return GA.doubleWeight(e);
265 } else {
266 return 1.0;
267 }
268}
269
270}
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Declaration of graph copy classes.
Declares base class for all module types.
Declaration of several shortest path algorithms.
Declaration of stopwatch classes.
Dynamic arrays indexed with edges.
Definition EdgeArray.h:125
void init()
Reinitializes the array. Associates the array with no graph.
Definition EdgeArray.h:292
Class for the representation of edges.
Definition Graph_d.h:300
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).
static const long edgeDoubleWeight
Corresponds to edge attribute doubleWeight(edge).
static const long edgeIntWeight
Corresponds to edge attribute intWeight(edge).
Copies of graphs with mapping between nodes and edges.
Definition GraphCopy.h:59
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition GraphCopy.h:173
void createEmpty(const Graph &G)
Re-initializes the copy using G, but does not create any nodes or edges.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:521
virtual void clear()
Removes all nodes and all edges from the graph.
Base class for modules.
Definition Module.h:47
ReturnType
The return type of a module.
Definition Module.h:50
@ TimeoutInfeasible
The solution is not feasible due to a timeout.
Dynamic arrays indexed with nodes.
Definition NodeArray.h:125
Class for the representation of nodes.
Definition Graph_d.h:177
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:154
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:94
Declaration of extended graph algorithms.
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:41
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.
A simple exception used to exit from the execution, if the timelimit is reached.