Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the top down approach of heap construction less efficient than bottom up even though its order of growth is lower O(log n) over O(n)?

How is the bottom up approach of heap construction of the order O(n) ? Anany Levitin says in his book that this is more efficient compared to top down approach which is of order O(log n). Why?

like image 321
sagar_jeevan Avatar asked Mar 25 '16 19:03

sagar_jeevan


People also ask

What is top down approach in heap?

In top down construction,the tree is constructed first and a heapify function is called on the nodes. The worst case would swap log n times ( to sift the element to the top of the tree where height of tree is log n) for all the n/2 leaf nodes. This results in a O(n log n) upper bound.

Is heapsort is more efficient than counting sort?

Heapsort is an efficient, unstable sorting algorithm with an average, best-case, and worst-case time complexity of O(n log n). Heapsort is significantly slower than Quicksort and Merge Sort, so Heapsort is less commonly encountered in practice.

What is the most efficient way to build a heap?

It would be O(n log n) if you built the heap by repeatedly inserting elements. However, you can create a new heap more efficiently by inserting the elements in arbitrary order and then applying an algorithm to "heapify" them into the proper order (depending on the type of heap of course).

What is the efficiency of heap sort?

Heap sort runs in O ( n lg ⁡ ( n ) ) O(n\lg(n)) O(nlg(n)) time, which scales well as n grows. Unlike quicksort, there's no worst-case O ( n 2 ) O(n^2) O(n2) complexity. Space efficient. Heap sort takes O ( 1 ) O(1) O(1) space.


3 Answers

That to me seems like a typo.

There are two standard algorithms for building a heap. The first is to start with an empty heap and to repeatedly insert elements into it one at a time. Each individual insertion takes time O(log n), so we can upper-bound the cost of this style of heap-building at O(n log n). It turns out that, in the worst case, the runtime is Θ(n log n), which happens if you insert the elements in reverse-sorted order.

The other approach is the heapify algorithm, which builds the heap directly by starting with each element in its own binary heap and progressively coalescing them together. This algorithm runs in time O(n) regardless of the input.

The reason why the first algorithm requires time Θ(n log n) is that, if you look at the second half of the elements being inserted, you'll see that each of them is inserted into a heap whose height is Θ(log n), so the cost of doing each bubble-up can be high. Since there are n / 2 elements and each of them might take time Θ(log n) to insert, the worst-case runtime is Θ(n log n).

On the other hand, the heapify algorithm spends the majority of its time working on small heaps. Half the elements are inserted into heaps of height 0, a quarter into heaps of height 1, an eighth into heaps of height 2, etc. This means that the bulk of the work is spent inserting elements into small heaps, which is significantly faster.

like image 121
templatetypedef Avatar answered Oct 05 '22 09:10

templatetypedef


If you consider swapping to be your basic operation -

In top down construction,the tree is constructed first and a heapify function is called on the nodes.The worst case would swap log n times ( to sift the element to the top of the tree where height of tree is log n) for all the n/2 leaf nodes. This results in a O(n log n) upper bound.

In bottom up construction, you assume all the leaf nodes to be in order in the first pass, so heapify is now called only on n/2 nodes. At each level, the number of possible swaps increases but the number of nodes on which it happens decreases.

For example - At the level right above leaf nodes, we have n/4 nodes that can have at most 1 swap each. At its' parent level we have, n/8 nodes that can have at most 2 swaps each and so on. On summation, we'll come up with a O(n) efficiency for bottom up construction of a heap.

like image 43
Poojita Raj Avatar answered Oct 05 '22 08:10

Poojita Raj


It generally refers to a way of solving a problem. Especially in computer science algorithms.

Top down :

Take the whole problem and split it into two or more parts. Find solution to these parts. If these parts turn out to be too big to be solved as a whole, split them further and find find solutions to those sub-parts. Merge solutions according to the sub-problem hierarchy thus created after all parts have been successfully solved.

In the regular heapify(), we perform two comparisons on each node from top to bottom to find the largest of three elements:

  1. Parent node with left child
  2. The larger node from the first comparison with the second child

Bottom up :

Breaking the problem into smallest possible(and practical) parts. Finding solutions to these small sub-problems. Merging the solutions you get iteratively(again and again) till you have merged all of them to get the final solution to the "big" problem. The main difference in approach is splitting versus merging. You either start big and split "down" as required or start with the smallest and merge your way "up" to the final solution.

Bottom-up Heapsort, on the other hand, only compares the two children and follows the larger child to the end of the tree ("top-down"). From there, the algorithm goes back towards the tree root (“bottom-up”) and searches for the first element larger than the root. From this position, all elements are moved one position towards the root, and the root element is placed in the field that has become free.

like image 39
Nitesh Joshi Avatar answered Oct 05 '22 07:10

Nitesh Joshi