Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MinSteinerTreeRZLoss.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <set>
36 #include <ogdf/basic/Queue.h>
44 
45 #define OGDF_STEINERTREE_RZLOSS_REDUCE_ON
46 
47 namespace ogdf {
48 
60 template<typename T>
62 public:
63  template<typename TYPE> using SaveStatic = steiner_tree::SaveStatic<TYPE>;
64 
66  : m_ssspDistances(true)
67  {
69  }
70 
72  : m_ssspDistances(true)
73  {
75  }
76 
77  virtual ~MinSteinerTreeRZLoss() { }
78 
79  virtual T call(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal, EdgeWeightedGraphCopy<T> *&finalSteinerTree) override
80  {
84  return MinSteinerTreeModule<T>::call(G, terminals, isTerminal, finalSteinerTree);
85  }
86 
91  void setMaxComponentSize(int k)
92  {
93  m_restricted = k;
94  }
95 
101  void forceAPSP(bool force = true)
102  {
103  m_ssspDistances = !force;
104  }
105 
108  return m_componentsGenerated;
109  }
110 
113  return m_componentsContracted;
114  }
115 
118  return m_componentsLookUps;
119  }
120 
121 protected:
130  virtual T computeSteinerTree(
131  const EdgeWeightedGraph<T> &G,
132  const List<node> &terminals,
133  const NodeArray<bool> &isTerminal,
134  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override;
135 
136 private:
138  {
139  for (edge e : graph.edges) {
140  const node u = graph.original(e->source());
141  const node v = graph.original(e->target());
142  if (m_pred[u][v] == nullptr) {
143  return false;
144  }
145  }
146  for (node v : graph.nodes) {
147  if ((*m_isTerminal)[graph.original(v)] // is a terminal
148  && v->degree() > 1) { // but not a leaf
149  return false;
150  }
151  }
152  return true;
153  }
154 
161  void findFull3Components(SubsetEnumerator<node> &terminalSubset,
162  const EdgeWeightedGraphCopy<T> &tree, const SaveStatic<T> &save);
163 
169 
176  void multiPass(SaveStatic<T> &save, EdgeWeightedGraphCopy<T> &steinerTree, NodeArray<bool> &isNewTerminal);
177 
185  double extractMaxComponent(const EdgeWeightedGraphCopy<T> &steinerTree, int &maxCompId, SaveStatic<T> &save);
186 
194  template<typename TERMINAL_CONTAINER>
195  T gain(const TERMINAL_CONTAINER &terminals, const EdgeWeightedGraphCopy<T> &steinerTree, const SaveStatic<T> &save);
196 
202  void contractLoss(EdgeWeightedGraphCopy<T> &steinerTree, int compId);
203 
213 
217 };
218 
219 template<typename T>
221 {
222  m_originalGraph = &G;
223  m_restricted = min(m_restricted, terminals.size());
224  m_terminals = terminals; // copy
225  steiner_tree::sortTerminals(m_terminals); // sort
226  m_isTerminal = &isTerminal;
228 
229  for (node u : G.nodes) {
230  if (!isTerminal[u]) {
231  m_nonterminals.pushBack(u);
232  }
233  }
234 
235  m_distance.init(G);
236  m_pred.init(G);
237  if (m_ssspDistances
238  && m_restricted <= 3) {
239  // for 2- and 3-restricted computations, it is ok to use SSSP from all terminals
240  for (node u : terminals) {
242  }
243  } else {
244  m_ssspDistances = false;
246  }
247 
248  NodeArray<bool> isNewTerminal(G, false);
249  for (node v : terminals) {
250  isNewTerminal[v] = true;
251  }
252 
253  // init terminal-spanning tree and its save-edge data structure
254  EdgeWeightedGraphCopy<T> steinerTree; // the terminal-spanning tree to be modified
255  steinerTree.createEmpty(*m_originalGraph);
257 
258  SaveStatic<T> save(steinerTree);
259 
260  // component enumeration phase
261  if (m_restricted >= 3) {
262  findFullComponents(steinerTree, save);
263  }
264  m_fullCompStore->computeAllLosses();
266 
267  // contraction phase
268  multiPass(save, steinerTree, isNewTerminal);
269 
270  // cleanup
271  delete m_fullCompStore;
272  m_nonterminals.clear();
273 
274  // obtain final Steiner Tree using (MST-based) Steiner tree approximation algorithm
275  return steiner_tree::obtainFinalSteinerTree(*m_originalGraph, isNewTerminal, *m_isTerminal, finalSteinerTree);
276 }
277 
278 template<typename T>
280 {
281  // generate complete graph
282  for (node v : m_terminals) {
283  steinerTree.newNode(v);
284  }
285  for (node u : steinerTree.nodes) {
286  const node uO = steinerTree.original(u);
287  const NodeArray<T> &dist = m_distance[uO];
288  const NodeArray<edge> &pred = m_pred[uO];
289  for (node v = u->succ(); v; v = v->succ()) {
290  const node vO = steinerTree.original(v);
291  if (pred[vO] != nullptr) {
292  steinerTree.newEdge(u, v, dist[vO]);
293  }
294  }
295  }
296  // compute MST
297  makeMinimumSpanningTree(steinerTree, steinerTree.edgeWeights());
298  OGDF_ASSERT(steinerTree.numberOfNodes() == steinerTree.numberOfEdges() + 1);
299 }
300 
301 template<typename T>
304  const SaveStatic<T> &save)
305 {
306  // compute special case of 3-components (3-stars)
307  for (terminalSubset.begin(3); terminalSubset.valid(); terminalSubset.next()) {
308  const node t0 = terminalSubset[0];
309  const node t1 = terminalSubset[1];
310  const node t2 = terminalSubset[2];
311  OGDF_ASSERT(m_distance[t0][t1] < numeric_limits<T>::max());
312  OGDF_ASSERT(m_distance[t0][t2] < numeric_limits<T>::max());
313  OGDF_ASSERT(m_distance[t1][t2] < numeric_limits<T>::max());
314  T minCost = // bounded by the sum of the 2-components
315  min(m_distance[t0][t1] + m_distance[t0][t2],
316  min(m_distance[t1][t0] + m_distance[t1][t2],
317  m_distance[t2][t0] + m_distance[t2][t1]));
318  node minCenter = nullptr;
319  for (node center : m_nonterminals) {
320  OGDF_ASSERT(m_distance[t0][center] < numeric_limits<T>::max());
321  OGDF_ASSERT(m_distance[t1][center] < numeric_limits<T>::max());
322  OGDF_ASSERT(m_distance[t2][center] < numeric_limits<T>::max());
323  const T cost = m_distance[t0][center] + m_distance[t1][center] + m_distance[t2][center];
324  if (cost < minCost) {
325  minCenter = center;
326  minCost = cost;
327  }
328  }
329  if (minCenter
330  && m_pred[t0][minCenter]
331  && m_pred[t1][minCenter]
332  && m_pred[t2][minCenter]) {
333  // create a full 3-component
334  EdgeWeightedGraphCopy<T> minComp;
335  minComp.createEmpty(*m_originalGraph);
336  node minCenterC = minComp.newNode(minCenter);
337  minComp.newEdge(minComp.newNode(t0), minCenterC, m_distance[t0][minCenter]);
338  minComp.newEdge(minComp.newNode(t1), minCenterC, m_distance[t1][minCenter]);
339  minComp.newEdge(minComp.newNode(t2), minCenterC, m_distance[t2][minCenter]);
340  OGDF_ASSERT(isTree(minComp));
341 #ifdef OGDF_STEINERTREE_RZLOSS_REDUCE_ON
342  Array<node> terminals;
343  terminalSubset.array(terminals);
344  if (gain(terminals, tree, save) > minCost) { // reduction
345  m_fullCompStore->insert(minComp);
346  }
347 #else
348  m_fullCompStore->insert(minComp);
349 #endif
350  }
351  }
352 }
353 
354 template<typename T>
357  const SaveStatic<T> &save)
358 {
359  SubsetEnumerator<node> terminalSubset(m_terminals);
360  if (m_restricted >= 4) { // use Dreyfus-Wagner based full component generation
362  fcg.call(m_restricted);
363  for (terminalSubset.begin(3, m_restricted); terminalSubset.valid(); terminalSubset.next()) {
364  EdgeWeightedGraphCopy<T> component;
365  List<node> terminals;
366  terminalSubset.list(terminals);
367 #ifdef OGDF_STEINERTREE_RZLOSS_REDUCE_ON
368  T cost = fcg.getSteinerTreeFor(terminals, component);
369  if (gain(terminals, tree, save) > cost
370  && isValidComponent(component)) {
371  m_fullCompStore->insert(component);
372  }
373 #else
374  fcg.getSteinerTreeFor(terminals, component);
375  if (isValidComponent(component)) {
376  m_fullCompStore->insert(component);
377  }
378 #endif
379  }
380  } else {
381  findFull3Components(terminalSubset, tree, save);
382  }
383 }
384 
385 template<typename T>
387 {
388  while (!m_fullCompStore->isEmpty()) {
389  int maxCompId;
390  double r = extractMaxComponent(steinerTree, maxCompId, save);
391  if (r > 1e-9) {
393 
394  // convert nodes of component to terminals
395  m_fullCompStore->foreachNode(maxCompId, m_pred, [&](node v) {
396  isNewTerminal[v] = true;
397  });
398 
399  contractLoss(steinerTree, maxCompId);
400  m_fullCompStore->remove(maxCompId);
401 
402  if (!m_fullCompStore->isEmpty()) {
403  save.rebuild();
404  }
405  } else {
406  return;
407  }
408  }
409 }
410 
411 template<typename T>
413  const EdgeWeightedGraphCopy<T> &steinerTree,
414  int &maxCompId,
415  SaveStatic<T> &save)
416 {
417  maxCompId = -1;
418  double max(0);
419  for (int i = 0; i < m_fullCompStore->size();) {
421  const double winAbs = gain(m_fullCompStore->terminals(i), steinerTree, save) - m_fullCompStore->cost(i);
422  if (winAbs > 1e-9) {
423  const double r = winAbs / m_fullCompStore->loss(i);
424  if (r > max) {
425  max = r;
426  maxCompId = i;
427  }
428  ++i;
429  }
430 #ifdef OGDF_STEINERTREE_RZLOSS_REDUCE_ON
431  else {
432  // reduction
433  m_fullCompStore->remove(i);
434  }
435 #endif
436  }
437  return max;
438 }
439 
440 
441 template<typename T>
442 template<typename TERMINAL_CONTAINER>
443 T MinSteinerTreeRZLoss<T>::gain(const TERMINAL_CONTAINER &terminals, const EdgeWeightedGraphCopy<T> &steinerTree, const SaveStatic<T> &save)
444 {
445  std::set<edge> saveEdges;
446  T result(0);
447 
448  // extract edges and compute their sum (result value)
449  for (auto it1 = terminals.begin(); it1 != terminals.end(); ++it1) {
450  auto next = it1;
451  ++next;
452  for (auto it2 = next; it2 != terminals.end(); ++it2) {
453  const edge e = save.saveEdge(*it1, *it2);
454  saveEdges.insert(e);
455  }
456  }
457 
458  for (edge e : saveEdges) {
459  result += steinerTree.weight(e);
460  }
461  return result;
462 }
463 
464 template<typename T>
466 {
467  // for every non-loss edge {st} in the component,
468  // where s belongs to the loss component of terminal u
469  // and t belongs to the loss component of terminal v,
470  // we insert edges from u to v in the terminal-spanning tree
471  for (edge e : m_fullCompStore->lossBridges(compId)) {
472  const node u = m_fullCompStore->lossTerminal(e->source());
473  OGDF_ASSERT(u);
474  const node v = m_fullCompStore->lossTerminal(e->target());
475  OGDF_ASSERT(v);
476  steinerTree.newEdge(steinerTree.copy(u), steinerTree.copy(v), m_fullCompStore->graph().weight(e));
477  // parallel edges are ok, will be removed by MST
478  }
479  if (steinerTree.numberOfNodes() != steinerTree.numberOfEdges() + 1) {
480  makeMinimumSpanningTree(steinerTree, steinerTree.edgeWeights());
481  }
482 }
483 
484 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
long numberOfContractedComponents()
Returns the number of contracted components.
A generator for full components (for Steiner tree approximations) based on the Dreyfus-Wagner algorit...
bool isValidComponent(const EdgeWeightedGraphCopy< T > &graph)
const EdgeWeightedGraph< T > * m_originalGraph
The original edge-weighted graph.
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:336
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:413
T getSteinerTreeFor(const List< node > &terminals, EdgeWeightedGraphCopy< T > &tree)
Construct a Steiner tree for the given set of terminals.
virtual T call(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.
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
Builds a minimum Steiner tree given a weighted graph and a list of terminals.
This class serves as an interface for various methods to compute minimum or approximations of minimum...
int degree() const
Returns the degree of the node (indegree + outdegree).
Definition: Graph_d.h:208
edge newEdge(node u, node v, T weight)
Interface of Minimum Steiner Tree Algorithms.
int m_restricted
Parameter for the number of terminals in a full component.
static void sortTerminals(List< node > &terminals)
Sort terminals by index.
void forceAPSP(bool force=true)
For the 3-restricted case, it is sufficient to compute an SSSP from every terminal instead of doing a...
long numberOfGeneratedComponents()
Returns the number of generated components.
Extends the GraphCopy concept to weighted graphs.
void findFull3Components(SubsetEnumerator< node > &terminalSubset, const EdgeWeightedGraphCopy< T > &tree, const SaveStatic< T > &save)
bool valid() const
Is the current subset valid? If not, all subsets have already been enumerated.
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal&#39;s algorithm.
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:216
void findFullComponents(const EdgeWeightedGraphCopy< T > &tree, const SaveStatic< T > &save)
Finds all k-restricted full components (except 2-components).
long m_componentsLookUps
Number of components lookups.
Implementation of the staticLCATree option for calculating save edges in Zelikovsky&#39;s 11/6-approximat...
T gain(const TERMINAL_CONTAINER &terminals, const EdgeWeightedGraphCopy< T > &steinerTree, const SaveStatic< T > &save)
Calculates the gain for a full component.
void createEmpty(const Graph &wG)
const NodeArray< bool > * m_isTerminal
Incidence vector for terminal nodes.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:548
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:581
void begin(int low, int high)
Initialize the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
virtual edge saveEdge(node u, node v) const
Determines the save edge between two nodes by a LCA query.
Definition: SaveStatic.h:83
double extractMaxComponent(const EdgeWeightedGraphCopy< T > &steinerTree, int &maxCompId, SaveStatic< T > &save)
Traverses over all full components and finds the one with the highest win-objective.
const EdgeArray< T > & edgeWeights() const
Enumerator for k-subsets of a given type.
Definition and implementation of FullComponentStore, a data structure to store full components...
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
NodeArray< NodeArray< T > > m_distance
List< node > m_nonterminals
List of Steiner (non-terminal) nodes.
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
long m_componentsGenerated
Number of generated components.
Class for the representation of edges.
Definition: Graph_d.h:291
void rebuild()
Rebuild the data structure (necessary if the tree has changed)
Definition: SaveStatic.h:113
void setMaxComponentSize(int k)
Sets the maximal number of terminals in a full component.
A data structure to store full components with additional "loss" functionality.
Class for the representation of nodes.
Definition: Graph_d.h:167
T obtainFinalSteinerTree(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
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.
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:287
void multiPass(SaveStatic< T > &save, EdgeWeightedGraphCopy< T > &steinerTree, NodeArray< bool > &isNewTerminal)
Contraction phase of the algorithm.
void generateInitialTerminalSpanningTree(EdgeWeightedGraphCopy< T > &steinerTree)
Builds a minimum terminal spanning tree (via a MST call on the complete terminal graph) ...
void list(List< T > &list) const
Obtain (append) a list of the elements in the subset.
void next()
Obtain the next subset if possible. The result should be checked using the valid() method...
int size() const
Returns the number of elements in the list.
Definition: List.h:1255
NodeArray< NodeArray< edge > > m_pred
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
steiner_tree::FullComponentWithLossStore< T > * m_fullCompStore
All generated full components.
Algorithms used by at least two functions of Steiner tree code or its internal helpers.
void contractLoss(EdgeWeightedGraphCopy< T > &steinerTree, int compId)
Contracts the loss of a full component and integrates it into the given terminal-spanning tree...
for every hyperedge e a minimal subcubic tree connecting all hypernodes incident with e together is a...
The namespace for all OGDF objects.
bool m_ssspDistances
True iff we only compute SSSP from terminals instead of APSP for full component construction.
static void allPairShortestPathsStrict(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:578
long m_componentsContracted
Number of contracted components.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
Definition and implementation of a full component generator based on the Dreyfus-Wagner algorithm...
static void singleSourceShortestPathsStrict(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &isTerminal, NodeArray< T > &distance, NodeArray< edge > &pred)
A class that allows to enumerate k-subsets of lists.
List< node > m_terminals
List of terminal nodes (will be copied and sorted)
#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...
void array(Array< T > &array) const
Obtain an array of the elements in the subset.