2

I am solving Segment tree and Quad Tree related problems; while I noticed that in segment tree we split the 1D array into 2 (2^1) segments and recursively do this until base case arrives. Similarly, in Quad tree We subdivide the 2D grid into 4 (2^2) segments in every step. All these divide-and-Conquer mechanism is for achieving logarithmic time complexity. No offense!

But why don't we subdivide the array into 4 (4^1) parts or more instead of 2 parts in segment tree? And why we don't split the grid into 16 (4^2) parts instead of 4? By doing these, We can achieve O(log(N)) performance, but it would be a better log as log(N)(base 4) is better than log(N)(base 2).

I know in this case, the implementation would be little bit difficult. Is there a memory overhead problem? Or anything?

Please correct me if I am wrong anywhere. Thanks!

OmG
  • 18,337
  • 10
  • 57
  • 90
Kaidul
  • 15,409
  • 15
  • 81
  • 150
  • Changing the base of the logarithms used doesn't alter calculations of computational complexity. Think of how you change the base of logarithms. You use a multiplier of `1/log_newbase(old_base)` which is, essentially, the same as a constant multiplier in an expression such as `O(k*n^2)`. Computational complexity ignores such factors, though programmers are often well-advised to pay serious attention to them. – High Performance Mark Aug 04 '14 at 13:52
  • Going from `log2` to `log4` cuts the number of steps in half, but each step is typically around `log2(4)=2` times as much work, so there is no net gain. It does turn out to be a net win if the work within a step is much lower than the cost of taking a step. Data structures like B-trees use a high branching factor for this reason. – Raymond Chen Aug 04 '14 at 13:53
  • This is not a programming question; it is theoretical. Try http://cs.stackexchange.com/ – Raymond Chen Aug 04 '14 at 13:54
  • It's also basically a duplicate of [Time complexity using N way merge](http://stackoverflow.com/questions/10420397/time-complexity-using-n-way-merge/10420565). – Raymond Chen Aug 04 '14 at 13:57

2 Answers2

1

log 4 (N) = log 2 (N) / log 2 (4) = log 2 (N) / 2

in general,the time complexity are both O(logn) , while four segments is much harder than two segments to maintain. In fact ,(In acm/icpc) two segments are much easy to code and it is suffcient to work.

mickeyandkaka
  • 1,452
  • 2
  • 11
  • 21
1

It wouldn't actually work faster. Let's assume that we divided it into 4 parts. Then we would have to merge 4 values instead of 2 in each node to answer the query. Assuming that merging 4 values takes 3 times longer(for example, to get the maximum of 2 numbers we need 1 call to max function, but to get the maximum of 4 values 3 calls are required), we have log4(n) * 3 > log2(n) * 1. Moreover, it would be harder to implement(more cases to be considered and so on).

kraskevich
  • 18,368
  • 4
  • 33
  • 45
  • See [Fusion Tree (Wikipedia)](http://en.wikipedia.org/wiki/Fusion_tree). I'm thinking you can do something similar with parallel processing, with P processors giving O(log_P(N)) performance, it's probably also practical with [GPGPU programming](http://en.wikipedia.org/wiki/GPGPU). Though as mentioned here before, if it's done with a single processor then it typically ends up speeding things up in one area just to slow it down in another. – Nuclearman Aug 06 '14 at 21:53