39 namespace steiner_tree {
52 std::vector<std::vector<node>>
chains;
69 void buildMaxSegmentTree(std::vector<T> &segmentTree,
const int nodeIndex,
const int left,
const int right,
const std::vector<T> &baseArray)
const 72 segmentTree[nodeIndex] = baseArray[left];
76 int middle = (left+right)>>1;
77 int leftNodeIndex = nodeIndex+nodeIndex+1;
78 int rightNodeIndex = leftNodeIndex + 1;
83 segmentTree[nodeIndex] = max(segmentTree[leftNodeIndex], segmentTree[rightNodeIndex]);
89 T
getMaxSegmentTree(
const std::vector<T> &segmentTree,
const int nodeIndex,
const int left,
const int right,
const int queryLeft,
const int queryRight)
const 91 if (queryLeft > queryRight
93 || queryLeft > right) {
98 && right <= queryRight) {
99 return segmentTree[nodeIndex];
102 int middleIndex = (left + right)>>1;
103 int leftNodeIndex = nodeIndex+nodeIndex+1;
104 int rightNodeIndex = leftNodeIndex + 1;
107 if (queryLeft <= middleIndex) {
110 if (queryRight > middleIndex) {
123 if (ancestor ==
nullptr) {
124 return distanceToRoot[v];
126 return distanceToRoot[v] - distanceToRoot[ancestor];
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]]);
142 Math::updateMax(longestDistToSteinerAncestorOnChain[i][j], longestDistToSteinerAncestorOnChain[i][j-1]);
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());
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]]);
165 buildMaxSegmentTree(longestDistToSteinerAncestorSegTree[i], 0, 0, (
int)chains[i].size()-1, distanceToSteinerAncestor_byPosition);
175 weightOfSubtree[v] = 1;
176 node heaviestSon =
nullptr;
177 closestSteinerAncestor[v] = closestSteinerUpNode;
183 if (weightOfSubtree[son] != 0) {
187 nodeLevel[son] = nodeLevel[v] + 1;
188 distanceToRoot[son] = distanceToRoot[v] + tree.
weight(e);
192 fatherOfChain[chainOfNode[son]] = v;
193 weightOfSubtree[v] += weightOfSubtree[son];
194 if (heaviestSon ==
nullptr 195 || weightOfSubtree[heaviestSon] < weightOfSubtree[son]) {
200 if (heaviestSon ==
nullptr) {
203 std::vector<node>new_chain;
204 chains.push_back(new_chain);
205 chainsOfTerminals.push_back(new_chain);
207 fatherOfChain.push_back(
nullptr);
208 chainOfNode[v] = (int)chains.
size()-1;
210 chainOfNode[v] = chainOfNode[heaviestSon];
213 chains[chainOfNode[v]].push_back(v);
214 chainsOfTerminals[chainOfNode[v]].push_back(v);
215 positionOnChain[v] = (int)chains[chainOfNode[v]].size()-1;
225 int left = 0, middle, right = (int)chainOfTerminals.size()-1;
226 while (left <= right) {
227 middle = (left+right)>>1;
229 if (nodeLevel[chainOfTerminals[middle]] >= nodeLevel[v]) {
236 if (left == (
int)chainOfTerminals.size()) {
239 return chainOfTerminals[left];
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]]);
258 if (!chainsOfTerminals[chainOfNode[x]].empty()
259 && nodeLevel[chainsOfTerminals[chainOfNode[x]][0]] <= nodeLevel[x]) {
260 upmostTerminal = chainsOfTerminals[chainOfNode[x]][0];
262 x = fatherOfChain[chainOfNode[x]];
267 if (nodeLevel[upmostTerminalLastChain] > nodeLevel[x]) {
268 upmostTerminalLastChain =
nullptr;
270 if (upmostTerminalLastChain !=
nullptr) {
271 upmostTerminal = upmostTerminalLastChain;
274 if (upmostTerminalLastChain !=
nullptr) {
277 static_cast<int>(chains[chainOfNode[x]].size()) - 1,
278 positionOnChain[upmostTerminalLastChain] + 1,
279 positionOnChain[x]));
287 : tree{treeEWGraphCopy},
288 chainOfNode{
tree, -1},
289 positionOnChain{
tree, -1},
290 weightOfSubtree{
tree, 0},
292 distanceToRoot{
tree, 0},
293 closestSteinerAncestor{
tree,
nullptr} {
298 fatherOfChain[chainOfNode[root]] =
nullptr;
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];
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;
324 if (xlevelOfFatherOfChain >= ylevelOfFatherOfChain) {
325 x = fatherOfChain[chainOfNode[x]];
327 y = fatherOfChain[chainOfNode[y]];
331 if (nodeLevel[x] <= nodeLevel[y]) {
343 T xLongestPathDistance = 0, yLongestPathDistance = 0;
344 T xFromLowestToLCA = 0, yFromLowestToLCA = 0;
350 T
maxValue = max(xLongestPathDistance, yLongestPathDistance);
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.
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'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).
NodeArray< int > positionOnChain
position of a node on his chain
node firstNode() const
Returns the first node in the list of all nodes.
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's subtree ...
An implementation of the heavy path decomposition on trees.
List< node > terminals
list of terminals of the desc tree
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.
Doubly linked lists (maintaining the length of the list).
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()).
Class for the representation of edges.
Class for the representation of nodes.
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.
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.
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.
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.
Class for adjacency list elements.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
T weight(const edge e) const
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.