The document discusses K-ary heaps and sparse sets. K-ary heaps generalize binary heaps by allowing each node to have K children instead of 2. Sparse sets store unsigned integers efficiently using two arrays, sparse and dense, to allow constant time insertion, lookup, and removal while also efficiently clearing all elements.
2. K-ary Heap
• K-ary heaps are a generalization of binary
heap(K=2) in which each node have K children
instead of 2.
3. K-ary Heap
• K-ary heaps are a generalization of binary heap(K=2)
in which each node have K children instead of 2.
4. K-ary Heap
• Just like binary heap, it follows two properties:
1) Nearly complete binary tree, with all levels having
maximum number of nodes except the last, which is filled
in left to right manner.
2) Like Binary Heap, it can be divided into two categories:
(a) Max k-ary heap (key at root is greater than all descendants
and same is recursively true for all nodes).
(b) Min k-ary heap (key at root is lesser than all descendants
and same is recursively true for all nodes).
5. K-ary Heap: Implementation
Assuming 0 based indexing of array, an array
represents a K-ary heap such that for any node
we consider:
– Parent of the node at index i (except root node) is
located at index (i-1)/k
– Children of the node at index i are at indices
(k*i)+1 , (k*i)+2 …. (k*i)+k
– The last non-leaf node of a heap of size n is
located at index (n-2)/k
7. Sparse Set
• The sparse set is a useful storage container
designed to store unsigned integers.
• It is essentially a simplified concept of a hash
map, allowing for guaranteed constant-time
insertion and lookup.
• Clearing the set is also extremely efficient
whilst removing individual elements.
8. • How to do the following operations efficiently if
there are large number of queries for them.
– Insertion
– Deletion
– Searching
– Clearing/Removing all the elements.
9. Sparse Set
• One solution is to use a Self-Balancing Binary Search
Tree like Red-Black Tree, AVL Tree, etc.
• Time complexity of this solution for insertion,
deletion and searching is O(Log n).
• We can also use Hashing. With hashing, time
complexity of first three operations is O(1). But time
complexity of the fourth operation is O(n).
10. Sparse Set
• Sparse Set outperforms BST and Hashing.
• We assume that we are given range of data (or
maximum value an element can have) and maximum
number of elements (capacity) that can be stored in
set.
• The idea is to maintain two arrays: sparse[] and
dense[].
11. Sparse Set
• The underlying structure of both the arrays be like
Dense
Sparse
12. Sparse Set: Insertion
• When an integer, x, is inserted, it is appended to
array Dense[].
• Its resultant index in Dense[] is then inserted
into Sparse[] at index x.
• For example, if inserting the integer 5 into the set in
the image above, we would
set Dense[3] to 5 and Sparse[5] to 3.
13. Sparse Set : Search
• To search an element x, we use x as index in sparse[].
• The value sparse[x] is used as index in dense[].
• And if value of dense[sparse[x]] is equal to x, we
return dense[x]. Else we return -1.
14. Sparse Set : Deletion
• To delete an element x, we replace it with last
element in dense[] and update index of last element in
sparse[]. Finally decrement n by 1.
• We can print all elements by simply traversing
dense[].
15. Sparse Set
• Let there be a set with two elements {3, 5}, maximum
value as 10 and capacity as 4. The set would be
represented as below.
• MaxVal = 10 // Size of sparse
• Capacity = 4 // Size of dense
• n = 2 // Current number of elements in set
16. Sparse Set
// dense[] Stores actual elements
• dense[] = {3, 5, _, _}
// Uses actual elements as index and stores indexes of
dense[]
• sparse[] = {_, _, _, 0, _, 1, _, _, _, _,}