Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

SteinerTreeLowerBoundDualAscent.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Math.h>
35 #include <ogdf/basic/EpsilonTest.h>
36 #include <ogdf/graphalg/Dijkstra.h>
38 
39 namespace ogdf {
40 namespace steiner_tree {
41 
43 template<typename T>
45  struct TerminalData;
49  };
50 
51  struct TerminalData {
57 
59  : terminal(t), cutIterators(graph, nullptr), inCut(graph, false) {}
60  };
61 
69 
72  auto it = m_terminals.begin();
73  auto bestIt = it;
74  for (; it != m_terminals.end(); ++it) {
75  if ((*it).cut.size() < (*bestIt).cut.size()) {
76  bestIt = it;
77  }
78  }
79 
80  return bestIt;
81  }
82 
86  if (t == m_root) {
87  return false;
88  }
89  TerminalData& td = *it;
90  td.inCut[t] = true;
91  td.references.push({t, m_inTerminalCut[t].pushBack(it)});
92  for (adjEntry adj : t->adjEntries) {
93  node w = adj->twinNode();
94  if (!td.inCut[w]) {
95  // not in cut, so check (recursively) if we should add nodes
96  if (m_eps.equal(m_reducedCost[adj], T(0))) {
97  if (!addNode(it, w)) { // recurse
98  return false;
99  }
100  } else {
101  td.cutIterators[adj] = td.cut.pushBack(adj);
102  }
103  }
104  }
105 
106  // delete arcs that are inside the cut
107  for (adjEntry adj : t->adjEntries) {
108  node w = adj->twinNode();
109  if (td.inCut[w]) {
110  auto& cutAdjIt = td.cutIterators[adj->twin()];
111  if (cutAdjIt != td.cut.end()) {
112  td.cut.del(cutAdjIt);
113  cutAdjIt = nullptr;
114  }
115  }
116  }
117 
118  return true;
119  }
120 
122  if (!(*it).inCut[w]) {
123  if (!addNode(it, w)) {
124  return false;
125  }
126  }
127  return true;
128  }
129 
131  for (auto ref : (*it).references) {
132  m_inTerminalCut[ref.v].del(ref.it);
133  }
134 
135  m_terminals.del(it);
136  }
137 
144  void extendCut(adjEntry adj) {
145  OGDF_ASSERT(m_eps.equal(m_reducedCost[adj], T(0)));
146  node v = adj->theNode();
147  node w = adj->twinNode();
148  for (auto it = m_inTerminalCut[v].begin(); it.valid();) {
149  if (!addNodeChecked(*it, w)) {
150  auto nextIt = it;
151  ++nextIt;
153  it = nextIt;
154  } else {
155  ++it;
156  }
157  }
158  }
159 
162  OGDF_ASSERT(!td.cut.empty());
163  T cost = std::numeric_limits<T>::max();
164  for (adjEntry adj : td.cut) {
165  Math::updateMin(cost, m_reducedCost[adj]);
166  }
167  OGDF_ASSERT(cost > 0);
168  return cost;
169  }
170 
172  void update(TerminalData& td, T delta) {
173  // reduce costs
174  ArrayBuffer<adjEntry> zeroed;
175  for (adjEntry adj : td.cut) {
176  m_reducedCost[adj] -= delta;
177  OGDF_ASSERT(m_eps.geq(m_reducedCost[adj], T(0)));
178  if (m_eps.leq(m_reducedCost[adj], T(0))) {
179  zeroed.push(adj);
180  }
181  }
182  m_lower += delta;
183 
184  // extend
185  for (adjEntry adj : zeroed) {
186  extendCut(adj);
187  }
188  }
189 
190 public:
192  LowerBoundDualAscent(const EdgeWeightedGraph<T>& graph, const List<node>& terminals, node root, double eps = 1e-6)
193  : m_eps(eps),
194  m_lower(0),
195  m_graph(graph),
196  m_root(nullptr),
197  m_reducedCost(m_graph),
198  m_inTerminalCut(m_graph) {
199  for (edge e : graph.edges) {
200  m_reducedCost[e->adjSource()] =
201  m_reducedCost[e->adjTarget()] = graph.weight(e);
202  }
203 
204  for (node t : terminals) {
205  if (t == root) {
206  m_root = root;
207  } else {
208  auto it = m_terminals.pushBack(TerminalData(m_graph, t));
209  if (!addNode(it, t)) {
211  }
212  }
213  }
214  OGDF_ASSERT(m_root != nullptr);
215  }
216 
218  LowerBoundDualAscent(const EdgeWeightedGraph<T>& graph, const List<node>& terminals, double eps = 1e-6)
219  : LowerBoundDualAscent(graph, terminals, terminals.front(), eps) {}
220 
222  void compute() {
223  while (!m_terminals.empty()) {
224  auto it = chooseTerminal();
225  TerminalData& td = *it;
227  }
228  }
229 
232  T reducedCost(adjEntry adj) const {
233  return m_reducedCost[adj];
234  }
235 
237  T get() const {
238  return m_lower;
239  }
240 };
241 }
242 
251 template<typename T>
253  int m_repetitions = 1;
254 
255  T compute(const EdgeWeightedGraph<T>& graph, const List<node>& terminals, node root) {
256  steiner_tree::LowerBoundDualAscent<T> alg(graph, terminals, root);
257  alg.compute();
258  return alg.get();
259  }
260 
261  void compute(const EdgeWeightedGraph<T>& graph, const List<node>& terminals, node root, NodeArray<T>& lbNodes, EdgeArray<T>& lbEdges) {
262  OGDF_ASSERT(isConnected(graph));
263 
264  // compute first
265  steiner_tree::LowerBoundDualAscent<T> alg(graph, terminals, root);
266  alg.compute();
267 
268  // generate the auxiliary network
269  Graph network;
270  NodeArray<node> copy(graph);
271  EdgeArray<T> weights(network);
272  EdgeArray<edge> orig(network);
273 
274  for (node v : graph.nodes) {
275  copy[v] = network.newNode();
276  }
277  for (edge e : graph.edges) {
278  edge uv = network.newEdge(copy[e->source()], copy[e->target()]);
279  edge vu = network.newEdge(copy[e->target()], copy[e->source()]);
280  weights[uv] = alg.reducedCost(e->adjTarget());
281  weights[vu] = alg.reducedCost(e->adjSource());
282  orig[uv] = orig[vu] = e;
283  }
284 
285  // compute shortest path tree on network starting from root
286  Dijkstra<T> sssp;
287  NodeArray<edge> pred;
288  NodeArray<T> distance;
289  sssp.call(network, weights, copy[root], pred, distance, true);
290 
291  // set all lower bounds to global lower bound
292  lbNodes.init(graph, alg.get());
293  lbEdges.init(graph, alg.get());
294  EdgeArray<T> lbArcs(network, alg.get());
295 
296  // add cost of path root -> v
297  for (node v : graph.nodes) {
298  lbNodes[v] += distance[copy[v]];
299  }
300  // add cost of path root -> e
301  for (edge a : network.edges) {
302  lbArcs[a] += distance[a->source()] + weights[a];
303  }
304 
305  // to find the (reduced) distance from v / e to any (non-root) terminal,
306  // hence compute (directed) Voronoi regions
307  network.reverseAllEdges();
308 
309  List<node> nonRootTerminals;
310  for (node t : terminals) {
311  if (t != root) {
312  nonRootTerminals.pushBack(copy[t]);
313  }
314  }
315  sssp.call(network, weights, nonRootTerminals, pred, distance, true);
316 
317  // add cost of path v -> any terminal
318  for (node v : graph.nodes) {
319  lbNodes[v] += distance[copy[v]];
320  }
321  // add cost of path e -> any terminal
322  for (edge a : network.edges) {
323  lbArcs[a] += distance[a->source()]; // the former target is now the source
324 
325  // both lower bounds must be larger than the upper bound, so take the minimum
326  Math::updateMin(lbEdges[orig[a]], lbArcs[a]);
327  }
328  }
329 
330 public:
332  void setRepetitions(int num) {
333  m_repetitions = num;
334  }
335 
337  T call(const EdgeWeightedGraph<T>& graph, const List<node>& terminals) {
338  T lb(0);
339  int i = 0;
340  for (node root : terminals) {
341  Math::updateMax(lb, compute(graph, terminals, root));
342  if (++i >= m_repetitions) {
343  break;
344  }
345  }
346  return lb;
347  }
348 
353  void call(const EdgeWeightedGraph<T>& graph, const List<node>& terminals, NodeArray<T>& lbNodes, EdgeArray<T>& lbEdges) {
354  if (m_repetitions <= 1) {
355  // catch this special case to avoid copying
356  compute(graph, terminals, terminals.front(), lbNodes, lbEdges);
357  } else {
358  lbNodes.init(graph, 0);
359  lbEdges.init(graph, 0);
360  int i = 0;
361  for (node root : terminals) {
362  NodeArray<T> nodes;
363  EdgeArray<T> edges;
364  compute(graph, terminals, root, nodes, edges);
365  for (node v : graph.nodes) {
366  Math::updateMax(lbNodes[v], nodes[v]);
367  }
368  for (edge e : graph.edges) {
369  Math::updateMax(lbEdges[e], edges[e]);
370  }
371  if (++i >= m_repetitions) {
372  break;
373  }
374  }
375  }
376  }
377 };
378 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
T call(const EdgeWeightedGraph< T > &graph, const List< node > &terminals)
Calls the algorithm and returns the lower bound.
std::enable_if< std::is_integral< T >::value, bool >::type leq(const T &x, const T &y) const
Compare if x is LEQ than y for integral types.
Definition: EpsilonTest.h:85
Dijkstra&#39;s single source shortest path algorithm.
Definition: Dijkstra.h:52
Implementation of a dual-ascent-based lower bound heuristic for Steiner tree problems.
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:283
std::enable_if< std::is_integral< T >::value, bool >::type equal(const T &x, const T &y) const
Compare if x is EQUAL to y for integral types.
Definition: EpsilonTest.h:113
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
adjEntry adjSource() const
Returns the corresponding adjacancy entry at source node.
Definition: Graph_d.h:333
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
void removeTerminalData(ListIterator< TerminalData > it)
void call(const Graph &G, const EdgeArray< T > &weight, const List< node > &sources, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false)
Calculates, based on the graph G with corresponding edge costs and source nodes, the shortest paths a...
Definition: Dijkstra.h:61
iterator begin()
Returns an iterator to the first element of the list.
Definition: List.h:352
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
Dynamic arrays indexed with adjacency entries.
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
bool addNode(ListIterator< TerminalData > &it, node t)
Adds a node to the cut and add neighbors recursively.
bool addNodeChecked(ListIterator< TerminalData > &it, node w)
Compare floating point numbers with epsilons and integral numbers with normal compare operators...
LowerBoundDualAscent(const EdgeWeightedGraph< T > &graph, const List< node > &terminals, double eps=1e-6)
Initializes the algorithm (and takes the first terminal as root)
void del(iterator it)
Removes it from the list.
Definition: List.h:1437
T weight(const edge e) const
T compute(const EdgeWeightedGraph< T > &graph, const List< node > &terminals, node root)
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition: Math.h:101
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
Implementation of Dijkstra&#39;s single source shortest path algorithm.
NodeArray< List< ListIterator< TerminalData > > > m_inTerminalCut
Mapping of nodes to the cuts they are in.
Mathematical Helpers.
static void copy(const T &from, T &to)
Definition: Alg.h:61
const_reference front() const
Returns a const reference to the first element.
Definition: List.h:274
void extendCut(adjEntry adj)
Assumes that the reduced cost of arc adj is zeroed and hence updates (in relevant cuts) the set of ar...
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.
iterator end()
Returns an iterator to one-past-last element of the list.
Definition: List.h:370
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class for the representation of edges.
Definition: Graph_d.h:292
adjEntry twin() const
Returns the corresponding adjacency element associated with the same edge.
Definition: Graph_d.h:105
Declaration of class EdgeWeightedGraph.
LowerBoundDualAscent(const EdgeWeightedGraph< T > &graph, const List< node > &terminals, node root, double eps=1e-6)
Initializes the algorithm.
void call(const EdgeWeightedGraph< T > &graph, const List< node > &terminals, NodeArray< T > &lbNodes, EdgeArray< T > &lbEdges)
Compute the lower bounds under the assumption nodes or edges are included in the solution.
void setRepetitions(int num)
Sets the number of repeated calls to the lower bound algorithm (runs with different roots) ...
Class for the representation of nodes.
Definition: Graph_d.h:169
Encapsulates a pointer to a list element.
Definition: List.h:42
void update(TerminalData &td, T delta)
Updates reduced costs and cut data.
void compute(const EdgeWeightedGraph< T > &graph, const List< node > &terminals, node root, NodeArray< T > &lbNodes, EdgeArray< T > &lbEdges)
T reducedCost(adjEntry adj) const
Returns the reduced cost of the arc given by adj.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
The namespace for all OGDF objects.
std::enable_if< std::is_integral< T >::value, bool >::type geq(const T &x, const T &y) const
Compare if x is GEQ to y for integral types.
Definition: EpsilonTest.h:141
T findCheapestCutArcCost(const TerminalData &td) const
Finds the cheapest arc in cut and returns its cost.
void reverseAllEdges()
Reverses all edges in the graph.
node newNode()
Creates a new node and returns it.
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:93
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
bool isConnected(const Graph &G)
Returns true iff G is connected.
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition: Graph_d.h:335
bool empty() const
Returns true iff the list is empty.
Definition: List.h:256
Class for adjacency list elements.
Definition: Graph_d.h:79
ListIterator< TerminalData > chooseTerminal()
Finds the terminal with the smallest cut arc set (of the last iteration)
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
Computes lower bounds for minimum Steiner tree instances.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200