r/algorithms • • 3d ago

Discussion An idea of deletion-only sorted array

It can be used as a building block for insertion-only (write-optimized) data structures composed of multiple sorted arrays.

Properties:

  1. Type of keys – any comparable type, like numbers, strings or comparable objects.
  2. Instant deletion of a key, it is zeroed-out immediately and no more accessed later for comparison during subsequent searches.
  3. Zero latency, freeze-free, cheap rebuilding. Deletion of a key takes no more than O((log N)^2).
  4. Search takes O(log N). It is similar to binary search. Deleted items are not compared during the search.
  5. Stability – equal keys keep the same order.
  6. Updating is possible, but new key must be equal to old key. It is for "key-value" pairs.

The task: Array should allow deletion, and search should be always performed in O(log N).
It is simple to do with numbers – just use bit mask to mark deleted items. If a number is deleted, it can still be compared with another items.
But in case of objects, comparing a deleted object might be tricky or impossible. The idea is to avoid any comparison of deleted items.

The idea: Empty and non-empty items should be placed in a specific way. If some item is empty (deleted) and its index divides by 2^k, it is assumed that all 2^k items are empty. This approach allows that search will remain in O(log N) complexity.

Data organizing.
Items in the array are considered as blocks of size 2^k. Blocks are composed of 2 smaller blocks – left and right.
The array is fit into one larger block. For example, an array of length 375 is considered to be placed into block of length 512. It can be also considered as placed into larger block of length 4096.
If a block contains any data, its first item must be non-empty. If first item of the block is empty, entire block considered empty. If a block contains data and its first item is empty, such block is inconsistent.

Searching. It is done recursively.
At the beginning we have a single block, for example its size is 512 for an array of 375 items.
We call a function SearchItemInBlock(item, block_512). The function checks if a block can contain the item; if not then return not_found. If the block can contain the item, then the block is split into 2 smaller blocks – left block_256 and right block_256, the function checks which one can contain searched item and recursively calls SearchItemInBlock(item, block_256) one or two times.
And so on. The complexity is O(log N).

Deletion.
An item is replaced with delete_marker, for example MIN_INT, -INF, default(T) or null_ptr.
After such deletion some block may become inconsistent. To restore consistency (rebuilding) one non-empty item should be moved to the beginning of the block. Any move is performed only inside the block. Finding the next non-empty item is O(log N) in worst-case.
After moving an item another block may become inconsistent. That block will be twice smaller then the previous one. Restoring its consistency requires O(log N). And so on, up to O(log N) blocks.
Totally, restoring consistency requires up to O(log N) searches per O(log N) blocks, that gives compexity O((log N)^2) for worst-case.

Packing/compressing/compaction.
It is optional, not necessary. It might increase the performance of searching and deletion.
After deletion of an item up to O(log N) items moved to more left position. I don't know if it helps in worst-case.

Worst scenario is deleting the items from the very beginning of the array each time, as I understand. It will definetely cause worst-case at least once, I believe.

3 Upvotes

13 comments sorted by

1

u/Max_Arbuzov 1d ago

Packing/compressing/compaction.
It is optional, not necessary. It might increase the performance of searching and deletion.

Actually, packing is necessary, otherwise deleting will degrade to O((log N)^2) average in worst scenario.

Items moving rule: If m items are moved to the left then the distance of moving must be not less than m.

1

u/xeow 3d ago

But in case of objects, comparing a deleted object might be tricky or impossible. The idea is to avoid any comparison of deleted items.

Impossible in general without making an immutable deep copy of the object, yes, but in many real-world cases, you could retain immutable tuple-based signatures of the objects, assuming your pairwise comparison routine can always take those as inputs.

As I understand it, the reason for retaining a "ghost" of a deleted object is to keep the searching easier, yes?

2

u/Max_Arbuzov 3d ago edited 3d ago

As I understand it, the reason for retaining a "ghost" of a deleted object is to keep the searching easier, yes?

Yes, having the copies of deleted items greatly simplifies the searching amid deleted items.
But my goal is to completely eliminate the comparing of deleted items.

1

u/jeffgerickson 3d ago

Why does it have to be an array? A balanced binary search tree (for example, a red-black tree or an AVL tree) satisfies all your other criteria, with O(log N) worst-case deletion time.

