Heaps Flashcards
How to construct a heap from an array (high level)
1) repeated insertions (log linear) or 2) the Floyd algorithm (linear time)
What data structure are heaps implemented with?
Arrays. This is a key difference from trees which are usually implemented with pointers (non contiguous memory)
Min heap property
The key of a child is always greater than or equal to the key of the parent
Heaps use what data structure to avoid holes in the array?
Complete binary trees. complete binary tree is a tree where each node has at most two children and the nodes at all levels are full, except for the leaf nodes which can be empty.
Build max heap from array
This is Kadens algo. Linear time
1) visually represent array as complete binary tree
2) start from last element of array and a) for each element, “heapify” it, meaning you ensure it is larger than both it’s children. If it’s not larger than both it’s children , you swap it with the larger of its children. b) you then call heapify on the child you did the swap with (so essentially heapify does recursion), because that child may now be smaller than IT’S children
Build max heap from array naive time complexity
O(nlgn), but the real runtime is actually O(n)
Heapsort
1) build min heap from array
2) delete/remove min element from heap (logn time bc must maintain heap property) and place into first el of New array
3) repeat #2, removing the ith min element and placing into the ith element of the new array (starting i at zero for both)until the heap is empty
Resulting array is sorted in increasing order
What index is the root often placed at?
1, to simplify arithmetic
Mergeable heap
heap (also called a meldable heap) is an abstract data type, which is a heap supporting a merge operation (e.g to merge with another heap)
List 5 examples of mergeable heap data structures
Binomial heap Fibonacci heap Leftist tree Pairing heap Skew heap
These all have more efficient merge operations than the naive merge algorithm for a binary heap
In most mergeable heap data structures what is the operation on which others are based
Merging
In most mergeable heap data structures how is insert implemented?
by merging a new single-element heap with the existing heap
In most mergeable heap data structures how is delete impelemented?
by merging the children of the deleted node.
Give a naive implementation of meld for a binary heap
X = extractMin H2
While X != null
Insert X, H1
X = extractMin H2
While both the heaps have to maintain the heap property (which would intuitively lead to nlgn time for the whole process), that one proof on build heap time shows that it takes O(n) [… but I think that’s for kadens algorithm for building heaps . . . Not for this algorithm for naive binary merge]
Chen 2007 study
examined priority queues specifically for use with Dijkstra’s algorithm and concluded that in normal cases using a d-ary heap without decrease-key (instead duplicating nodes on the heap and ignoring redundant instances) resulted in better performance, despite the inferior theoretical performance guarantees.