Editing Heavy-light decomposition

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 +
Heavy-light decomposition
 +
 +
Heavy-light decomposition - it is a common method which allows to solve many problems, which reduce to queries from a tree.
 +
 +
The simplest example of this type of problems - is the next task. Given a tree, each node is assigned a number. There have been queries of the form (a, b), where a and b - number of vertices of the tree, and you want to know the maximum number of vertices in the path between a and b.
 +
 +
Description of the algorithm
 +
 +
So, suppose we are given a tree G with n vertices, suspended for a certain root.
 +
 +
The point of this decomposition is to split a tree in several ways so that for any vertex v turned out that if we are to rise from v to the root, then we change the path of no more than \ log n paths. In addition, all paths should not cross each other in the ribs.
 +
 +
Clearly, if we learn to seek a decomposition for any tree, it will bring any kind of inquiry "to learn something on the way from a to b" to several requests like "learn something on the interval [l; r] k-th the way. "
 +
 +
Construction of heavy-light decomposition
 +
 +
Count for each vertex v the size of its subtree s (v) (ie, the number of vertices in the subtree of vertex v, including the very top).
 +
 +
Next, we consider all edges leading to the sons of a vertex v. We call an edge (v, c) severe, if it leads to a vertex c such that:
 +
 +
s (c) \ ge \ frac {s (v)} {2} ~ ~ ~ ~ \ Leftrightarrow [...]
 +
 +
All other edges are said to light. Obviously, one of v may come down heavy at most one edge (since otherwise a vertex v had two sons would be the size s (v) / 2, taking into account the very top of v gives a size of 2 \ cdot s ( v) / 2 + 1> s (v), ie a contradiction).
 +
 +
Now we construct a decomposition tree itself into disjoint paths. Consider all the vertices of which does not go down no hard edges, and we will go from each of them up until you get to the root of the tree or not to go through a slight edge. As a result, we obtain a number of ways - we will show that this is the desired way heavy-light decomposition.
 +
 +
The proof of the correctness of the algorithm
 +
 +
First, note that the resulting algorithm will be non-intersecting paths. In fact, if any two paths have a common edge, it would mean that some of the top comes down two heavy edges, which can not be.
 +
 +
Second, we show that down from the root to any vertex, we will change the path of no more than \ log n paths. In fact, the passage down the slight edge reduces the size of the current subtree more than doubled
 +
 +
s (c) <\ frac {s (v)} {2} ~ ~ ~ ~ \ Leftrightarrow ~ ~ [...]
 +
 +
Thus, we could not pass over \ log n light edges. However, the move from one track to another, we can only through a slight edge (because every path, except ending at the root, has a slight edge in the end, and get right in the middle of the way, we can not).
 +
 +
Consequently, the path from the root to any vertex, we can not change over \ log n paths as required.
 +
 +
Applications in solving problems
 +
 +
In solving problems it is sometimes convenient to consider the heavy-light as a set of vertex-disjoint paths (not edge-disjoint). It's enough of each path, delete the last edge, if it is an easy point-blank - if no properties are not violated, but now each vertex belongs to exactly one path.
 +
 +
Below we consider some typical problems that can be solved using the heavy-light decomposition.
 +
 +
Pay special attention to the problem of the sum of the numbers on the road, because this is an example of the problem, which can be solved by simpler techniques.
 +
The maximum number in the path between two vertices
 +
 +
Given a tree, each node is assigned a number. There have been queries of the form (a, b), where a and b - number of vertices of the tree, and you want to know the maximum number of vertices in the path between a and b.
 +
 +
We construct a pre-heavy-light decomposition. Above each of the resulting construct the tree through the segments to the maximum, which will search for the top with the maximum number ascribed in this segment of the path for the O (\ log n). Although the number of paths in the decomposition of heavy-light can reach n-1, the total size of all paths is the value of O (n), so the total size of the trees and the segments will be linear.
 +
 +
