Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SpannerIteratedWrapper.h
Go to the documentation of this file.
1 
31 #pragma once
32 
33 #include <ogdf/basic/Graph.h>
34 #include <ogdf/basic/GraphCopy.h>
35 #include <ogdf/basic/GraphList.h>
37 
38 #include <algorithm>
39 #include <cstdint>
40 #include <limits>
41 #include <memory>
42 #include <string>
43 
44 namespace ogdf {
45 class GraphAttributes;
46 
65 template<typename TWeight>
66 class SpannerIteratedWrapper : public SpannerModule<TWeight> {
67 public:
75  SpannerIteratedWrapper(SpannerModule<TWeight>* module, int maxIterations)
76  : m_module(module), m_maxIterations(maxIterations) { }
77 
79  virtual bool preconditionsOk(const GraphAttributes& GA, double stretch,
80  std::string& error) override {
81  return m_module->preconditionsOk(GA, stretch, error);
82  }
83 
88 
89 private:
90  std::unique_ptr<SpannerModule<TWeight>> m_module;
91  const int m_maxIterations;
93 
95  virtual typename SpannerModule<TWeight>::ReturnType execute() override {
96  const Graph& G = m_GA->constGraph();
97  if (isTimelimitEnabled()) {
98  int64_t timeLeft = max(getTimeLeft(), static_cast<int64_t>(0));
99  m_module->setTimelimit(timeLeft);
100  }
101 
102  int bestSize = std::numeric_limits<int>::max();
103 
105  // No assertTimeLeft here: If there is an timeout, check, if we had a previous solution
106  if (isTimelimitEnabled() && getTimeLeft() <= 0) {
107  if (bestSize == std::numeric_limits<int>::max()) {
109  } else {
111  }
112  }
113 
114  GraphCopySimple spanner(G);
115  EdgeArray<bool> inSpanner(G);
116 
118  m_module->call(*m_GA, m_stretch, spanner, inSpanner);
120  continue;
121  }
123  // do we found at least one solution?
124  if (bestSize == std::numeric_limits<int>::max()) {
126  } else {
128  }
129  }
131  return r; // return errors directly
132  }
133 
134  if (spanner.numberOfEdges() < bestSize) {
135  // found a new best solution
136  bestSize = spanner.numberOfEdges();
137 
138  // Copy solution to the members
139  m_spanner->clear();
141  for (node n : G.nodes) {
142  m_spanner->newNode(n);
143  }
144  m_inSpanner->init(G, false);
145 
146  for (edge e : spanner.edges) {
147  edge eOrig = spanner.original(e);
148  (*m_inSpanner)[eOrig] = true;
149  m_spanner->newEdge(eOrig);
150  }
151  }
152  }
153  m_iterations--; // remove the overflow iteration, which is not executed
154 
155  if (bestSize == std::numeric_limits<int>::max()) {
157  } else {
159  }
160  }
161 
168 };
169 
170 }
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
ogdf::SpannerModule::isTimelimitEnabled
bool isTimelimitEnabled()
Definition: SpannerModule.h:167
ogdf::SpannerIteratedWrapper
A implementation-independed wrapper class to execute a spanner algorithm multiple times.
Definition: SpannerIteratedWrapper.h:66
Graph.h
Includes declaration of graph class.
ogdf::SpannerModule::m_spanner
GraphCopySimple * m_spanner
Definition: SpannerModule.h:136
ogdf::SpannerIteratedWrapper::execute
virtual SpannerModule< TWeight >::ReturnType execute() override
Executes the core algorithm.
Definition: SpannerIteratedWrapper.h:95
ogdf::SpannerIteratedWrapper::m_iterations
int m_iterations
Definition: SpannerIteratedWrapper.h:92
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::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:223
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:79
ogdf::SpannerIteratedWrapper::m_maxIterations
const int m_maxIterations
Definition: SpannerIteratedWrapper.h:91
r
int r[]
Definition: hierarchical-ranking.cpp:13
ogdf::Graph::numberOfEdges
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:985
ogdf::SpannerModule::getTimeLeft
int64_t getTimeLeft()
Definition: SpannerModule.h:172
ogdf::SpannerIteratedWrapper::getExecutedIterations
int getExecutedIterations()
Definition: SpannerIteratedWrapper.h:87
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::SpannerIteratedWrapper::m_module
std::unique_ptr< SpannerModule< TWeight > > m_module
Definition: SpannerIteratedWrapper.h:90
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::SpannerModule::m_stretch
double m_stretch
Definition: SpannerModule.h:135
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::SpannerIteratedWrapper::SpannerIteratedWrapper
SpannerIteratedWrapper(SpannerModule< TWeight > *module, int maxIterations)
Initializes the wrapper.
Definition: SpannerIteratedWrapper.h:75
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::SpannerModule::m_GA
const GraphAttributes * m_GA
Definition: SpannerModule.h:134
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::Module::ReturnType
ReturnType
The return type of a module.
Definition: Module.h:52
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::GraphCopySimple::newEdge
edge newEdge(edge eOrig)
Creates a new edge in the graph copy with original edge eOrig.
Definition: GraphCopy.h:321
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