Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SpannerBasicGreedy.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Array.h>
35 #include <ogdf/basic/EpsilonTest.h>
36 #include <ogdf/basic/Graph.h>
38 #include <ogdf/basic/basic.h>
40 
41 #include <string>
42 
43 namespace ogdf {
44 class GraphCopySimple;
45 
66 template<typename TWeight>
67 class SpannerBasicGreedy : public SpannerModule<TWeight> {
70 
71 public:
73  virtual bool preconditionsOk(const GraphAttributes& GA, double stretch,
74  std::string& error) override {
75  if (GA.directed()) {
76  error = "The graph must be undirected";
77  return false;
78  }
79  if (stretch < 1.0) {
80  error = "The stretch must be >= 1.0";
81  return false;
82  }
83  return true;
84  }
85 
86 private:
87  virtual void init(const GraphAttributes& GA, double stretch, GraphCopySimple& spanner,
88  EdgeArray<bool>& inSpanner) override {
89  SpannerModule<TWeight>::init(GA, stretch, spanner, inSpanner);
90  m_spannerWeights.init(spanner);
91  }
92 
93  virtual typename SpannerModule<TWeight>::ReturnType execute() override {
95  int i = 0;
96  for (edge e : m_GA->constGraph().edges) {
97  edges[i++] = e;
98  }
100 
101  assertTimeLeft();
102 
103  for (edge e : edges) {
104  node u = m_spanner->copy(e->source());
105  node v = m_spanner->copy(e->target());
106  double maxDistance = m_stretch * weight(e);
107  double currentSpannerDistance = distanceInSpanner(u, v, maxDistance);
108  if (m_eps.greater(currentSpannerDistance, maxDistance)) {
109  edge newEdge = m_spanner->newEdge(e);
110  m_spannerWeights[newEdge] = weight(e);
111  (*m_inSpanner)[e] = true;
112  }
113  assertTimeLeft();
114  }
115 
117  }
118 
119  OGDF_EXPORT double distanceInSpanner(node s, node t, double maxLookupDist);
120 
124  inline TWeight weight(edge e) { return getWeight(*m_GA, e); }
125 
128  EdgeWeightComparator() = delete;
131 
132  bool less(edge a, edge b) const {
133  return m_eps.less(getWeight(m_GA, a), getWeight(m_GA, b));
134  }
135 
136  private:
139  };
140 
147 };
148 
149 }
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::SpannerBasicGreedy::preconditionsOk
virtual bool preconditionsOk(const GraphAttributes &GA, double stretch, std::string &error) override
Definition: SpannerBasicGreedy.h:73
EpsilonTest.h
Compare floating point numbers with epsilons and integral numbers with normal compare operators.
Graph.h
Includes declaration of graph class.
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::SpannerBasicGreedy::EdgeWeightComparator::m_eps
const EpsilonTest m_eps
Definition: SpannerBasicGreedy.h:138
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::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::SpannerBasicGreedy::execute
virtual SpannerModule< TWeight >::ReturnType execute() override
Executes the core algorithm.
Definition: SpannerBasicGreedy.h:93
ogdf::SpannerBasicGreedy::EdgeWeightComparator::m_GA
const GraphAttributes & m_GA
Definition: SpannerBasicGreedy.h:137
ogdf::GraphAttributes::directed
bool directed() const
Returns if the graph is directed.
Definition: GraphAttributes.h:232
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::EpsilonTest::less
std::enable_if< std::is_integral< T >::value, bool >::type less(const T &x, const T &y) const
Compare if x is LESS than y for integral types.
Definition: EpsilonTest.h:55
ogdf::SpannerBasicGreedy::EdgeWeightComparator::EdgeWeightComparator
EdgeWeightComparator()=delete
ogdf::SpannerBasicGreedy::init
virtual void init(const GraphAttributes &GA, double stretch, GraphCopySimple &spanner, EdgeArray< bool > &inSpanner) override
Initializes members and create an empty spanner.
Definition: SpannerBasicGreedy.h:87
ogdf::Graph::numberOfEdges
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:985
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:219
ogdf::SpannerBasicGreedy::m_spannerWeights
EdgeArray< TWeight > m_spannerWeights
Definition: SpannerBasicGreedy.h:68
ogdf::SpannerBasicGreedy::EdgeWeightComparator::operator=
EdgeWeightComparator & operator=(const EdgeWeightComparator &)=delete
ogdf::SpannerBasicGreedy
Multiplicative spanner by greedily adding edges.
Definition: SpannerBasicGreedy.h:67
ogdf::SpannerModule::m_stretch
double m_stretch
Definition: SpannerModule.h:135
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::SpannerBasicGreedy::EdgeWeightComparator
Definition: SpannerBasicGreedy.h:126
ogdf::SpannerBasicGreedy::EdgeWeightComparator::EdgeWeightComparator
EdgeWeightComparator(const GraphAttributes &GA)
Definition: SpannerBasicGreedy.h:127
ogdf::SpannerBasicGreedy::distanceInSpanner
double distanceInSpanner(node s, node t, double maxLookupDist)
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:935
ogdf::SpannerBasicGreedy::weight
TWeight weight(edge e)
Definition: SpannerBasicGreedy.h:124
ogdf::SpannerBasicGreedy::EdgeWeightComparator::less
bool less(edge a, edge b) const
Definition: SpannerBasicGreedy.h:132
basic.h
Basic declarations, included by all source files.
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::SpannerModule::m_GA
const GraphAttributes * m_GA
Definition: SpannerModule.h:134
Array.h
Declaration and implementation of Array class and Array algorithms.
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::Array::quicksort
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:639
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::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)
ogdf::SpannerBasicGreedy::m_eps
EpsilonTest m_eps
Definition: SpannerBasicGreedy.h:69