The data structure for an ordered set with many defined subsets; get subsets in the same order

I am looking for an efficient way to store an ordered list / set of elements, where:

  • The order of the elements in the main set changes quickly (subsets support the order of the wizard tasks)
  • Many subsets can be defined and retrieved.
  • The number of members in the master set is growing rapidly.
  • Members are added and removed from subsets often
  • Must allow multiple efficient merging of any number of subsets

Performance would ideally be biased toward retrieving the first N elements of any subset (or a merged subset), and the storage would be in memory (and possibly eventually permanent on disk)

+5
source share
1 answer

I am a new member of this forum, I hope you have not forgotten about this old question :)

Decision

Store the master set in an indexed data structure - such as an array (or an arraylist, if your library supports it). Suppose you can associate an identifier with each set (if not, how do you know which set you need to get?). So, now we need a way to find out which elements of your array are involved in this set and which are not.

Use the matrix (n x m)with nas the number of elements in your array, and m- the initial number of sets. I refer to the row index, and j refers to the column index.

A[i][j] = 0 if ith element is not in jth set
A[i][j] = 1 if ith element is in jth set

, ArrayList<ArrayList>. Java/#/++ , , Perl. # DataTable.

O(n) . 1 . , .

O(log n). ( at, 0 ). 1, . , O(log n) + O(m).

N

, O(1) , n , 1. .

, j1 j2 j3.

for (int i = 0; i < n - 1; i++) {
    A[i][j3] = A[i][j1] | A[i][j2];
}

.

- O(log n). .

, . /, , . , . , .

+2

All Articles