VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sparse-set/

⇱ Sparse Set - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sparse Set

Last Updated : 23 Jul, 2025

How to do the following operations efficiently if there are large number of queries for them. 

  1. Insertion
  2. Deletion
  3. Searching
  4. Clearing/Removing all the elements.

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). 
We can also use bit-vector (or direct access table), but bit-vector also requires O(n) time for clearing.

Sparse Set outperforms all BST, Hashing and bit vector. We assume that we are given range of data (or maximum value an element can have) and maximum number of elements that can be stored in set. The idea is to maintain two arrays: sparse[] and dense[]. 

dense[] ==> Stores the actual elements
sparse[] ==> This is like bit-vector where 
 we use elements as index. Here 
 values are not binary, but
 indexes of dense array.
maxVal ==> Maximum value this set can 
 store. Size of sparse[] is
 equal to maxVal + 1.
capacity ==> Capacity of Set. Size of sparse
 is equal to capacity. 
n ==> Current number of elements in
 Set.

insert(x): Let x be the element to be inserted. If x is greater than maxVal or n (current number of elements) is greater than equal to capacity, we return. 
If none of the above conditions is true, we insert x in dense[] at index n (position after last element in a 0 based indexed array), increment n by one (Current number of elements) and store n (index of x in dense[]) at sparse[x]. 
search(x): 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.
delete(x): 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.
clear(): Set n = 0.
print(): We can print all elements by simply traversing dense[]. 

Illustration: 

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.

Initially:
maxVal = 10 // Size of sparse
capacity = 4 // Size of dense
n = 2 // Current number of elements in set

// dense[] Stores actual elements
dense[] = {3, 5, _, _}

// Uses actual elements as index and stores
// indexes of dense[]
sparse[] = {_, _, _, 0, _, 1, _, _, _, _,}

'_' means it can be any value and not used in 
sparse set


Insert 7:
n = 3
dense[] = {3, 5, 7, _}
sparse[] = {_, _, _, 0, _, 1, _, 2, _, _,}

Insert 4:
n = 4
dense[] = {3, 5, 7, 4}
sparse[] = {_, _, _, 0, 3, 1, _, 2, _, _,}

Delete 3:
n = 3
dense[] = {4, 5, 7, _}
sparse[] = {_, _, _, _, 0, 1, _, 2, _, _,}

Clear (Remove All):
n = 0
dense[] = {_, _, _, _}
sparse[] = {_, _, _, _, _, _, _, _, _, _,}

Below is the implementation of above functions. 

Output : 

The elements in set1 are
5 3 9 10 

3 is found at index 1 in set1
5 3 10 

The elements in set2 are-
4 3 7 200 

Intersection of set1 and set2
3 

Union of set1 and set2
5 3 10 4 7 200 

This is a C++ program that implements a sparse set and its operations. A sparse set is a data structure that is useful when the maximum value of the set is known, and the set contains only a small subset of the maximum value range. The sparse set uses two arrays, sparse and dense, to store the elements. The sparse array maps each element to its index in the dense array, and the dense array contains the actual elements of the set.

The program defines a class SSet to represent the sparse set. The constructor takes the maximum value of the set and the capacity of the dense array as input parameters. The search, insert, deletion, print, clear, intersection, and setUnion functions are defined for the class.

The search function takes an element as input and returns its index in the dense array if it is present, otherwise -1. The insert function takes an element as input and inserts it into the set if it is not already present and the set is not full. The deletion function takes an element as input and removes it from the set if it is present. The print function prints the elements of the set in the dense array. The clear function removes all elements from the set.

The intersection function takes another SSet object as input and returns a new SSet object that contains the intersection of the two sets. The setUnion function takes another SSet object as input and returns a new SSet object that contains the union of the two sets. The intersection and setUnion functions use the search and insert functions to perform the set operations.

Overall, this program provides a simple and efficient implementation of a sparse set and its operations.


Additional Operations: 
The following operations are also efficiently implemented using sparse set. It outperforms all the solutions discussed here and bit vector based solution, under the assumptions that range and maximum number of elements are known. 

union(): 
1) Create an empty sparse set, say result. 
2) Traverse the first set and insert all elements of it in result. 
3) Traverse the second set and insert all elements of it in result (Note that sparse set doesn't insert an entry if it is already present) 
4) Return result. 

intersection(): 
1) Create an empty sparse set, say result. 
2) Let the smaller of two given sets be first set and larger be second. 
3) Consider the smaller set and search every element of it in second. If element is found, add it to result. 
4) Return result.
A common use of this data structure is with register allocation algorithms in compilers, which have a fixed universe(the number of registers in the machine) and are updated and cleared frequently (just like- Q queries) during a single processing run.

References: 
http://research.switch.com/sparse 
http://codingplayground.blogspot.com/2009/03/sparse-sets-with-o1-insert-delete.html
 
 

Comment
Article Tags: