Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SpannerModule.h
Go to the documentation of this file.
1 
35 #pragma once
36 
37 #include <ogdf/basic/EpsilonTest.h>
38 #include <ogdf/basic/Graph.h>
40 #include <ogdf/basic/GraphCopy.h>
41 #include <ogdf/basic/GraphList.h>
42 #include <ogdf/basic/Module.h>
43 #include <ogdf/basic/Stopwatch.h>
44 #include <ogdf/basic/basic.h>
46 
47 #include <cstdint>
48 #include <string>
49 
50 namespace ogdf {
51 
62 template<typename TWeight>
63 class SpannerModule : public Module {
64 public:
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 
133 protected:
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 
178  void assertTimeLeft() {
179  if (isTimelimitEnabled() && getTimeLeft() <= 0) {
180  throw TimeoutException();
181  }
182  }
183 
184 private:
185  int64_t m_timelimit = -1;
187 
188 protected:
190  };
191 
192 public:
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 
207 protected:
211  static TWeight getWeight(const GraphAttributes& GA, edge e);
212 };
213 
214 template<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 
224 template<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 
258 template<>
261  return GA.intWeight(e);
262  } else {
263  return 1;
264  }
265 }
266 
267 template<>
270  return GA.doubleWeight(e);
271  } else {
272  return 1.0;
273  }
274 }
275 
276 }
ogdf::Stopwatch::milliSeconds
int64_t milliSeconds() const
Returns the currently elapsed time in milliseconds.
Definition: Stopwatch.h:97
ogdf::GraphAttributes::edgeIntWeight
static const long edgeIntWeight
Corresponds to edge attribute intWeight(edge).
Definition: GraphAttributes.h:125
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:72
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
ogdf::SpannerModule::isTimelimitEnabled
bool isTimelimitEnabled()
Definition: SpannerModule.h:167
ogdf::SpannerModule::TimeoutException
Definition: SpannerModule.h:189
EpsilonTest.h
Compare floating point numbers with epsilons and integral numbers with normal compare operators.
Graph.h
Includes declaration of graph class.
ogdf::Stopwatch::start
void start(bool reset=false)
Starts the stopwatch.
ogdf::EpsilonTest
Definition: EpsilonTest.h:39
ogdf::GraphCopySimple::copy
edge copy(edge e) const override
Returns the edge in the graph copy corresponding to e.
Definition: GraphCopy.h:295
ogdf::SpannerModule::m_spanner
GraphCopySimple * m_spanner
Definition: SpannerModule.h:136
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::SpannerModule::m_timelimit
int64_t m_timelimit
Timelimit in milliseconds.
Definition: SpannerModule.h:185
ogdf::SpannerModule::isMultiplicativeSpanner
static bool isMultiplicativeSpanner(const GraphAttributes &GA, const GraphCopySimple &spanner, double stretch)
Validates a spanner.
Definition: SpannerModule.h:225
ogdf::SpannerModule::init
virtual void init(const GraphAttributes &GA, double stretch, GraphCopySimple &spanner, EdgeArray< bool > &inSpanner)
Initializes members and create an empty spanner.
Definition: SpannerModule.h:142
ogdf::SpannerModule::m_watch
StopwatchCPU m_watch
Used for keeping track of time.
Definition: SpannerModule.h:186
ogdf::SpannerModule::assertTimeLeft
void assertTimeLeft()
Assert, that time is left.
Definition: SpannerModule.h:178
ogdf::GraphCopySimple
Copies of graphs with mapping between nodes and edges.
Definition: GraphCopy.h:261
ogdf::GraphCopySimple::setOriginalGraph
void setOriginalGraph(const Graph *G) override
Re-initializes the copy using G (which might be null), but does not create any nodes or edges.
ogdf::StopwatchCPU
Implements a stopwatch measuring CPU time.
Definition: Stopwatch.h:157
ogdf::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:223
ogdf::matching_blossom::getWeight
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
ogdf::Module::ReturnType::TimeoutInfeasible
@ TimeoutInfeasible
The solution is not feasible due to a timeout.
ogdf::SpannerModule::setTimelimit
void setTimelimit(int64_t milliseconds)
Sets the timelimit for the algorithm in milliseconds.
Definition: SpannerModule.h:126
ogdf::SpannerModule::SpannerModule
SpannerModule()
Initializes a spanner module.
Definition: SpannerModule.h:66
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:982
ogdf::SpannerModule::~SpannerModule
virtual ~SpannerModule()
Definition: SpannerModule.h:69
ogdf::SpannerModule::getTimeLeft
int64_t getTimeLeft()
Definition: SpannerModule.h:172
ogdf::Module
Base class for modules.
Definition: Module.h:49
ogdf::SpannerModule::getTimeNeeded
int64_t getTimeNeeded()
Definition: SpannerModule.h:131
GraphList.h
Decralation of GraphElement and GraphList classes.
Stopwatch.h
Declaration of stopwatch classes.
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:192
GraphCopy.h
Declaration of graph copy classes.
ogdf::dijkstra_SPAP
double dijkstra_SPAP(const GraphAttributes &GA, NodeArray< NodeArray< TCost >> &shortestPathMatrix)
Computes all-pairs shortest paths in GA using Dijkstra's algorithm.
Definition: ShortestPathAlgorithms.h:93
ogdf::SpannerModule::m_stretch
double m_stretch
Definition: SpannerModule.h:135
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::SpannerModule
Interface for spanner algorithms.
Definition: SpannerModule.h:63
ogdf::EpsilonTest::greater
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:159
ogdf::GraphCopySimple::clear
void clear() override
Removes all nodes and edges from this copy but does not break the link with the original graph.
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:935
ogdf::Stopwatch::stop
void stop()
Stops the stopwatch and adds the difference between the current time and the starting time to the tot...
basic.h
Basic declarations, included by all source files.
ShortestPathAlgorithms.h
Declaration of several shortest path algorithms.
ogdf::SpannerModule::m_GA
const GraphAttributes * m_GA
Definition: SpannerModule.h:134
ogdf::SpannerModule::call
virtual ReturnType call(const GraphAttributes &GA, double stretch, GraphCopySimple &spanner, EdgeArray< bool > &inSpanner)
Executes the algorithm.
Definition: SpannerModule.h:92
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::SpannerModule::m_inSpanner
EdgeArray< bool > * m_inSpanner
Definition: SpannerModule.h:137
ogdf::GraphAttributes::intWeight
int intWeight(edge e) const
Returns the (integer) weight of edge e.
Definition: GraphAttributes.h:774
Module.h
Declares base class for all module types.
ogdf::GraphAttributes::edgeDoubleWeight
static const long edgeDoubleWeight
Corresponds to edge attribute doubleWeight(edge).
Definition: GraphAttributes.h:128
ogdf::GraphAttributes::doubleWeight
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
Definition: GraphAttributes.h:792
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
ogdf::Module::ReturnType
ReturnType
The return type of a module.
Definition: Module.h:52
ogdf::SpannerModule::execute
virtual ReturnType execute()=0
Executes the core algorithm.
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::SpannerModule::apspSpanner
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.
Definition: SpannerModule.h:215
ogdf::SpannerModule::preconditionsOk
virtual bool preconditionsOk(const GraphAttributes &GA, double stretch, std::string &error)=0
ogdf::GraphAttributes::has
bool has(long attr) const
Returns true iff all attributes in attr are available.
Definition: GraphAttributes.h:200
ogdf::GraphCopyBase::original
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:105
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::SpannerModule::getWeight
static TWeight getWeight(const GraphAttributes &GA, edge e)