Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MinSteinerTreeGoemans139.h
Go to the documentation of this file.
1 
33 #pragma once
34 
43 
44 namespace ogdf {
45 
63 template<typename T>
65 {
66 private:
67  class Main;
68 
69 protected:
70  int m_restricted;
74  int m_seed;
75 
76 public:
78  : m_restricted(3)
79  , m_preprocess(true)
80  , m_use2approx(false)
81  , m_separateCycles(false)
82  , m_seed(1337)
83  {
84  }
85 
87 
92  void setMaxComponentSize(int k)
93  {
94  m_restricted = k;
95  }
96 
101  void setSeed(int seed)
102  {
103  m_seed = seed;
104  }
105 
111  void use2Approximation(bool use2approx = true)
112  {
113  m_use2approx = use2approx;
114  }
115 
121  void disablePreprocessing(bool preprocess = true)
122  {
123  m_preprocess = !preprocess;
124  }
125 
130  void separateCycles(bool separateCycles = true)
131  {
132  m_separateCycles = separateCycles;
133  }
134 
135 protected:
144  virtual T computeSteinerTree(
145  const EdgeWeightedGraph<T> &G,
146  const List<node> &terminals,
147  const NodeArray<bool> &isTerminal,
148  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override;
149 };
150 
151 template<typename T>
153 {
154  std::minstd_rand rng(m_seed);
155  List<node> sortedTerminals(terminals);
157  Main main(G, sortedTerminals, isTerminal, m_restricted, m_use2approx, m_separateCycles);
158  return main.getApproximation(finalSteinerTree, rng, m_preprocess);
159 }
160 
163 template<typename T>
165 {
170 
172  enum class Approx2State {
173  Off,
174  On,
175  JustUseIt,
176  };
178 
179  const double m_eps;
180 
183 
186 
188  void findFull2Components(const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred);
190  void findFull3Components(const NodeArray<NodeArray<T>>& distance, const NodeArray<NodeArray<edge>>& pred);
192  void find3RestrictedComponents();
194  void findFullComponentsDW();
196  void findFullComponentsEMV();
198  template<typename FCG>
199  void retrieveComponents(const FCG& fcg);
201  void findFullComponents();
202 
206 
209  for (int k = m_fullCompStore.size() - 1; k >= 0; --k) {
210  OGDF_ASSERT(k < m_fullCompStore.size());
211  if (m_fullCompStore.extra(k) <= m_eps) {
212  m_fullCompStore.remove(k);
213  }
214  }
215  }
216 
219  {
220  ids.quicksort();
221  for (int i = ids.size() - 1; i >= 0; --i) {
222  m_fullCompStore.remove(ids[i]);
223  }
224  }
225 
227  void addComponent(NodeArray<bool> &isNewTerminal, int id)
228  {
229  m_fullCompStore.foreachNode(id, [&](node v) {
230  isNewTerminal[v] = true;
231  });
232  }
233 
236  void preprocess(NodeArray<bool> &isNewTerminal)
237  {
238  Graph H; // a graph where each component is a star
239  NodeArray<int> id(H); // ids each center of the star to the component id
240  NodeArray<node> copy(m_G, nullptr); // ids orig in m_G -> copy in H
241 
242  List<node> centers; // all centers
243  for (int i = 0; i < m_fullCompStore.size(); ++i) {
244  const node center = H.newNode();
245  centers.pushBack(center);
246  id[center] = i;
247 
248  for (node vG : m_fullCompStore.terminals(i)) {
249  node vH = copy[vG];
250  if (!vH) {
251  vH = H.newNode();
252  copy[vG] = vH;
253  }
254  H.newEdge(vH, center); // target is always center
255  }
256  }
257 
258  // find components to be inserted into the steinerTree and insert them
259  ArrayBuffer<int> inactive; // ids of components we insert; we have to remove them from the set of active components afterwards
260  bool changed;
261  do {
262  changed = false;
263  ListIterator<node> it2;
264  for (ListIterator<node> it = centers.begin(); it.valid(); it = it2) {
265  it2 = it.succ();
266  node c = *it;
267  int innerNodes = 0; // count inner nodes
268  for (adjEntry adj : c->adjEntries) {
269  innerNodes += (adj->twinNode()->degree() != 1);
270  }
271  if (innerNodes <= 1) { // this center represents a component to add to steinerTree
272  // insert component into steinerTree
273  addComponent(isNewTerminal, id[c]);
274 
275  // remove center from H (adjacent leaves can remain being isolated nodes)
276  inactive.push(id[c]);
277  H.delNode(c);
278  centers.del(it);
279 
280  changed = true;
281  }
282  }
283  } while (changed);
284 
285  removeComponents(inactive);
286  }
287 
289 
290 public:
292  Main(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal,
293  int restricted, bool use2approx, bool separateCycles, double eps = 1e-8)
294  : m_G(G)
295  , m_isTerminal(isTerminal)
296  , m_terminals(terminals)
297  , m_fullCompStore(G, m_terminals, isTerminal)
298  , m_restricted(restricted)
299  , m_use2approx(use2approx ? Approx2State::On : Approx2State::Off)
300  , m_eps(eps)
301  , m_approx2SteinerTree(nullptr)
302  , m_approx2Weight(0)
303  {
304  if (m_use2approx == Approx2State::On) { // add upper bound by 2-approximation
306  m_approx2Weight = mstT.call(m_G, m_terminals, m_isTerminal, m_approx2SteinerTree);
307  }
308 
309  if (m_restricted > m_terminals.size()) {
310  m_restricted = m_terminals.size();
311  }
312 
313  findFullComponents();
314 
315  steiner_tree::LPRelaxationSER<T> lp(m_G, m_terminals, m_isTerminal, m_fullCompStore, m_approx2Weight, separateCycles ? m_restricted + 1 : 0, m_eps);
316  if (!lp.solve()) {
317  OGDF_ASSERT(m_use2approx == Approx2State::On);
318  m_use2approx = Approx2State::JustUseIt;
319  }
320  }
321 
323  {
324  }
325 
327  T getApproximation(EdgeWeightedGraphCopy<T> *&finalSteinerTree, const std::minstd_rand &rng, const bool doPreprocessing = true);
328 };
329 
330 template<typename T>
332 {
334  fcg.call(m_G, m_terminals, distance, pred,
335  [&](node s, node t, T cost) {
336  EdgeWeightedGraphCopy<T> minComp;
337  minComp.createEmpty(m_G);
338  minComp.newEdge(minComp.newNode(s), minComp.newNode(t), distance[s][t]);
339  m_fullCompStore.insert(minComp);
340  });
341 }
342 
343 template<typename T>
345 {
347  fcg.call(m_G, m_terminals, m_isTerminal, distance, pred,
348  [&](node t0, node t1, node t2, node minCenter, T minCost) {
349  // create a full 3-component
350  EdgeWeightedGraphCopy<T> minComp;
351  minComp.createEmpty(m_G);
352  node minCenterC = minComp.newNode(minCenter);
353  minComp.newEdge(minComp.newNode(t0), minCenterC, distance[t0][minCenter]);
354  minComp.newEdge(minComp.newNode(t1), minCenterC, distance[t1][minCenter]);
355  minComp.newEdge(minComp.newNode(t2), minCenterC, distance[t2][minCenter]);
356  m_fullCompStore.insert(minComp);
357  });
358 }
359 
360 template<typename T>
362  NodeArray<NodeArray<T>> distance;
364 
365  steiner_tree::FullComponentGeneratorCaller<T>::computeDistanceMatrix(distance, pred, m_G, m_terminals, m_isTerminal, m_restricted);
366 
367  findFull2Components(distance, pred);
368  if (m_restricted == 3) {
369  findFull3Components(distance, pred);
370  }
371 }
372 
373 template<typename T>
374 template<typename FCG>
376  SubsetEnumerator<node> terminalSubset(m_terminals);
377  for (terminalSubset.begin(2, m_restricted); terminalSubset.valid(); terminalSubset.next()) {
378  EdgeWeightedGraphCopy<T> component;
379  List<node> terminals;
380  terminalSubset.list(terminals);
381  fcg.getSteinerTreeFor(terminals, component);
382  if (fcg.isValidComponent(component)) {
383  m_fullCompStore.insert(component);
384  }
385  }
386 }
387 
388 template<typename T>
390  NodeArray<NodeArray<T>> distance;
392  steiner_tree::FullComponentGeneratorCaller<T>::computeDistanceMatrix(distance, pred, m_G, m_terminals, m_isTerminal, m_restricted);
393 
394  steiner_tree::FullComponentGeneratorDreyfusWagner<T> fcg(m_G, m_terminals, m_isTerminal, distance, pred);
395  fcg.call(m_restricted);
396  retrieveComponents(fcg);
397 }
398 
399 template<typename T>
401  steiner_tree::FullComponentGeneratorDreyfusWagnerWithoutMatrix<T> fcg(m_G, m_terminals, m_isTerminal);
402  fcg.call(m_restricted);
403  retrieveComponents(fcg);
404 }
405 
406 template<typename T>
408 {
409  if (m_restricted >= 4) { // use Dreyfus-Wagner based full component generation
410  if (steiner_tree::FullComponentDecisions::shouldUseErickson(m_G.numberOfNodes(), m_G.numberOfEdges())) {
411  findFullComponentsEMV();
412  } else {
413  findFullComponentsDW();
414  }
415  } else {
416  find3RestrictedComponents();
417  }
418 }
419 
420 template<typename T>
421 T
422 MinSteinerTreeGoemans139<T>::Main::getApproximation(EdgeWeightedGraphCopy<T> *&finalSteinerTree, const std::minstd_rand &rng, const bool doPreprocessing)
423 {
424  if (m_use2approx == Approx2State::JustUseIt) {
425  // no remaining components
426  finalSteinerTree = m_approx2SteinerTree;
427  return m_approx2Weight;
428  }
429 
430  removeInactiveComponents();
431 
432  NodeArray<bool> isNewTerminal(m_G, false);
433  for (node v : m_terminals) {
434  isNewTerminal[v] = true;
435  }
436 
437  if (doPreprocessing) {
438  preprocess(isNewTerminal);
439  }
440 
441  if (!m_fullCompStore.isEmpty()) {
442  steiner_tree::goemans::Approximation<T> approx(m_G, m_terminals, m_isTerminal, m_fullCompStore, rng, m_eps);
443  approx.solve(isNewTerminal);
444  }
445 
446  T cost = steiner_tree::obtainFinalSteinerTree(m_G, isNewTerminal, m_isTerminal, finalSteinerTree);
447 
448  if (m_use2approx != Approx2State::Off) {
449  if (m_approx2Weight < cost) {
450  delete finalSteinerTree;
451  finalSteinerTree = m_approx2SteinerTree;
452  cost = m_approx2Weight;
453  } else {
454  delete m_approx2SteinerTree;
455  }
456  }
457 
458  return cost;
459 }
460 
461 }
void removeInactiveComponents()
Remove inactive components from m_fullCompStore (since we do not need them any longer) ...
Main(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, int restricted, bool use2approx, bool separateCycles, double eps=1e-8)
Initialize all attributes, sort the terminal list.
void findFullComponentsEMV()
Find full components using algorithm by Erickson et al.
void findFullComponentsDW()
Find full components using algorithm by Dreyfus-Wagner.
ListIteratorBase< E, isConst, isReverse > succ() const
Returns successor iterator.
Definition: List.h:145
T getApproximation(EdgeWeightedGraphCopy< T > *&finalSteinerTree, const std::minstd_rand &rng, const bool doPreprocessing=true)
Obtain an (1.39+epsilon)-approximation based on the LP solution.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:412
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
Full 3-component generation using Voronoi regions.
virtual void delNode(node v)
Removes node v and all incident edges from the graph.
void call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred, std::function< void(node, node, T)> generateFunction) const
Generate full 2-components and call generateFunction for each full 2-component.
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...
int degree() const
Returns the degree of the node (indegree + outdegree).
Definition: Graph_d.h:210
void retrieveComponents(const FCG &fcg)
Auxiliary function to retrieve components by Dreyfus-Wagner/Erickson et al implementation.
edge newEdge(node u, node v, T weight)
iterator begin()
Returns an iterator to the first element of the list.
Definition: List.h:352
const Array< node > & terminals(int id) const
Returns the list of terminals in the full component with given id.
steiner_tree::FullComponentWithExtraStore< T, double > m_fullCompStore
all enumerated full components, with solution
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
bool valid() const
Checks if the current subset is valid. If not, the subset is either not initialized or all subsets ha...
void quicksort()
Sorts buffer using Quicksort.
Definition: ArrayBuffer.h:314
Definition of ogdf::steiner_tree::goemans::Approximation class template.
bool solve()
Solve the LP. The solution will be written to the extra data of the full component store...
void del(iterator it)
Removes it from the list.
Definition: List.h:1437
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...
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)
void remove(int id)
Removes a component by its id.
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree) override
Builds a minimum Steiner tree for a given weighted graph with terminals.
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree, const node startNode)
An extended call method with specific start node.
void begin(int low, int high)
Initializes the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
static void copy(const T &from, T &to)
Definition: Alg.h:61
A generator for restricted full components (for Steiner tree approximations) based on the Dreyfus-Wag...
Enumerator for k-subsets of a given type.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
edge newEdge(node v, node w)
Creates a new edge (v,w) and returns it.
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...
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class managing the component-based subtour elimination LP relaxation for the Steiner tree problem and...
Definition of ogdf::steiner_tree::Full2ComponentGenerator class template.
The actual 1.39-approximation algorithm by Goemans et al. with a set of terminalized nodes as result...
Definition: Approximation.h:48
Class for the representation of nodes.
Definition: Graph_d.h:169
void removeComponents(ArrayBuffer< int > &ids)
Remove the full components with the given ids.
Encapsulates a pointer to a list element.
Definition: List.h:42
T obtainFinalSteinerTree(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:122
const double m_eps
epsilon for double operations
This class implements the minimum Steiner tree 2-approximation algorithm by Takahashi and Matsuyama w...
void solve(NodeArray< bool > &isNewTerminal)
Perform the actual approximation algorithm on the LP solution.
void setMaxComponentSize(int k)
Sets the maximal number of terminals in a full component.
void addComponent(NodeArray< bool > &isNewTerminal, int id)
Add a full component to the final solution (by changing nonterminals to terminals) ...
ExtraDataType & extra(int i)
Returns a reference to the extra data of this full component.
void list(List< T > &subset) const
Obtains (appends) a list of the subset members.
void next()
Obtains 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:1312
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.
Algorithms used by at least two functions of Steiner tree code or its internal helpers.
EdgeWeightedGraphCopy< T > * m_approx2SteinerTree
The namespace for all OGDF objects.
node newNode()
Creates a new node and returns it.
int size() const
Returns the number of full components in the store.
Definition of ogdf::steiner_tree::LPRelaxationSER class template.
Trivial full 2-component generation by lookups of shortest paths between terminal pairs...
This class implements the (1.39+epsilon)-approximation algorithm for the Steiner tree problem by Goem...
void findFull3Components(const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred)
Find full components of size 3.
void findFullComponents()
Find full components.
void findFull2Components(const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred)
Find full components of size 2.
const List< node > & m_terminals
List of terminals.
void setSeed(int seed)
Set seed for the random number generation.
Definition of the ogdf::steiner_tree::FullComponentGeneratorDreyfusWagner class template.
Class managing LP-based approximation.
void use2Approximation(bool use2approx=true)
Use Takahashi-Matsuyama 2-approximation as upper bounds.
Class for adjacency list elements.
Definition: Graph_d.h:79
void separateCycles(bool separateCycles=true)
Use stronger LP relaxation (not recommended in general)
void preprocess(NodeArray< bool > &isNewTerminal)
Preprocess LP solution.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
void disablePreprocessing(bool preprocess=true)
Disable preprocessing of LP solutions.
void find3RestrictedComponents()
Find 3-restricted components.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200