Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

Approximation.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <memory>
39 
40 //#define OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
41 
42 namespace ogdf {
43 namespace steiner_tree {
44 namespace goemans {
45 
47 template<typename T>
49 {
54 
55  const double m_eps;
56 
57  std::minstd_rand m_rng;
58 
60  struct TemporaryEdges : ArrayBuffer<edge> {
62  TemporaryEdges(BlowupGraph<T>& blowupGraph) : m_blowupGraph(blowupGraph) {}
63 
65  edge add(node v, node w, T cost, int capacity) {
66  edge e = m_blowupGraph.newEdge(v, w, cost, capacity);
67  this->push(e);
68  return e;
69  }
70 
73  m_blowupGraph.delEdges(*this);
74  }
75 
76  private:
78  };
79 
81  int gammoidGetRank(const BlowupGraph<T> &blowupGraph) const
82  {
83  MaxFlowGoldbergTarjan<int> maxFlow(blowupGraph.getGraph());
84  return maxFlow.computeValue(blowupGraph.capacities(), blowupGraph.getSource(), blowupGraph.getTarget());
85  }
86 
89  int findComponentAndMaxBasis(std::unique_ptr<ArrayBuffer<std::pair<node,int>>> &maxBasis,
90  BlowupGraph<T> &blowupGraph,
92  {
93  // there should always be saturated flow to the component roots
94  // (contracted matroid)
95  EdgeArray<int> lB(blowupGraph.getGraph(), 0);
96  for (adjEntry adj : blowupGraph.getSource()->adjEntries) {
97  const edge e = adj->theEdge();
98  lB[e] = blowupGraph.getCapacity(e);
99  }
100 
101  // compute weights of core edges and add source->core edges
102  TemporaryEdges sourceCoreEdges(blowupGraph);
103  EdgeArray<double> cost(blowupGraph.getGraph(), 0);
104  for (node v : blowupGraph.core()) {
105  // compute weight of core edge
106  double weight = blowupGraph.computeCoreWeight(v);
107 
108  // add edges from source to core edges v
109  edge e = sourceCoreEdges.add(blowupGraph.getSource(), v, 0, blowupGraph.getCoreCapacity(v));
110  cost[e] = -weight;
111  }
112 
113  NodeArray<int> supply(blowupGraph.getGraph(), 0);
114  EdgeArray<int> flow(blowupGraph.getGraph());
116 
117  for (int id = 1; id <= gamma.size(); ++id) {
118  /* We want to find maximum-weight basis B \in B^K_Q
119  * B_Q = minimal edge set to remove after contracting Q to obtain feasible solution (blowup graph)
120  * = bases of gammoid M_Q
121  * B^K_Q = { B \in B_Q | B \subseteq K } [K is the set of core edges]
122  * = bases of gammoid M^K_Q
123  * M^K_Q is gammoid "obtained by restricting M_Q to K"
124  * M_Q = M'_Q / X' (M'_Q contracted by X') is a gammoid
125  * M'_Q = gammoid from X \cup X' to Y in D' with arc-disjointness instead of vertex-disjointness
126  * D' is D like for separation but without source node and with interim node v_e for each edge e
127  * X = inserted nodes v_e in each edge in blowup graph
128  * X' = the (arbitrary) roots of each component
129  * Y = Q \cup {t}
130  * that means:
131  * I (subset of X) is an independent set of M_Q
132  * <=> (if X' is always an independent set in M'_Q ... which we assume here)
133  * I \cup X' is an independent set of M'_Q
134  * Restricting to K means:
135  * only put v_e in X with e \in K (that is, we only need to *generate* v_e if e \in K)
136  * Hence:
137  * - we generate D'^K (this is blowupGraph)
138  * - compute the max flow from X^K \cup X' to Q \cup {t}
139  * - ASSERT that X' is "saturated"!
140  * - check which subset of X is saturated -> these are the nodes representing the edge set we need
141  */
142  // add edges from component's terminals to target
143  TemporaryEdges Q_to_target(blowupGraph);
144  for (node t : gamma.terminals(id)) {
145  Q_to_target.add(t, blowupGraph.getTarget(), 0, blowupGraph.getLCM() * blowupGraph.getY());
146  // the last value is an upper bound for the capacity
147  }
148  // TODO: we could also use static edges from all terminals to the target
149  // and just change their capacities each time; needs extra data structures
150 
151  std::unique_ptr<ArrayBuffer<std::pair<node,int>>> basis(new ArrayBuffer<std::pair<node,int>>);
152 
153  int rank = gammoidGetRank(blowupGraph);
154  OGDF_ASSERT(rank >= blowupGraph.getY() + blowupGraph.getLCM());
155  supply[blowupGraph.getSource()] = rank;
156  supply[blowupGraph.getTarget()] = -rank;
157 
158 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
159  std::cout << "Computing min-cost flow for blowup component " << id << " of " << gamma.size() << std::endl;
160  std::cout << " * terminals of component are " << gamma.terminals(id) << std::endl;
161  for (node v : blowupGraph.getGraph().nodes) {
162  if (supply[v] > 0) {
163  std::cout << " * supply node " << v << " with supply " << supply[v] << std::endl;
164  }
165  if (supply[v] < 0) {
166  std::cout << " * demand node " << v << " with demand " << -supply[v] << std::endl;
167  }
168  }
169  for (edge e : blowupGraph.getGraph().edges) {
170  std::cout
171  << " * edge " << e
172  << " with cost " << blowupGraph.getCost(e)
173  << " and flow bounds [" << lB[e]
174  << ", " << blowupGraph.getCapacity(e)
175  << "]" << std::endl;
176  }
177 #endif
178 
179  // find maximum weight basis
180 #ifdef OGDF_DEBUG
181  bool feasible =
182 #endif
183  mcf.call(blowupGraph.getGraph(), lB, blowupGraph.capacities(), cost, supply, flow);
184  OGDF_ASSERT(feasible);
185  OGDF_ASSERT(mcf.checkComputedFlow(blowupGraph.getGraph(), lB, blowupGraph.capacities(), cost, supply, flow));
186 
187  double weight(0);
188  for (edge e : sourceCoreEdges) {
189  if (flow[e] > 0) {
190  basis->push(std::make_pair(e->target(), flow[e]));
191  weight -= flow[e] * cost[e];
192  }
193  }
194 
195 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
196  std::cout
197  << "Basis weight is " << weight << std::endl
198  << "Checking if "
199  << gamma.cost(id) << "(component cost) * " << blowupGraph.getLCM()
200  << "(lcm) <= " << weight
201  << "(basis weight)" << std::endl;
202 #endif
203 
204  // we choose the component with max cost*N <= weight
205  if (gamma.cost(id) * blowupGraph.getLCM() <= weight + m_eps) {
206 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
207  std::cout << "Using basis because it is feasible." << std::endl;
208 #endif
209  maxBasis.swap(basis);
210  return id;
211  }
212  }
213  return 0; // no component chosen, fail
214  }
215 
218  int findCheapestComponentAndRemainingBasis(std::unique_ptr<ArrayBuffer<std::pair<node,int>>> &maxBasis,
219  const BlowupGraph<T> &blowupGraph,
220  const BlowupComponents<T> &gamma)
221  {
222  // find cheapest component
223  int compId = 0;
224  double cost = 0;
225  for (int id = 1; id <= gamma.size(); ++id) {
226  if (gamma.cost(id) > cost) {
227  cost = gamma.cost(id);
228  compId = id;
229  }
230  }
231  // use all core edges as basis
232  maxBasis.reset(new ArrayBuffer<std::pair<node,int>>());
233  for (node v : blowupGraph.core()) {
234  maxBasis->push(std::make_pair(v, blowupGraph.getCoreCapacity(v)));
235  }
236  return compId;
237  }
238 
240  void addComponent(NodeArray<bool> &isNewTerminal, const BlowupGraph<T> &blowupGraph, const edge rootEdge)
241  {
242  OGDF_ASSERT(blowupGraph.isTerminal(rootEdge->source()));
243  ArrayBuffer<node> stack;
244  stack.push(rootEdge->target());
245  while (!stack.empty()) {
246  const node v = stack.popRet();
247  if (blowupGraph.isTerminal(v)) {
248  continue;
249  }
250  const node vO = blowupGraph.getOriginal(v);
251  if (vO) {
252  isNewTerminal[vO] = true;
253  }
254  for (adjEntry adj : v->adjEntries) {
255  const node w = adj->theEdge()->target();
256  if (v != w) { // outgoing edge
257  stack.push(w);
258  }
259  }
260  }
261  }
262 
264  void removeFractionalBasisAndCleanup(ArrayBuffer<std::pair<node,int>> &basis,
265  BlowupGraph<T> &blowupGraph,
267  {
268 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
269  std::cout << "Remove basis from blowup graph" << std::endl;
270 #endif
271  // remove B from K (K := K \ B) and from blowup graph (X := X - B)
272  // and, while at it, remove cleanup edges from blowup graph (X := X - F)
273  // and fix components that have no incoming edges
274  ArrayBuffer<Prioritized<node, int>> fractionalCoreEdges; // we defer fractional basis elements
275  for (auto p : basis) {
276  const node v = p.first;
277  const int count = p.second;
278  int origCap = blowupGraph.getCoreCapacity(v);
279  OGDF_ASSERT(count <= origCap);
280 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
281  std::cout << " * node " << v << " with count " << count << " (of " << origCap << ")" << std::endl;
282 #endif
283  if (count < origCap) { // only remove a fraction?
284  fractionalCoreEdges.push(Prioritized<node,int>(v, -count));
285 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
286  std::cout << " -> deferred because fractional" << std::endl;
287 #endif
288  } else {
289  // we are deleting the core edge from the whole component
290  blowupGraph.delCore(v);
291  blowupGraph.removeBasis(v);
292 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
293  std::cout << " -> done" << std::endl;
294 #endif
295  }
296  }
297  fractionalCoreEdges.quicksort(); // sort decreasing by flow value
298 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
299  if (!fractionalCoreEdges.empty()) {
300  std::cout << "Deferred core edges:" << std::endl;
301  }
302 #endif
303  for (auto p : fractionalCoreEdges) {
304  const node v = p.item();
305  const int count = -p.priority();
306  int origCap = blowupGraph.getCoreCapacity(v);
307 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
308  std::cout << " * node " << v << " with count " << count << " of " << origCap << std::endl;
309 #endif
310  OGDF_ASSERT(count <= origCap);
311  // copy (split) the component
312  blowupGraph.copyComponent(blowupGraph.findRootEdge(v), count, origCap - count);
313  // we are deleting the core edge from the whole component
314  blowupGraph.delCore(v);
315  blowupGraph.removeBasis(v);
316  }
317  }
318 
319 public:
321  Approximation(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal,
322  const FullComponentWithExtraStore<T, double> &fullCompStore,
323  const std::minstd_rand &rng, double eps = 1e-8)
324  : m_G(G)
325  , m_isTerminal(isTerminal)
326  , m_terminals(terminals)
327  , m_fullCompStore(fullCompStore)
328  , m_eps(eps)
329  , m_rng(rng)
330  {
331  }
332 
335  void solve(NodeArray<bool> &isNewTerminal);
336 };
337 
338 template<typename T>
339 void
341 {
342 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
343  std::cout << "Start solving based on LP solution" << std::endl;
344  int iteration = 0;
345 #endif
347  BlowupGraph<T> blowupGraph(m_G, m_terminals, m_fullCompStore, cer, m_eps);
348 
349  while (blowupGraph.terminals().size() > 1) { // T is not a Steiner tree
350 #ifdef OGDF_STEINER_TREE_GOEMANS_APPROXIMATION_LOGGING
351  std::cout << "Iteration " << ++iteration << " with " << blowupGraph.terminals().size() << " terminals" << std::endl;
352 #endif
353  BlowupComponents<T> gamma(blowupGraph); // Gamma(X)
354 
355  OGDF_ASSERT(isLoopFree(blowupGraph.getGraph()));
356 
357  // take a component Q in Gamma(X)
358  std::unique_ptr<ArrayBuffer<std::pair<node,int>>> maxBasis;
359  int compId = blowupGraph.getY() > 0
360  ? findComponentAndMaxBasis(maxBasis, blowupGraph, gamma)
361  : findCheapestComponentAndRemainingBasis(maxBasis, blowupGraph, gamma);
362  OGDF_ASSERT(compId != 0);
363 
364  // add component Q to T
365  addComponent(isNewTerminal, blowupGraph, gamma.rootEdge(compId));
366 
367  // remove (maybe fractional) basis and do all the small things necessary for update
368  removeFractionalBasisAndCleanup(*maxBasis, blowupGraph, gamma);
369 
370  // contract (X := X / Q)
371  blowupGraph.contract(gamma.terminals(compId));
372 
373  if (blowupGraph.terminals().size() > 1) {
374  blowupGraph.updateSpecialCapacities();
375  }
376  }
377 }
378 
379 }
380 }
381 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
node getSource() const
Returns the source node.
Definition: BlowupGraph.h:503
int findComponentAndMaxBasis(std::unique_ptr< ArrayBuffer< std::pair< node, int >>> &maxBasis, BlowupGraph< T > &blowupGraph, const BlowupComponents< T > &gamma)
Finds the best component and its maximum-weight basis in the given blowup graph with given core and w...
Definition: Approximation.h:89
Definition of ogdf::steiner_tree::goemans::CoreEdgeRandomSpanningTree class template.
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
Definition of ogdf::MinCostFlowReinelt class template.
Add edges into a blowup graph and delete them on destruction.
Definition: Approximation.h:60
int findCheapestComponentAndRemainingBasis(std::unique_ptr< ArrayBuffer< std::pair< node, int >>> &maxBasis, const BlowupGraph< T > &blowupGraph, const BlowupComponents< T > &gamma)
For the end of the algorithm: find cheapest component and choose all remaining core edges as basis...
void removeFractionalBasisAndCleanup(ArrayBuffer< std::pair< node, int >> &basis, BlowupGraph< T > &blowupGraph, BlowupComponents< T > &gamma)
Remove a given basis and cleanup, the basis may be given fractionally.
const List< node > & terminals() const
Returns a reference to the list containing all terminals in the blowup graph.
Definition: BlowupGraph.h:548
const ArrayBuffer< node > & terminals(int id) const
Return list of terminals for a given component.
Obtain and provides information about components in a given blowup graph.
const FullComponentWithExtraStore< T, double > & m_fullCompStore
all enumerated full components, with solution
Definition: Approximation.h:53
int capacity() const
Returns the current capacity of the datastructure. Note that this value is rather irrelevant if the a...
Definition: ArrayBuffer.h:125
void push(edge e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
void contract(node &v, node t)
Contracts node v and terminal t into v.
Definition: BlowupGraph.h:621
edge add(node v, node w, T cost, int capacity)
Add a temporary edge to the blowup graph.
Definition: Approximation.h:65
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
void copyComponent(const edge origEdge, const int origCap, const int copyCap)
Copy a component in the blowup graph and set original capacity to origCap and capacity of copy to cop...
Definition: BlowupGraph.h:730
int getLCM() const
Returns the least common multiple of all denominators.
Definition: BlowupGraph.h:538
void quicksort()
Sorts buffer using Quicksort.
Definition: ArrayBuffer.h:314
int getY() const
Returns the y-value of all terminals.
Definition: BlowupGraph.h:543
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer...
Definition: comparer.h:279
void delCore(node e)
Remove a core edge.
Definition: BlowupGraph.h:771
Declaration and implementation of Goldberg-Tarjan max-flow algorithm with global relabeling and gap r...
const EdgeWeightedGraph< T > & m_G
Definition: Approximation.h:50
edge findRootEdge(node v)
Finds the root node of a component given by v, an arbitrary inner nonterminal of the component...
Definition: BlowupGraph.h:708
double computeCoreWeight(node v) const
Compute the weight of a core edge.
Definition: BlowupGraph.h:578
const NodeArray< bool > & m_isTerminal
Definition: Approximation.h:51
const double m_eps
epsilon for double operations
Definition: Approximation.h:55
const T & cost(int id) const
Return total cost of a given component.
const EdgeArray< int > & capacities() const
Returns a reference to the edge array containing all capacities.
Definition: BlowupGraph.h:523
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:116
node getTarget() const
Returns the target node.
Definition: BlowupGraph.h:513
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
T getCoreCapacity(node v) const
Get capacity of a core edge.
Definition: BlowupGraph.h:562
TCap computeValue(const EdgeArray< TCap > &cap, const node &s, const node &t)
Compute only the value of the flow.
bool isLoopFree(const Graph &G)
Returns true iff G contains no self-loop.
Class for the representation of edges.
Definition: Graph_d.h:292
void updateSpecialCapacities()
Updates capacities from source to terminals and terminals to pseudotarget.
Definition: BlowupGraph.h:590
constexpr double gamma
The Euler-Mascheroni constant gamma.
Definition: Math.h:77
The actual 1.39-approximation algorithm by Goemans et al. with a set of terminalized nodes as result...
Definition: Approximation.h:48
int getCapacity(edge e) const
Returns the capacity of e.
Definition: BlowupGraph.h:518
Class for the representation of nodes.
Definition: Graph_d.h:169
edge rootEdge(int id) const
Return the edge coming from the root of a given component.
bool isTerminal(node v) const
Returns true if and only if v is a terminal.
Definition: BlowupGraph.h:553
void removeBasis(node v)
Removes a basis and cleans up.
Definition: BlowupGraph.h:653
Approximation(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const FullComponentWithExtraStore< T, double > &fullCompStore, const std::minstd_rand &rng, double eps=1e-8)
Initialize everything.
void solve(NodeArray< bool > &isNewTerminal)
Perform the actual approximation algorithm on the LP solution.
void addComponent(NodeArray< bool > &isNewTerminal, const BlowupGraph< T > &blowupGraph, const edge rootEdge)
Add a component of the blowup graph to the final solution (by changing nonterminals to terminals) ...
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:113
A special-purpose blowup graph for gammoid computation: directed, with special source and target...
Definition: BlowupGraph.h:50
TemporaryEdges(BlowupGraph< T > &blowupGraph)
Construct object for a specific blowupGraph.
Definition: Approximation.h:62
Definition of the ogdf::steiner_tree::goemans::BlowupComponents class template.
T getCost(edge e) const
Returns the cost of e.
Definition: BlowupGraph.h:528
int size() const
Return number of components.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
The namespace for all OGDF objects.
Computes a min-cost flow using a network simplex method.
int gammoidGetRank(const BlowupGraph< T > &blowupGraph) const
Computes the rank of the gammoid (given by the blowup graph)
Definition: Approximation.h:81
node getOriginal(node v) const
Returns the original node of v.
Definition: BlowupGraph.h:533
Computes a max flow via Preflow-Push (global relabeling and gap relabeling heuristic).
Class for adjacency list elements.
Definition: Graph_d.h:79
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200
const List< node > & core() const
Return list of core edges (implemented by nodes)
Definition: BlowupGraph.h:765