<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Algorithms on Ujjwal Chowdhury</title><link>https://u77w41.github.io/tags/algorithms/</link><description>Recent content in Algorithms on Ujjwal Chowdhury</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 08 Dec 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://u77w41.github.io/tags/algorithms/index.xml" rel="self" type="application/rss+xml"/><item><title>Analysis on Complete Works of Swami Vivekananda</title><link>https://u77w41.github.io/p/analysis-on-complete-works-of-swami-vivekananda/</link><pubDate>Fri, 08 Dec 2023 00:00:00 +0000</pubDate><guid>https://u77w41.github.io/p/analysis-on-complete-works-of-swami-vivekananda/</guid><description>&lt;img src="https://u77w41.github.io/p/analysis-on-complete-works-of-swami-vivekananda/swami-ji.png" alt="Featured image of post Analysis on Complete Works of Swami Vivekananda" /&gt;&lt;p&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/U77w41/Analysis-on-Complete-Works-of-Swami-Vivekananda/" target="_blank" rel="noopener"&#10; &gt;GitHub Repository&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&#10;&lt;h2 id="overview"&gt;Overview&#10;&lt;/h2&gt;&lt;p&gt;Calculating the exact count of unique items in a large data stream typically requires maintaining an in-memory hash table of every distinct entry seen so far. As the corpus grows into millions of tokens, memory consumption scales linearly ($O(N)$), creating bottlenecks in streaming and distributed pipelines.&lt;/p&gt;&#10;&lt;p&gt;The &lt;strong&gt;Flajolet-Martin (FM) algorithm&lt;/strong&gt; solves this by probabilistically estimating stream cardinality using a fixed, minimal amount of memory ($O(\log N)$ bits). This project applies the Flajolet-Martin algorithm to estimate the unique vocabulary size across the nine volumes of the &lt;em&gt;Complete Works of Swami Vivekananda&lt;/em&gt;, comparing the probabilistic estimate against exact set cardinality.&lt;/p&gt;&#10;&lt;h2 id="the-flajolet-martin-algorithm"&gt;The Flajolet-Martin Algorithm&#10;&lt;/h2&gt;&lt;p&gt;The algorithm estimates distinct elements by tracking the distribution of trailing zeros in hashed bit representations:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;Hash Mapping:&lt;/strong&gt; Each token $w$ in the text stream is hashed to a uniform 32-bit or 64-bit integer $h(w)$.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Trailing Zeros ($\rho$):&lt;/strong&gt; For each hash value, the algorithm determines $\rho(h(w))$—the number of trailing zeros before the first 1-bit. A hash ending in $k$ zeros occurs with probability $2^{-(k+1)}$.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Maximum Tracking ($R$):&lt;/strong&gt; The algorithm maintains $R = \max(\rho(h(w)))$ across all processed tokens. If we observe a sequence ending in $R$ trailing zeros, we can infer that roughly $2^R$ unique elements have passed through the stream.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Correction Factor:&lt;/strong&gt; The raw estimate is adjusted using the Flajolet-Martin correction factor $\phi \approx 0.77351$:&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;$$\text{Estimated Cardinality} \approx \frac{2^R}{\phi}$$&lt;/p&gt;&#10;&lt;h3 id="variance-reduction-stochastic-averaging"&gt;Variance Reduction (Stochastic Averaging)&#10;&lt;/h3&gt;&lt;p&gt;A single hash function has significant statistical variance. To produce a stable estimate:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The stream is evaluated across $m$ independent hash functions.&lt;/li&gt;&#10;&lt;li&gt;The hash functions are divided into $g$ distinct groups.&lt;/li&gt;&#10;&lt;li&gt;The arithmetic mean of estimates is computed within each group.&lt;/li&gt;&#10;&lt;li&gt;The overall result is obtained by taking the &lt;strong&gt;median of group averages&lt;/strong&gt;, filtering out extreme outlier hash values.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="processing-pipeline"&gt;Processing Pipeline&#10;&lt;/h2&gt;&lt;p&gt;The text processing pipeline handles the multi-volume corpus in Python:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;Text Extraction &amp;amp; Cleaning:&lt;/strong&gt; Reads raw volume texts, stripping metadata, page numbers, and punctuation while normalizing text to lowercase tokens.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Token Streaming:&lt;/strong&gt; Simulates a continuous token stream rather than holding the entire preprocessed vocabulary in memory.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Multi-Hash Simulation:&lt;/strong&gt; Generates independent hash functions using linear congruential generators:&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;$$h_i(x) = (a_i \cdot x + b_i) \pmod p$$&lt;/p&gt;&#10;&lt;ol start="4"&gt;&#10;&lt;li&gt;&lt;strong&gt;Benchmarking:&lt;/strong&gt; Compares the FM probabilistic cardinality against standard Python &lt;code&gt;set()&lt;/code&gt; counting to evaluate error bounds across different numbers of hash buckets.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="key-insights"&gt;Key Insights&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Memory Efficiency:&lt;/strong&gt; While exact counting requires storing hundreds of thousands of unique string tokens in memory, the Flajolet-Martin register array requires only a few bytes of storage.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Accuracy Trade-off:&lt;/strong&gt; Increasing the number of hash functions significantly tightens the standard error, bringing the estimated vocabulary count within 5-10% of the true distinct word count.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item></channel></rss>