1
1
# Heap
2
2
3
+ ## Background
3
4
### Binary Heap
4
5
A binary heap is often used to introduce the concept of heaps. It is a tree-based data structure that satisfies the
5
6
following properties:
6
7
1 . 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
8
9
9
10
This makes it a powerful data structure that provides efficient access to the highest (or lowest) priority element,
10
11
making 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
28
29
Further, this operation, without augmentation, would take O(n) due to having to search for the object to begin with
29
30
(see under Notes).
30
31
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)).
32
33
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.
36
49
37
50
## 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)
41
57
42
58
## Notes
43
59
1 . 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
50
66
- The trade-off would be that the heap does not support insertion of duplicate objects else the Map would not work
51
67
as intended.
52
68
3 . 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