Now, in order to respond to a query (a, b) find the lowest common ancestor of l vertices (for example, by lifting binary). Now the problem is reduced to two inquiries: (a, l) and (b, l), each of which we can answer this way: we find, in what way is the lower top, make a request to this path, we go into the top-end of the way, again to determine in which way we were and make a request to him, and so on, until we reach the path that contains l.
 +
 +
Carefully should be the case when, for example, a and l are in one way - then the maximum request to this path should be done not on the suffix, as in the domestic subsegments.
 +
 +
Thus, in answer to a subquery, we go through the O (\ log n) paths, each of them making a request to the maximum suffix or prefix / subsegments (request for prefix / subsegments could be only one time).
 +
 +
So we got a solution for the O (\ log ^ 2 n) on a request.
 +
 +
If you have additional predposchitat highs in every way for all suffixes, we get a solution for the O (n \ log n) - as request the maximum suffix not happen only once, when we get to the top of l.
 +
 +
The sum of the numbers on the path between two vertices
 +
 +
Given a tree, each node is assigned a number. There have been queries of the form (a, b), where a and b - number of vertices of the tree, and you want to know the sum of the path between vertices a and b. The variant of this problem when there are additional requests for changes in the number, assigned a particular node.
 +
 +
Although this problem can be solved with the help of heavy-light decomposition, by building on each tree for the sum of the segments (or just predposchitav partial sums, if there are no requests for changes in the task), this problem can be solved by simpler techniques.
 +
 +
If the modification requests are not available, the amount of learning on the path between two vertices can be parallel to the LCA with the search algorithm, two vertices in the binary lifting - just during pre-LCA to calculate not only the 2 ^ k-th ancestor of each vertex, but the amount of numbers on the way to this ancestor.
 +
 +
There is also a fundamentally different approach to this problem - to consider the Eulerian traversal of the tree and construct a tree of segments on it. This algorithm is discussed in the article with a similar solution of the problem. (And if there are no requests for modifications - it is enough to do predposchetom partial sums, without a tree of segments.)
 +
 +
Both of these methods give relatively simple solutions with the asymptotics O (\ log n) per query.
 +
 +
Repainting edges of the path between two vertices
 +
 +
Given a tree, each edge originally painted white. There have been queries of the form (a, b, c), where a and b - number of vertices, c - the color, which means that all the edges on the path from a to b should be repainted in the color c. Require repainting after all to tell, but in the end turned the edges of each color.
 +
 +
The solution - just do a painting of a tree of segments on a segment on a set of ways of heavy-light decomposition.
 +
 +
Each request is repainted on the road (a, b) turn into two sub-query (a, l) and (b, l), where l - the lowest common ancestor of vertices a and b (found, for example, lifting a binary algorithm), and each of these subqueries - in O (\ log n) queries to the trees above the track segments.
 +
 +
Altogether it is a solution with the asymptotic behavior O (\ log ^ 2 n) on a request.
 +
 +
Problems in the online judges
 +
 +
The list of tasks that can be solved using the heavy-light decomposition:
 +
 +
    * TIMUS # 1553 "Caves and Tunnels" [difficulty: medium]
 +
 +
    * IPSC 2009 L "Let there be rainbows!" [Difficulty: medium]
 +
 +
    * SPOJ # 2798 "Query on a tree again!" [Difficulty: medium]
 +
 +
    * Codeforces Beta Round # 88 E "A tree or a tree" [difficulty: high]
 +
 
The '''heavy-light''' (H-L) '''decomposition''' of a rooted tree is a method of partitioning of the vertices of the tree into disjoint paths (all vertices have degree two, except the endpoints of a path, with degree one) that gives important asymptotic time bounds for certain problems involving trees. It appears to have been introduced in passing in Sleator and Tarjan's analysis of the performance of the [[link-cut tree]] data structure.
 
The '''heavy-light''' (H-L) '''decomposition''' of a rooted tree is a method of partitioning of the vertices of the tree into disjoint paths (all vertices have degree two, except the endpoints of a path, with degree one) that gives important asymptotic time bounds for certain problems involving trees. It appears to have been introduced in passing in Sleator and Tarjan's analysis of the performance of the [[link-cut tree]] data structure.
  

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)