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/HashArray.h>
36 #include <ogdf/basic/Queue.h>
40 
41 namespace ogdf {
42 namespace steiner_tree {
43 
47 template<typename T>
48 class SaveEnum : public Save<T> {
49 public:
54  explicit SaveEnum(EdgeWeightedGraphCopy<T>& steinerTree)
55  : Save<T>()
56  , m_save(0, steinerTree.maxNodeIndex(), 0, steinerTree.maxNodeIndex())
57  , m_steinerTree(&steinerTree) {
58  build();
59  }
60 
61  virtual ~SaveEnum() { }
62 
69  virtual T saveWeight(node u, node v) const {
70  OGDF_ASSERT(u);
71  OGDF_ASSERT(v);
72  return m_steinerTree->weight(
73  m_save(m_steinerTree->copy(u)->index(), m_steinerTree->copy(v)->index()));
74  }
75 
82  virtual edge saveEdge(node u, node v) const {
83  OGDF_ASSERT(u);
84  OGDF_ASSERT(v);
85  return m_save(m_steinerTree->copy(u)->index(), m_steinerTree->copy(v)->index());
86  }
87 
95  virtual T gain(node u, node v, node w) const {
96  const int uIndex = m_steinerTree->copy(u)->index();
97  const int vIndex = m_steinerTree->copy(v)->index();
98  const int wIndex = m_steinerTree->copy(w)->index();
99  const edge uvSave = m_save(uIndex, vIndex);
100  const edge vwSave = m_save(vIndex, wIndex);
101 
102  if (uvSave == vwSave) {
103  return m_steinerTree->weight(uvSave) + m_steinerTree->weight(m_save(uIndex, wIndex));
104  }
105  return m_steinerTree->weight(uvSave) + m_steinerTree->weight(vwSave);
106  }
107 
111  void rebuild() {
112  m_save.init(0, m_steinerTree->maxNodeIndex(), 0, m_steinerTree->maxNodeIndex());
113  build();
114  }
115 
123  virtual void update(const Triple<T>& t) {
124  const int uIndex = m_steinerTree->copy(t.s0())->index();
125  const int vIndex = m_steinerTree->copy(t.s1())->index();
126  const int wIndex = m_steinerTree->copy(t.s2())->index();
127  contractTripleInSteinerTree(t, *m_steinerTree, m_save(uIndex, vIndex),
128  m_save(vIndex, wIndex), m_save(uIndex, wIndex));
129  build();
130  }
131 
132 
133 protected:
137  inline void build() {
138  m_save.fill(nullptr);
139  EdgeArray<bool> hidden(*m_steinerTree, false);
140  List<node> processedNodes;
141  buildRecursively(hidden, m_steinerTree->firstNode(), processedNodes);
142  }
143 
156  void buildRecursively(EdgeArray<bool>& hidden, node u, List<node>& processedNodes) {
157  Queue<node> q;
158  q.append(u);
159 
160  NodeArray<bool> processed(*m_steinerTree, false);
161  processed[u] = true;
162 
163  // traverse through tree and find heaviest edge
164  T maxWeight = -1;
165  edge maxE = nullptr;
166  while (!q.empty()) {
167  const node v = q.pop();
168  processedNodes.pushBack(v);
169  for (adjEntry adj : v->adjEntries) {
170  const edge e = adj->theEdge();
171  if (!hidden[e]) {
172  const node w = adj->twinNode();
173  if (!processed[w]) {
174  q.append(w);
175  processed[w] = true;
176  if (m_steinerTree->weight(e) > maxWeight) {
177  maxE = e;
178  maxWeight = m_steinerTree->weight(e);
179  }
180  }
181  }
182  }
183  }
184 
185  if (maxE) {
186  OGDF_ASSERT(maxWeight > -1);
187 
188  hidden[maxE] = true;
189 
190  List<node> processedNodes1;
191  buildRecursively(hidden, maxE->source(), processedNodes1);
192  List<node> processedNodes2;
193  buildRecursively(hidden, maxE->target(), processedNodes2);
194 
195  for (node f : processedNodes1) {
196  for (node g : processedNodes2) {
197  m_save(f->index(), g->index()) = maxE;
198  m_save(g->index(), f->index()) = maxE;
199  }
200  }
201  }
202  }
203 
204 private:
207 };
208 
209 }
210 }
HashArray.h
Declaration and implementation of HashArray class.
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::Queue::append
iterator append(const E &x)
Adds x at the end of queue.
Definition: Queue.h:319
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:143
ogdf::steiner_tree::Triple
This class represents a triple used by various contraction-based minimum Steiner tree approximations.
Definition: Triple.h:44
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
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:82
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:47
Triple.h
Definition of a Triple used in contraction-based approximation algorithm for the minimum Steiner tree...
ogdf::steiner_tree::Save
This class serves as an interface for different approaches concerning the calculation of save edges.
Definition: Save.h:45
ogdf::steiner_tree::SaveEnum::build
void build()
Build the lookup table.
Definition: SaveEnum.h:137
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:153
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:48
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:111
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:69
ogdf::Queue::pop
E pop()
Removes front element and returns it.
Definition: Queue.h:331
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:95
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:264
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:54
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:123
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:391
ogdf::steiner_tree::SaveEnum::m_steinerTree
EdgeWeightedGraphCopy< T > * m_steinerTree
The current terminal spanning tree.
Definition: SaveEnum.h:206
ogdf::steiner_tree::SaveEnum::m_save
Array2D< edge > m_save
Data structure for the lookup table.
Definition: SaveEnum.h:205
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: List.h:42
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Queue
The parameterized class Queue<E> implements list-based queues.
Definition: Queue.h:200
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:168
ogdf::Queue::empty
bool empty() const
Returns true iff the queue is empty.
Definition: Queue.h:238
ogdf::steiner_tree::SaveEnum::~SaveEnum
virtual ~SaveEnum()
Definition: SaveEnum.h:61
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:156
ogdf::EdgeWeightedGraphCopy
Definition: EdgeWeightedGraphCopy.h:40
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
Save.h
Interface for various LCA methods.
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:394
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1537
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709