Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
SaveEnum.h
Go to the documentation of this file.
1
33#pragma once
34
36#include <ogdf/basic/Queue.h>
40
41namespace ogdf {
42namespace steiner_tree {
43
47template<typename T>
48class SaveEnum : public Save<T> {
49public:
55 : Save<T>()
56 , m_save(0, steinerTree.maxNodeIndex(), 0, steinerTree.maxNodeIndex())
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();
129 build();
130 }
131
132
133protected:
137 inline void build() {
138 m_save.fill(nullptr);
142 }
143
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) {
187
188 hidden[maxE] = true;
189
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
204private:
207};
208
209}
210}
Declaration and implementation of HashArray class.
Interface for various LCA methods.
Definition of a Triple used in contraction-based approximation algorithm for the minimum Steiner tree...
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
Class for adjacency list elements.
Definition Graph_d.h:79
The parameterized class Array2D implements dynamic two-dimensional arrays.
Definition Array2D.h:47
Dynamic arrays indexed with edges.
Definition EdgeArray.h:125
Class for the representation of edges.
Definition Graph_d.h:300
Doubly linked lists (maintaining the length of the list).
Definition List.h:1435
Dynamic arrays indexed with nodes.
Definition NodeArray.h:125
Class for the representation of nodes.
Definition Graph_d.h:177
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:208
The parameterized class Queue<E> implements list-based queues.
Definition Queue.h:200
iterator append(const E &x)
Adds x at the end of queue.
Definition Queue.h:319
This class computes save edges recursively and stores for every node pair their save edge in a HashAr...
Definition SaveEnum.h:48
SaveEnum(EdgeWeightedGraphCopy< T > &steinerTree)
Initializes the data structures and calculates a MST of the given complete terminal graph.
Definition SaveEnum.h:54
void build()
Build the lookup table.
Definition SaveEnum.h:137
void buildRecursively(EdgeArray< bool > &hidden, node u, List< node > &processedNodes)
Builds the lookup table for the save edges recursively.
Definition SaveEnum.h:156
Array2D< edge > m_save
Data structure for the lookup table.
Definition SaveEnum.h:205
EdgeWeightedGraphCopy< T > * m_steinerTree
The current terminal spanning tree.
Definition SaveEnum.h:206
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
void rebuild()
Rebuild the lookup table (necessary if the tree has changed)
Definition SaveEnum.h:111
virtual edge saveEdge(node u, node v) const
Determines the save edge between two nodes by a table lookup.
Definition SaveEnum.h:82
virtual void update(const Triple< T > &t)
Updates the weighted tree data structure given a contracted triple.
Definition SaveEnum.h:123
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
This class serves as an interface for different approaches concerning the calculation of save edges.
Definition Save.h:45
This class represents a triple used by various contraction-based minimum Steiner tree approximations.
Definition Triple.h:44
Algorithms used by at least two functions of Steiner tree code or its internal helpers.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
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...
The namespace for all OGDF objects.