Asymptotic time complexity of inserting n elements to a binary heap already containing n elements
Asked Answered
P

3

8

Suppose we have a binary heap of n elements and wish to insert n more elements(not necessarily one after other). What would be the total time required for this?

I think it's theta (n logn) as one insertion takes logn.

Phile answered 4/11, 2011 at 15:22 Comment(1)
I think the size of the existing heap should enter the result somehow.Tinea
A
4

Assuming we are given:

  • priority queue implemented by standard binary heap H (implemented on array)
  • n current size of heap

We have following insertion properties:

  • W(n) = WorstCase(n) = Θ(lg n) (Theta). -> W(n)=Ω(lg n) and W(n)=O(lg n)
  • A(n) = AverageCase(n) = Θ(lg n) (Theta). -> W(n)=Ω(lg n) and W(n)=O(lg n)
  • B(n) = BestCase(n) = Θ(1) (Theta). -> W(n)=Ω(1) and W(n)=O(1)

So for every case, we have

  • T(n) = Ω(1) and T(n) = O(lg n)

WorstCase is when, we insert new minimal value, so up-heap has to travel whole branch.

BestCase is when, for minimal-heap (heap with minimal on top) we insert BIG (biggest on updated branch) value (so up-heap stops immediately).

You've asked about series of n operations on heap containing already n elements, it's size will grow

from n to 2*n

what asymptotically is ...

n=Θ(n)
2*n=Θ(n)

What simplifies our equations. (We don't have to worry about growth of n , as it's growth is by constant factor).

So, we have "for n insertions" of operation:

  • Xi(n) = X_for_n_insertions(n)
    • Wi(n) = Θ(n lg n)
    • Ai(n) = Θ(n lg n)
    • Bi(n) = Θ(n)
  • it implies, for "all case":
    • Ti(n) = Ω(n) and Ti(n) = O(n lg n)

P.S. For displaying Theta Θ , Omega Ω symbols, you need to have UTF-8 installed/be compatible.

Arlenaarlene answered 9/1, 2012 at 15:6 Comment(0)
K
6

given : heap of n elements and n more elements to be inserted. So in the end there will be 2*n elements. since heap can be created in 2 ways 1. Successive insertion and 2. Build heap method. Amoung these build heap method takes O(n) time to construct heap which is explained in How can building a heap be O(n) time complexity?. so total time required is O(2*n) which is same as O(n)

Kalinda answered 23/1, 2013 at 4:59 Comment(0)
A
4

Assuming we are given:

  • priority queue implemented by standard binary heap H (implemented on array)
  • n current size of heap

We have following insertion properties:

  • W(n) = WorstCase(n) = Θ(lg n) (Theta). -> W(n)=Ω(lg n) and W(n)=O(lg n)
  • A(n) = AverageCase(n) = Θ(lg n) (Theta). -> W(n)=Ω(lg n) and W(n)=O(lg n)
  • B(n) = BestCase(n) = Θ(1) (Theta). -> W(n)=Ω(1) and W(n)=O(1)

So for every case, we have

  • T(n) = Ω(1) and T(n) = O(lg n)

WorstCase is when, we insert new minimal value, so up-heap has to travel whole branch.

BestCase is when, for minimal-heap (heap with minimal on top) we insert BIG (biggest on updated branch) value (so up-heap stops immediately).

You've asked about series of n operations on heap containing already n elements, it's size will grow

from n to 2*n

what asymptotically is ...

n=Θ(n)
2*n=Θ(n)

What simplifies our equations. (We don't have to worry about growth of n , as it's growth is by constant factor).

So, we have "for n insertions" of operation:

  • Xi(n) = X_for_n_insertions(n)
    • Wi(n) = Θ(n lg n)
    • Ai(n) = Θ(n lg n)
    • Bi(n) = Θ(n)
  • it implies, for "all case":
    • Ti(n) = Ω(n) and Ti(n) = O(n lg n)

P.S. For displaying Theta Θ , Omega Ω symbols, you need to have UTF-8 installed/be compatible.

Arlenaarlene answered 9/1, 2012 at 15:6 Comment(0)
V
1

its not theeta(nlogn)... its order(nlogn) since some of the insertions can take less then exact logn time... therefore for n insertions it will take time <=nlogn

=> time complexity=O(nlogn)

Vietcong answered 28/12, 2011 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.