Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MinSteinerTreeRZLoss.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <memory>
36 #include <set>
44 
45 #define OGDF_STEINERTREE_RZLOSS_REDUCE_ON
46 
47 namespace ogdf {
48 
60 template<typename T>
63 
64  class Main;
65  std::unique_ptr<Main> m_alg;
66 
67 public:
69  {
71  }
72 
74  {
76  }
77 
78  virtual ~MinSteinerTreeRZLoss() { }
79 
84  void setMaxComponentSize(int k)
85  {
86  m_restricted = k;
87  }
88 
91  if (m_alg == nullptr) {
92  return 0;
93  }
94  return m_alg->numberOfGeneratedComponents();
95  }
96 
99  if (m_alg == nullptr) {
100  return 0;
101  }
102  return m_alg->numberOfContractedComponents();
103  }
104 
107  if (m_alg == nullptr) {
108  return 0;
109  }
110  return m_alg->numberOfComponentLookUps();
111  }
112 
113 protected:
123  const EdgeWeightedGraph<T> &G,
124  const List<node> &terminals,
125  const NodeArray<bool> &isTerminal,
126  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override
127  {
128  m_alg.reset(new Main(G, terminals, isTerminal, m_restricted));
129  return m_alg->getApproximation(finalSteinerTree);
130  }
131 };
132 
133 template<typename T>
135  template<typename TYPE> using SaveStatic = steiner_tree::SaveStatic<TYPE>;
136 
139 
146  void findFullComponentsDW(const EdgeWeightedGraphCopy<T> &tree, const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred);
148  void findFullComponentsEMV(const EdgeWeightedGraphCopy<T> &tree);
150  void findFull3Components(const EdgeWeightedGraphCopy<T> &tree, const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred);
152  template<typename FCG>
153  void retrieveComponents(const FCG& fcg, const EdgeWeightedGraphCopy<T> &tree);
154 
156 
163  void generateInitialTerminalSpanningTree(EdgeWeightedGraphCopy<T> &steinerTree, const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred);
164 
167  tree.createEmpty(m_G);
168 
169  if (m_restricted >= 4 &&
170  steiner_tree::FullComponentDecisions::shouldUseErickson(m_G.numberOfNodes(), m_G.numberOfEdges())) {
172  m_save.reset(new steiner_tree::SaveStatic<T>(tree));
173  findFullComponentsEMV(tree);
174  } else {
175  NodeArray<NodeArray<T>> distance;
177 
178  steiner_tree::FullComponentGeneratorCaller<T>::computeDistanceMatrix(distance, pred, m_G, m_terminals, m_isTerminal, m_restricted);
179 
180  generateInitialTerminalSpanningTree(tree, distance, pred);
181  m_save.reset(new steiner_tree::SaveStatic<T>(tree));
182 
183  if (m_restricted >= 4) { // use Dreyfus-Wagner based full component generation
184  findFullComponentsDW(tree, distance, pred);
185  } else {
186  findFull3Components(tree, distance, pred);
187  }
188  }
189 
190  m_fullCompStore.computeAllLosses();
191  m_componentsGenerated = m_fullCompStore.size();
192  }
193 
198  void multiPass(EdgeWeightedGraphCopy<T> &steinerTree);
199 
206  double extractMaxComponent(const EdgeWeightedGraphCopy<T> &steinerTree, int &maxCompId);
207 
214  template<typename TERMINAL_CONTAINER>
215  T gain(const TERMINAL_CONTAINER &terminals, const EdgeWeightedGraphCopy<T> &steinerTree);
216 
222  void contractLoss(EdgeWeightedGraphCopy<T> &steinerTree, int compId);
223 
228  std::unique_ptr<steiner_tree::SaveStatic<T>> m_save;
231 
235 
236 public:
237  Main(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal, int restricted);
238 
239  T getApproximation(EdgeWeightedGraphCopy<T> *&finalSteinerTree) const {
240  // obtain final Steiner Tree using (MST-based) Steiner tree approximation algorithm
241  return steiner_tree::obtainFinalSteinerTree(m_G, m_isNewTerminal, m_isTerminal, finalSteinerTree);
242  }
243 
246  return m_componentsGenerated;
247  }
248 
251  return m_componentsContracted;
252  }
253 
256  return m_componentsLookUps;
257  }
258 };
259 
260 template<typename T>
261 MinSteinerTreeRZLoss<T>::Main::Main(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal, int restricted) :
262  m_G(G),
263  m_isTerminal(isTerminal),
264  m_terminals(terminals), // copy
265  m_restricted(min(restricted, terminals.size())),
266  m_fullCompStore(m_G, m_terminals, m_isTerminal),
267  m_isNewTerminal(m_G, false),
268  m_componentsGenerated(0),
269  m_componentsContracted(0),
270  m_componentsLookUps(0) {
272 
273  for (node v : terminals) {
274  m_isNewTerminal[v] = true;
275  }
276 
277  // init terminal-spanning tree and its save-edge data structure
278  EdgeWeightedGraphCopy<T> steinerTree; // the terminal-spanning tree to be modified
279 
280  setup(steinerTree);
281 
282  // contraction phase
283  multiPass(steinerTree);
284 
285  m_save.reset();
286 }
287 
288 template<typename T>
290  const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred) {
291  // generate complete graph
292  for (node v : m_terminals) {
293  steinerTree.newNode(v);
294  }
295  for (node u : steinerTree.nodes) {
296  const node uO = steinerTree.original(u);
297  const NodeArray<T> &dist = distance[uO];
298  const NodeArray<edge> &p = pred[uO];
299  for (node v = u->succ(); v; v = v->succ()) {
300  const node vO = steinerTree.original(v);
301  if (p[vO] != nullptr) {
302  steinerTree.newEdge(u, v, dist[vO]);
303  }
304  }
305  }
306  // compute MST
307  makeMinimumSpanningTree(steinerTree, steinerTree.edgeWeights());
308  OGDF_ASSERT(steinerTree.numberOfNodes() == steinerTree.numberOfEdges() + 1);
309 }
310 
311 template<typename T>
313  const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred) {
315  fcg.call(m_G, m_terminals, m_isTerminal, distance, pred,
316  [&](node t0, node t1, node t2, node minCenter, T minCost) {
317  // create a full 3-component
318  EdgeWeightedGraphCopy<T> minComp;
319  minComp.createEmpty(m_G);
320  node minCenterC = minComp.newNode(minCenter);
321  minComp.newEdge(minComp.newNode(t0), minCenterC, distance[t0][minCenter]);
322  minComp.newEdge(minComp.newNode(t1), minCenterC, distance[t1][minCenter]);
323  minComp.newEdge(minComp.newNode(t2), minCenterC, distance[t2][minCenter]);
324  OGDF_ASSERT(isTree(minComp));
325 #ifdef OGDF_STEINERTREE_RZLOSS_REDUCE_ON
326  if (gain(std::vector<node>{t0, t1, t2}, tree) > minCost) { // reduction
327  m_fullCompStore.insert(minComp);
328  }
329 #else
330  m_fullCompStore.insert(minComp);
331 #endif
332  });
333 }
334 
335 template<typename T>
336 template<typename FCG>
338  SubsetEnumerator<node> terminalSubset(m_terminals);
339  for (terminalSubset.begin(3, m_restricted); terminalSubset.valid(); terminalSubset.next()) {
340  EdgeWeightedGraphCopy<T> component;
341  List<node> terminals;
342  terminalSubset.list(terminals);
343 #ifdef OGDF_STEINERTREE_RZLOSS_REDUCE_ON
344  T cost =
345 #endif
346  fcg.getSteinerTreeFor(terminals, component);
347  if (
349  gain(terminals, tree) > cost &&
350 #endif
351  fcg.isValidComponent(component)) {
352  m_fullCompStore.insert(component);
353  }
354  }
355 }
356 
357 template<typename T>
359  const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred) {
361  fcg.call(m_restricted);
362  retrieveComponents(fcg, tree);
363 }
364 
365 template<typename T>
368  fcg.call(m_restricted);
369  retrieveComponents(fcg, tree);
370 }
371 
372 template<typename T>
374  while (!m_fullCompStore.isEmpty()) {
375  int maxCompId;
376  double r = extractMaxComponent(steinerTree, maxCompId);
377  if (r > 1e-9) {
379 
380  // convert nodes of component to terminals
381  m_fullCompStore.foreachNode(maxCompId, [&](node v) {
382  m_isNewTerminal[v] = true;
383  });
384 
385  contractLoss(steinerTree, maxCompId);
386  m_fullCompStore.remove(maxCompId);
387 
388  if (!m_fullCompStore.isEmpty()) {
389  m_save->rebuild();
390  }
391  } else {
392  return;
393  }
394  }
395 }
396 
397 template<typename T>
399  const EdgeWeightedGraphCopy<T> &steinerTree,
400  int &maxCompId) {
401  maxCompId = -1;
402  double max(0);
403  for (int i = 0; i < m_fullCompStore.size();) {
405  const double winAbs = gain(m_fullCompStore.terminals(i), steinerTree) - m_fullCompStore.cost(i);
406  if (winAbs > 1e-9) {
407  const double r = winAbs / m_fullCompStore.loss(i);
408  if (r > max) {
409  max = r;
410  maxCompId = i;
411  }
412  ++i;
413  }
414 #ifdef OGDF_STEINERTREE_RZLOSS_REDUCE_ON
415  else {
416  // reduction
417  m_fullCompStore.remove(i);
418  }
419 #endif
420  }
421  return max;
422 }
423 
424 template<typename T>
425 template<typename TERMINAL_CONTAINER>
426 T MinSteinerTreeRZLoss<T>::Main::gain(const TERMINAL_CONTAINER &terminals, const EdgeWeightedGraphCopy<T> &steinerTree) {
427  std::set<edge> saveEdges;
428  T result(0);
429 
430  // extract edges and compute their sum (result value)
431  for (auto it1 = terminals.begin(); it1 != terminals.end(); ++it1) {
432  auto next = it1;
433  ++next;
434  for (auto it2 = next; it2 != terminals.end(); ++it2) {
435  const edge e = m_save->saveEdge(*it1, *it2);
436  saveEdges.insert(e);
437  }
438  }
439 
440  for (edge e : saveEdges) {
441  result += steinerTree.weight(e);
442  }
443  return result;
444 }
445 
446 template<typename T>
448 {
449  // for every non-loss edge {st} in the component,
450  // where s belongs to the loss component of terminal u
451  // and t belongs to the loss component of terminal v,
452  // we insert edges from u to v in the terminal-spanning tree
453  for (edge e : m_fullCompStore.lossBridges(compId)) {
454  const node u = m_fullCompStore.lossTerminal(e->source());
455  OGDF_ASSERT(u);
456  const node v = m_fullCompStore.lossTerminal(e->target());
457  OGDF_ASSERT(v);
458  steinerTree.newEdge(steinerTree.copy(u), steinerTree.copy(v), m_fullCompStore.graph().weight(e));
459  // parallel edges are ok, will be removed by MST
460  }
461  if (steinerTree.numberOfNodes() != steinerTree.numberOfEdges() + 1) {
462  makeMinimumSpanningTree(steinerTree, steinerTree.edgeWeights());
463  }
464 }
465 
466 }
const NodeArray< bool > & m_isTerminal
Incidence vector for terminal nodes.
long numberOfContractedComponents()
Returns the number of contracted components.
steiner_tree::FullComponentWithLossStore< T > m_fullCompStore
All generated full components.
const EdgeWeightedGraph< T > & m_G
The original edge-weighted graph.
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:335
std::unique_ptr< Main > m_alg
This class behaves basically the same as SaveDynamic except that the update of the weighted graph is ...
Definition: SaveStatic.h:48
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:412
Full 3-component generation using Voronoi regions.
void findFullComponentsDW(const EdgeWeightedGraphCopy< T > &tree, const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred)
Find full components using algorithm by Dreyfus-Wagner.
Definition of the FullComponentGeneratorCaller class template.
static void sortTerminals(List< node > &terminals)
Sort terminals by index.
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
Main(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, int restricted)
edge newEdge(node u, node v, T weight)
void generateInitialTerminalSpanningTree(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred)
Builds a minimum terminal spanning tree (via a MST call on the complete terminal graph) ...
Implementation of Mehlhorn&#39;s minimum Steiner tree 2(1-1/l)-approximation algorithm.
long numberOfGeneratedComponents()
Returns the number of generated components.
bool valid() const
Checks if the current subset is valid. If not, the subset is either not initialized or all subsets ha...
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal&#39;s algorithm.
long m_componentsContracted
Number of contracted components.
long numberOfComponentLookUps()
Returns the number of components lookups during execution time.
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:218
List< node > m_terminals
List of terminal nodes (will be copied and sorted)
long numberOfContractedComponents()
Returns the number of contracted components.
Implementation of the staticLCATree option for calculating save edges in Zelikovsky&#39;s 11/6-approximat...
static bool shouldUseErickson(int n, int m)
Returns true iff the rule of thumb predicts to use the algorithm by Erickson et al instead of the Dre...
void multiPass(EdgeWeightedGraphCopy< T > &steinerTree)
Contraction phase of the algorithm.
static void computeDistanceMatrix(NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred, const EdgeWeightedGraph< T > &graph, const List< node > &terminals, const NodeArray< bool > &isTerminal, int restricted)
Computes distance and predecessor matrix.
void createEmpty(const Graph &wG)
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:601
void begin(int low, int high)
Initializes the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
int m_restricted
Parameter for the number of terminals in a full component.
A generator for restricted full components (for Steiner tree approximations) based on the Dreyfus-Wag...
const EdgeArray< T > & edgeWeights() const
long numberOfGeneratedComponents()
Returns the number of generated components.
Enumerator for k-subsets of a given type.
NodeArray< bool > m_isNewTerminal
Incidence vector for nonterminal nodes marked as terminals for improvement.
Definition of the FullComponentStore class template.
std::unique_ptr< steiner_tree::SaveStatic< T > > m_save
The save data structure.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
double extractMaxComponent(const EdgeWeightedGraphCopy< T > &steinerTree, int &maxCompId)
Traverses over all full components and finds the one with the highest win-objective.
Definition of ogdf::steiner_tree::Full3ComponentGeneratorVoronoi class template.
A generator for restricted full components (for Steiner tree approximations) based on the Dreyfus-Wag...
Definition of the ogdf::steiner_tree::FullComponentGeneratorDreyfusWagnerWithoutMatrix class template...
Class for the representation of edges.
Definition: Graph_d.h:292
void setMaxComponentSize(int k)
Sets the maximal number of terminals in a full component.
void setup(EdgeWeightedGraphCopy< T > &tree)
Setup (build initial terminal-spanning tree, its save data structure, and find all components) ...
T gain(const TERMINAL_CONTAINER &terminals, const EdgeWeightedGraphCopy< T > &steinerTree)
Calculates the gain for a full component.
A data structure to store full components with additional "loss" functionality.
Class for the representation of nodes.
Definition: Graph_d.h:169
T getApproximation(EdgeWeightedGraphCopy< T > *&finalSteinerTree) const
T obtainFinalSteinerTree(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:286
#define OGDF_STEINERTREE_RZLOSS_REDUCE_ON
void list(List< T > &subset) const
Obtains (appends) a list of the subset members.
void findFull3Components(const EdgeWeightedGraphCopy< T > &tree, const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred)
Find full components of size 3.
void next()
Obtains the next subset if possible. The result should be checked using the valid() method...
long m_componentsLookUps
Number of components lookups.
T constructTerminalSpanningTreeUsingVoronoiRegions(EdgeWeightedGraphCopy< T > &terminalSpanningTree, const EdgeWeightedGraph< T > &graph, const List< node > &terminals)
void retrieveComponents(const FCG &fcg, const EdgeWeightedGraphCopy< T > &tree)
Auxiliary function to retrieve components by Dreyfus-Wagner/Erickson et al implementation.
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
void call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred, std::function< void(node, node, node, node, T)> generateFunction) const
Generate full components and call generateFunction for each full component.
for every hyperedge e a minimal subcubic tree connecting all hypernodes incident with e together is a...
void findFullComponentsEMV(const EdgeWeightedGraphCopy< T > &tree)
Find full components using algorithm by Erickson et al.
The namespace for all OGDF objects.
long numberOfComponentLookUps()
Returns the number of components lookups during execution time.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
long m_componentsGenerated
Number of generated components.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
void contractLoss(EdgeWeightedGraphCopy< T > &steinerTree, int compId)
Contracts the loss of a full component and integrates it into the given terminal-spanning tree...
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree) override
Builds a minimum Steiner tree given a weighted graph and a list of terminals.
Definition of the ogdf::steiner_tree::FullComponentGeneratorDreyfusWagner class template.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
This class implements the loss-contracting (1.55+epsilon)-approximation algorithm for the Steiner tre...