<?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 Krupa Ka.patel on Medium]]></title>
        <description><![CDATA[Stories by Krupa Ka.patel on Medium]]></description>
        <link>https://medium.com/@krupa_110?source=rss-9463bc65fbdd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GjNV4Be8ygHK_FTvtx68yw.jpeg</url>
            <title>Stories by Krupa Ka.patel on Medium</title>
            <link>https://medium.com/@krupa_110?source=rss-9463bc65fbdd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 12:46:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@krupa_110/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[The Naive String Matching Algorithm]]></title>
            <link>https://medium.com/@krupa_110/the-naive-string-matching-algorithm-be7992ebbd1d?source=rss-9463bc65fbdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/be7992ebbd1d</guid>
            <category><![CDATA[string-matching]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[naive]]></category>
            <dc:creator><![CDATA[Krupa Ka.patel]]></dc:creator>
            <pubDate>Sun, 08 Sep 2019 17:00:44 GMT</pubDate>
            <atom:updated>2019-09-08T17:00:44.196Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is String matching??</strong></p><p><strong>Ans: </strong>Finding all occurrences of a pattern in a given text(or body of text).</p><ul><li>Naive pattern searching is the simplest method among other pattern searching algorithms. It checks for all character of the main string to the pattern.</li><li>Naive algorithm is exact string matching(means finding one or all exact occurrences of a pattern in a text) algorithm.</li><li>This algorithm is helpful for smaller texts. It does not need any pre-processing phases. We can find substring by checking once for the string. It also does not occupy extra space to perform the operation.</li><li>The naive approach tests all the possible placement of Pattern P [1…….m] relative to text T [1……n]. We try shift s = 0, 1…….n-m, successively and for each shift s. Compare T [s+1…….s+m] to P [1……m].It returns all the valid shifts found.</li></ul><pre><strong>NAIVE-STRING-MATCHER (T, P)</strong><br> 1. n ← length [T]<br> 2. m ← length [P]<br> 3. for s ← 0 to n -m<br> 4. do if P [1.....m] = T [s + 1....s + m]<br> 5. then print &quot;Pattern occurs with shift&quot; </pre><p><strong>Analysis:</strong> This for loop from 3 to 5 executes for n-m + 1(we need at least m characters at the end) times and in iteration we are doing m comparisons. So the total <strong>complexity is O (n-m+1).</strong></p><ul><li>The test on line 4 determines whether the current shift is valid or not;this test involves an implicit loop to check corresponding character positions until all positions.</li><li>Line 5 prints out each valid shift s.</li></ul><p><strong>Working of Naive String Matching</strong></p><p>The naive-string-matching procedure can be interpreted graphically as sliding a “template” containing the pattern over the text, noting for which shifts all of the characters on the template equal the corresponding characters in the text.</p><p><strong>Example 1:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*V9cSmKN2Hha3YB9QJfbZcA.jpeg" /></figure><p><strong>Example 2:</strong></p><pre><strong>Input:</strong>  txt[] = &quot;THIS IS STRING MATCHING ALGORITHM&quot;<br>        pat[] = &quot;STRING&quot;<br><strong>Output:</strong> Pattern found at position 10</pre><p><strong>Example 3:<br>Input:</strong><br>Main String: “ABAAABCDBBABCDDEBCABC”<br>pattern: “ABC”<br><strong>Output:</strong><br>Pattern found at position: 4<br>Pattern found at position: 10<br>Pattern found at position: 18</p><p><strong>What is the best case?</strong><br> →The best case occurs when the first character of the pattern is not present in text at all.</p><p>txt[] = &quot;BBACCAADDEE&quot;;</p><p>pat[] = &quot;HBB&quot;;</p><p>The number of comparisons in best case is O(n).</p><p><strong>What is the worst case ?</strong><br> →The worst case of Naive Pattern Searching occurs in following scenarios.<br>1) When all characters of the text and pattern are same.</p><p>txt[] = &quot;DDDDDDDDDDDD&quot;;</p><p>pat[] = &quot;DDDDD&quot;;</p><p>2) Worst case also occurs when only the last character is different.</p><p>txt[] = &quot;VVVVVVVVVVVVK&quot;;</p><p>pat[] = &quot;VVVK&quot;;</p><p>The number of comparisons in the worst case is O(m*(n-m+1)).</p><p><strong>Advantages:</strong></p><ol><li>No Pre-processing phase required because the running time of Naive-String-Matcher is equal to its matching time.</li><li>No extra space are needed.</li><li>Also,the comparisons can be done in any order.</li></ol><p><strong>Disadvantage:</strong></p><p>Naive method is inefficient because information from a shift is not used again.</p><h4><strong>Problem with Naive Algorithm</strong></h4><p>Suppose T=cabababcd and P=ababc</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*dXXOjpVXOTm4jpKA8_925A.jpeg" /></figure><ul><li>Whenever a character mismatch occurs after matching of several characters, the comparison begins by going back in from the character which follows the last beginning character.</li></ul><p><strong>Thank You…</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be7992ebbd1d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Matrix Chain Multiplication]]></title>
            <link>https://medium.com/@krupa_110/matrix-chain-multiplication-1ea0015655e1?source=rss-9463bc65fbdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ea0015655e1</guid>
            <dc:creator><![CDATA[Krupa Ka.patel]]></dc:creator>
            <pubDate>Sun, 08 Sep 2019 11:57:22 GMT</pubDate>
            <atom:updated>2019-09-08T12:55:25.648Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Matrix chain multiplication</strong> is an optimization problem that can be solved using dynamic programming.</p><p>Given a sequence of matrices, the goal is to find the most efficient way to multiply these matrices.</p><ul><li>Matrix table M[i][j] stores cost of multiplications</li><li>Matrix chain orders table S[i][j] stores order of multiplication</li><li>To generate M[i][j] use following steps</li></ul><p><strong>Step-1 if i = j then M[i][j] = 0 <br> Step-2 if i &lt; j then M[i][j] = min( M[i][k]+M[k+1][j]+P[i-1]* P [k]* P [j]) with i ≤ k &lt; j</strong></p><p><strong>Example:</strong><br> Here dimensions are p0=5,p1=4,p2=6,p3=2,p4=7</p><p>For i = j M[i][j]=0 <br>M[1][1]=0,M[2][2]=0,M[3][3]=0,M[4][4]=0 <br>For i &gt; j we does not calculate cost <br>so M[i][j] will be blank for i&gt; j</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*o_TItFT_Aabj-oWtqI0j0w.jpeg" /></figure><blockquote><strong>Calculation of Product of 2 matrices:</strong><br>1. m (1,2) = m1 *m2<br> = 5*4*6<br> = 120<br> <br>2. m (2, 3) = m2 *m3<br> = 4*6*2<br> = 48<br> <br>3. m (3, 4) = m3 *m4 <br> = 6*2*7<br> = 84</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*m3p-qtPcZZnCvs0i1oli5g.jpeg" /></figure><blockquote><strong>Calculation of Product of 3 matrices:</strong></blockquote><pre>M [1, 3] = M1 M2 M3</pre><p>M[1][3], here i &lt; j for i=1,j=3 <br>k=1 or 2 because i ≤ k ≤ j-1 <br>Use M[i][j] =min( M[i][k]+M[k+1][j]+P[i-1]*p[k]*p[j])</p><p><strong>For k=1</strong> <br>M[1][3]=M[1][1]+M[2][3]+p0*p1*p3 <br> = 0+48+5*4*2 <br> <strong>=88</strong></p><p><strong>For k=2</strong> <br>M[1][3]=M[1][2]+M[3][3]+p0*p2*p3 <br> =120+0+5*6*2 <br> =180</p><pre>M [2, 4] = M2 M3 M4</pre><p>M[2][4], here i &lt; j for i=2,j=4 <br>k=2 or 3 because i ≤ k ≤ j-1 <br>Use M[i][j] =min( M[i][k]+M[k+1][j]+P[i-1]*p[k]*p[j])</p><p><strong>For k=2</strong> <br>M[2][4]=M[2][2]+M[3][4]+p1*p2*p4 <br> = 0+84+4*6*7 <br> =252</p><p><strong>For k=3</strong> <br>M[2][4]=M[2][3]+M[4][4]+p1*p3*p4 <br> = 48+0+4*2*7 <br> <strong>=104</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*ZNoeJ6lf-Yu_NJ47rAwtzw.jpeg" /></figure><blockquote><strong>Calculation of Product of 4 matrices:</strong></blockquote><pre>M [1, 4] = M1 M2 M3 M4</pre><p>M[1][4], here i &lt; j for i=1,j=4 <br>k=1 or 2 or 3 because i ≤ k ≤ j-1 <br>Use M[i][j] =min( M[i][k]+M[k+1][j]+P[i-1]*p[k]*p[j])</p><p><strong>For k=1</strong> <br>M[1][4]=M[1][1]+M[2][4]+p0*p1*p4 <br> = 0+104+5*4*7 <br> =244</p><p><strong>For k=2</strong> <br>M[1][4]=M[1][2]+M[3][4]+p0*p2*p4 <br> = 120+84+5*6*7 <br> =414</p><p><strong>For k=3</strong> <br>M[1][4]=M[1][3]+M[4][4]+p0*p3*p4 <br> = 88+0+5*2*7 <br> <strong>=158</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/1*1h3Xr9ahA1LVwaFLAK72JA.jpeg" /></figure><h3><strong>Order of Multiplication</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*6bbG-oiU0QmB5XWQ5xkJrg.jpeg" /></figure><p><strong>Constructing an optimal solution</strong></p><blockquote>PRINT_OPTIMAL (A, i, j) <br>1 If i=j <br>2 print Ai <br>3 else <br>4 print ‘(‘ <br>5 PRINT_OPTIMAL (A, i, k) <br>6 PRINT_OPTIMAL (A, k+1, j) <br>7 print ‘)‘</blockquote><p>Here we are having 4 matrices let us denote it as A1,A2,A3,A4 so Calling PRINT_OPTIMAL (A, 1, 4)<br><strong>Note</strong> :For values of k see matrix S[i][j]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*SQP92kjU8vVcM8GVkE0eKw.jpeg" /></figure><p>In above example,the call PRINT_OPTIMAL (A, 1, 4) prints the</p><p>(((A1 A2)A3)A4)</p><p><strong>Reference</strong>:</p><blockquote>“ Introduction to Algorithm” by Thomas H Cormen, Charles E Leiserson, Ronald L Rivest, Clifford Stein.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ea0015655e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>