How can building a heap be O(n) time complexity?

后端 未结 17 2614
礼貌的吻别
礼貌的吻别 2020-11-22 05:33

Can someone help explain how can building a heap be O(n) complexity?

Inserting an item into a heap is O(log n), and the insert is repeated n/2 times (t

17条回答
  •  一整个雨季
    2020-11-22 05:58

    Intuitively:

    "The complexity should be O(nLog n)... for each item we "heapify", it has the potential to have to filter down once for each level for the heap so far (which is log n levels)."

    Not quite. Your logic does not produce a tight bound -- it over estimates the complexity of each heapify. If built from the bottom up, insertion (heapify) can be much less than O(log(n)). The process is as follows:

    ( Step 1 ) The first n/2 elements go on the bottom row of the heap. h=0, so heapify is not needed.

    ( Step 2 ) The next n/22 elements go on the row 1 up from the bottom. h=1, heapify filters 1 level down.

    ( Step i ) The next n/2i elements go in row i up from the bottom. h=i, heapify filters i levels down.

    ( Step log(n) ) The last n/2log2(n) = 1 element goes in row log(n) up from the bottom. h=log(n), heapify filters log(n) levels down.

    NOTICE: that after step one, 1/2 of the elements (n/2) are already in the heap, and we didn't even need to call heapify once. Also, notice that only a single element, the root, actually incurs the full log(n) complexity.


    Theoretically:

    The Total steps N to build a heap of size n, can be written out mathematically.

    At height i, we've shown (above) that there will be n/2i+1 elements that need to call heapify, and we know heapify at height i is O(i). This gives:

    enter image description here

    The solution to the last summation can be found by taking the derivative of both sides of the well known geometric series equation:

    enter image description here

    Finally, plugging in x = 1/2 into the above equation yields 2. Plugging this into the first equation gives:

    enter image description here

    Thus, the total number of steps is of size O(n)

提交回复
热议问题