Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

HeavyPathDecomposition.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <vector>
35 #include <ogdf/basic/Math.h>
37 
38 namespace ogdf {
39 namespace steiner_tree {
40 
45 template<typename T>
47 private:
51 
52  std::vector<std::vector<node>> chains;
53  std::vector<std::vector<node>> chainsOfTerminals;
60  std::vector<node> fatherOfChain;
61 
62  std::vector<std::vector<T>> longestDistToSteinerAncestorOnChain;
63  std::vector<std::vector<T>> longestDistToSteinerAncestorSegTree;
64 
69  void buildMaxSegmentTree(std::vector<T> &segmentTree, const int nodeIndex, const int left, const int right, const std::vector<T> &baseArray) const
70  {
71  if (left == right) { // leaf
72  segmentTree[nodeIndex] = baseArray[left];
73  return;
74  }
75 
76  int middle = (left+right)>>1;
77  int leftNodeIndex = nodeIndex+nodeIndex+1;
78  int rightNodeIndex = leftNodeIndex + 1;
79 
80  buildMaxSegmentTree(segmentTree, leftNodeIndex, left, middle, baseArray);
81  buildMaxSegmentTree(segmentTree, rightNodeIndex, middle+1, right, baseArray);
82 
83  segmentTree[nodeIndex] = max(segmentTree[leftNodeIndex], segmentTree[rightNodeIndex]);
84  }
89  T getMaxSegmentTree(const std::vector<T> &segmentTree, const int nodeIndex, const int left, const int right, const int queryLeft, const int queryRight) const
90  {
91  if (queryLeft > queryRight
92  || left > queryRight
93  || queryLeft > right) {
94  return 0;
95  }
96 
97  if (queryLeft <= left
98  && right <= queryRight) { // included
99  return segmentTree[nodeIndex];
100  }
101 
102  int middleIndex = (left + right)>>1;
103  int leftNodeIndex = nodeIndex+nodeIndex+1;
104  int rightNodeIndex = leftNodeIndex + 1;
105 
106  T maxValue(0);
107  if (queryLeft <= middleIndex) {
108  Math::updateMax(maxValue, getMaxSegmentTree(segmentTree, leftNodeIndex, left, middleIndex, queryLeft, queryRight));
109  }
110  if (queryRight > middleIndex) {
111  Math::updateMax(maxValue, getMaxSegmentTree(segmentTree, rightNodeIndex, middleIndex+1, right, queryLeft, queryRight));
112  }
113 
114  return maxValue;
115  }
116 
121  T distanceToAncestor(node v, node ancestor) const
122  {
123  if (ancestor == nullptr) {
124  return distanceToRoot[v];
125  }
126  return distanceToRoot[v] - distanceToRoot[ancestor];
127  }
128 
135  {
136  longestDistToSteinerAncestorOnChain.resize((int)chains.size());
137  for (int i = 0; i < (int)chains.size(); ++i) {
138  longestDistToSteinerAncestorOnChain[i].resize((int)chains[i].size());
139  for (int j = 0; j < (int)chains[i].size(); ++j) {
140  longestDistToSteinerAncestorOnChain[i][j] = distanceToAncestor(chains[i][j], closestSteinerAncestor[chains[i][j]]);
141  if (j > 0) {
142  Math::updateMax(longestDistToSteinerAncestorOnChain[i][j], longestDistToSteinerAncestorOnChain[i][j-1]);
143  }
144  }
145  }
146  }
147 
154  {
155  longestDistToSteinerAncestorSegTree.resize((int)chains.size());
156  for (int i = 0; i < (int)chains.size(); ++i) {
157  longestDistToSteinerAncestorSegTree[i].resize(4*(int)chains[i].size());
158 
159  std::vector<T> distanceToSteinerAncestor_byPosition;
160  distanceToSteinerAncestor_byPosition.resize(chains[i].size());
161  for (int j = 0; j < (int)chains[i].size(); ++j) {
162  distanceToSteinerAncestor_byPosition[j] = distanceToAncestor(chains[i][j], closestSteinerAncestor[chains[i][j]]);
163  }
164 
165  buildMaxSegmentTree(longestDistToSteinerAncestorSegTree[i], 0, 0, (int)chains[i].size()-1, distanceToSteinerAncestor_byPosition);
166  }
167  }
168 
173  void dfsHeavyPathDecomposition(node v, node closestSteinerUpNode)
174  {
175  weightOfSubtree[v] = 1;
176  node heaviestSon = nullptr;
177  closestSteinerAncestor[v] = closestSteinerUpNode;
178 
179  for(adjEntry adj : v->adjEntries) {
180  edge e = adj->theEdge();
181  node son = adj->twinNode();
182 
183  if (weightOfSubtree[son] != 0) { // the parent
184  continue;
185  }
186 
187  nodeLevel[son] = nodeLevel[v] + 1;
188  distanceToRoot[son] = distanceToRoot[v] + tree.weight(e);
189 
191 
192  fatherOfChain[chainOfNode[son]] = v;
193  weightOfSubtree[v] += weightOfSubtree[son];
194  if (heaviestSon == nullptr
195  || weightOfSubtree[heaviestSon] < weightOfSubtree[son]) {
196  heaviestSon = son;
197  }
198  }
199 
200  if (heaviestSon == nullptr) { // it's leaf => new chain
201  OGDF_ASSERT((v->degree() <= 1));
202 
203  std::vector<node>new_chain;
204  chains.push_back(new_chain);
205  chainsOfTerminals.push_back(new_chain);
206 
207  fatherOfChain.push_back(nullptr);
208  chainOfNode[v] = (int)chains.size()-1;
209  } else {
210  chainOfNode[v] = chainOfNode[heaviestSon];
211  }
212 
213  chains[chainOfNode[v]].push_back(v);
214  chainsOfTerminals[chainOfNode[v]].push_back(v);
215  positionOnChain[v] = (int)chains[chainOfNode[v]].size()-1;
216  }
217 
223  node binarySearchUpmostTerminal(node v, const std::vector<node> &chainOfTerminals) const
224  {
225  int left = 0, middle, right = (int)chainOfTerminals.size()-1;
226  while (left <= right) {
227  middle = (left+right)>>1;
228 
229  if (nodeLevel[chainOfTerminals[middle]] >= nodeLevel[v]) {
230  right = middle-1;
231  } else {
232  left = middle+1;
233  }
234  }
235 
236  if (left == (int)chainOfTerminals.size()) {
237  return nullptr;
238  }
239  return chainOfTerminals[left];
240  }
241 
251  void computeBottleneckOnBranch(node x, node ancestor, T &longestPathDistance, T &fromLowestToAncestor) const
252  {
253  node upmostTerminal = x;
254  while (closestSteinerAncestor[chains[chainOfNode[x]][0]] != nullptr
255  && nodeLevel[closestSteinerAncestor[chains[chainOfNode[x]][0]]] >= nodeLevel[ancestor]) {
256  Math::updateMax(longestPathDistance, longestDistToSteinerAncestorOnChain[chainOfNode[x]][positionOnChain[x]]);
257 
258  if (!chainsOfTerminals[chainOfNode[x]].empty()
259  && nodeLevel[chainsOfTerminals[chainOfNode[x]][0]] <= nodeLevel[x]) {
260  upmostTerminal = chainsOfTerminals[chainOfNode[x]][0];
261  }
262  x = fatherOfChain[chainOfNode[x]];
263  }
264 
265  // search the upmost terminal on the current chain that has level >= level[ancestor]
266  node upmostTerminalLastChain = binarySearchUpmostTerminal(ancestor, chainsOfTerminals[chainOfNode[x]]);
267  if (nodeLevel[upmostTerminalLastChain] > nodeLevel[x]) {
268  upmostTerminalLastChain = nullptr;
269  }
270  if (upmostTerminalLastChain != nullptr) {
271  upmostTerminal = upmostTerminalLastChain;
272  }
273 
274  if (upmostTerminalLastChain != nullptr) {
275  Math::updateMax(longestPathDistance,
276  getMaxSegmentTree(longestDistToSteinerAncestorSegTree[chainOfNode[x]], 0, 0,
277  static_cast<int>(chains[chainOfNode[x]].size()) - 1,
278  positionOnChain[upmostTerminalLastChain] + 1,
279  positionOnChain[x]));
280  }
281 
282  fromLowestToAncestor = distanceToAncestor(upmostTerminal, ancestor);
283  }
284 
285 public:
287  : tree{treeEWGraphCopy},
288  chainOfNode{tree, -1},
289  positionOnChain{tree, -1},
290  weightOfSubtree{tree, 0},
291  nodeLevel{tree, 0},
292  distanceToRoot{tree, 0},
293  closestSteinerAncestor{tree, nullptr} {
294  OGDF_ASSERT(!tree.empty());
295  node root = tree.firstNode();
296 
297  dfsHeavyPathDecomposition(root, nullptr);
298  fatherOfChain[chainOfNode[root]] = nullptr;
299 
300  // reverse the obtained chains
301  int numberOfChains = static_cast<int>(chains.size());
302  for (int i = 0; i < numberOfChains; ++i) {
303  reverse(chains[i].begin(), chains[i].end());
304  reverse(chainsOfTerminals[i].begin(), chainsOfTerminals[i].end());
305  for (node v : chains[i]) {
306  positionOnChain[v] = (int)chains[i].size()-1-positionOnChain[v];
307  }
308  }
309 
312  }
313 
319  {
320  while (chainOfNode[x] != chainOfNode[y]) {
321  int xlevelOfFatherOfChain = fatherOfChain[chainOfNode[x]] != nullptr ? nodeLevel[fatherOfChain[chainOfNode[x]]] : -1;
322  int ylevelOfFatherOfChain = fatherOfChain[chainOfNode[y]] != nullptr ? nodeLevel[fatherOfChain[chainOfNode[y]]] : -1;
323 
324  if (xlevelOfFatherOfChain >= ylevelOfFatherOfChain) {
325  x = fatherOfChain[chainOfNode[x]];
326  } else {
327  y = fatherOfChain[chainOfNode[y]];
328  }
329  }
330 
331  if (nodeLevel[x] <= nodeLevel[y]) {
332  return x;
333  }
334  return y;
335  }
336 
342  {
343  T xLongestPathDistance = 0, yLongestPathDistance = 0;
344  T xFromLowestToLCA = 0, yFromLowestToLCA = 0;
345  node xyLowestCommonAncestor = lowestCommonAncestor(x, y);
346 
347  computeBottleneckOnBranch(x, xyLowestCommonAncestor, xLongestPathDistance, xFromLowestToLCA);
348  computeBottleneckOnBranch(y, xyLowestCommonAncestor, yLongestPathDistance, yFromLowestToLCA);
349 
350  T maxValue = max(xLongestPathDistance, yLongestPathDistance);
351  Math::updateMax(maxValue, xFromLowestToLCA+yFromLowestToLCA);
352 
353  return maxValue;
354  }
355 };
356 
357 }
358 }
void computeLongestDistToSteinerAncestorOnChain()
creates for every chain an array with the maximum of every prefix of a fictive array which keeps for ...
std::vector< std::vector< node > > chainsOfTerminals
list of chains only of terminals corresponding to the decomposition
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
void buildMaxSegmentTree(std::vector< T > &segmentTree, const int nodeIndex, const int left, const int right, const std::vector< T > &baseArray) const
builds a max segment tree on the baseArray
NodeArray< node > closestSteinerAncestor
the highest-level Steiner ancestor of the current node
NodeArray< int > chainOfNode
the index of a node&#39;s chain
std::vector< std::vector< T > > longestDistToSteinerAncestorSegTree
segment tree for segment maxs on every chain
int degree() const
Returns the degree of the node (indegree + outdegree).
Definition: Graph_d.h:210
NodeArray< int > positionOnChain
position of a node on his chain
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:618
Extends the GraphCopy concept to weighted graphs.
HeavyPathDecomposition(const EdgeWeightedGraphCopy< T > &treeEWGraphCopy)
std::vector< std::vector< T > > longestDistToSteinerAncestorOnChain
the max of the interval 0..i for every i on chains
NodeArray< int > nodeLevel
the level of a node in the tree
T distanceToAncestor(node v, node ancestor) const
computes the sum of all edges on the path from v to ancestor v must be in ancestor&#39;s subtree ...
An implementation of the heavy path decomposition on trees.
List< node > terminals
list of terminals of the desc tree
Mathematical Helpers.
std::vector< std::vector< node > > chains
list of chains of nodes corresponding to the decomposition
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:282
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
T getBottleneckSteinerDistance(node x, node y) const
computes in the bottleneck distance between terminals x and y
T getMaxSegmentTree(const std::vector< T > &segmentTree, const int nodeIndex, const int left, const int right, const int queryLeft, const int queryRight) const
extracts the maximum on the required interval
NodeArray< T > distanceToRoot
the length of the edge to his father
node binarySearchUpmostTerminal(node v, const std::vector< node > &chainOfTerminals) const
performs a binary search on chainOfTerminals, searches for the closest node to the root on chainOfTer...
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
Class for the representation of nodes.
Definition: Graph_d.h:169
NodeArray< int > weightOfSubtree
weight of the subtree rooted in one node
Container::value_type maxValue(const Container &values)
Returns the maximum of an iterable container of given values.
Definition: Math.h:238
const EdgeWeightedGraphCopy< T > & tree
constant ref to the tree to be decomposed
The namespace for all OGDF objects.
const NodeArray< bool > isTerminal
isTerminal of the desc tree
std::vector< node > fatherOfChain
the first node from bottom up that does not belong to the chain
void computeBottleneckOnBranch(node x, node ancestor, T &longestPathDistance, T &fromLowestToAncestor) const
computes for the path from x to ancestor the maximum distance (sum of edges) between any two consecut...
void computeLongestDistToSteinerAncestorSegTree()
creates for every chain a segment tree built on a fictive array which keeps for every node the sum of...
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:93
void dfsHeavyPathDecomposition(node v, node closestSteinerUpNode)
performs the heavy path decomposition on the tree belonging to the class updates the fields of the cl...
Reverse< T > reverse(T &container)
Provides iterators for container to make it easily iterable in reverse.
Definition: Reverse.h:75
node lowestCommonAncestor(node x, node y) const
computes the lowest common ancestor of nodes x and y using the hpd
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition: Graph_d.h:595
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