Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

SaveEnum.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/Queue.h>
36 #include <ogdf/basic/HashArray.h>
40 
41 namespace ogdf {
42 namespace steiner_tree {
43 
47 template<typename T>
48 class SaveEnum: public Save<T> {
49 
50 public:
55  explicit SaveEnum(EdgeWeightedGraphCopy<T> &steinerTree)
56  : Save<T>()
57  , m_save(0, steinerTree.maxNodeIndex(), 0, steinerTree.maxNodeIndex())
58  , m_steinerTree(&steinerTree)
59  {
60  build();
61  }
62 
63  virtual ~SaveEnum()
64  {
65  }
66 
73  virtual T saveWeight(node u, node v) const
74  {
75  OGDF_ASSERT(u);
76  OGDF_ASSERT(v);
77  return m_steinerTree->weight(m_save(m_steinerTree->copy(u)->index(), m_steinerTree->copy(v)->index()));
78  }
79 
86  virtual edge saveEdge(node u, node v) const
87  {
88  OGDF_ASSERT(u);
89  OGDF_ASSERT(v);
90  return m_save(m_steinerTree->copy(u)->index(), m_steinerTree->copy(v)->index());
91  }
92 
100  virtual T gain(node u, node v, node w) const
101  {
102  const int uIndex = m_steinerTree->copy(u)->index();
103  const int vIndex = m_steinerTree->copy(v)->index();
104  const int wIndex = m_steinerTree->copy(w)->index();
105  const edge uvSave = m_save(uIndex, vIndex);
106  const edge vwSave = m_save(vIndex, wIndex);
107 
108  if (uvSave == vwSave) {
109  return m_steinerTree->weight(uvSave) + m_steinerTree->weight(m_save(uIndex, wIndex));
110  }
111  return m_steinerTree->weight(uvSave) + m_steinerTree->weight(vwSave);
112  }
113 
117  void rebuild()
118  {
119  m_save.init(0, m_steinerTree->maxNodeIndex(), 0, m_steinerTree->maxNodeIndex());
120  build();
121  }
122 
130  virtual void update(const Triple<T> &t)
131  {
132  const int uIndex = m_steinerTree->copy(t.s0())->index();
133  const int vIndex = m_steinerTree->copy(t.s1())->index();
134  const int wIndex = m_steinerTree->copy(t.s2())->index();
135  contractTripleInSteinerTree(t, *m_steinerTree, m_save(uIndex, vIndex), m_save(vIndex, wIndex), m_save(uIndex, wIndex));
136  build();
137  }
138 
139 
140 protected:
141 
145  inline void build()
146  {
147  m_save.fill(nullptr);
148  EdgeArray<bool> hidden(*m_steinerTree, false);
149  List<node> processedNodes;
150  buildRecursively(hidden, m_steinerTree->firstNode(), processedNodes);
151  }
152 
165  void buildRecursively(EdgeArray<bool> &hidden, node u, List<node> &processedNodes)
166  {
167  Queue<node> q;
168  q.append(u);
169 
171  processed[u] = true;
172 
173  // traverse through tree and find heaviest edge
174  T maxWeight = -1;
175  edge maxE = nullptr;
176  while (!q.empty()) {
177  const node v = q.pop();
178  processedNodes.pushBack(v);
179  for (adjEntry adj : v->adjEntries) {
180  const edge e = adj->theEdge();
181  if (!hidden[e]) {
182  const node w = adj->twinNode();
183  if (!processed[w]) {
184  q.append(w);
185  processed[w] = true;
186  if (m_steinerTree->weight(e) > maxWeight) {
187  maxE = e;
188  maxWeight = m_steinerTree->weight(e);
189  }
190  }
191  }
192  }
193  }
194 
195  if (maxE) {
196  OGDF_ASSERT(maxWeight > -1);
197 
198  hidden[maxE] = true;
199 
200  List<node> processedNodes1;
201  buildRecursively(hidden, maxE->source(), processedNodes1);
202  List<node> processedNodes2;
203  buildRecursively(hidden, maxE->target(), processedNodes2);
204 
205  for (node f : processedNodes1) {
206  for (node g : processedNodes2) {
207  m_save(f->index(), g->index()) = maxE;
208  m_save(g->index(), f->index()) = maxE;
209  }
210  }
211  }
212  }
213 
214 private:
217 
218 };
219 
220 } // end namespace steiner_tree
221 } // end namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
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:100
void build()
Build the lookup table.
Definition: SaveEnum.h:145
Declaration and implementation of HashArray class.
The parameterized class Queue<E> implements list-based queues.
Definition: Queue.h:213
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:95
iterator append(const E &x)
Adds x at the end of queue.
Definition: Queue.h:343
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
SaveEnum(EdgeWeightedGraphCopy< T > &steinerTree)
Initializes the data structures and calculates a MST of the given complete terminal graph...
Definition: SaveEnum.h:55
virtual void update(const Triple< T > &t)
Updates the weighted tree data structure given a contracted triple.
Definition: SaveEnum.h:130
void buildRecursively(EdgeArray< bool > &hidden, node u, List< node > &processedNodes)
Builds the lookup table for the save edges recursively.
Definition: SaveEnum.h:165
virtual edge saveEdge(node u, node v) const
Determines the save edge between two nodes by a table lookup.
Definition: SaveEnum.h:86
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
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:73
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:105
Class for the representation of edges.
Definition: Graph_d.h:291
The parameterized class Array2D implements dynamic two-dimensional arrays.
Definition: Array2D.h:46
bool empty() const
Returns true iff the queue is empty.
Definition: Queue.h:251
Class for the representation of nodes.
Definition: Graph_d.h:167
Definition of a Triple used in contraction-based approximation algorithm for the minimum Steiner tree...
This class computes save edges recursively and stores for every node pair their save edge in a HashAr...
Definition: SaveEnum.h:48
void rebuild()
Rebuild the lookup table (necessary if the tree has changed)
Definition: SaveEnum.h:117
Interface for various LCA methods.
Array2D< edge > m_save
Data structure for the lookup table.
Definition: SaveEnum.h:215
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
Algorithms used by at least two functions of Steiner tree code or its internal helpers.
The namespace for all OGDF objects.
EdgeWeightedGraphCopy< T > * m_steinerTree
The current terminal spanning tree.
Definition: SaveEnum.h:216
E pop()
Removes front element and returns it.
Definition: Queue.h:357
This class serves as an interface for different approaches concerning the calculation of save edges...
Definition: Save.h:45
Class for adjacency list elements.
Definition: Graph_d.h:78
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...
This class represents a triple used by various contraction-based minimum Steiner tree approximations...
Definition: Triple.h:44
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:198