Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

FullComponentStore.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/NodeArray.h>
39 
40 namespace ogdf {
41 namespace steiner_tree {
42 
43 #define OGDF_FULLCOMPONENTSTORE_REMOVE_IN_GRAPH_REPRESENTATION_ALSO // unnecessary but may save memory
44 
48 template<typename T, typename ExtraDataType = void>
50 {
51 protected:
58  m_nodeOrig;
59 
60  template<class Y, class Enable = void> // Metadata without extra data
61  struct Metadata {
64  T cost;
66  : start(nullptr)
67  , terminals(0)
68  , cost(0)
69  {
70  }
71  };
72  template<class Y> // Metadata with extra data
73  struct Metadata<Y, typename std::enable_if<!std::is_void<Y>::value>::type> {
76  T cost;
77  Y extra;
79  : start(nullptr)
80  , terminals(0)
81  , cost(0)
82  {
83  }
84  };
86 
87 public:
89  : m_originalGraph(G)
90  , m_terminals(terminals)
91  , m_isTerminal(isTerminal)
92  , m_nodeCopy(G, nullptr)
93  , m_nodeOrig(m_graph)
94  {
95  for (node v : m_terminals) {
96  node u = m_graph.newNode();
97  m_nodeCopy[v] = u;
98  m_nodeOrig[u] = v;
99  }
100  }
101 
103  {
104  OGDF_ASSERT(!comp.empty());
105  OGDF_ASSERT(isTree(comp));
106 
107  // we temporarily use m_nodeCopy for nonterminals also
108  ArrayBuffer<node> tempUse(comp.numberOfNodes() / 2);
109 
110  // add all nonterminals of comp to m_graph
111  // and find terminals
112  Metadata<ExtraDataType> data;
113  for (node v : comp.nodes) {
114  node vO = comp.original(v);
115  if (m_nodeCopy[vO] == nullptr) {
116  node vC = m_graph.newNode();
117  m_nodeCopy[vO] = vC;
118  m_nodeOrig[vC] = vO;
119  tempUse.push(vO);
120  } else {
121  data.terminals.grow(1, vO);
122  }
123  }
125  data.terminals.quicksort(idx);
126 
127  // add all edges of comp to m_graph
128  // and find start adjEntry
129  for (edge e : comp.edges) {
130  node uO = comp.original(e->source());
131  node vO = comp.original(e->target());
132  T weight = comp.weight(e);
133  edge eC = m_graph.newEdge(m_nodeCopy[uO], m_nodeCopy[vO], weight);
134  data.cost += weight;
135  if (m_isTerminal[uO]) {
136  data.start = eC->adjSource();
137  } else
138  if (m_isTerminal[vO]) {
139  data.start = eC->adjTarget();
140  }
141  }
142 
143  // cleanup m_nodeCopy
144  for (node vO : tempUse) {
145  m_nodeCopy[vO] = nullptr;
146  }
147 
148  m_components.push(data);
149  }
150 
151  void remove(int id)
152  {
153 #ifdef OGDF_FULLCOMPONENTSTORE_REMOVE_IN_GRAPH_REPRESENTATION_ALSO
154  // TODO: remove in graph also
155  auto &comp = m_components[id];
156  if (comp.terminals.size() == 2) {
157  m_graph.delEdge(comp.start->theEdge());
158  } else {
159  BoundedStack<node> stack(2 * comp.terminals.size() - 3);
160  stack.push(comp.start->twinNode());
161  m_graph.delEdge(comp.start->theEdge());
162  while (!stack.empty()) {
163  const node v = stack.pop();
164  if (!isTerminal(v)) {
165  for (adjEntry adj : v->adjEntries) {
166  stack.push(adj->twinNode());
167  }
168  m_graph.delNode(v);
169  }
170  }
171  }
172 #endif
173  if (m_components.size() == id + 1) {
174  m_components.pop();
175  } else {
176  m_components[id] = m_components.popRet();
177  }
178  }
179 
181  int size() const
182  {
183  return m_components.size();
184  }
185 
187  bool isEmpty() const
188  {
189  return m_components.empty();
190  }
191 
193  const Array<node> &terminals(int id) const
194  {
195  OGDF_ASSERT(id >= 0);
196  OGDF_ASSERT(id < m_components.size());
197  return m_components[id].terminals;
198  }
199 
201  bool isTerminal(int id, node t) const
202  {
203  OGDF_ASSERT(id >= 0);
204  OGDF_ASSERT(id < m_components.size());
205  return m_components[id].terminals.linearSearch(t) != -1;
206  }
207 
208  bool isTerminal(node v) const
209  {
210  return m_isTerminal[m_nodeOrig[v]];
211  }
212 
214  T cost(int i) const
215  {
216  OGDF_ASSERT(i >= 0);
217  OGDF_ASSERT(i < m_components.size());
218  return m_components[i].cost;
219  }
220 
221  adjEntry start(int i) const
222  {
223  OGDF_ASSERT(i >= 0);
224  OGDF_ASSERT(i < m_components.size());
225  return m_components[i].start;
226  }
227 
229  {
230  return m_graph;
231  }
232 
233  node original(node v) const
234  {
235  OGDF_ASSERT(m_nodeOrig[v] != nullptr);
236  return m_nodeOrig[v];
237  }
238 
239  template<typename Fun>
240  void foreachAdjEntry(int i, Fun f) const
241  {
242  adjEntry start = m_components[i].start;
243  int size = m_components[i].terminals.size();
244  if (size == 2) {
245  f(start->twin());
246  return;
247  }
248  // size >= 3: do DFS over nonterminals (terminals are only leaves)
249  BoundedStack<adjEntry> stack(2*size - 2);
250  stack.push(start);
251  while (!stack.empty()) {
252  const adjEntry back = stack.pop()->twin();
253  f(back);
254  if (!this->isTerminal(back->theNode())) {
255  for (adjEntry adj = back->cyclicSucc(); adj != back; adj = adj->cyclicSucc()) {
256  stack.push(adj);
257  }
258  }
259  }
260  }
261 
262  // \brief Do f(v) for each (original) node v of degree at least 3 in component with given id
263  template<typename Fun>
264  void foreachNode(int id, Fun f) const
265  {
266  f(original(start(id)->theNode()));
267  foreachAdjEntry(id, [&](adjEntry back) {
268  f(original(back->theNode()));
269  });
270  }
271 
272  // \brief Do f(e) for each (original) edge e in component with given id
273  template<typename Fun>
274  void foreachEdge(int id, const NodeArray<NodeArray<edge>> &pred, Fun f) const
275  {
276  foreachAdjEntry(id, [&](adjEntry back) {
277  const node u = original(back->twinNode());
278  for (node v = original(back->theNode()); pred[u][v]; v = pred[u][v]->opposite(v)) {
279  f(pred[u][v]);
280  }
281  });
282  }
283 
284  // \brief Do f(v) for each node v (also of degree 2) in component with given id
285  template<typename Fun>
286  void foreachNode(int id, const NodeArray<NodeArray<edge>> &pred, Fun f) const
287  {
288  if (m_components[id].terminals.size() == 3) {
289  // use a variant that works when only pred[t] has been filled for all terminals t
290  adjEntry start = m_components[id].start;
291  const node c = start->twinNode();
292  f(original(c));
293  for (adjEntry adj : c->adjEntries) {
294  const node u = original(adj->twinNode());
295  node v = original(c);
296  while (v != u) {
297  v = pred[u][v]->opposite(v);
298  f(v);
299  }
300  }
301  return;
302  }
303  f(original(start(id)->theNode()));
304  foreachAdjEntry(id, [&](adjEntry back) {
305  const node u = original(back->twinNode());
306  for (node v = original(back->theNode()); pred[u][v]; v = pred[u][v]->opposite(v)) {
307  f(v);
308  }
309  });
310  }
311 };
312 
316 template<typename T, typename ExtraDataType>
317 class FullComponentWithExtraStore : public FullComponentStore<T, ExtraDataType>
318 {
319 public:
321 
323  ExtraDataType &extra(int i)
324  {
325  OGDF_ASSERT(i >= 0);
326  OGDF_ASSERT(i < this->m_components.size());
327  return this->m_components[i].extra;
328  }
329 
331  const ExtraDataType &extra(int i) const
332  {
333  OGDF_ASSERT(i >= 0);
334  OGDF_ASSERT(i < this->m_components.size());
335  return this->m_components[i].extra;
336  }
337 };
338 
339 template<typename T>
340 struct LossMetadata {
341  T loss;
344  : loss(0)
345  , bridges()
346  {
347  }
348 };
349 
353 template<typename T>
354 class FullComponentWithLossStore : public FullComponentWithExtraStore<T, LossMetadata<T>>
355 {
356 protected:
358 
366  {
367  if (!m_lossTerminal[u]
368  && pred[u]) {
369  m_lossTerminal[u] = findLossTerminal(pred[u]->opposite(u), pred);
370  }
371 
372  return m_lossTerminal[u];
373  }
374 
375 public:
377 
380  {
381  m_lossTerminal.init(this->m_graph, nullptr);
382 
383  // add zero-cost edges between all terminals (to be removed later),
384  // and set m_lossTerminal mapping for terminals
385  List<edge> zeroEdges;
386  const node s = this->m_terminals.front();
387  const node sC = this->m_nodeCopy[s];
388  m_lossTerminal[sC] = s;
389  for (ListConstIterator<node> it = this->m_terminals.begin().succ(); it.valid(); ++it) {
390  const node v = *it;
391  const node vC = this->m_nodeCopy[v];
392  m_lossTerminal[vC] = v;
393  zeroEdges.pushBack(this->m_graph.newEdge(sC, vC, 0));
394  }
395 
396  // compute minimum spanning tree
397  NodeArray<edge> pred(this->m_graph);
398  EdgeArray<bool> isLossEdge(this->m_graph, false);
399  computeMinST(sC, this->m_graph, this->m_graph.edgeWeights(), pred, isLossEdge);
400 
401  // remove zero-cost edges again
402  for (edge e : zeroEdges) {
403  this->m_graph.delEdge(e);
404  }
405 
406  // find loss bridges and compute loss value
407  for (int id = 0; id < this->size(); ++id) {
408  this->foreachAdjEntry(id, [&](adjEntry adj) {
409  edge e = adj->theEdge();
410  if (!isLossEdge[e]) {
411  this->extra(id).bridges.pushBack(e);
412  findLossTerminal(e->source(), pred);
413  findLossTerminal(e->target(), pred);
414  } else {
415  this->extra(id).loss += this->m_graph.weight(e);
416  }
417  });
418  }
419  }
420 
422  T loss(int id) const
423  {
424  return this->extra(id).loss;
425  }
426 
428  const List<edge> &lossBridges(int id) const
429  {
430  return this->extra(id).bridges;
431  }
432 
440  {
441  OGDF_ASSERT(m_lossTerminal.valid());
442  return m_lossTerminal[v];
443  }
444 };
445 
446 } // end namespace steiner_tree
447 } // end namespace ogdf
List< edge > bridges
List of non-loss edges.
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
ArrayBuffer< Metadata< ExtraDataType > > m_components
List of full components (based on metadata)
void foreachEdge(int id, const NodeArray< NodeArray< edge >> &pred, Fun f) const
INDEX linearSearch(const E &x) const
Performs a linear search for element x.
Definition: ArrayBuffer.h:259
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:95
void pop()
Removes the newest element from the buffer.
Definition: ArrayBuffer.h:106
const List< edge > & lossBridges(int id) const
Returns a list of non-loss edges (that are bridges between the Loss components) of full component wit...
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
virtual void delNode(node v)
Removes node v and all incident edges from the graph.
adjEntry adjSource() const
Returns the corresponding adjacancy entry at source node.
Definition: Graph_d.h:330
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim&#39;s algorithm.
NodeArray< node > m_nodeOrig
Mapping of m_graph nodes to original nodes.
const Array< node > & terminals(int id) const
Returns the list of terminals in the full component with given id.
virtual void delEdge(edge e)
Removes edge e from the graph.
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:95
Array< node > terminals
Terminals, sorted by node index.
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:44
Extends the GraphCopy concept to weighted graphs.
const List< node > & m_terminals
The terminal list of the original Steiner instance.
Declaration and implementation of bounded stack class.
void quicksort()
Sorts buffer using Quicksort.
Definition: ArrayBuffer.h:281
node lossTerminal(node v) const
Returns the terminal (in the original graph) that belongs to a given node v (in the store) according ...
void computeAllLosses()
Compute the loss, both edge set and value, of all full components.
adjEntry cyclicSucc() const
Returns the cyclic successor in the adjacency list.
Definition: Graph_d.h:278
bool isTerminal(int id, node t) const
checks if a given node t is a terminal in the full component with given id
NodeArray< node > m_nodeCopy
Mapping of original terminals to m_graph nodes.
EdgeWeightedGraph< T > m_graph
Our graph representation for the full component store.
edge newEdge(node v, node w, T weight)
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:548
const ExtraDataType & extra(int i) const
Returns a const reference to the extra data of this full component.
adjEntry start
Adjacency entry on a terminal where a non-terminal BFS yields the component.
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:111
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:134
A data structure to store full components.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
static const int Y
Definition: GmlParser.h:66
The parameterized class BoundedStack implements stacks with bounded size.
Definition: BoundedStack.h:46
void insert(const EdgeWeightedGraphCopy< T > &comp)
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:611
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:105
Class for the representation of edges.
Definition: Graph_d.h:291
adjEntry twin() const
Returns the corresponding adjacency element associated with the same edge.
Definition: Graph_d.h:102
A data structure to store full components with additional "loss" functionality.
node findLossTerminal(const node u, const NodeArray< edge > &pred)
Starting from a Steiner node find the nearest terminal along a shortest path.
adjEntry start
Adjacency entry on a terminal where a non-terminal BFS yields the component.
Class for the representation of nodes.
Definition: Graph_d.h:167
bool valid() const
Returns true iff the array is associated with a graph.
Definition: NodeArray.h:157
Declaration and implementation of NodeArray class.
Encapsulates a pointer to a list element.
Definition: List.h:41
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:117
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:287
void foreachNode(int id, const NodeArray< NodeArray< edge >> &pred, Fun f) const
The IndexComparer compares entities like nodes, edges, adjacency entries, based on their index...
Definition: IndexComparer.h:44
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:108
T loss
The loss of a component.
ExtraDataType & extra(int i)
Returns a reference to the extra data of this full component.
IndexComparer definition.
bool isEmpty() const
Checks if the store does not contain any full components.
E pop()
Removes the top-most element from the stack and returns it.
Definition: BoundedStack.h:168
NodeArray< node > m_lossTerminal
Indicates which Steiner node is connected to which terminal through the loss edges, indexed by the Steiner node.
FullComponentStore(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal)
T loss(int id) const
Returns the loss value of full component with given id.
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
const EdgeWeightedGraph< T > & m_originalGraph
The original Steiner instance.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
const NodeArray< bool > & m_isTerminal
Incidence vector for terminal nodes.
const EdgeWeightedGraph< T > & graph() const
The namespace for all OGDF objects.
T cost(int i) const
Returns the sum of edge costs of this full component.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:578
int size() const
Returns the number of full components in the store.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
A data structure to store full components with extra data for each component.
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition: Graph_d.h:332
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition: Graph_d.h:575
node theNode() const
Returns the node whose adjacency list contains this element.
Definition: Graph_d.h:99
Class for adjacency list elements.
Definition: Graph_d.h:78
bool empty()
Returns true iff the stack is empty.
Definition: BoundedStack.h:109
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
void push(const E &x)
Adds element x as top-most element to the stack.
Definition: BoundedStack.h:162
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:198