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/NodeSet.h>
36 
37 namespace ogdf {
38 
59 template<typename TWeight>
60 class SpannerBasicGreedy : public SpannerModule<TWeight> {
63 
64 public:
66  virtual bool preconditionsOk(const GraphAttributes& GA, double stretch,
67  std::string& error) override {
68  if (GA.directed()) {
69  error = "The graph must be undirected";
70  return false;
71  }
72  if (stretch < 1.0) {
73  error = "The stretch must be >= 1.0";
74  return false;
75  }
76  return true;
77  }
78 
79 private:
80  virtual void init(const GraphAttributes& GA, double stretch, GraphCopySimple& spanner,
81  EdgeArray<bool>& inSpanner) override {
82  SpannerModule<TWeight>::init(GA, stretch, spanner, inSpanner);
83  m_spannerWeights.init(spanner);
84  }
85 
86  virtual typename SpannerModule<TWeight>::ReturnType execute() override {
88  int i = 0;
89  for (edge e : m_GA->constGraph().edges) {
90  edges[i++] = e;
91  }
93 
95 
96  for (edge e : edges) {
97  node u = m_spanner->copy(e->source());
98  node v = m_spanner->copy(e->target());
99  double maxDistance = m_stretch * weight(e);
100  double currentSpannerDistance = distanceInSpanner(u, v, maxDistance);
101  if (m_eps.greater(currentSpannerDistance, maxDistance)) {
102  edge newEdge = m_spanner->newEdge(e);
103  m_spannerWeights[newEdge] = weight(e);
104  (*m_inSpanner)[e] = true;
105  }
106  assertTimeLeft();
107  }
108 
110  }
111 
112  OGDF_EXPORT double distanceInSpanner(node s, node t, double maxLookupDist);
113 
117  inline TWeight weight(edge e) { return getWeight(*m_GA, e); }
118 
121  EdgeWeightComparator() = delete;
124 
125  bool less(edge a, edge b) const {
126  return m_eps.less(getWeight(m_GA, a), getWeight(m_GA, b));
127  }
128 
129  private:
132  };
133 
140 };
141 
142 }
NodeSet.h
Declaration and implementation of ogdf::NodeSet.
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::SpannerBasicGreedy::preconditionsOk
virtual bool preconditionsOk(const GraphAttributes &GA, double stretch, std::string &error) override
Definition: SpannerBasicGreedy.h:66
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::SpannerBasicGreedy::EdgeWeightComparator::m_eps
const EpsilonTest m_eps
Definition: SpannerBasicGreedy.h:131
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::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::SpannerBasicGreedy::execute
virtual SpannerModule< TWeight >::ReturnType execute() override
Executes the core algorithm.
Definition: SpannerBasicGreedy.h:86
ogdf::SpannerBasicGreedy::EdgeWeightComparator::m_GA
const GraphAttributes & m_GA
Definition: SpannerBasicGreedy.h:130
ogdf::GraphAttributes::directed
bool directed() const
Returns if the graph is directed.
Definition: GraphAttributes.h:226
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::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:57
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:80
ogdf::Graph::numberOfEdges
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:977
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:214
ogdf::SpannerBasicGreedy::m_spannerWeights
EdgeArray< TWeight > m_spannerWeights
Definition: SpannerBasicGreedy.h:61
ogdf::SpannerBasicGreedy::EdgeWeightComparator::operator=
EdgeWeightComparator & operator=(const EdgeWeightComparator &)=delete
ogdf::SpannerBasicGreedy
Multiplicative spanner by greedily adding edges.
Definition: SpannerBasicGreedy.h:60
ogdf::SpannerModule::m_stretch
double m_stretch
Definition: SpannerModule.h:129
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::SpannerBasicGreedy::EdgeWeightComparator
Definition: SpannerBasicGreedy.h:119
ogdf::SpannerBasicGreedy::EdgeWeightComparator::EdgeWeightComparator
EdgeWeightComparator(const GraphAttributes &GA)
Definition: SpannerBasicGreedy.h:120
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:927
ogdf::SpannerBasicGreedy::weight
TWeight weight(edge e)
Definition: SpannerBasicGreedy.h:117
ogdf::SpannerBasicGreedy::EdgeWeightComparator::less
bool less(edge a, edge b) const
Definition: SpannerBasicGreedy.h:125
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:128
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::Array::quicksort
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:634
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::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)
ogdf::SpannerBasicGreedy::m_eps
EpsilonTest m_eps
Definition: SpannerBasicGreedy.h:62