Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
SpannerIteratedWrapper.h
Go to the documentation of this file.
1
31#pragma once
32
33#include <ogdf/basic/Graph.h>
37
38#include <algorithm>
39#include <cstdint>
40#include <limits>
41#include <memory>
42#include <string>
43
44namespace ogdf {
45class GraphAttributes;
46
65template<typename TWeight>
66class SpannerIteratedWrapper : public SpannerModule<TWeight> {
67public:
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
89private:
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
162 using SpannerModule<TWeight>::getTimeLeft;
164 using SpannerModule<TWeight>::m_GA;
165 using SpannerModule<TWeight>::m_stretch;
166 using SpannerModule<TWeight>::m_spanner;
167 using SpannerModule<TWeight>::m_inSpanner;
168};
169
170}
Includes declaration of graph class.
Declaration of graph copy classes.
Decralation of GraphElement and GraphList classes.
Basic module for spanner algorithms.
Class for the representation of edges.
Definition Graph_d.h:364
Stores additional attributes of a graph (like layout information).
const Graph & constGraph() const
Returns a reference to the associated graph.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition GraphCopy.h:191
const Graph & original() const
Returns a reference to the original graph.
Definition GraphCopy.h:104
Copies of graphs with mapping between nodes and edges.
Definition GraphCopy.h:260
edge newEdge(edge eOrig)
Creates a new edge in the graph copy with original edge eOrig.
Definition GraphCopy.h:320
void setOriginalGraph(const Graph *G) override
Re-initializes the copy using G (which might be null), but does not create any nodes or edges.
void clear() override
Removes all nodes and edges from this copy but does not break the link with the original graph.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
int numberOfEdges() const
Returns the number of edges in the graph.
Definition Graph_d.h:982
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition Graph_d.h:932
ReturnType
The return type of a module.
Definition Module.h:52
Class for the representation of nodes.
Definition Graph_d.h:241
A implementation-independed wrapper class to execute a spanner algorithm multiple times.
virtual SpannerModule< TWeight >::ReturnType execute() override
Executes the core algorithm.
std::unique_ptr< SpannerModule< TWeight > > m_module
SpannerIteratedWrapper(SpannerModule< TWeight > *module, int maxIterations)
Initializes the wrapper.
virtual bool preconditionsOk(const GraphAttributes &GA, double stretch, std::string &error) override
Interface for spanner algorithms.
const GraphAttributes * m_GA
EdgeArray< bool > * m_inSpanner
GraphCopySimple * m_spanner
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
The namespace for all OGDF objects.