Home/Data Structures & Algorithms/System-Scale & Probabilistic Structures

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
LSM-trees, HyperLogLog, Count-Min sketches, Roaring Bitmaps, Fenwick trees, Segment trees, and HNSW graphs.
pgvector: HNSW index build fails with out-of-memory or high swap: How to tune maintenance_work_mem & parallel workers
Direct Technical Solution: Unlike IVFFlat (which partitions vector spaces with k-means centroids), HNSW (Hierarchical Navigable Small World) constructs a multi-layer proximity graph in physical RAM during build time. For 2,000,000 vectors with 1,536 dimensions, the raw vectors alone occupy 2,000,000Read more
Direct Technical Solution: Unlike IVFFlat (which partitions vector spaces with k-means centroids), HNSW (Hierarchical Navigable Small World) constructs a multi-layer proximity graph in physical RAM during build time. For 2,000,000 vectors with 1,536 dimensions, the raw vectors alone occupy
2,000,000 × 1,536 × 4 bytes = 12.28 GB. Adding the HNSW graph neighbor connectivity lists (with m=16) increases total build RAM requirement to approximately 18 to 22 GB.1. The Formula to Calculate Required `maintenance_work_mem`
2. PostgreSQL Configuration Tuning
Temporarily allocate sufficient RAM to the session before triggering the index build, and utilize parallel CPU worker cores to accelerate graph edge exploration:
3. When to Use IVFFlat vs HNSW
How does a Fenwick Tree (Binary Indexed Tree) query and update prefix sums in O(log N) using i & (-i)?
The Fenwick Tree (invented by Peter Fenwick in 1994) is one of the most compact data structures ever devised. It gives you the full power of a dynamic segment tree in just one flat array of size N with 10 lines of code. 1. The Secret: Powers of 2 Range Decomposition Any positive integer can be uniquRead more
The Fenwick Tree (invented by Peter Fenwick in 1994) is one of the most compact data structures ever devised. It gives you the full power of a dynamic segment tree in just one flat array of size N with 10 lines of code.
1. The Secret: Powers of 2 Range Decomposition
Any positive integer can be uniquely represented as a sum of powers of 2. For example:
13 = 8 + 4 + 1.Fenwick took this idea and applied it to prefix ranges: instead of storing every single element,
tree[i]stores the sum of a contiguous range of length equal to its lowest set bit!The length of the range responsible by index
iis given by:lowbit(i) = i & (-i).i = 12 (1100_2):lowbit(12) = 4. Sotree[12]stores the sum of 4 elements: indices[9, 10, 11, 12]!i = 8 (1000_2):lowbit(8) = 8. Sotree[8]stores the sum of the first 8 elements:[1 ... 8]!2. The Two Operations
A. Prefix Sum Query: `i -= (i & -i)`
To calculate the prefix sum up to index
13:tree[13](covers index 13).13 - lowbit(13) = 13 - 1 = 12.tree[12](covers indices 9 through 12).12 - lowbit(12) = 12 - 4 = 8.tree[8](covers indices 1 through 8).8 - lowbit(8) = 8 - 8 = 0(Done!).Total reads: only 3 steps to sum 13 numbers! At each step, you strip off one binary bit, taking at most
O(log N)operations.B. Point Update: `i += (i & -i)`
When you add delta to element
i, which parent ranges need to be updated? Every index whose range coversi! You navigate up the tree simply by adding the lowest set bit:i += (i & -i)!Clean C++20 Implementation
Why Fenwick Beats Segment Trees in Production
How does a Count-Min Sketch estimate heavy-hitter item frequencies under bounded RAM?
The Count-Min Sketch (CMS) is the gold-standard probabilistic algorithm for tracking frequencies in massive, unconstrained data streams (used extensively in Apache Spark, network switches, and Google search analytics). 1. Architectural Layout A Count-Min Sketch consists of a 2D matrix of integer couRead more
The Count-Min Sketch (CMS) is the gold-standard probabilistic algorithm for tracking frequencies in massive, unconstrained data streams (used extensively in Apache Spark, network switches, and Google search analytics).
1. Architectural Layout
A Count-Min Sketch consists of a 2D matrix of integer counters with
drows (depth) andwcolumns (width), paired withdindependent hash functions:2. The Operations
A. Add Item `x` (Increment):
For each row
ifrom0tod - 1, compute column indexcol = h_i(x) % w, and increment that counter:B. Query Frequency of `x` (Point Query):
Because multiple items might collide at the same counter bucket, hash collisions can only increase a counter, never decrease it!
Therefore, to get the best possible estimate, we take the MINIMUM across all d rows:
The Golden Invariant: A Count-Min Sketch NEVER underestimates the true count! True frequency is always $le$ estimated frequency.
3. Mathematical Dimensioning Rules
If you want an error bound within $epsilon cdot N$ with confidence probability $1 – delta$:
ceil pprox lceil rac{2.718}{epsilon}
ceil$
ceil$
For example, to guarantee $le 0.1%$ error with $99%$ confidence, you need only $w = 2718$ columns and $d = 5$ rows. That’s just 13,590 integer counters (~54 KB of RAM) to monitor billions of events!
Clean Python 3.12 Implementation
Complexity Breakdown
- Add Time:
- Query Time:
- Memory Footprint:
See lessO(d)— strictly constant time ($5$ hash calculations and memory writes).O(d)— strictly constant time ($5$ lookups).O(w * d)— strictly fixed in size. Bounded memory that never grows regardless of how many billions of packets arrive!