<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aditya Naikwad on Medium]]></title>
        <description><![CDATA[Stories by Aditya Naikwad on Medium]]></description>
        <link>https://medium.com/@aditya007india?source=rss-afa93501b912------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*tMJL1kuQgP7CWJP0</url>
            <title>Stories by Aditya Naikwad on Medium</title>
            <link>https://medium.com/@aditya007india?source=rss-afa93501b912------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 07:00:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aditya007india/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Huffman Coding]]></title>
            <link>https://medium.com/@aditya007india/huffman-coding-06360d1873fe?source=rss-afa93501b912------2</link>
            <guid isPermaLink="false">https://medium.com/p/06360d1873fe</guid>
            <dc:creator><![CDATA[Aditya Naikwad]]></dc:creator>
            <pubDate>Tue, 23 Apr 2024 09:00:39 GMT</pubDate>
            <atom:updated>2024-04-23T09:00:39.824Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/275/0*SPSYt439XljUtySh.png" /></figure><p>In this fast-growing digital world, there is a lot of data being sent and received. People are trying to develop <em>different methods and algorithms to reduce space complexity and transmission time</em>. Huffman Coding is one such example that helped in reducing the message size while encoding it thereby decreasing space and transmission time. I’ll try to explain how Huffman Coding decreases the message size while encoding. By the end of the blog, I hope you’ll get a good idea of Huffman Coding.</p><p><strong>What is Huffman Coding?</strong></p><p>Huffman Coding is a famous <em>greedy algorithm</em>. It was developed by David A. Huffman in the year 1951. It is an <em>optimal prefix code</em> used for <em>lossless data compression</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/0*PfVKsZgFxoyA-JeV.png" /></figure><p><strong>Why Huffman Coding?</strong></p><p>First, let us take an example to study different approaches to encode data:</p><p>Suppose we have a message with the following different characters along with the number of times these characters are occurring in the message (frequency).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/0*vzHvc80g77TGwihH.png" /></figure><p><strong>Fixed Size Coding</strong></p><p><strong>1.</strong> <strong>ASCII coding method</strong></p><p>Now the simplest method would be using ASCII values. Here we have a total of 58 characters and we know that in ASCII value each character is represented using 8-bit code. So, while encoding the total message size will be 58*8=<strong>464 bits</strong>.</p><p><strong>2.</strong> <strong>n-bit binary representation</strong></p><p>But since we know that we are not using all the possible symbols and characters available on the keyboard so <em>can we give our own codes to the characters</em>?</p><p>Yes, we can do that! How? Let us see how.</p><p>Here we have 7 different characters. So, for representing 7 different characters we can use 3 bits because we know that 2³=8, which means we can represent 8 different combinations, but here we need only 7, so we can easily use 3 bits.</p><p>Now, I can assign the following codes to each character:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/0*rPlDWhnSHPhD2GeY.png" /></figure><p>Now, let us calculate the message size:</p><p>We have a total of 58 characters and each character is represented using a 3-bit code, so the message size will be 58*3=174 bits.</p><p>But we need to send the corresponding table as well because at the time of decoding, how will it be known which code is for which character because it is our own codes. So now we’ll calculate the table size.</p><p>The characters will be sent in their ASCII values and we have 7 different characters, so it will be (7*8) + (7*3) because each character has a 3-bit code. So, the table size becomes 56+21=77 bits.</p><p>Now the total size of the message becomes:</p><p>message size + table size</p><p>= 174+77</p><p>=<strong>251 bits</strong>.</p><p>We can observe that n-bit binary representation has reduced the message size significantly from 464 bits to 251 bits.</p><p>But Huffman Coding allows us to reduce this size further. Let us see how.</p><p><strong>Huffman Coding (Variable Size Coding)</strong></p><p>Till now we were looking at the fixed size coding. But Huffman says that we can have variable size codes for different characters which will help us to reduce the size of the message.</p><p>Let us take the same example as above so that it becomes easy to compare the results.</p><p>So, again we have 7 different characters along with their frequency (Table 1). Now the first thing we need to do is to draw a Huffman tree.</p><p>For this, we will arrange the characters in ascending order of their count (frequency).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*Sov5P8Q6JWP5w6cf.png" /></figure><p>Now we will choose two characters with the minimum counts (here t-1 and o-3) and sum it up (1+3=4). Then we will pop those two counts (1 and 3) and push the new sum (sum=4). It will then look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*16uG5byZ0QKGFCs6.png" /></figure><p>We will repeat the above steps till we form a complete tree. So, now the minimum counts will be 4 and 4 and their sum will be 8.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*2OAoHymL9NfKTtDw.png" /></figure><p>Now, the minimum count will be 8 and 10. Sum=18.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*EVVAHn7mIt41TrBg.png" /></figure><p>Since now it is no longer sorted, so first, we will sort it again. After sorting we will again pick the minimum count and sum it up (i.e., 12+13=25).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/0*VGsi6L1IK0Upp-rF.png" /></figure><p>We will sort the new counts again and then pick the least two (15+18=33).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/0*Rh382qp9Yhs5d1vp.png" /></figure><p>After sorting, we will complete the tree with the two counts that were left.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/0*_YXfgHgwRhIaxfC1.png" /></figure><p>This is how it will finally look. This is the optimal merge pattern tree or the Huffman Tree.</p><p>Now, to write the codes we first need to assign weight to the edges. I will assign ‘0’ to the left edges and ‘1’ to the right edges.</p><p>(Note: You can assign it the other way as well but remember to follow the same convention while decoding that you chose at the time of encoding.)</p><p>After assigning the weight to all the edges, the Huffman Tree will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/0*mtDRzXRFGekUfBn3.png" /></figure><p>To assign the codes to each character, we will traverse the tree from the root node to the leaf node of each character.</p><p>So, the code for each character will be as given below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/0*ciTQ20H5RrH0YDYa.png" /></figure><p>Table 3</p><p>One thing we should observe from the above table is that the <strong>character which is appearing more frequently has less number of bits in its code</strong> whereas the <strong>characters appearing less frequently have more bits in its code</strong>. This is done to reduce the total size of the message.</p><p>Here, the message size will be calculated as ∑ ( frequency<em>i</em> x code length<em>i </em>) that will be:</p><p>(10 x 3) + (15 x 2) + (12 x 2) + (3 x 5) + (4 x 4) + (13 x 2) + (1 x 5)</p><p>=30+30+24+15+16+26+5</p><p>=146 bits</p><p>Now, just like the previous example of n-bit binary representation, we need to send the tree/table so that the message can be decoded.</p><p>So, let us calculate the table size.</p><p>Here also we have 7 different characters which will be sent in ASCII values each of 8 bits (7*8=56). And the total number of bits in the codes of all characters is (3+2+2+5+4+2+5=23).</p><p>Therefore, the table size is 56+23=79 bits.</p><p>Now, the total size of the message along with the table becomes 146+79=<strong>225 bits</strong> which is much lesser than that came in the ASCII method.</p><p><strong>Huffman Coding Algorithm:</strong></p><p>Below is the snapshot of the algorithm of Huffman Coding:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/0*K0UpvO_aFm3kHLJo.png" /></figure><p><strong>Time complexity</strong></p><p>In the above implementation, Huffman Coding is implemented using a priority queue which can be implemented using different data structures like arrays, linked lists, heaps, etc. But Heaps are preferred because it reduces the time complexity as the complexity for push() and pop() using heaps is O(log(n)).</p><p>And if we have ’n’ characters, then the total<strong> time complexity will be O(n*log(n))</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/0*P_0griWyfJu6Hijj.png" /></figure><p>Image Source: Google</p><p>Now let’s take an example:</p><p>Suppose we have 3 different characters with the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/138/0*ayEIYIXcryX06INC.png" /></figure><p>Table 4</p><p>If I received a message, say 101 and I want to decode it, there will be an ambiguity that whether this message is ‘bab’ or ‘bc’.</p><p>So, to avoid such ambiguity, <strong>Huffman Coding always follows the prefix rule</strong>, which says that no code should be the prefix of another code.</p><p><strong>Analysis of Transmission Time:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/0*tyUOyJU36Pw-OzzI.png" /></figure><p>Image Source: Google</p><h3>Transmission time (Tt) =Message Size (MS) /Bandwidth (BW)</h3><p>With the above formula, we derive that Transmission time is directly proportional to the Message Size.</p><p>With this information we can infer that:</p><p><strong><em>Tt(Huffman) &lt; Tt(n-bit) &lt; Tt(ACSII)</em></strong></p><p>Thus, Huffman Coding is preferred over the traditional ASCII coding method because it reduces the message size thus reducing the transmission time.</p><p><strong>Conclusion:</strong></p><p>I hope you enjoyed reading the blog and found it informative.</p><p>I would also like to thank <strong>Bennett University (Computer Science Engineering)</strong> for motivating me to write this blog and enhance my knowledge on the topic.</p><p>Happy learning!</p><p><strong>Reference:</strong></p><p><a href="https://www.gatevidyalay.com/huffman-coding-huffman-encoding/">https://www.gatevidyalay.com/huffman-coding-huffman-encoding/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=06360d1873fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[nice content]]></title>
            <link>https://medium.com/@aditya007india/nice-content-59b705e381ec?source=rss-afa93501b912------2</link>
            <guid isPermaLink="false">https://medium.com/p/59b705e381ec</guid>
            <dc:creator><![CDATA[Aditya Naikwad]]></dc:creator>
            <pubDate>Sat, 29 Apr 2023 11:45:50 GMT</pubDate>
            <atom:updated>2023-04-29T11:45:50.320Z</atom:updated>
            <content:encoded><![CDATA[<p>nice content👍👍</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59b705e381ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>