Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

DreyfusWagnerFullComponentGenerator.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/Hashing.h>
39 
40 namespace ogdf {
41 namespace steiner_tree {
42 
44 static void sortTerminals(List<node> &terminals)
45 {
47  terminals.quicksort(idx);
48 }
49 
51 template<typename T>
53 {
57 
59  struct DWMData {
60  T cost;
62  DWMData(T _cost, NodePairs _nodepairs)
63  : cost(_cost)
64  , nodepairs(_nodepairs)
65  {
66  }
68  : cost(numeric_limits<T>::max())
69  , nodepairs()
70  {
71  }
72  };
73 
76 
77  const DWMData dataOf(const List<node> &key) const
78  {
79  OGDF_ASSERT(key.size() > 1);
80  if (key.size() == 2) {
81  NodePairs nodepairs(0, 0, NodePair(key.front(), key.back()));
82  return DWMData(m_distance[key.front()][key.back()], nodepairs);
83  }
84  return m_map.lookup(key)->info(); // copy
85  }
86 
87  T costOf(const List<node> &key) const
88  {
89  OGDF_ASSERT(key.size() > 1);
90  if (key.size() == 2) {
91  return m_distance[key.front()][key.back()];
92  }
93  OGDF_ASSERT(m_map.member(key));
94  return m_map.lookup(key)->info().cost;
95  }
96 
97  void insertSorted(List<node> &list, node v) const
98  {
99  for (ListIterator<node> it = list.begin(); it.valid(); ++it) {
100  OGDF_ASSERT((*it)->index() != v->index());
101  if (v->index() < (*it)->index()) {
102  list.insertBefore(v, it);
103  return;
104  }
105  }
106  list.pushBack(v);
107  }
108 
109 public:
114  : m_G(G)
115  , m_terminals(terminals)
116  , m_distance(distance)
117  , m_map(1 << 22) // we initially allocate 4MB*sizeof(DWMData) for hashing
118  {
119  }
120 
121  void call(int restricted)
122  {
123  SubsetEnumerator<node> terminalSubset(m_terminals);
124  for (terminalSubset.begin(2, restricted-1); terminalSubset.valid(); terminalSubset.next()) {
125  List<node> terminals;
126  terminalSubset.list(terminals);
127  SubsetEnumerator<node> subset(terminals);
128  // populate "split"
129  NodeArray<DWMData> split(m_G);
130  auto computeSplit = [&](node v) {
131  OGDF_ASSERT(!terminalSubset.hasMember(v));
132  DWMData best;
133  for (subset.begin(1, terminals.size()-1); subset.valid(); subset.next()) {
134  List<node> list1, list2;
135  subset.list(list1, list2);
136  insertSorted(list1, v);
137  insertSorted(list2, v);
138  T tmp = costOf(list1) + costOf(list2);
139  if (tmp < best.cost) {
140  best = dataOf(list1);
141  DWMData data = dataOf(list2);
142  best.cost += data.cost;
143  int bsize = best.nodepairs.size();
144  int dsize = data.nodepairs.size();
145  best.nodepairs.grow(dsize);
146  for (int i = 0; i < dsize; ++i) { // copy
147  best.nodepairs[bsize + i] = data.nodepairs[i];
148  }
149  }
150  }
151  split[v] = best;
152  };
153  // compute partial solutions
154  auto computePartialSolutions = [&](node v) {
155  List<node> newTerminals(terminals);
156  insertSorted(newTerminals, v);
157  if (m_map.member(newTerminals)) { // already defined
158  return; // abort
159  }
160  DWMData best;
161  for (node w : m_G.nodes) {
162  T dist = m_distance[v][w];
163  if (v == w) {
164  dist = 0;
165  }
166  if (terminalSubset.hasMember(w)) {
167  // we attach edge vw to tree containing terminal w
168  T tmp;
169  tmp = costOf(terminals) + dist;
170  if (tmp < best.cost) {
171  best = dataOf(terminals);
172  best.cost += dist;
173  best.nodepairs.grow(1, NodePair(v,w));
174  }
175  } else {
176  // we attach edge vw to tree split[w]
177  T tmp;
178  if (split[w].nodepairs.size() == 0) {
179  computeSplit(w);
180  }
181  tmp = split[w].cost + dist;
182  if (tmp < best.cost) {
183  best = split[w];
184  if (v != w) {
185  best.cost += dist;
186  best.nodepairs.grow(1, NodePair(v,w));
187  }
188  }
189  }
190  }
191  m_map.fastInsert(newTerminals, best);
192  };
193  if (terminalSubset.size() != restricted - 1) {
194  for (node v : m_G.nodes) {
195  if (!terminalSubset.hasMember(v)) {
196  computePartialSolutions(v);
197  }
198  }
199  } else { // maximal terminal subset
200  for (node v : m_terminals) { // save time by only adding terminals instead of all nodes
201  if (!terminalSubset.hasMember(v)) {
202  computePartialSolutions(v);
203  }
204  }
205  }
206  }
207  }
208 
211  {
212  T cost(0);
213  DWMData data = dataOf(terminals);
214  tree.createEmpty(m_G);
215  for (auto nodepair : data.nodepairs) {
216  node uO = nodepair.source;
217  node vO = nodepair.target;
218  node uC = tree.copy(uO);
219  node vC = tree.copy(vO);
220  if (uC == nullptr) {
221  uC = tree.newNode(uO);
222  }
223  if (vC == nullptr) {
224  vC = tree.newNode(vO);
225  }
226  const T dist = m_distance[uO][vO];
227  tree.newEdge(uC, vC, dist);
228  cost += dist;
229  }
230  OGDF_ASSERT(isTree(tree));
231  return cost;
232  }
233 };
234 
235 template<typename T>
237 {
238  static const int c_prime = 0x7fffffff; // mersenne prime 2**31 - 1
239  // would be nicer: 0x1fffffffffffffff; // mersenne prime 2**61 - 1
240  const int m_random;
241 
242 public:
244  : m_random(randomNumber(2, c_prime - 1))
245  {
246  }
247 
248  int hash(const List<node> &key) const
249  {
250  ListConstIterator<node> it = key.begin();
251  int hash = (*it)->index();
252  for (; it.valid(); ++it) {
253  hash = (hash * m_random + (*it)->index()) % c_prime;
254  }
255  return hash;
256  }
257 };
258 
259 } // end namespace steiner_tree
260 } // end namespace ogdf
HashElement< K, I > * lookup(const K &key) const
Returns the hash element with key key in the hash table; returns 0 if no such element.
Definition: Hashing.h:296
bool member(const K &key) const
Returns true iff the hash table contains an element with key key.
Definition: Hashing.h:290
A generator for full components (for Steiner tree approximations) based on the Dreyfus-Wagner algorit...
DreyfusWagnerFullComponentGenerator(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< NodeArray< T >> &distance)
The constructor.
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:336
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:413
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
T getSteinerTreeFor(const List< node > &terminals, EdgeWeightedGraphCopy< T > &tree)
Construct a Steiner tree for the given set of terminals.
int randomNumber(int low, int high)
Returns random integer between low and high (including).
edge newEdge(node u, node v, T weight)
iterator begin()
Returns an iterator to the first element of the list.
Definition: List.h:332
static void sortTerminals(List< node > &terminals)
Sort terminals by index.
Declaration of classes used for hashing.
Extends the GraphCopy concept to weighted graphs.
bool valid() const
Is the current subset valid? If not, all subsets have already been enumerated.
void createEmpty(const Graph &wG)
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
void quicksort()
Sorts the list using Quicksort.
Definition: List.h:1097
void begin(int low, int high)
Initialize the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:134
const_reference front() const
Returns a const reference to the first element.
Definition: List.h:254
Enumerator for k-subsets of a given type.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
Hashing< List< node >, DWMData, SortedNodeListHashFunc > m_map
A hash array for keys of size > 2.
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:611
static uint32_t hash(uint32_t x)
Definition: Map.h:38
Class for the representation of nodes.
Definition: Graph_d.h:167
Encapsulates a pointer to a list element.
Definition: List.h:41
const NodeArray< NodeArray< T > > & m_distance
A reference to the full distance matrix.
The IndexComparer compares entities like nodes, edges, adjacency entries, based on their index...
Definition: IndexComparer.h:44
bool hasMember(const T &element) const
Check in O(subset cardinality) whether the argument is a member of the subset.
int index() const
Returns the (unique) node index.
Definition: Graph_d.h:201
HashElement< K, I > * fastInsert(const K &key, const I &info)
Inserts a new element with key key and information info into the hash table.
Definition: Hashing.h:353
void list(List< T > &list) const
Obtain (append) a list of the elements in the subset.
const EdgeWeightedGraph< T > & m_G
A reference to the graph instance.
void next()
Obtain the next subset if possible. The result should be checked using the valid() method...
IndexComparer definition.
int size() const
Returns the number of elements in the list.
Definition: List.h:1255
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
iterator insertBefore(const E &x, iterator it)
Inserts element x before it.
Definition: List.h:1335
Hashing with chaining and table doubling.
Definition: Hashing.h:264
int size() const
Return the cardinality of the subset.
The namespace for all OGDF objects.
const List< node > & m_terminals
A reference to the index-sorted list of terminals.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
A class that allows to enumerate k-subsets of lists.
bool valid() const
Returns true iff the iterator points to an element.
Definition: List.h:112
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
const_reference back() const
Returns a const reference to the last element.
Definition: List.h:272