Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segment tree time complexity analysis

How can we prove that the update and query operations on a segment tree (http://letuskode.blogspot.in/2013/01/segtrees.html) (not to be confused with an interval tree) are O(log n)?

I thought of a way which goes like this - At every node, we make at most two recursive calls on the left and right sub-trees. If we could prove that one of these calls terminates fairly quickly, the time complexity would be logarithmically bounded. But how do we prove this?

like image 892
adijo Avatar asked Nov 28 '14 09:11

adijo


People also ask

What is the complexity of a segment tree for build query and update?

Since the segment tree has a height of log(n) and that at any level there are at most 4 nodes that can be visited, the upper bound is actually 4*log(n). The time complexity is therefore O(log(n)).

Why size of segment tree is 4 * n?

This should give the most optimal space allocation for a segment tree. 4n allocates more space according to me.

What is the purpose of segment tree?

Segment Tree is a basically a binary tree used for storing the intervals or segments. Each node in the Segment Tree represents an interval. Consider an array of size and a corresponding Segment Tree : The root of will represent the whole array A [ 0 : N − 1 ] .

How do you calculate the size of a segment tree?

of nodes in the segment tree upto the previous power of 2 will be 2N-1. So, the no. of nodes in the new level will be 2N-1+1 = 2N So, the size of the Segment Tree Array = 2N + 2N - 1 = 4N - 1 = 4N (approx.) Thus, Size of the Segment Tree Array = 4N (approx.)


2 Answers

Lemma: at most 2 nodes are used at each level of the tree(a level is set of nodes with a fixed distance from the root).
Proof: Let's assume that at the level h at least 3 nodes were used(let's call them L, M and R). It means that the entire interval from the left bound of the L node to the right bound of the R node lies inside the query range. That's why M is fully covered by a node(let's call it UP) from the h - 1 level that fully lies inside the query range. But it implies that M could not be visited at all because the traversal would stop in the UP node or higher. Here are some pictures to clarify this step of the proof:

 h - 1:  UP          UP        UP
         /\          /\        /\
 h:     L  M R    L M  R    L  M   R

That's why at most two nodes at each level are used. There are only log N levels in a segment tree so at most 2 * log N are used in total.

like image 107
kraskevich Avatar answered Oct 08 '22 19:10

kraskevich


The claim is that there are at most 2 nodes which are expanded at each level. We will prove this by contradiction. Consider the segment tree given below.

enter image description here

Let's say that there are 3 nodes that are expanded in this tree. This means that the range is from the left most colored node to the right most colored node. But notice that if the range extends to the right most node, then the full range of the middle node is covered. Thus, this node will immediately return the value and won't be expanded. Thus, we prove that at each level, we expand at most 2 nodes and since there are log⁡n levels, the nodes that are expanded are 2⋅logn=Θ(logn)

like image 5
Tokala Sai Teja Avatar answered Oct 08 '22 20:10

Tokala Sai Teja