<?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 A058_PratyushSingh_B3 on Medium]]></title>
        <description><![CDATA[Stories by A058_PratyushSingh_B3 on Medium]]></description>
        <link>https://medium.com/@a058.pratyushsingh?source=rss-52cf351b65f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SoRDV87DbbvbRcuT.jpg</url>
            <title>Stories by A058_PratyushSingh_B3 on Medium</title>
            <link>https://medium.com/@a058.pratyushsingh?source=rss-52cf351b65f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 21:49:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@a058.pratyushsingh/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[Rabin-Karp Algorithm]]></title>
            <link>https://medium.com/pattern-searching-algorithm/rabin-karp-algorithm-f1d58bb2f0c1?source=rss-52cf351b65f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1d58bb2f0c1</guid>
            <dc:creator><![CDATA[A058_PratyushSingh_B3]]></dc:creator>
            <pubDate>Tue, 13 Apr 2021 17:18:55 GMT</pubDate>
            <atom:updated>2021-04-21T05:07:20.252Z</atom:updated>
            <content:encoded><![CDATA[<p>It is an algorithm used for searching/matching patterns in the text using a hash function. A hash function is a tool to map a larger input value to a smaller output value. And this output value is called the hash value. Other than Naive string matching algorithm, it does not travel through every character in the initial phase rather it filters the characters that do not match and then performs the comparison.</p><blockquote>Algorithm Working</blockquote><p>A sequence of characters is taken and checked for the possibility of the presence of the required string. If the possibility is found then, character matching is performed.</p><p>Let us understand the algorithm with the following steps:</p><p><strong>1.</strong>Let the text be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kGKpVowllFR_2WgXCqMKYw.jpeg" /></figure><p><strong>2.</strong> Let us assign a numerical value(v) / weight for the characters we will be using in the problem. Here, we have taken first ten alphabets only (i.e. A to J).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMHLbNCd4QO6w58X96XnSw.jpeg" /></figure><p><strong>3.</strong> m be the length of the pattern and n be the length of the text. Here, m = 10 and n = 3. Let d be the number of characters in the input set. Here, we have taken the input set {A, B, C, …, J}. So, d = 10. You can assume any suitable value for d.</p><p>4. Let us calculate the hash value of the pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1C0Z5U-gpr1Z8LldfwcJ3w.jpeg" /></figure><blockquote>hash value for pattern(p)</blockquote><blockquote>= Σ(v * dm-1) mod 13</blockquote><blockquote>= ((3 * 102) + (4 * 101) +</blockquote><blockquote>(4 * 100)) mod 13</blockquote><blockquote>= 344 mod 13</blockquote><blockquote>= 6</blockquote><p>In the calculation above, choose a prime number (here, 13) in such a way that we can perform all the calculations with single-precision arithmetic.The reason for calculating the modulus is given in point 5.</p><p><strong>5.</strong>Calculate the hash value for the text-window of size m.</p><p>For the first window ABC,</p><pre>hash value for text(t) = Σ(v * dn-1) mod 13 <br>                  = ((1 * 102) + (2 * 101) + (3 * 100)) mod 13 <br>                  = 123 mod 13  <br>                  = 6</pre><p>= 6</p><p><strong>6. </strong>Compare the hash value of the pattern with the hash value of the text. If they match then, character-matching is performed. In the above examples, the hash value of the first window (i.e. t) matches with p so, go for character matching between ABC and CDD. Since they do not match so, go for the next window.</p><p><strong>7.</strong> We calculate the hash value of the next window by subtracting the first term and adding the next term as shown below.</p><pre>t = ((1 * 102) + ((2 * 101) + (3 * 100)) * 10 + (3 * 100)) mod 13 <br>  = 233 mod 13  <br>  = 12</pre><p>In order to optimize this process, we make use of the previous hash value in the following way.</p><pre>t = ((d * (t - v[character to be removed] * h)<br>    + v[character to be added] ) mod 13  <br>  = ((10 * (6 - 1 * 9) + 3 )mod 13  <br>  = 12<br>Where, h = dm-1 = 103-1 = 100.</pre><p><strong>8.</strong> For BCC, t = 12 (≠6). Therefore, go for the next window.After a few searches, we will get the match for the window CDA in the text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I-469XI1ddiIV6xC70Sy8A.jpeg" /></figure><blockquote><strong>Algorithm</strong></blockquote><pre>n = t.length<br>m = p.length<br>h = dm-1 mod q<br>p = 0<br>t0 = 0<br>for i = 1 to m<br>    p = (dp + p[i]) mod q<br>    t0 = (dt0 + t[i]) mod q<br>for s = 0 to n - m<br>    if p = ts<br>        if p[1.....m] = t[s + 1..... s + m]<br>            print &quot;pattern found at position&quot; s<br>    If s &lt; n-m<br>        ts + 1 = (d (ts - t[s + 1]h) + t[s + m + 1]) mod q</pre><blockquote><strong>Code</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*SSmwGT9rWbJKgqiAnKe84w.png" /></figure><p>Output:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*WkjLyFvqLYqKwtCnfvGUPg.png" /></figure><blockquote><strong>Algorithm Complexity</strong></blockquote><p>The average case and best case complexity of Rabin-Karp algorithm is <strong>O(m + n)</strong> and the worst case complexity is <strong>O(mn).</strong>The worst-case complexity occurs when spurious hits occur a number for all the windows.</p><blockquote>Algorithm Applications</blockquote><ul><li>Can be used for pattern matching.</li><li>Can be used for searching string in a bigger text.</li></ul><h3>Limitations of Rabin-Karp Algorithm</h3><blockquote>Spurious Hit</blockquote><p>If the hash value of the pattern matches with the hash value of a window of the text but the window is not the actual pattern then it is know to be as spurious hit.It will result into the increases in the time complexity of the algorithm. We have to use modulus in order to minimize spurious hit from the algorithm. Hence, this greatly reduces the spurious hit.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1d58bb2f0c1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pattern-searching-algorithm/rabin-karp-algorithm-f1d58bb2f0c1">Rabin-Karp Algorithm</a> was originally published in <a href="https://medium.com/pattern-searching-algorithm">Pattern Searching Algorithm</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>