Optimal 2D Data Structure

I thought a lot about this, but could not think of anything.

Suppose I want X n to collect items sorted by any column and any row under O (m * n), as well as the ability to insert or delete a row in O (m + n) or less ... is this possible?

What I came up with is a linked grid where the nodes are inserted into the vector, so I have indexes for them and the first row and column are indexed to remove the need to go through the list in any direction, with my method I achieved the above complexity, but I had it’s just interesting if this can be further reduced with the help of a variable factor.

Sort example:

1 100 25 34
2 20  15 16
3 165 1  27

Sorted by 3rd row:

25 1 34 100
15 2 16 20
 1 3 27 165

Sort THAT by 1st column:

 1 3 27 165
15 2 16 20
25 1 34 100
+7
5

, .

1 100 25 34
2 20  15 16
3 165 1  27   

:

  • cols = [0, 1, 2, 3]
  • rows = [0, 1, 2]

, 3- , , :

  • cols = [2, 0, 3, 1]
  • rows = [0, 1, 2]

, . , m[x][y] m[cols[x]][rows[y]]. m[cols[x]][rows[y]] row/cols.

- O(n*log(n)), - O(1).

:

+-+
|0| -> [0 1 2 3 4]
|1| -> [0 1 2 3 4]
|2| -> [0 1 2 3 4]
+-+

, rows . , rows [0, 1, 2] , [3, 0, 1, 2]. , O(n).

, . - O(m), - O(n).

O(n) O(m), /, , , .

+7

: , , , , , . , " LU " " LU ". , , "".

+2

​​ , . . , .

:

// These need to be set up elsewhere.
size_t nRows, nCols;
std::vector<T> data;

// Remapping vectors.  Initially a straight-through mapping.
std::vector<size_t> rowMapping(nRows), colMapping(nCols);
for(size_t y = 0; y < nRows; ++y)
    rowMapping[y] = y;
for(size_t x = 0; x < nCols; ++x)
    colMapping[x] = x;

// Then you read data(row, col) with
T value = data[rowMapping[row] * nCols + colMapping[col]];

P.S. , rowMapping . T value = rowMapping[row][colMapping[col]];, , data, .

+1

- (i, j) β†’ node, (i, j) 2-, 2 . , Equals GetHash() ... Python .

... - ? !

0

, ?

, , , . MySql . , . ( ..). .

-2
source

All Articles