Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SpannerIteratedWrapper.h
Go to the documentation of this file.
1 
31 #pragma once
32 
34 
35 #include <memory>
36 
37 namespace ogdf {
38 
57 template<typename TWeight>
58 class SpannerIteratedWrapper : public SpannerModule<TWeight> {
59 public:
67  SpannerIteratedWrapper(SpannerModule<TWeight>* module, int maxIterations)
68  : m_module(module), m_maxIterations(maxIterations) { }
69 
71  virtual bool preconditionsOk(const GraphAttributes& GA, double stretch,
72  std::string& error) override {
73  return m_module->preconditionsOk(GA, stretch, error);
74  }
75 
80 
81 private:
82  std::unique_ptr<SpannerModule<TWeight>> m_module;
83  const int m_maxIterations;
85 
87  virtual typename SpannerModule<TWeight>::ReturnType execute() override {
88  const Graph& G = m_GA->constGraph();
89  if (isTimelimitEnabled()) {
90  int64_t timeLeft = max(getTimeLeft(), static_cast<int64_t>(0));
91  m_module->setTimelimit(timeLeft);
92  }
93 
94  int bestSize = std::numeric_limits<int>::max();
95 
97  // No assertTimeLeft here: If there is an timeout, check, if we had a previous solution
98  if (isTimelimitEnabled() && getTimeLeft() <= 0) {
99  if (bestSize == std::numeric_limits<int>::max()) {
101  } else {
103  }
104  }
105 
106  GraphCopySimple spanner(G);
107  EdgeArray<bool> inSpanner(G);
108 
110  m_module->call(*m_GA, m_stretch, spanner, inSpanner);
112  continue;
113  }
115  // do we found at least one solution?
116  if (bestSize == std::numeric_limits<int>::max()) {
118  } else {
120  }
121  }
123  return r; // return errors directly
124  }
125 
126  if (spanner.numberOfEdges() < bestSize) {
127  // found a new best solution
128  bestSize = spanner.numberOfEdges();
129 
130  // Copy solution to the members
131  m_spanner->clear();
133  for (node n : G.nodes) {
134  m_spanner->newNode(n);
135  }
136  m_inSpanner->init(G, false);
137 
138  for (edge e : spanner.edges) {
139  edge eOrig = spanner.original(e);
140  (*m_inSpanner)[eOrig] = true;
141  m_spanner->newEdge(eOrig);
142  }
143  }
144  }
145  m_iterations--; // remove the overflow iteration, which is not executed
146 
147  if (bestSize == std::numeric_limits<int>::max()) {
149  } else {
151  }
152  }
153 
160 };
161 
162 }
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
ogdf::SpannerModule::isTimelimitEnabled
bool isTimelimitEnabled()
Definition: SpannerModule.h:161
ogdf::SpannerIteratedWrapper
A implementation-independed wrapper class to execute a spanner algorithm multiple times.
Definition: SpannerIteratedWrapper.h:58
ogdf::SpannerModule::m_spanner
GraphCopySimple * m_spanner
Definition: SpannerModule.h:130
ogdf::SpannerIteratedWrapper::execute
virtual SpannerModule< TWeight >::ReturnType execute() override
Executes the core algorithm.
Definition: SpannerIteratedWrapper.h:87
ogdf::SpannerIteratedWrapper::m_iterations
int m_iterations
Definition: SpannerIteratedWrapper.h:84
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::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:217
SpannerModule.h
Basic module for spanner algorithms.
ogdf::SpannerIteratedWrapper::preconditionsOk
virtual bool preconditionsOk(const GraphAttributes &GA, double stretch, std::string &error) override
Definition: SpannerIteratedWrapper.h:71
ogdf::SpannerIteratedWrapper::m_maxIterations
const int m_maxIterations
Definition: SpannerIteratedWrapper.h:83
r
int r[]
Definition: hierarchical-ranking.cpp:8
ogdf::Graph::numberOfEdges
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:977
ogdf::SpannerModule::getTimeLeft
int64_t getTimeLeft()
Definition: SpannerModule.h:166
ogdf::SpannerIteratedWrapper::getExecutedIterations
int getExecutedIterations()
Definition: SpannerIteratedWrapper.h:79
ogdf::SpannerIteratedWrapper::m_module
std::unique_ptr< SpannerModule< TWeight > > m_module
Definition: SpannerIteratedWrapper.h:82
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:185
ogdf::SpannerModule::m_stretch
double m_stretch
Definition: SpannerModule.h:129
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::SpannerIteratedWrapper::SpannerIteratedWrapper
SpannerIteratedWrapper(SpannerModule< TWeight > *module, int maxIterations)
Initializes the wrapper.
Definition: SpannerIteratedWrapper.h:67
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::SpannerModule::m_GA
const GraphAttributes * m_GA
Definition: SpannerModule.h:128
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::Module::ReturnType
ReturnType
The return type of a module.
Definition: Module.h:50
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::GraphCopySimple::newEdge
edge newEdge(edge eOrig)
Creates a new edge in the graph copy with original edge eOrig.
Definition: GraphCopy.h:314
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