1

u/Max_Arbuzov 3d ago
  1. Array requires less memory. RAM is in a scarcity now, as I know.
  2. Array might be faster. Benchmark required to prove.
  3. Some data structures handle arrays. Converting an array into a tree looks to be costly.

2

u/jeffgerickson 2d ago

Data structures can't be "faster", for the same reason cats can't be prime. Only the algorithms used to access and modify data structures can be faster.

You're struggling to find an algorithm to do what you want with an array in O((log n)^2) time, when an AVL tree can do it out of the box in O(log n) time. In what sense is that "faster"?

Benchmarks apply to space just as much as time. Have you benchmarked a binary tree to see if it's actually a memory bottleneck for you application? Or do you have other reasons beside "I just don't like trees"?

1

u/La-ze 29m ago

Different data structures can in-fact be faster because of cache locality . With on-demand paging, cache misses are expensive, jumping around non-contiguous memory with pointers ( like a link-list or tree ) is going to be more likely to miss the cache over a contiguous structure like an array.

2

u/drinkcoffeeandcode 3d ago

Cache locality is the only thing I could think of, and even there, we’ve got in memory b+ trees

1

u/dgreensp 3d ago

1

u/Max_Arbuzov 1d ago

As I understand, heaps use array item index for additional information. This deletion-only array also uses item index for information.
This array reminds me trees.

1

u/fun__friday 3d ago

Can you search for an element in a heap in log(n)?

1

u/dgreensp 2d ago

Right, oops. I dashed that off too quickly. I was thinking of heaps because of the way they are often packed into arrays. Also, the "root" of each subtree is its minimum, so its seems like the min-heap property is present.

What's actually being described is more like packing a binary search tree into an array. When deleting a node that isn't a leaf, you move its successor into its position.

One nice way to pack a binary tree into an array is the root is at position 0, and the left and right children of node i are at locations 2i+1 and 2i+2. So the children of 0 are 1 and 2. The children of 1 are 3 and 4. The children of 2 are 5 and 6. And so on. (Alternatively, the root is 1, and children are 2i and 2i+1. The binary digits after the leading 1 tell you the path taken to the node, with 0 being left and 1 being right.) But that doesn't keep the items in order as OP's scheme does. It does keep the higher levels of the tree relatively tightly packed, though.

OP's algorithm works.

If you have an occupancy bitmap, there is actually a way to reorder the bitmap so that all the "probes" you need to do in a deletion are consecutive bits, which basically makes deletion worst-case log(N).

1

u/dgreensp 2d ago edited 2d ago

Ok, OP, here is the shuffling of the occupancy bitmap that makes deletion log(N).

One way to think of it is, when deleting at position p, the positions you need to probe are given by setting each of the trailing 0s of p to a 1. For example, for 8 (1000), the positions are 9 (1001), 10 (1010), and 12 (1100). That's one step of the deletion, and there are log(N) steps. However, if you can make bits 9, 10, and 12 consecutive in the occupancy bitmap, you can read them in one go, making each step constant time. There is an order to store the occupancy bits in that satisfies this. For example, for N=16, the order is 1, 2, 4, 8, 3, 5, 6, 7, 9, 10, 12, 11, 13, 14, 15. The positions are sorted by the number you get if you clear the lowest one bit, then by the number itself.

There is a closed form formula for this. Note that operations on an integer like "number of trailing zeros" and "number of one digits" (also known as "population count" or popcount) can be done in constant time.

Given a position m, its occupancy bit goes at index: to(m) = p + a + Z, where:

  • p = m with its lowest bit cleared: m & (m - 1)
  • a = the position of that bit, which is the number of trailing zeroes in m
  • Z = the number of 0 bits in p above p's lowest one bit (or 0 if p = 0)

As a formula, with p written in n bits (N = 2^n):

Z = n − popcount(p) − tz(p)

This works for p = 0 too if you define tz(0) = n, since it gives n − 0 − n = 0.

Note m=0 doesn't get any place in the permuted occupancy bitmap because it doesn't have a parent and isn't part of any probe for a successor. to(m) takes input from 1 to N-1 and gives output from 0 to N-2.