GATE / CS ARCHIVE

GATE CSE 1992 / QUESTION 9(iii)

Constant-time set insertion

Data Structures · Arrays & Linked Lists · Full GATE CSE 1992 paper

GATE 1992, Q9(iii) Descriptive
Report issue

Arrays & Linked Lists

Question 9(iii), GATE 1992: Represent S⊆{1,…,n} so MEMBER, FIND-ONE, ADD and DELETE each take worst-case constant time independent of |S|. Give pictorial examples and routines, and state initialization assumptions. Give ADD(x) using the common representation.
Question 9(iii), GATE 1992: Represent S⊆{1,…,n} so MEMBER, FIND-ONE, ADD and DELETE each take worst-case constant time independent of |S|. Give pictorial examples and routines, and state initialization assumptions. Give ADD(x) using the common representation.
Question 9(iii), GATE 1992: Represent S⊆{1,…,n} so MEMBER, FIND-ONE, ADD and DELETE each take worst-case constant time independent of |S|. Give pictorial examples and routines, and state initialization assumptions. Give ADD(x) using the common representation.
Tap to enlarge
Answer & solution

Answer: If pos[x]=0, append x to dense and set pos[x] to the new m; otherwise do nothing. (no official key was published)

Use two length-n arrays: dense[1..m] stores precisely the current set in arbitrary order; pos[x] is x’s dense position if present, otherwise 0. The invariant is pos[dense[j]]=j for 1≤j≤m, and pos[x]=0 exactly for absent elements. Initialize m:=0 and pos[1..n]:=0, which takes Θ(n) time once. dense need not be initialized because only its active prefix is read. Space is Θ(n). Array indexing, integer comparison and assignment are constant-time in the word-RAM model, with words large enough for 0..n. Operations assume 1≤x≤n or reject out-of-range x before indexing.

Pictorial example, n=7 and S={2,5,7}:
dense positions: 1  2  3 | 4 5 6 7
dense values:    2  5  7 | unused
pos index:      1  2  3  4  5  6  7
pos value:      0  1  0  0  2  0  3

m=3; arrows are 2→1, 5→2, 7→3. A plain Boolean array gives easy membership but could require Θ(n) scanning to FIND-ONE; the dense prefix supplies an immediate element. Set order is deliberately not preserved, which enables constant-time deletion.

ADD(x):
  require 1<=x<=n;
  if pos[x]=0 then begin
    m:=m+1;
    dense[m]:=x;
    pos[x]:=m
  end.

Sets do not contain duplicate elements, so the initial presence test is essential. If x is already present, returning without changes preserves the representation. If it is absent, adding a new dense entry and its inverse position establishes the invariant for x without changing any existing entry. Since an absent x exists in a universe of size n, m<n before the increment; the append cannot exceed capacity.

For example ADD(4) changes dense [2,5,7] to [2,5,7,4], changes pos[4] from 0 to 4, and changes m from 3 to 4; all other positions stay fixed. ADD(5) thereafter changes nothing. There is no search, shifting or allocation during an operation because storage was reserved at initialization. The fixed number of reads/writes gives worst-case O(1), not merely amortized O(1).

Original paper, page 6