Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SaveEnum.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/Array2D.h>
36 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/GraphList.h>
38 #include <ogdf/basic/List.h>
39 #include <ogdf/basic/Queue.h>
40 #include <ogdf/basic/basic.h>
43 
44 namespace ogdf::steiner_tree {
45 template<typename T>
46 class Triple;
47 } // namespace ogdf::steiner_tree
48 
49 namespace ogdf {
50 template<typename T>
51 class EdgeWeightedGraphCopy;
52 
53 namespace steiner_tree {
54 
58 template<typename T>
59 class SaveEnum : public Save<T> {
60 public:
65  explicit SaveEnum(EdgeWeightedGraphCopy<T>& steinerTree)
66  : Save<T>()
67  , m_save(0, steinerTree.maxNodeIndex(), 0, steinerTree.maxNodeIndex())
68  , m_steinerTree(&steinerTree) {
69  build();
70  }
71 
72  virtual ~SaveEnum() { }
73 
80  virtual T saveWeight(node u, node v) const {
81  OGDF_ASSERT(u);
82  OGDF_ASSERT(v);
83  return m_steinerTree->weight(
84  m_save(m_steinerTree->copy(u)->index(), m_steinerTree->copy(v)->index()));
85  }
86 
93  virtual edge saveEdge(node u, node v) const {
94  OGDF_ASSERT(u);
95  OGDF_ASSERT(v);
96  return m_save(m_steinerTree->copy(u)->index(), m_steinerTree->copy(v)->index());
97  }
98 
106  virtual T gain(node u, node v, node w) const {
107  const int uIndex = m_steinerTree->copy(u)->index();
108  const int vIndex = m_steinerTree->copy(v)->index();
109  const int wIndex = m_steinerTree->copy(w)->index();
110  const edge uvSave = m_save(uIndex, vIndex);
111  const edge vwSave = m_save(vIndex, wIndex);
112 
113  if (uvSave == vwSave) {
114  return m_steinerTree->weight(uvSave) + m_steinerTree->weight(m_save(uIndex, wIndex));
115  }
116  return m_steinerTree->weight(uvSave) + m_steinerTree->weight(vwSave);
117  }
118 
122  void rebuild() {
123  m_save.init(0, m_steinerTree->maxNodeIndex(), 0, m_steinerTree->maxNodeIndex());
124  build();
125  }
126 
134  virtual void update(const Triple<T>& t) {
135  const int uIndex = m_steinerTree->copy(t.s0())->index();
136  const int vIndex = m_steinerTree->copy(t.s1())->index();
137  const int wIndex = m_steinerTree->copy(t.s2())->index();
138  contractTripleInSteinerTree(t, *m_steinerTree, m_save(uIndex, vIndex),
139  m_save(vIndex, wIndex), m_save(uIndex, wIndex));
140  build();
141  }
142 
143 
144 protected:
148  inline void build() {
149  m_save.fill(nullptr);
150  EdgeArray<bool> hidden(*m_steinerTree, false);
151  List<node> processedNodes;
152  buildRecursively(hidden, m_steinerTree->firstNode(), processedNodes);
153  }
154 
167  void buildRecursively(EdgeArray<bool>& hidden, node u, List<node>& processedNodes) {
168  Queue<node> q;
169  q.append(u);
170 
171  NodeArray<bool> processed(*m_steinerTree, false);
172  processed[u] = true;
173 
174  // traverse through tree and find heaviest edge
175  T maxWeight = -1;
176  edge maxE = nullptr;
177  while (!q.empty()) {
178  const node v = q.pop();
179  processedNodes.pushBack(v);
180  for (adjEntry adj : v->adjEntries) {
181  const edge e = adj->theEdge();
182  if (!hidden[e]) {
183  const node w = adj->twinNode();
184  if (!processed[w]) {
185  q.append(w);
186  processed[w] = true;
187  if (m_steinerTree->weight(e) > maxWeight) {
188  maxE = e;
189  maxWeight = m_steinerTree->weight(e);
190  }
191  }
192  }
193  }
194  }
195 
196  if (maxE) {
197  OGDF_ASSERT(maxWeight > -1);
198 
199  hidden[maxE] = true;
200 
201  List<node> processedNodes1;
202  buildRecursively(hidden, maxE->source(), processedNodes1);
203  List<node> processedNodes2;
204  buildRecursively(hidden, maxE->target(), processedNodes2);
205 
206  for (node f : processedNodes1) {
207  for (node g : processedNodes2) {
208  m_save(f->index(), g->index()) = maxE;
209  m_save(g->index(), f->index()) = maxE;
210  }
211  }
212  }
213  }
214 
215 private:
218 };
219 
220 }
221 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::Queue::append
iterator append(const E &x)
Adds x at the end of queue.
Definition: Queue.h:324
Graph.h
Includes declaration of graph class.
ogdf::steiner_tree::contractTripleInSteinerTree
void contractTripleInSteinerTree(const Triple< T > &t, EdgeWeightedGraphCopy< T > &st, edge save0, edge save1, edge save2, edge &ne0, edge &ne1)
Updates the Steiner tree by deleting save edges, removing all direct connections between the terminal...
Definition: common_algorithms.h:159
ogdf::steiner_tree::Triple
This class represents a triple used by various contraction-based minimum Steiner tree approximations.
Definition: common_algorithms.h:48
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::steiner_tree::Triple::s2
node s2() const
Definition: Triple.h:54
ogdf::steiner_tree::SaveEnum::saveEdge
virtual edge saveEdge(node u, node v) const
Determines the save edge between two nodes by a table lookup.
Definition: SaveEnum.h:93
ogdf::steiner_tree::Triple::s1
node s1() const
Definition: Triple.h:52
Queue.h
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
ogdf::Array2D
The parameterized class Array2D implements dynamic two-dimensional arrays.
Definition: Array2D.h:53
ogdf::steiner_tree::Save
This class serves as an interface for different approaches concerning the calculation of save edges.
Definition: Save.h:50
ogdf::steiner_tree::SaveEnum::build
void build()
Build the lookup table.
Definition: SaveEnum.h:148
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::steiner_tree
Definition: MinSteinerTreeMehlhorn.h:297
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:160
ogdf::steiner_tree::SaveEnum
This class computes save edges recursively and stores for every node pair their save edge in a HashAr...
Definition: SaveEnum.h:59
common_algorithms.h
Algorithms used by at least two functions of Steiner tree code or its internal helpers.
ogdf::steiner_tree::SaveEnum::rebuild
void rebuild()
Rebuild the lookup table (necessary if the tree has changed)
Definition: SaveEnum.h:122
ogdf::steiner_tree::SaveEnum::saveWeight
virtual T saveWeight(node u, node v) const
Determines the weight of the save edge between two nodes by a table lookup.
Definition: SaveEnum.h:80
ogdf::Queue::pop
E pop()
Removes front element and returns it.
Definition: Queue.h:336
ogdf::steiner_tree::SaveEnum::gain
virtual T gain(node u, node v, node w) const
Returns the gain (sum of the save edges) of a node triple by an table lookup.
Definition: SaveEnum.h:106
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
ogdf::steiner_tree::SaveEnum::SaveEnum
SaveEnum(EdgeWeightedGraphCopy< T > &steinerTree)
Initializes the data structures and calculates a MST of the given complete terminal graph.
Definition: SaveEnum.h:65
ogdf::steiner_tree::SaveEnum::update
virtual void update(const Triple< T > &t)
Updates the weighted tree data structure given a contracted triple.
Definition: SaveEnum.h:134
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
ogdf::steiner_tree::SaveEnum::m_steinerTree
EdgeWeightedGraphCopy< T > * m_steinerTree
The current terminal spanning tree.
Definition: SaveEnum.h:217
ogdf::steiner_tree::SaveEnum::m_save
Array2D< edge > m_save
Data structure for the lookup table.
Definition: SaveEnum.h:216
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Queue
The parameterized class Queue<E> implements list-based queues.
Definition: Queue.h:205
ogdf::steiner_tree::Triple::s0
node s0() const
Definition: Triple.h:50
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
ogdf::Queue::empty
bool empty() const
Returns true iff the queue is empty.
Definition: Queue.h:243
ogdf::steiner_tree::SaveEnum::~SaveEnum
virtual ~SaveEnum()
Definition: SaveEnum.h:72
basic.h
Basic declarations, included by all source files.
ogdf::steiner_tree::SaveEnum::buildRecursively
void buildRecursively(EdgeArray< bool > &hidden, node u, List< node > &processedNodes)
Builds the lookup table for the save edges recursively.
Definition: SaveEnum.h:167
ogdf::EdgeWeightedGraphCopy
Definition: EdgeWeightedGraphCopy.h:44
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
List.h
Declaration of doubly linked lists and iterators.
Save.h
Interface for various LCA methods.
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
Array2D.h
Declaration and implementation of class Array2D which implements dynamic two dimensional arrays.
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1547
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716