Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SpannerModule.h
Go to the documentation of this file.
1 
35 #pragma once
36 
38 #include <ogdf/basic/GraphCopy.h>
39 #include <ogdf/basic/Module.h>
40 #include <ogdf/basic/Stopwatch.h>
43 
44 namespace ogdf {
45 
56 template<typename TWeight>
57 class SpannerModule : public Module {
58 public:
60  SpannerModule() : m_GA(nullptr) { }
61 
62  // destruction
63  virtual ~SpannerModule() { }
64 
86  virtual ReturnType call(const GraphAttributes& GA, double stretch, GraphCopySimple& spanner,
87  EdgeArray<bool>& inSpanner) {
88  init(GA, stretch, spanner, inSpanner);
89 #ifdef OGDF_DEBUG
90  std::string error;
91  OGDF_ASSERT(preconditionsOk(GA, stretch, 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 
120  void setTimelimit(int64_t milliseconds) { m_timelimit = milliseconds; }
121 
125  int64_t getTimeNeeded() { return m_watch.milliSeconds(); }
126 
127 protected:
129  double m_stretch;
132 
136  virtual void init(const GraphAttributes& GA, double stretch, GraphCopySimple& spanner,
137  EdgeArray<bool>& inSpanner) {
138  m_GA = &GA;
139  m_stretch = stretch;
140  m_spanner = &spanner;
141  m_inSpanner = &inSpanner;
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 
166  int64_t getTimeLeft() { return m_timelimit - getTimeNeeded(); }
167 
172  void assertTimeLeft() {
173  if (isTimelimitEnabled() && getTimeLeft() <= 0) {
174  throw TimeoutException();
175  }
176  }
177 
178 private:
179  int64_t m_timelimit = -1;
181 
182 protected:
184  };
185 
186 public:
192  static bool isMultiplicativeSpanner(const GraphAttributes& GA, const GraphCopySimple& spanner,
193  double stretch);
194 
198  static void apspSpanner(const GraphAttributes& GA, const GraphCopySimple& spanner,
199  NodeArray<NodeArray<TWeight>>& shortestPathMatrix);
200 
201 protected:
205  static TWeight getWeight(const GraphAttributes& GA, edge e);
206 };
207 
208 template<typename TWeight>
210  NodeArray<NodeArray<TWeight>>& shortestPathMatrix) {
211  EdgeArray<TWeight> weights(spanner);
212  for (edge e : spanner.edges) {
213  weights[e] = getWeight(GA, spanner.original(e));
214  }
215  dijkstra_SPAP(spanner, shortestPathMatrix, weights);
216 }
217 
218 template<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 
228  NodeArray<NodeArray<TWeight>> originalDistances(G);
229  EdgeArray<TWeight> weights(G);
230  for (edge e : G.edges) {
231  weights[e] = getWeight(GA, e);
232  }
233  dijkstra_SPAP(G, originalDistances, weights);
234 
235  NodeArray<NodeArray<TWeight>> newDistances(spanner);
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();
242  TWeight originalDistance = originalDistances[u][v];
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 
252 template<>
255  return GA.intWeight(e);
256  } else {
257  return 1;
258  }
259 }
260 
261 template<>
264  return GA.doubleWeight(e);
265  } else {
266  return 1.0;
267  }
268 }
269 
270 }
ogdf::Stopwatch::milliSeconds
int64_t milliSeconds() const
Returns the currently elapsed time in milliseconds.
Definition: Stopwatch.h:94
ogdf::GraphAttributes::edgeIntWeight
static const long edgeIntWeight
Corresponds to edge attribute intWeight(edge).
Definition: GraphAttributes.h:119
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:66
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
ogdf::SpannerModule::isTimelimitEnabled
bool isTimelimitEnabled()
Definition: SpannerModule.h:161
ogdf::SpannerModule::TimeoutException
Definition: SpannerModule.h:183
ogdf::Stopwatch::start
void start(bool reset=false)
Starts the stopwatch.
ogdf::EpsilonTest
Definition: EpsilonTest.h:41
ogdf::GraphCopySimple::copy
edge copy(edge e) const override
Returns the edge in the graph copy corresponding to e.
Definition: GraphCopy.h:288
ogdf::SpannerModule::m_spanner
GraphCopySimple * m_spanner
Definition: SpannerModule.h:130
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::SpannerModule::m_timelimit
int64_t m_timelimit
Timelimit in milliseconds.
Definition: SpannerModule.h:179
ogdf::SpannerModule::isMultiplicativeSpanner
static bool isMultiplicativeSpanner(const GraphAttributes &GA, const GraphCopySimple &spanner, double stretch)
Validates a spanner.
Definition: SpannerModule.h:219
extended_graph_alg.h
Declaration of extended graph algorithms.
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:136
ogdf::SpannerModule::m_watch
StopwatchCPU m_watch
Used for keeping track of time.
Definition: SpannerModule.h:180
ogdf::SpannerModule::assertTimeLeft
void assertTimeLeft()
Assert, that time is left.
Definition: SpannerModule.h:172
ogdf::GraphCopySimple
Copies of graphs with mapping between nodes and edges.
Definition: GraphCopy.h:254
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:154
ogdf::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:217
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:62
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:120
ogdf::SpannerModule::SpannerModule
SpannerModule()
Initializes a spanner module.
Definition: SpannerModule.h:60
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:974
ogdf::SpannerModule::~SpannerModule
virtual ~SpannerModule()
Definition: SpannerModule.h:63
ogdf::SpannerModule::getTimeLeft
int64_t getTimeLeft()
Definition: SpannerModule.h:166
ogdf::Module
Base class for modules.
Definition: Module.h:47
ogdf::SpannerModule::getTimeNeeded
int64_t getTimeNeeded()
Definition: SpannerModule.h:125
Stopwatch.h
Declaration of stopwatch classes.
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:391
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:185
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:129
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::SpannerModule
Interface for spanner algorithms.
Definition: SpannerModule.h:57
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:161
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:927
ogdf::Stopwatch::stop
void stop()
Stops the stopwatch and adds the difference between the current time and the starting time to the tot...
ShortestPathAlgorithms.h
Declaration of several shortest path algorithms.
ogdf::SpannerModule::m_GA
const GraphAttributes * m_GA
Definition: SpannerModule.h:128
ogdf::SpannerModule::call
virtual ReturnType call(const GraphAttributes &GA, double stretch, GraphCopySimple &spanner, EdgeArray< bool > &inSpanner)
Executes the algorithm.
Definition: SpannerModule.h:86
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::SpannerModule::m_inSpanner
EdgeArray< bool > * m_inSpanner
Definition: SpannerModule.h:131
ogdf::GraphAttributes::intWeight
int intWeight(edge e) const
Returns the (integer) weight of edge e.
Definition: GraphAttributes.h:768
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:122
ogdf::GraphAttributes::doubleWeight
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
Definition: GraphAttributes.h:786
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:394
ogdf::Module::ReturnType
ReturnType
The return type of a module.
Definition: Module.h:50
ogdf::SpannerModule::execute
virtual ReturnType execute()=0
Executes the core algorithm.
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
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:209
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:194
ogdf::GraphCopyBase::original
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:98
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709
ogdf::SpannerModule::getWeight
static TWeight getWeight(const GraphAttributes &GA, edge e)