Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SpringEmbedderKK.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
36 #include <ogdf/basic/basic.h>
37 #include <ogdf/basic/tuples.h>
38 
39 #include <limits>
40 
41 namespace ogdf {
42 class GraphAttributes;
43 
45 
78 public:
80 
83  : m_tolerance(0.001)
84  , m_ltolerance(0.0001)
85  , m_computeMaxIt(true)
86  , m_K(5.0)
87  , m_prevEnergy(startVal)
88  , m_prevLEnergy(startVal)
89  , m_zeroLength(-1.0)
90  , m_desLength(0.0)
91  , m_distFactor(2.0)
92  , m_useLayout(true)
93  , m_gItBaseVal(50)
94  , m_gItFactor(16) {
95  m_maxLocalIt = m_maxGlobalIt = maxVal;
96  }
97 
101  virtual void call(GraphAttributes& GA) override;
102 
106  void call(GraphAttributes& GA, const EdgeArray<double>& eLength);
107 
110  void setStopTolerance(double s) { m_tolerance = s; }
111 
113  void setUseLayout(bool b) { m_useLayout = b; }
114 
115  bool useLayout() { return m_useLayout; }
116 
120  void setZeroLength(double d) { m_zeroLength = d; }
121 
122  double zeroLength() { return m_zeroLength; }
123 
125  void setDesLength(double d) { m_desLength = d; }
126 
130  int maxLocalIterations() const { return m_maxLocalIt; }
131 
133  if (i > 0) {
134  m_gItFactor = i;
135  }
136  }
137 
138  int maxGlobalIterations() const { return m_maxGlobalIt; }
139 
142  if (i > 0) {
143  m_maxGlobalIt = i;
144  }
145  }
146 
148  void setMaxLocalIterations(int i) {
149  if (i > 0) {
150  m_maxLocalIt = i;
151  }
152  }
153 
155  void computeMaxIterations(bool b) { m_computeMaxIt = b; }
156 #if 0
157  //We could add some noise to the computation
158 
160  bool noise() const {
161  return m_noise;
162  }
163 
165  void noise(bool on) {
166  m_noise = on;
167  }
168 #endif
169 
170 protected:
172  void doCall(GraphAttributes& GA, const EdgeArray<double>& eLength, bool simpleBFS);
173 
175  bool finished(double maxdelta) {
176  if (m_prevEnergy == startVal) // first step
177  {
178  m_prevEnergy = maxdelta;
179  return false;
180  }
181 
182 #if 0
183  double diff = m_prevEnergy - maxdelta; // energy difference
184  if (diff < 0.0) diff = -diff;
185 # ifdef OGDF_DEBUG
186  std::cout << "Finished(): maxdelta: " << maxdelta << " diff/prev: " << diff / m_prevEnergy << std::endl;
187 # endif
188 #endif
189  // check if we want to stop
190  bool done = (maxdelta < m_tolerance); // || (diff / m_prevEnergy) < m_tolerance);
191 
192  m_prevEnergy = maxdelta; // save previous energy level
193  m_prevLEnergy = startVal; // reset energy level for local node decision
194 
195  return done;
196  }
197 
199  bool finishedNode(double deltav) {
200  if (m_prevLEnergy == startVal) {
201  m_prevLEnergy = deltav;
202  return deltav == 0.0; // m_ltolerance; locally stable
203  }
204 #if 0
205 # ifdef OGDF_DEBUG
206  std::cout << "Local delta: " << deltav << "\n";
207 # endif
208 #endif
209  double diff = m_prevLEnergy - deltav;
210  // check if we want to stop
211  bool done = deltav == 0.0 || diff / m_prevLEnergy < m_ltolerance;
212 
213  m_prevLEnergy = deltav; // save previous energy level
214 
215  return done;
216  }
217 
220  void adaptLengths(const Graph& G, const GraphAttributes& GA, const EdgeArray<double>& eLengths,
221  EdgeArray<double>& adaptedLengths);
222 
224  void shufflePositions(GraphAttributes& GA);
225 
228  dpair computeParDer(node m, node u, GraphAttributes& GA, NodeArray<NodeArray<double>>& ss,
230 
232  dpair computeParDers(node v, GraphAttributes& GA, NodeArray<NodeArray<double>>& ss,
234 
236  void initialize(GraphAttributes& GA, NodeArray<dpair>& partialDer,
237  const EdgeArray<double>& eLength, NodeArray<NodeArray<double>>& oLength,
238  NodeArray<NodeArray<double>>& sstrength, bool simpleBFS);
239 
241  void mainStep(GraphAttributes& GA, NodeArray<dpair>& partialDer,
242  NodeArray<NodeArray<double>>& oLength, NodeArray<NodeArray<double>>& sstrength);
243 
246  void scale(GraphAttributes& GA);
247 
248 private:
251  double m_tolerance;
252  double m_ltolerance;
256  double m_K;
257  double m_prevEnergy;
258  double m_prevLEnergy;
259  double m_zeroLength;
260  double m_desLength;
261  double m_distFactor; //< introduces some distance for scaling in case BFS is used
262  bool m_useLayout;
265 
266  static const double startVal;
267  static const double minVal;
268  static const double desMinLength;
269  static const int maxVal;
270 
271  double allpairsspBFS(const Graph& G, NodeArray<NodeArray<double>>& distance);
272  double allpairssp(const Graph& G, const EdgeArray<double>& eLengths,
273  NodeArray<NodeArray<double>>& distance,
274  const double threshold = std::numeric_limits<double>::max());
275 };
276 
277 #if 0
278  //Things that potentially could be added
279 
281  double pageRatio() { return m_pageRatio; }
282 
284  void pageRatio(double x) { m_pageRatio = x; }
285 
287  Scaling scaling() const {
288  return m_scaling;
289  }
290 
292  void scaling(Scaling sc) {
293  m_scaling = sc;
294  }
295 
297  double scaleFunctionFactor() const {
298  return m_scaleFactor;
299  }
300 
302  void scaleFunctionFactor(double f) {
303  m_scaleFactor = f;
304  }
305 
307  void userBoundingBox(double xmin, double ymin, double xmax, double ymax) {
308  m_bbXmin = xmin;
309  m_bbYmin = ymin;
310  m_bbXmax = xmax;
311  m_bbYmax = ymax;
312  }
313 #endif
314 }
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
Graph.h
Includes declaration of graph class.
ogdf::SpringEmbedderKK::m_prevEnergy
double m_prevEnergy
Big K constant for strength computation.
Definition: SpringEmbedderKK.h:257
ogdf::SpringEmbedderKK::setUseLayout
void setUseLayout(bool b)
If set to true, the given layout is used for the initial positions.
Definition: SpringEmbedderKK.h:113
ogdf::SpringEmbedderKK::finishedNode
bool finishedNode(double deltav)
Checks if inner loop (current node) is finished.
Definition: SpringEmbedderKK.h:199
ogdf::SpringEmbedderKK::m_desLength
double m_desLength
Desirable edge length, used instead if > 0.
Definition: SpringEmbedderKK.h:260
ogdf::Tuple2
Tuples of two elements (2-tuples).
Definition: tuples.h:49
ogdf::SpringEmbedderKK::desMinLength
static const double desMinLength
Defines minimum desired edge length.
Definition: SpringEmbedderKK.h:268
ogdf::SpringEmbedderKK::setDesLength
void setDesLength(double d)
Sets desirable edge length directly.
Definition: SpringEmbedderKK.h:125
ogdf::SpringEmbedderKK::maxVal
static const int maxVal
defines infinite upper bound for iteration number
Definition: SpringEmbedderKK.h:269
ogdf::SpringEmbedderKK::startVal
static const double startVal
Definition: SpringEmbedderKK.h:266
ogdf::SpringEmbedderKK::setGlobalIterationFactor
void setGlobalIterationFactor(int i)
Definition: SpringEmbedderKK.h:132
ogdf::SpringEmbedderKK::m_useLayout
bool m_useLayout
use positions or allow to shuffle nodes to avoid degeneration
Definition: SpringEmbedderKK.h:262
LayoutModule.h
Declaration of interface for layout algorithms (class LayoutModule)
ogdf::SpringEmbedderKK::maxLocalIterations
int maxLocalIterations() const
It is possible to limit the number of iterations to a fixed value Returns the current setting of iter...
Definition: SpringEmbedderKK.h:130
ogdf::SpringEmbedderKK::m_gItFactor
int m_gItFactor
factor for global iterations: m_gItBaseVal+m_gItFactor*|V|
Definition: SpringEmbedderKK.h:264
ogdf::SpringEmbedderKK::m_distFactor
double m_distFactor
Definition: SpringEmbedderKK.h:261
ogdf::SpringEmbedderKK::useLayout
bool useLayout()
Definition: SpringEmbedderKK.h:115
ogdf::SpringEmbedderKK::m_maxGlobalIt
int m_maxGlobalIt
Maximum number of global iterations.
Definition: SpringEmbedderKK.h:253
ogdf::SpringEmbedderKK::SpringEmbedderKK
SpringEmbedderKK()
Constructor: Constructs instance of Kamada Kawai Layout.
Definition: SpringEmbedderKK.h:82
ogdf::SpringEmbedderKK::setZeroLength
void setZeroLength(double d)
If set != 0, value zerolength is used to determine the desirable edge length by L = zerolength / max ...
Definition: SpringEmbedderKK.h:120
ogdf::SpringEmbedderKK::m_K
double m_K
Definition: SpringEmbedderKK.h:256
ogdf::SpringEmbedderKK::m_computeMaxIt
bool m_computeMaxIt
If true, number of iterations is computed depending on number of nodes.
Definition: SpringEmbedderKK.h:255
ogdf::SpringEmbedderKK::setMaxGlobalIterations
void setMaxGlobalIterations(int i)
Sets the number of global iterations to i.
Definition: SpringEmbedderKK.h:141
ogdf::SpringEmbedderKK::maxGlobalIterations
int maxGlobalIterations() const
Definition: SpringEmbedderKK.h:138
ogdf::SpringEmbedderKK
The spring-embedder layout algorithm by Kamada and Kawai.
Definition: SpringEmbedderKK.h:77
ogdf::SpringEmbedderKK::m_gItBaseVal
int m_gItBaseVal
minimum number of global iterations
Definition: SpringEmbedderKK.h:263
ogdf::SpringEmbedderKK::setStopTolerance
void setStopTolerance(double s)
Sets the value for the stop tolerance, below which the system is regarded stable (balanced) and the o...
Definition: SpringEmbedderKK.h:110
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::SpringEmbedderKK::finished
bool finished(double maxdelta)
Checks if main loop is finished because local optimum reached.
Definition: SpringEmbedderKK.h:175
ogdf::SpringEmbedderKK::zeroLength
double zeroLength()
Definition: SpringEmbedderKK.h:122
ogdf::SpringEmbedderKK::minVal
static const double minVal
Definition: SpringEmbedderKK.h:267
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::SpringEmbedderKK::m_zeroLength
double m_zeroLength
Length of a side of the display area, used for edge length computation if > 0.
Definition: SpringEmbedderKK.h:259
ogdf::SpringEmbedderKK::computeMaxIterations
void computeMaxIterations(bool b)
If set to true, number of iterations is computed depending on G.
Definition: SpringEmbedderKK.h:155
ogdf::SpringEmbedderKK::m_maxLocalIt
int m_maxLocalIt
Maximum number of local iterations.
Definition: SpringEmbedderKK.h:254
ogdf::SpringEmbedderKK::m_ltolerance
double m_ltolerance
value for local stop criterion
Definition: SpringEmbedderKK.h:252
ogdf::SpringEmbedderKK::setMaxLocalIterations
void setMaxLocalIterations(int i)
Sets the number of local iterations to i.
Definition: SpringEmbedderKK.h:148
ogdf::SpringEmbedderKK::m_prevLEnergy
double m_prevLEnergy
local energy
Definition: SpringEmbedderKK.h:258
ogdf::SpringEmbedderKK::m_tolerance
double m_tolerance
The stop criterion when the forces of all strings are considered to be balanced.
Definition: SpringEmbedderKK.h:251
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
tuples.h
Declaration and implementation of class Tuple2, Tuple3 and Tuple4.
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::LayoutModule
Interface of general layout algorithms.
Definition: LayoutModule.h:45