11# Heap
22
3+ ## Background
34### Binary Heap
45A binary heap is often used to introduce the concept of heaps. It is a tree-based data structure that satisfies the
56following properties:
671 . Complete binary tree - every level, except possibly the last, is completely filled
7- 2 . Max (min) heap property - the value of every vertex in the binary tree is >= (<=) than that of its child vertices
8+ 2 . Max (min) heap property - the value of every vertex in the binary tree is >= (<=) than that of its child nodes
89
910This makes it a powerful data structure that provides efficient access to the highest (or lowest) priority element,
1011making it suitable as an underlying implementation of the ADT, priority queue.
@@ -28,16 +29,31 @@ heaps in Python and Java is primarily due to design choices and considerations o
2829Further, this operation, without augmentation, would take O(n) due to having to search for the object to begin with
2930(see under Notes).
3031
31- Still, one can circumvent the lack of such operations by simply removing and re-inserting (still O(n)).
32+ One can circumvent the lack of such operations by simply removing and re-inserting (albeit, still O(n)).
3233
33- This is worth a mention. In cases like Dijkstra where the concern is having to maintain V nodes in the priority queue
34- by constantly updating rather than insertion of all edges, it is not really a big issue. After all, the log factor
35- in the order of growth will turn log(E) = log(V^2) in the worst case of a complete graph, to 2log(V) = O(log(V)).
34+ ** This is worth a mention:** <br >
35+ In cases like Dijkstra algorithm, there is no need to strictly maintain V nodes in the priority queue for O(nlogn).
36+ One can just insert all edges rather than constantly updating (hence, no need for updateKey operations). <br >
37+ After all, the log factor in the order of growth will turn log(E) = log(V^2) in the worst case of a complete graph,
38+ to 2log(V) = O(log(V)).
39+
40+ ### Heapify - Choice between bubbleUp and bubbleDown
41+ Heapify deals with bubbling down (for max heap) all elements starting from the back. <br >
42+ What about bubbling-up all elements starting from the front instead? <br >
43+ ** No issue with correctness, problem lies with efficiency of operation.**
44+
45+ The number of operations required for bubbleUp and bubbleDown (to maintain heap property), is proportional to the
46+ distance the node have to move. bubbleDown starts from the bottom level whereas bubbleUp starts from the top level.
47+ Only 1 node is at the top whereas (approx) half the nodes is at the bottom level. It therefore makes sense to use
48+ bubbleDown.
3649
3750## Complexity Analysis
38- Time: O(log(n)) in general for most native operations,
39- except heapify (building a heap from a sequence of elements) that takes o(n) <br >
40- Space: O(n) where n is the number of elements (whatever the structure, it must store at least n nodes) <br >
51+ ** Time** : O(log(n)) in general for most native operations,
52+ except heapify (building a heap from a sequence of elements) that takes o(n)
53+
54+ ** Space** : O(n)
55+
56+ where n is the number of elements (whatever the structure, it must store at least n nodes)
4157
4258## Notes
43591 . Heaps are often presented as max-heaps (eg. in textbooks), hence the implementation follows a max-heap structure
@@ -50,3 +66,5 @@ Space: O(n) where n is the number of elements (whatever the structure, it must s
5066 - The trade-off would be that the heap does not support insertion of duplicate objects else the Map would not work
5167 as intended.
52683 . Rather than using Java arrays, where size must be declared upon initializing, we use list here in the implementation.
69+ 4 . [ Good read] ( https://stackoverflow.com/questions/9755721/how-can-building-a-heap-be-on-time-complexity? ) on the
70+ time complexity of heapify and making the correct choice between bubbleUp and bubbleDown.
0 commit comments