What data structure should I use for these operations?

I need a data structure that stores a subset called S-of {1,. ,, n} (n initially) and supports only these operations:

• Initially given n, S = {1,. ,, n} at the beginning.

• delete (i): delete I from S. If I am no longer in S, no effect.

• pred (i): returns the predecessor in S i. This means that max {j ∈ S | j <i}, the largest element in S which is strictly less than i. If they are not, return 0. The parameter i is guaranteed to be in {1,. ,, n}, but may or may not be in S.

For example, if n = 7 and S = {1, 3, 6, 7}, then pred (1) returns 0, pred (2) and pred (3) returns 1.

I need to find out:

  • data structure representing S
  • initialization algorithm (time O (n))
  • algorithm for removing (O (α (n)) amortized time)
  • algorithm for pre-start (O (α (n)) amortized time)

Would thank for any help (I don't need the code - only the algorithms).

+6
source share
3 answers

You can use a data structure with unrelated sets .

Imagine our subset as disjoint. Each element of a disjoint set is an element of a subset i(including the always-present zero), combined with all the missing elements in the set, which are larger than iand smaller than the next given element.

Example:

n = 10
s = [1, 4, 7, 8], disjoint-set = [{0}, {1,2,3}, {4,5,6}, {7}, {8, 9, 10}]
s = [3, 5, 6, 10], disjoint-set = [{0, 1, 2}, {3, 4}, {5}, {6, 7, 8, 9}, {10}]

, n+1 ( ). , leftmost .

leftmost(i) - leftmost disjoint-set, i.

leftmost(i) Find . i leftmost, . : O(α(n))

, i , i leftmost(i). ( i > 0), i .

pred(i) leftmost(i), i leftmost(i-1), i . : O(α(n))

delete(i) , i . i , , i ( , i-1). Union . leftmost leftmost(i-1). : O(α(n))

. " , " , .

+5

, , O (α (n)) , , van Emde Boas trees y-fast try

vEB . , n = 2 ^ b b = 2 ^ k

  • ,

  • b/2.
    vEB ('summary') √n vBE ( ). , .

O (n) O (log log n) = O (k) , .
, , . n 32-, , Dementiev et al. , .

y-fast x-fast:
trie , - .

y-fast , (), x-fast trie. .

vEB. , , vEB, .

: , log log n < 6, , ,

+1

O (α (n)) . :

  • i, 1 n, BST, AVL tree. AVL DataNode. :

    public class DataNode{
    int value;
    boolean type;
    DataNode(int value, boolean type){
        this.value = value;
        this.type = type;
    
        }
    }
    

    value 1 n. type true, , , S. , false.

O (n) . O(logn) . pred (i) , O(logn), . pred (i) :

  • i . true, inorder i, true.
  • If this is the case false, repeat for the next predecessor of this element (i.e. the predecessor i-1) until we find the element i whose type = true.
  • If no such predecessor is found type = true, then return 0.

Hopefully we can optimize this approach to make it in O (α (n)) for pred (i).

0
source

All Articles