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
35#include <ogdf/basic/Array2D.h>
36#include <ogdf/basic/Graph.h>
38#include <ogdf/basic/List.h>
39#include <ogdf/basic/Queue.h>
40#include <ogdf/basic/basic.h>
43
44namespace ogdf::steiner_tree {
45template<typename T>
46class Triple;
47} // namespace ogdf::steiner_tree
48
49namespace ogdf {
50template<typename T>
51class EdgeWeightedGraphCopy;
52
53namespace steiner_tree {
54
58template<typename T>
59class SaveEnum : public Save<T> {
60public:
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();
139 m_save(vIndex, wIndex), m_save(uIndex, wIndex));
140 build();
141 }
142
143
144protected:
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
215private:
218};
219
220}
221}
Declaration and implementation of class Array2D which implements dynamic two dimensional arrays.
Includes declaration of graph class.
Decralation of GraphElement and GraphList classes.
Declaration of doubly linked lists and iterators.
Interface for various LCA methods.
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
The parameterized class Array2D implements dynamic two-dimensional arrays.
Definition Array2D.h:53
Class for the representation of edges.
Definition Graph_d.h:364
node target() const
Returns the target node of the edge.
Definition Graph_d.h:402
node source() const
Returns the source node of the edge.
Definition Graph_d.h:399
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition List.h:1547
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
The parameterized class Queue<E> implements list-based queues.
Definition Queue.h:205
E pop()
Removes front element and returns it.
Definition Queue.h:336
bool empty() const
Returns true iff the queue is empty.
Definition Queue.h:243
iterator append(const E &x)
Adds x at the end of queue.
Definition Queue.h:324
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
This class computes save edges recursively and stores for every node pair their save edge in a HashAr...
Definition SaveEnum.h:59
SaveEnum(EdgeWeightedGraphCopy< T > &steinerTree)
Initializes the data structures and calculates a MST of the given complete terminal graph.
Definition SaveEnum.h:65
void build()
Build the lookup table.
Definition SaveEnum.h:148
void buildRecursively(EdgeArray< bool > &hidden, node u, List< node > &processedNodes)
Builds the lookup table for the save edges recursively.
Definition SaveEnum.h:167
Array2D< edge > m_save
Data structure for the lookup table.
Definition SaveEnum.h:216
EdgeWeightedGraphCopy< T > * m_steinerTree
The current terminal spanning tree.
Definition SaveEnum.h:217
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
void rebuild()
Rebuild the lookup table (necessary if the tree has changed)
Definition SaveEnum.h:122
virtual edge saveEdge(node u, node v) const
Determines the save edge between two nodes by a table lookup.
Definition SaveEnum.h:93
virtual void update(const Triple< T > &t)
Updates the weighted tree data structure given a contracted triple.
Definition SaveEnum.h:134
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
This class serves as an interface for different approaches concerning the calculation of save edges.
Definition Save.h:50
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:52
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.