<?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 dilli_hangrae on Medium]]></title>
        <description><![CDATA[Stories by dilli_hangrae on Medium]]></description>
        <link>https://medium.com/@dillihangrae?source=rss-2062f3579091------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MBhTDlhth5eBh9asJnxWbQ.jpeg</url>
            <title>Stories by dilli_hangrae on Medium</title>
            <link>https://medium.com/@dillihangrae?source=rss-2062f3579091------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 08:09:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dillihangrae/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[Radial-Basis Function and Cover’s Theorem]]></title>
            <link>https://medium.com/@dillihangrae/radial-basis-function-and-covers-theorem-3c64e2136463?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c64e2136463</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[rbf-kernel]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Sat, 26 Apr 2025 06:22:35 GMT</pubDate>
            <atom:updated>2025-06-04T06:45:40.703Z</atom:updated>
            <content:encoded><![CDATA[<p>Implementation of RBF</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CmQwvx68_mnPqQT2-lQF2A.png" /></figure><h3>Abstract</h3><p>A Radial Basis Function <strong>(RBF)</strong> is a <strong>real-valued function</strong> whose output depends only on the <strong>distance</strong> <strong>between</strong> the <strong>input x </strong>and <strong>a fixed point c</strong>, called the center[1]. Data points are both linearly separable and non-linearly separable. Non-linearly separable data requires a <strong>curved decision boundary </strong>(<strong>not a straight line or hyperplane</strong>) to separate different classes perfectly. The Radial Basis Function (RBF) is the core component that gives RBF networks an <strong>RBF function as an activation function</strong>. It enables the network’s key characteristics, such as <strong>non-linear mapping</strong> and effective <strong>function approximation.</strong> This article dives into <strong>Cover’s theorem</strong> and <strong>RBF</strong> <strong>implementation</strong> with the required mathematics and algorithm.</p><h3>1. Introduction</h3><p>A Radial Basis Function (RBF) is a function φ that depends on the distance between two points. It’s used with a norm (a way to measure distance) in a vector space. The RBF kernel centered at a point c is written as φ_c = φ(∥x − c∥), where ∥x − c∥ is the distance between x and c[1].</p><p>Section 2 discusses Cover’s theorem, Section 3 outlines the RBF Functions with a numerical example, Section 4 is the RBF Algorithm, Section 5 provides detailed handwritten lecture notes, and Section 6 is the Conclusion.</p><h3>2. Cover’s Theorem on the Separability of Patterns</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_YkoB9HOZz5vTqT4OsioDA.png" /></figure><p><strong>Cover’s theorems states that:</strong></p><p><strong>“A complex pattern-classification problem, cast in a high-dimensional space nonlinearly, is more likely to be linearly separable than in a low-dimensional space.” </strong>This means the <strong>probability of separability</strong> increases with the <strong>increase in dimensionality</strong>. Thus, it also forms the <strong>theoretical basis for kernel-based learning algorithms.</strong></p><pre>Mathematical Interpretation:<br>----------------------------<br>Let:<br>- φ: ℝⁿ → ℝᴺ be a nonlinear transformation, where N &gt;&gt; n<br>- Data in ℝⁿ may not be linearly separable.<br><br>Then:<br>There exists a high probability that φ(data) ∈ ℝᴺ becomes linearly separable.</pre><h3>3. Numerical Examples of <strong>RBF</strong></h3><h4>Example:</h4><p><strong>Given:</strong></p><p>x = [4,5], [2, 3]] (fixed input)<br>c₁ = [1, 2], c₂ = [3, 4] (centers, randomly selected)<br>σ = ? (spread, computed using σ = d_max / √(2K))<br>w = Weights to be computed using the formula: w = (Φᵀ Φ)⁻¹ Φᵀ y<br>b = 0.1 (bias, randomly selected)</p><h4><strong>Step 1: Compute the Spread (σ)</strong></h4><p><strong>Formula for σ:</strong><br>σ = d_max / √(2K)</p><p>Where:<br>- d_max is the maximum distance between any two centers.<br>- K is the number of centers.</p><p><strong>Step 1.1: Calculate d_max (distance between c₁ and c₂):</strong><br>d_max = √((c₂₁ — c₁₁)² + (c₂₂ — c₁₂)²)<br>d_max = √((3–1)² + (4–2)²)<br>d_max = √2² + 2 ²<br>d_max = √(4 + 4)<br>d_max = √8 ≈ 2.828</p><p><strong>Step 1.2: Compute σ:</strong><br>σ = d_max / √(2K)<br>σ = 2.828 / √(2 * 2)<br>σ = 2.828 / √4<br>σ = 2.828 / 2<br><strong>σ ≈ 1.414</strong></p><p>Thus, the spread (σ) is approximately 1.414.</p><h4><strong>Step 2: Compute RBF Activations</strong></h4><p>The RBF activation for each center is computed using the formula:<br><strong>ϕₖ(x) = exp(- ||x — cₖ||² / 2σ²)</strong></p><p>Where:<br>- x is the input vector [ [4,5], [2, 3] ].<br>- c₁ = [1, 2] and c₂ = [3, 4] are the centers.<br>- σ ≈ 1.414 is the spread.</p><h4>Step 2.1: Compute ϕ</h4><pre>Step 2.1: Compute ϕ₁₁ (for c₁ = [1, 2]) for X = [4, 5]:<br><br>ϕ₁₁ = exp(- ((4 – 1)² + (5 – 2)²) / (2 * (1.414)²))<br>ϕ₁₁ = exp(- (3² + 3²) / (2 * 2))<br>ϕ₁₁ = exp(- (18 / 4))<br>ϕ₁₁ = exp(-4.5)<br>ϕ₁₁ ≈ 0.0111<br><br>Thus, ϕ₁₁ ≈ 0.0111<br><br>--------------------------------------------<br><br>Step 2.2: Compute ϕ₂₂ (for c₂ = [3, 4]) for X = [2, 3]:<br><br>ϕ₂₂ = exp(- ((2 – 3)² + (3 – 4)²) / (2 * (1.414)²))<br>ϕ₂₂ = exp(- (1² + 1²) / (2 * 2))<br>ϕ₂₂ = exp(- (2 / 4))<br>ϕ₂₂ = exp(-0.5)<br>ϕ₂₂ ≈ 0.6065<br><br>Thus, ϕ₂₂ ≈ 0.6065<br><br>--------------------------------------------<br><br>Summary (ϕ matrix):<br><br>Let:<br>- ϕ₁₁ = RBF between X₁ = [4,5] and c₁ = [1,2]<br>- ϕ₁₂ = RBF between X₁ = [4,5] and c₂ = [3,4]<br>- ϕ₂₁ = RBF between X₂ = [2,3] and c₁ = [1,2]<br>- ϕ₂₂ = RBF between X₂ = [2,3] and c₂ = [3,4]<br><br>You have already computed:<br>- ϕ₁₁ ≈ 0.0111<br>- ϕ₂₂ ≈ 0.6065<br><br>Now, compute:<br>ϕ₁₂ = exp(- ((4 – 3)² + (5 – 4)²) / (2 * (1.414)²))<br>ϕ₁₂ = exp(- (1² + 1²) / (2 * 2))<br>ϕ₁₂ = exp(-0.5)<br>ϕ₁₂ ≈ 0.6065<br><br>ϕ₂₁ = exp(- ((2 – 1)² + (3 – 2)²) / (2 * (1.414)²))<br>ϕ₂₁ = exp(- (1² + 1²) / (2 * 2))<br>ϕ₂₁ = exp(-0.5)<br>ϕ₂₁ ≈ 0.6065<br><br>Final ϕ matrix:<br><br>       c₁       c₂<br>X₁   0.0111   0.6065<br>X₂   0.6065   0.6065</pre><h4>Step 3: Compute <strong>(Φᵀ Φ)</strong></h4><pre><br>Compute (Φᵀ Φ)<br>Given:<br>Φ = <br>[ 0.0111   0.6065 ]<br>[ 0.6065   0.6065 ]<br><br>Now, compute ΦᵀΦ:<br><br>Φᵀ = <br>[ 0.0111   0.6065 ]<br>[ 0.6065   0.6065 ]<br><br>ΦᵀΦ = <br>[ (0.0111)^2 + (0.6065)^2     (0.0111)(0.6065) + (0.6065)^2 ]<br>[ (0.0111)(0.6065) + (0.6065)^2   (0.6065)^2 + (0.6065)^2   ]<br><br>= <br>[ 0.00012321 + 0.368       0.0067 + 0.368 ]<br>[ 0.0067 + 0.368           0.368 + 0.368  ]<br><br>≈<br>[ 0.3681     0.3747 ]<br>[ 0.3747     0.736  ]</pre><h4><strong>Step 4: Compute </strong>(ΦᵀΦ)⁻¹</h4><pre>Step 4: Compute (ΦᵀΦ)⁻¹<br><br>Given:<br>ΦᵀΦ =<br>[ 0.3681   0.3747 ]<br>[ 0.3747   0.736  ]<br><br>To find the inverse of a 2x2 matrix:<br>If A = [a  b]<br>         [c  d]<br>Then A⁻¹ = (1 / (ad - bc)) * [ d  -b]<br>                                      [-c  a]<br><br>So,<br><br>Determinant = (0.3681)(0.736) - (0.3747)(0.3747)<br>            ≈ 0.270970 - 0.140399<br>            ≈ 0.130571<br><br>Now, compute the inverse:<br><br>(ΦᵀΦ)⁻¹ = (1 / 0.130571) *<br>          [ 0.736   -0.3747 ]<br>          [ -0.3747  0.3681 ]<br><br>≈<br>[ 5.636  -2.868 ]<br>[ -2.868  2.818 ]</pre><h4>Step 5: Compute <strong>Φᵀ y</strong></h4><pre>Step 5: Compute Φᵀ y with y = [0.5, 0.7]<br><br>Given:<br>Φ =<br>[ 0.6065  0.6065 ]<br>[ 0.0111  0.6065 ]<br><br>y = [ 0.5, 0.7 ]<br><br>Now, compute Φᵀ:<br><br>Φᵀ =<br>[ 0.6065   0.0111 ]<br>[ 0.6065   0.6065 ]<br><br>Now, multiply Φᵀ by y:<br><br>Φᵀ y =<br>[ 0.6065   0.0111 ]   *   [ 0.5 ]<br>[ 0.6065   0.6065 ]       [ 0.7 ]<br><br>= [ (0.6065 * 0.5) + (0.0111 * 0.7) ]<br>  [ (0.6065 * 0.5) + (0.6065 * 0.7) ]<br><br>= [ 0.30325 + 0.00777 ]<br>  [ 0.30325 + 0.42455 ]<br><br>= [ 0.31102 ]<br>  [ 0.72780 ]<br><br>Thus, Φᵀ y =<br>[ 0.31102 ]<br>[ 0.72780 ]</pre><h4><strong>Step 6: Compute Weights (w)</strong></h4><p>Now, we will compute the weights w using the formula:<br><strong>w = (Φᵀ Φ)⁻¹ Φᵀ y</strong></p><p>Where:<br>- Φ is the matrix of RBF activations for all the centers.<br>- y is the target output vector. We assume them later.</p><p><strong>Assume y = [0.5, 0.7] for simplicity.</strong></p><pre>Step 6: Compute Weight Vector w<br>w = (ΦᵀΦ)⁻¹ * Φᵀy<br><br>Given:<br>(ΦᵀΦ)⁻¹ =<br>[  5.636  -2.868 ]<br>[ -2.868   2.818 ]<br><br>Φᵀy =<br>[ 0.31102 ]<br>[ 0.72780 ]<br><br>Now compute w:<br><br>w₁ = (5.636)(0.31102) + (-2.868)(0.72780)<br>   ≈ 1.75256 - 2.08712<br>   ≈ -0.33456<br><br>w₂ = (-2.868)(0.31102) + (2.818)(0.72780)<br>   ≈ -0.89245 + 2.05067<br>   ≈ 1.15822<br><br>So, w ≈<br>[ -0.33456 ]<br>[  1.15822 ]<br><br>------------------------------------------------------</pre><h3>Step 7: Compute Feature Vector ϕ(X) for X = [2, 3]</h3><pre>Step 7: Compute Feature Vector ϕ(X) for X = [2, 3]<br><br>ϕ₁ = exp(- ((2–1)² + (3–2)²) / (2 * (1.414)²))<br>    = exp(- (1² + 1²) / (2 * 2))<br>    = exp(- (2 / 4))<br>    = exp(-0.5)<br>    ≈ 0.6065<br><br>ϕ₂ = exp(- ((2–3)² + (3–4)²) / (2 * (1.414)²))<br>    = exp(- (1² + 1²) / (2 * 2))<br>    = exp(- (2 / 4))<br>    = exp(-0.5)<br>    ≈ 0.6065<br><br>So, ϕ(X) = [0.6065, 0.6065]</pre><h3>Step 8: Compute Prediction ŷ</h3><pre>------------------------------------------------------<br><br>ŷ = ϕ(X) * w<br>   = [0.6065, 0.6065] * [ -0.33456, 1.15822 ]ᵀ<br>   = (0.6065)(-0.33456) + (0.6065)(1.15822)<br>   ≈ -0.2029 + 0.7025<br>   ≈ 0.4996<br><br>------------------------------------------------------<br><br>Final Prediction:<br>ŷ(X = [2, 3]) ≈ 0.4996</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ai8hT2sCev-TfU2bGkDIQw.png" /></figure><h3>Special Case Computing Pseudo Inverse for the det = 0 case in RBF</h3><h4>Finding (Φᵀ Φ)⁻¹</h4><p>To find (Φᵀ Φ)⁻¹, we need to compute the determinant and adjugate of the matrix Φᵀ Φ.</p><p>Given:</p><pre>Φᵀ Φ = <br>[ 0.7358, 0.7358 ]<br>[ 0.7358, 0.7358 ]</pre><p>### Step 1: Compute the Determinant of Φᵀ Φ</p><p>The determinant of a 2x2 matrix is calculated using the formula:</p><pre>det(A) = (a * d) - (b * c)</pre><p>For the matrix Φᵀ Φ, where:</p><pre>a = 0.7358, b = 0.7358, c = 0.7358, d = 0.7358<br>det(Φᵀ Φ) = (0.7358 * 0.7358) - (0.7358 * 0.7358)<br>det(Φᵀ Φ) = 0–0 = 0</pre><p><strong>### Step 2: Compute the Adjugate of Φᵀ Φ</strong></p><p>The adjugate of a 2x2 matrix is computed by swapping the diagonal elements and negating the off-diagonal elements. The adjugate of Φᵀ Φ is:</p><pre>adj(Φᵀ Φ) = <br>[ d, -b ]<br>[ -c, a ]</pre><p>For the matrix Φᵀ Φ, this becomes:</p><pre>adj(Φᵀ Φ) = <br>[ 0.7358, -0.7358 ]<br>[ -0.7358, 0.7358 ]</pre><p><strong>### Step 3: Compute the Inverse of Φᵀ Φ</strong></p><p>The inverse of a 2x2 matrix is calculated using the formula:</p><pre>(Φᵀ Φ)⁻¹ = 1 / det(Φᵀ Φ) * adj(Φᵀ Φ)</pre><p>However, since the determinant of Φᵀ Φ is zero, this means that the matrix is singular and does not have an inverse.</p><p><strong>Thus, (Φᵀ Φ)⁻¹ does not exist because the determinant is zero.</strong></p><p><strong>Step 1: Add a regularization term to Φᵀ Φ</strong></p><pre>Φᵀ Φ = <br>[ [0.7358, 0.7358], <br>  [0.7358, 0.7358] ]</pre><p><strong>Regularization parameter λ = 0.01</strong></p><pre>Φᵀ Φ + λI = <br>[ [0.7358 + 0.01, 0.7358], <br>  [0.7358, 0.7358 + 0.01] ]<br><br>Φᵀ Φ + λI = <br>[ [0.7458, 0.7358], <br>  [0.7358, 0.7458] ]<br>here I is the identity matrix</pre><p><strong>Step 2: Compute the inverse of Φᵀ Φ + λI</strong></p><pre>To compute the inverse of a 2x2 matrix<br> [ a, b ]<br> [ c, d ]</pre><p>The formula for the inverse is:</p><pre>(1 / (ad - bc)) * [ d, -b ]<br>                  [ -c, a ]<br><br>For Φᵀ Φ + λI = [ [0.7458, 0.7358], <br>                  [0.7358, 0.7458] ]<br><br>The determinant is:<br>det = (0.7458 * 0.7458) - (0.7358 * 0.7358) = 0.014816 (approximately)</pre><p>Now compute the inverse:</p><pre>(Φᵀ Φ + λI)⁻¹ = (1 / 0.014816) * [ [0.7458, -0.7358], <br>                                  [-0.7358, 0.7458] ]<br><br>(Φᵀ Φ + λI)⁻¹ = 67.49 * [ [0.7458, -0.7358], <br>                           [-0.7358, 0.7458] ]<br><br>(Φᵀ Φ + λI)⁻¹ = [ [50.3340, -49.6591], <br>                   [-49.6591, 50.3340] ]</pre><p>Step 3: Multiply the inverse by Φᵀ y to compute w</p><pre><br>Φᵀ y = [ [0.7278]]<br><br>Now multiply (Φᵀ Φ + λI)⁻¹ by Φᵀ y:<br><br>w = [ [50.3340, -49.6591], <br>      [-49.6591, 50.3340] ] * [ [0.7278], <br>                                 [0.7278] ]<br><br>Performing the matrix multiplication:<br><br>w_1 = (50.3340 * 0.7278) + (-49.6591 * 0.7278) = 36.6330 - 36.1418 = 0.4911<br><br>Thus, the final weights are:<br><br>w = [ [0.4911] ]<br><br>Final Result:<br><br>w ≈ [0.4911]</pre><h3>4. <strong>RBF Algorithm</strong></h3><p><strong>Input:</strong><br> Training data: {(xᵢ, yᵢ)} for i = 1 to N<br> Number of RBF centers: K(randomly selected)</p><h4><strong>Algorithm Steps:</strong></h4><ol><li><strong>Choose centers (cⱼ):</strong><br> Use K-Means clustering or randomly select K samples from the training data.</li></ol><p><strong>2. Choose spread (σ):</strong><br> A common choice:<br> σ = d_max / √(2K)<br> where d_max is the maximum distance between any two centers.</p><p><strong>3. Compute RBF activations for each input:</strong><br> For each input xᵢ and center cⱼ, compute:<br> Φᵢⱼ = ϕⱼ(xᵢ) = exp( — ||xᵢ — cⱼ||² / (2σ²) )</p><p><strong>4. Train output weights using linear regression:</strong><br> w = (ΦᵀΦ)⁻¹ Φᵀ y</p><p><strong>5. Predict for new input x:</strong><br> ŷ = Σⱼ=1 to K [ wⱼ ⋅ ϕⱼ(x) ] + b</p><h3>5. Handwritten Lecture Notes</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QtMt0JZAA0O2Xp0WlaLEYg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yKhuGXOcQIVUtfV1wY6Vcw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mWblfYJdieHsGa2rqAhTnQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Jnf75r7HcSJzwhLwHK4_w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2ahRC5vd7deEz-Lk1ad8bA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lp2RIC0MKdkqpdG5_8pdxA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qQ_lHklFnF2wZZGgpOVbGQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9_HzweZxwEC0PQsGLJaDw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G1KBiJswovhUjmgJTMQ1pw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zLTv76rvGMJdr_H6cch0_Q.png" /></figure><h3>6. Conclusion</h3><p>The Gaussian function is the most commonly used type ofRBF. We explored the cover’s theorem and implemented kernel regression using RBF. The <strong>pseudo-inverse</strong> is used in cases where the matrix is <strong>non-square</strong> or <strong>singular</strong> (i.e., it does not have an inverse). RBF is used in machine learning for classification, regression, clustering, anomaly detection, and applications like face recognition and robotics to model non-linear data patterns. This article only demonstrates the RBF implementation without directly using it in Neural Networks.</p><h3>Sources:</h3><ol><li>Wikipedia Contributors. (2025, April 26). <em>Radial basis function</em>. In <em>Wikipedia, The Free Encyclopedia</em>. Retrieved from <a href="https://en.wikipedia.org/wiki/Radial_basis_function">https://en.wikipedia.org/wiki/Radial_basis_function</a></li><li>Sengupta, S. (2009, September 23). <em>Lec-24 Radial Basis Function Networks: Cover’s Theorem </em>[<a href="https://youtu.be/yk_dsPu9Hmg?si=5iXABCVg8AKQEt-Y">Video</a>]. YouTube (nptelhrd).</li><li>Haykin, S. (1998). *Neural Networks: A Comprehensive Foundation*. Prentice Hall.</li></ol><h3>Appendix:</h3><p>A. Lab Resources and Code Repositories<br> — GitHub Repository: <a href="https://github.com/Dilli822/CSC-372-Neural-Networks">https://github.com/dilli822/Neural-Network-Labs</a><br> — Contains implementations of various neural network models, algorithms, and experiments.</p><p>B. Implementation of RBF &amp; Cover’s theorem</p><ul><li><a href="https://colab.research.google.com/drive/1DPHNWS10kiF3fiEElRiBYCwl5-hR8vfw?usp=sharing">Google Colab</a></li></ul><h3>Tools</h3><ol><li>OpenAI. (n.d). <em>ChatGPT</em> (April 2025 version)</li><li>Gemini (n.d). <em>Google</em>.(April 2025 version)</li><li>DeepSeek R1 (n.d). DeepSeek. (April 2025 version)</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c64e2136463" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Least Mean Square Algorithm Part II]]></title>
            <link>https://medium.com/@dillihangrae/least-mean-square-algorithm-part-ii-7020405cf39e?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/7020405cf39e</guid>
            <category><![CDATA[adaptive-filter]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[signal-processing]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[least-mean-square]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Fri, 18 Apr 2025 05:29:16 GMT</pubDate>
            <atom:updated>2025-06-04T06:46:30.370Z</atom:updated>
            <content:encoded><![CDATA[<p>Adaptive Filter: Signal Processing | Neural Networks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*em0OyThFZOfaVoCzO4tQ-w.png" /></figure><h3>Abstract</h3><p>The <strong>Least Mean Square Algorithm</strong> adaptively adjusts filter weights to minimize the <strong>mean squared error</strong> between the desired output and the actual output. The <strong>Linear Least Squares Filter </strong>estimates the filter weight to <strong>minimize the sum of squared errors</strong> over all samples. LMS algorithm updates the weights using a <strong>stochastic gradient descent approach</strong>, where the gradient is estimated using only the <strong>current (instantaneous) error sample.</strong> This article dives into the mathematical and theoretical concepts related to the derivation and understanding of the LMS Algorithm.</p><h3>1. Introduction</h3><p>The least-mean-square (LMS) algorithm is configured to minimize the instantaneous value of the cost function[1]. Section 2 includes the discussion of the Wiener solution, Section 3 outlines the algorithm, and Section 4 includes the detailed mathematical derivation of LMS required in both signal processing and learning models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TcCfgqHPn27DcTkRNrJnUg.png" /></figure><h3>2. Wiener Filter</h3><p>The <strong>Wiener solution</strong>, also known as the <strong>Wiener filter</strong>, represents the <strong>optimal linear filter</strong> that minimizes the <strong>mean square error (MSE).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/0*qjH9qV0DgHJiqvSp.png" /><figcaption>Retrived from <a href="https://wirelesspi.com/wp-content/uploads/2022/01/figure-equalization-lms-error-surface.png">https://wirelesspi.com/wp-content/uploads/2022/01/figure-equalization-lms-error-surface.png</a></figcaption></figure><pre>**Mathematical Formulation (Discrete-Time Case):**<br>Let:<br>d(n) be the desired signal.<br>x(n) be the input signal.<br>w be the filter weight vector (for a finite impulse response - FIR filter).<br>y(n) = wᵀ x(n) be the output of the filter.<br>e(n) = d(n) - y(n) be the error signal.<br><br>The goal is to find the weight vector w that minimizes E[e²(n)], <br>where E[⋅] denotes the expectation operator.<br><br>The Wiener solution for the optimal weight vector w_opt is <br>given by the Wiener-Hopf equation:<br>R_x w_opt = r_dx<br><br>where:<br><br>R_x = E[x(n)xᵀ(n)] is the autocorrelation matrix of the input signal x(n).<br>r_dx = E[d(n)x(n)] is the cross-correlation vector between the desired <br>signal d(n) and the input signal x(n).<br><br>If R_x is invertible, the Wiener solution for the optimal weight vector is:<br>w_opt = R_x⁻¹ r_dx</pre><h4>2.1 Intuition behind the Wiener Filter</h4><p>Okay, imagine you’re trying to listen to your friend talking in a noisy room (like a busy cafe here in Itahari). Your friend’s voice is the <strong>desired signal</strong>, and all the other chatter, music, and clanking cups are the <strong>noise</strong> in the <strong>input signal</strong>.</p><p>Your brain is trying to act like a filter to isolate your friend’s voice and suppress the noise. The <strong>Wiener filter</strong> is like the <em>ideal</em> version of this brain filter, but for signals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*pBbYsEKXSdBJRnVu.png" /><figcaption>Retrived from <a href="https://www.astroml.org/_images_1ed/fig_wiener_filter_1.png">https://www.astroml.org/_images_1ed/fig_wiener_filter_1.png</a></figcaption></figure><h3>3. Algorithm</h3><pre>Input signal vector: x(n)<br>x(n)<br>Desired output: d(n)<br>d(n)<br>Learning rate: η<br>η<br>Initialization:<br><br>Set weights: w⁰ = 0<br>w⁰=0<br>For each time step n=1,2,…<br>n=1,2,…:<br><br>  Compute output:<br>    yⁿ = wᵀⁿ x(n)<br>    yⁿ=wᵀⁿx(n)<br>  Compute error:<br>    e(n) = d(n) - yⁿ<br>    e(n)=d(n)−yⁿ<br>  Update weights:<br>    wⁿ⁺¹ = wⁿ + η ⋅ x(n) ⋅ e(n)<br>    wⁿ⁺¹=wⁿ+η⋅x(n)⋅e(n)</pre><h3>4. Handwritten Derivations</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wLt05aSLEWohQL0riS0e9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z4eUXdeLdZz5Bu_WEKJGzw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*liTExzSEbnQXFjeyeNDtWA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f6sWVKseF8raezfQ3fiWrA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AFErusIu2wv55oZ7Jwu-jQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pt2tZ8fhv2YZ20204kEpoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WTkcCmt9NbOcgc7FhQn0ag.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dwms83Wm_4SBblbLCrHJOQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VpNDvXhZFC7dJEcgzBgvXg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F6-ianmCsAUc0zk7WxHFoA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ABVKDcd2qzFBfThyP4FcdQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*00IU1iKUFpJM0SOnV3UvaQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nCEBHBqNuJDF2fp41bJS3g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QMu1KuXPAq9puu_wj-7-3w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e6kL7oKuX_UaqXOZ5PKEYg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7pYuXisiB-tQMnMnjU8x1Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UjsXlczy38tIQcHdJy6FjA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DBszzvKgAZqWFDrHyFBdLA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9V1qUaIjwZjQutOpUPLkig.png" /></figure><h3>5. Conclusion</h3><p>LMS algorithm with a small learning rate, with the valid convergence assumptions, is simple to implement and effective. But it lacks speed in convergence, and large η can <strong>increase the convergence speed,</strong> but may lead to <strong>instability</strong> or even <strong>divergence</strong> of the algorithm. Adaptive Control Systems, <strong>Biomedical Signal Processing, and Echo Cancellation are some applications of LMS. </strong>Adaptive Linear Elements (<strong>ADALINE</strong>) and Multiple ADALINE (<strong>MADALINE</strong>) were the earliest neural networks to use LMS for weight updates</p><h3>References:</h3><ol><li>Haykin, S. (2009). <em>Neural Networks and Learning Machines</em> (3rd ed.). Pearson Education.​</li></ol><h3>Sources:</h3><ol><li>YouTube. (n.d.). <em>Linear least squares filters</em> [Video]. YouTube. Retrieved from <a href="https://www.youtube.com/watch?v=wX7hy4Z9WAc">Lec-15 Least Mean Squares Algorithm</a></li><li>Sengupta, S. (2009). <em>Neural Networks and Applications</em> [Video lecture series]. Indian Institute of Technology Kharagpur. Retrieved from <a href="https://www.youtube.com/watch?v=wX7hy4Z9WAc">Lec-15 Least Mean Squares Algorithm</a></li><li>Widrow, B., &amp; Hoff, T. (1959). <em>The ADALINE — Theory and Implementation of the First Neural Network Trained With Gradient Descent</em>. Retrieved from <a href="https://pabloinsente.github.io/the-adaline">https://pabloinsente.github.io/the-adaline</a></li></ol><h3>Appendix:</h3><p><strong>Example:</strong> <em>“Appendix A: Code Implementation”<br>The full code for the Least Mean Square filter implementation used in this study is available in the following Google Colab notebook:</em><a href="https://colab.research.google.com/drive/1Mm3XDJZkD9iB-YnC1QIZl_vQXkmeRD8N?usp=sharing"><em> Google Colab notebook.”</em></a></p><h3>Tools</h3><ol><li>OpenAI. (n.d). <em>ChatGPT</em> (April 2025 version)</li><li>Google. (n.d.). <em>Google search</em>. Retrieved April 2025</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7020405cf39e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linear Least Squares Filters: LMS Algorithm Part I]]></title>
            <link>https://medium.com/@dillihangrae/linear-least-squares-filters-lms-algorithm-part-i-550cf8690d1c?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/550cf8690d1c</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[least-mean-square]]></category>
            <category><![CDATA[adaptive-filter]]></category>
            <category><![CDATA[signal-processing]]></category>
            <category><![CDATA[neural-networks]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 06:08:21 GMT</pubDate>
            <atom:updated>2025-06-04T06:46:54.571Z</atom:updated>
            <content:encoded><![CDATA[<p>Least Mean Squares Algorithm: Foundation of Neural Networks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J7D9qTIJv2J_2uvV-mLlSQ.png" /></figure><h3>Abstract</h3><p>Linear Least Squares Filter is a fundamental concept in signal processing that minimizes the sum of the error squares between the estimated and actual signal value. Perceptron is a linear classifier whereas the linear least squares filters facilitate perception as an adaptive filter providing a one-time solution. <strong>Gauss-Newton method</strong> can be used to make the <strong>Perceptron</strong> algorithm behave similarly to a <strong>Linear Least Squares (LLS) filter. </strong>This article focuses on the mathematical and theoretical concepts behind the LLS filter derivation.</p><h3>Introduction</h3><p>The least-mean-square (LMS) algorithm, developed by Widrow and Hoff (1960), was the first linear adaptive-filtering algorithm for solving problems such as prediction and communication-channel equalization[1].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jDZnS6kKeXiS_KzpvrriLw.png" /></figure><h3>What is a Pseudo-Inverse?</h3><p>The <strong>pseudo-inverse</strong> is a generalization of the <strong>matrix inverse</strong> that can be used even when the matrix:</p><ul><li>is <strong>not square</strong> (like m×n, where m≠n)</li><li>is <strong>not full rank</strong> (some rows/columns are linearly dependent)</li></ul><p>It is used to <strong>solve systems of linear equations</strong> when an exact inverse does <strong>not exist</strong>.</p><p><strong>Notation:</strong></p><p>The pseudo-inverse of matrix A is written as:</p><p><strong>In Least Squares Problems:</strong></p><p>When solving:</p><blockquote><strong>Ax = b</strong></blockquote><p>and A is not invertible or not square, we use the pseudo-inverse:</p><blockquote><strong>x = A⁺ b.</strong></blockquote><p>In our equation:</p><blockquote><strong>(xᵀx)⁻¹xᵀ</strong></blockquote><p>This expression <strong>acts like the pseudo-inverse</strong> of x. In fact, if x is full-rank, the <strong>Moore-Penrose Pseudo-Inverse</strong> of x is:</p><blockquote><strong>x⁺ = (xᵀx)⁻¹xᵀ</strong></blockquote><p>This is how you find the <strong>best fit</strong> w in the <strong>least squares</strong> sense, even when there’s no perfect solution.</p><h3>Concept of Stationary Process, <strong>Random Process X(t)</strong> and Ergodicity</h3><h4>1. Random Process and Sample Function:</h4><p>A random process is denoted by: <strong>X(t)</strong></p><p>where ‘t’ represents time (continuous or discrete).</p><p>A single realization of the process (i.e., one possible outcome) is denoted by:</p><p><strong>x(t) ∈ X(t)</strong>. This is also called a sample function.</p><p>In discrete-time, the process is represented as: <strong>x(n)</strong></p><p><strong>Stationarity</strong> means the statistical properties of a random process don’t change over time. This is a core property for random processes in many fields, including signal processing.</p><p><strong>Random Process X(t)</strong>:</p><p>X(t) refers to a random process at time ‘t’. A “sample function” is one realization or outcome of this random process, denoted by x(t).</p><h3>Ergodicity Assumption:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/195/1*5a2TT0QuHvKTo5fgyNcN8Q.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*n6thYj5NJPlTyley025wGA.gif" /></figure><p>A <strong>random process</strong> is said to be <strong>ergodic</strong> if the <strong>time averages</strong> of a single realization are equal to the <strong>ensemble averages</strong> across all realizations.</p><p>In practice, when we only observe <strong>one realization</strong> of a process (e.g., one signal or data stream), ergodicity lets us assume that analyzing this one sample over a long time gives us the same result as analyzing <strong>many different possible outcomes</strong> at a single moment in time.</p><pre>🧾 Mathematically:<br><br>Let X(t) be a random process and x(t) be a single realization (sample function). <br>Then:<br><br>Time Average:<br><br>   x̄ = lim (T→∞) [1 / (2T)] ∫[-T, T] x(t) dt<br><br>Ensemble Average (Expectation):<br><br>   E[X(t)] = ∫[-∞, ∞] x * p_X(x, t) dx<br><br>Ergodicity Assumption implies:<br><br>   x̄ = E[X(t)]</pre><h3>Handwritten Derivations of LLS Filters</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GTu1pRlvBEPEyfhq1OLF4A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wtSqFj-nL-n3StIcdRHxKQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OSDcZY-AD7fmt6b2Fal1aA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vnEuI0Tsq13rVoljqq5T6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*up0Y3-4kp7BnJfSYQMRF2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MT1Aq1h7kmhvuA-SyeMShg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*49N-Dy8YwtuttXx1xzQ7ow.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QS2Zhw540m61yGc1t7GU5A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B8NvrvoXlEIKGn6WqVjJvA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8a2a3yVJuHgJWBXDIaX26A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dXWDj-vCq2A6cS6C6tPtWQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xlwLi2bJvYKTZ7LSRVhMbw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aACScByt8x_9R5ZJ1N-8Ig.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cZOkIDnVzGyYwbUehb5BOw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6HtQU74r4axAfING8B5mbw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dx2F4iJEbMHpBNoo7Rmj4A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bdJwjWhpGpnN1tTR9OsAkg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kOW4v3vJ4npOgty0Tx_XQw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cuxZCXOgn_pSUrw46vrkyg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8nGJk8GD0tG-LZCPAhrWdA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D2z5h2MX92FnYzxFKGidqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6iJwIse2LXDvlYDBPhZ_rw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wjiL0K5flAZ7CzAdHV1z9w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*grdUrQlwVdUhaoDX_Mnj-A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ILaTEhnzqyyo6ROBXYj0DQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AXAt7ZGl3t6GQ1ggof5Kkg.png" /></figure><h3>Conclusion</h3><p><strong>The adaptive filtering</strong> process allows the system to learn and reduce error over time through weight adjustments, forming the foundation for both <strong>neural networks</strong> and <strong>signal processing</strong>. In the derivation of the <strong>Linear Least Squares (LLS) Filter</strong>, several mathematical concepts and methods are used, including the <strong>Pseudo-inverse</strong>, <strong>Jacobian</strong>, <strong>transpose</strong>, and <strong>Gauss-Newton modification</strong>. LLS filters are limited to linear systems that require full data, with performance affected by initial weights and linearity assumptions.</p><h3>Sources:</h3><ol><li>Haykin, S. (2009). <em>Neural Networks and Learning Machines</em> (3rd ed.). Pearson Education.​</li><li>YouTube. (n.d.). <em>Linear least squares filters</em> [Video]. YouTube. Retrieved from <a href="https://www.youtube.com/watch?v=7L6JqX6oJZQ&amp;list=PL3EA65335EAC29EE8&amp;index=14">https://www.youtube.com/watch?v=7L6JqX6oJZQ&amp;list=PL3EA65335EAC29EE8&amp;index=14</a></li><li>Sengupta, S. (2009). <em>Neural Networks and Applications</em> [Video lecture series]. Indian Institute of Technology Kharagpur. Retrieved from <a href="https://www.youtube.com/watch?v=7L6JqX6oJZQ&amp;list=PL3EA65335EAC29EE8&amp;index=14">https://www.youtube.com/watch?v=7L6JqX6oJZQ&amp;list=PL3EA65335EAC29EE8&amp;index=14</a></li></ol><h3>Appendix:</h3><p><strong>Example:</strong> <em>“Appendix A: Code Implementation”<br> The full code for the Linear Least Squares filter implementation used in this study is available in the following Google Colab notebook: </em><a href="https://colab.research.google.com/drive/1dA9G8jYJg5NZi5Xhzv0p1k21b72hruuO?usp=sharing"><em>Google Colab notebook.</em></a><em>”</em></p><h3>Tools</h3><ol><li>OpenAI. (n.d). <em>ChatGPT</em> (April 2025 version)</li><li>Google. (n.d.). <em>Google search</em>. Retrieved April, 2025</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=550cf8690d1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Parsers Part II: SLR(1),CLR(1),LALR(1)]]></title>
            <link>https://medium.com/@dillihangrae/introduction-to-parsers-part-ii-slr-1-clr-1-lalr-1-4248bfea22a6?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/4248bfea22a6</guid>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[automata]]></category>
            <category><![CDATA[parser]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[compiler-design]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Sat, 22 Mar 2025 13:57:25 GMT</pubDate>
            <atom:updated>2025-06-04T06:50:53.134Z</atom:updated>
            <content:encoded><![CDATA[<p>Compiler Design and Construction | Tutorial</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XjepiAkcIfp3leHCpKaDdQ.jpeg" /></figure><h3>Abstract</h3><p>Compiler design and construction is the process of building software that translates high-level code into machine-executable instructions. A parser is found in the syntax analysis of the compilation process. SLR(1) is the simplest but is limited in handling complex grammar. LALR(1) is more powerful as it merges states for better complexity management. CLR(1) is the most powerful, handling all unambiguous grammars with the largest parsing table. This article explores the tutorials and demonstrates some simple handwritten numerical examples on bottom-up parsers SLR(1), CLR(1), and LALR(1).</p><h3>1. Introduction</h3><p>SLR (Simple LR) is a basic LR parser that uses smaller tables. While it is less powerful, it is efficient for certain tasks. LALR (Look-Ahead LR) improves upon SLR by merging states to reduce the size of the parsing table, making it more powerful than SLR and commonly used in many compilers. CLR (Canonical LR(1)) represents the full LR(1) parsing approach; it is the most powerful of the three, but it requires more memory.</p><p>A <strong>Canonical Set </strong>in LR parsing is a collection of parsing steps (called items) that show how far we have read a grammar rule. Each item has a dot (·) marking progress and a lookahead symbol to guide decisions.</p><pre><br>Example:<br>For the grammar:<br><br>S → A a  <br>A → b | c  <br>Some items in a Canonical Set could be:<br><br>S → A · a   (We have read A, waiting for ‘a’)  <br>A → · b     (We expect ‘b’ next)  <br>A → · c     (We expect ‘c’ next)   which is set and which are the items</pre><p><strong>Items</strong> are the individual rules with the dot (·) showing parsing progress. Each line below is an <strong>item</strong>:</p><pre>Canonical Items:<br>S → A · a   (We have read A, waiting for ‘a’)  <br>A → · b     (We expect ‘b’ next)  <br>A → · c     (We expect ‘c’ next)  <br><br>Canonical Set:<br>{  <br>  S → A · a  <br>  A → · b  <br>  A → · c  <br>}  </pre><h3>2. Inadequate State in LR Parsing</h3><p>A state in an LR parsing table is called <strong>inadequate</strong> if it contains <strong>at least one Reduce-Reduce (RR) conflict or Shift-Reduce (SR) conflict</strong>, making it <strong>impossible to determine the next parsing action uniquely</strong>.</p><h4>2.1 Intuitive Examples of Inadequate States in LR Parsing</h4><p>Let’s break it down with simple, real-world analogies and examples to make the idea of <strong>inadequate states</strong> (which cause parsing conflicts) more intuitive.</p><h4><strong>Reduce-Reduce (RR) Conflict: Two People Giving Conflicting Directions</strong></h4><p>Imagine someone is at a crossroads and asking <strong>two different people</strong> for directions.</p><ul><li>Person 1 says: <strong>“Go left.”</strong></li><li>Person 2 says: <strong>“Go right.”</strong></li><li><strong>Here someone is stuck</strong> because he/she doesn’t know whom to follow.</li></ul><p><strong>Parsing Analogy:</strong></p><pre>Suppose a state has two reduction rules:<br>A→α⋅ <br>B→β⋅<br>The parser doesn&#39;t know whether to reduce A → α or B → β.</pre><pre>💡 Example Grammar:<br>S→EF <br>E→ϵ<br>F→ϵ</pre><ul><li>Both E and F can reduce to <strong>ϵ (empty string)</strong>.</li><li>If the parser reaches a state where both reductions are possible, it <strong>doesn’t know which one to choose</strong>, leading to an <strong>RR conflict</strong> → <strong>inadequate state</strong>.</li></ul><h4><strong>2.2 Shift-Reduce (SR) Conflict: When a Cashier is Confused</strong></h4><pre>Parsing Analogy:<br>Suppose a state has both:<br><br>S → A⋅a<br>A → α⋅<br>The parser doesn&#39;t know whether to shift (read more input) or reduce A to α.<br><br>💡 Example Grammar:<br>S→Aa<br>A→ϵ<br>When the parser reads A → ε, it has two choices:<br>Reduce A→ϵ mmediately.<br>Shift the next input symbol a.<br>The parser is confused, leading to an SR conflict → inadequate state.</pre><h4><strong>Simplest Possible Example of Shift-Reduce (SR) Conflict 🚦</strong></h4><p>Imagine we are at a <strong>traffic light</strong> 🚦.</p><p>🔴 <strong>RED LIGHT (Stop!)</strong> → You should <strong>stop</strong> (Reduce).<br>🟢 <strong>GREEN LIGHT (Go!)</strong> → You should <strong>keep moving</strong> (Shift).</p><p>Now, what if <strong>BOTH LIGHTS</strong> are on <strong>at the same time</strong>? 😲</p><p>💡 We <strong>don’t know what to do!</strong></p><ul><li>Should you <strong>STOP</strong>? (Reduce ❌)</li><li>Should you <strong>GO</strong>? (Shift ❌)</li></ul><p>This <strong>confusion</strong> is a <strong>Shift-Reduce Conflict</strong>!</p><h3>3. Parsers Power Comparison</h3><h4>LL(1) &lt; SLR(1) &lt; LALR(1) &lt; CLR(1) = LR(1)</h4><ul><li><strong>LL(1)</strong> is the least powerful (top-down).</li><li><strong>LR(1)</strong> and <strong>CLR(1)</strong> are the most powerful (bottom-up).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IZEGO4No2TQpG801.png" /></figure><h3>4. SLR(1) HandWritten Example</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7VCwIhjwSBGHKcGcydPjQw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yXJJMVLXgiPgfDcjZdLmWg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4YRT42bYxQgwm4wNBXB3Fg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3OIUIIVN29fCE8zbXHoJ1A.jpeg" /></figure><h3>5. CLR(1) HandWritten Example</h3><h4>CLR(1) Example 1</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1q-mFpp9t-9o6AWE3LOotQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FKJ8A0XrVRwX12gq-9F93Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zleatsue0u-57rBoyrZzdw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1q-mFpp9t-9o6AWE3LOotQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FKJ8A0XrVRwX12gq-9F93Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zleatsue0u-57rBoyrZzdw.jpeg" /></figure><h4>CLR(1) Example 2</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1q-mFpp9t-9o6AWE3LOotQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FKJ8A0XrVRwX12gq-9F93Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zleatsue0u-57rBoyrZzdw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1hAJ3S_B96798qZZllXCGw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c9r_qlJYf6ubhvjiAJlAUQ.jpeg" /></figure><h4>CLR(1) <strong>Example 3</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RoSFI9cnxbzX2bB86zR08A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uXJfB5oK9G14vBshRDlVmQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T1xSQ1V6Qmq5b1JyGTYSRA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1fjvBguORsTBjK1aSLvTlw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_idC7QtOs-U-KxSz0aFzBw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R94rLHzDhxIe1X9HE2C8SQ.jpeg" /></figure><h3>5. LALR Handwritten and TextBook Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ooQtSMDzSBnqelNwLyc-Ng.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8XSYFPsXvQATtLUggqQYfw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*5CGEt7gKOCIPcdfXmlxROA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*y1GgFMCuDgXNo2E26s5X2A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*AkyRtSwR4_e4xlqzplCK_g.png" /></figure><h3>Conclusion</h3><p>The Deterministic Finite Automata (DFA), along with the dot concept (item representation) and follow sets, play a crucial role in constructing these parsers by defining valid transitions and reducing conflicts(SR, RR). LALR(1) is the minimized version of CLR(1) because CLR(1) is a powerful parser that is computationally expensive. CLR(1) is the advanced and successor of SLR(1), LR(0) bottom-up parsers. LL(1) overpower SLR(1), LR(0) parser. With accurate algorithms and parser programs, the practical implementation of these parsing techniques can be effectively realized. Even though CLR(1) most real-world language parser, it can still suffer from ambiguous grammar and Conflicts(SR-RR) in certain cases.</p><h3>References:</h3><ol><li>Aho, A. V., Sethi, R., &amp; Ullman, J. D. (2007). *Compilers: Principles, techniques, and tools*. Pearson Education.</li></ol><h3>Sources:</h3><ol><li>Compiler design. (2023, October 26). <em>Wikipedia</em>. <a href="https://en.wikipedia.org/wiki/Compiler_design">https://en.wikipedia.org/wiki/Compiler_design</a></li><li>Bhupendra Singh Saud and Indra Chaudhary, <em>Compiler Design and Construction</em>, First Edition (KEC Publication and Distribution Pvt. Ltd., 2077</li><li>Gate Smashers. (2020, October 25). <em>Lec-14: LALR Parsing Table | LALR vs CLR | Compiler Design</em> [Video]. YouTube. <a href="http://www.youtube.com/watch?v=GOlsYofJjyQ">http://www.youtube.com/watch?v=GOlsYofJjyQ</a></li><li>Gate Smashers. (2020, October 4). <em>Lec-12: SLR(1) Parsing Table | Check Whether a Grammar is SLR(1) or Not | Bottom-Up Parser</em> [Video]. YouTube. <a href="http://www.youtube.com/watch?v=Z1Hu9TIef9k">http://www.youtube.com/watch?v=Z1Hu9TIef9k</a></li><li>KnowledgeGATE by Sanchit Sir. (2023, January 21). <em>SLR (1) Parsing Table | SLR Parser algorithm | Bottom up parsers | Compiler Design</em> [Video]. YouTube. <a href="http://www.youtube.com/watch?v=p7gsXHibb_w">http://www.youtube.com/watch?v=p7gsXHibb_w</a></li><li>THE GATEHUB. (2022, November 28). <em>SLR (1) Parser Example 1 | Simple LR Parser | SLR Parsing Table Example | Compiler Design</em> [Video]. YouTube. <a href="https://www.youtube.com/watch?v=eons9sss1xE&amp;pp=0gcJCfcAhR29_xXO">https://www.youtube.com/watch?v=eons9sss1xE&amp;pp=0gcJCfcAhR29_xXO</a></li><li>THE GATEHUB. (2020, July 13). CLR(1) Parser Example 1 | Canonical LR Parser | Compiler Design [Video]. YouTube. <a href="https://www.google.com/search?q=http://www.youtube.com/watch%3Fv%3Ds2V8P_aOOSk">http://www.youtube.com/watch?v=s2V8P_aOOSk</a></li><li>THE GATEHUB. (2020, July 13). CLR(1) Parser Example 2 | Canonical LR Parser | Compiler Design [Video]. YouTube. <a href="http://www.youtube.com/watch?v=Go4cG3jHnNE">http://www.youtube.com/watch?v=Go4cG3jHnNE</a></li><li>THE GATEHUB. (2020, July 14). CLR(1) Parser Example 3 | Canonical LR Parser | Compiler Design [Video]. YouTube. <a href="http://www.youtube.com/watch?v=JwStWxFWfzQ">http://www.youtube.com/watch?v=JwStWxFWfzQ</a></li></ol><h3>Tools</h3><ol><li>OpenAI. (n.d). <em>ChatGPT</em> (March 2025 version)</li><li>Google. (n.d.). <em>Google search</em>. Retrieved March, 2025</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4248bfea22a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Parsers Part I: LL(1) And LR(0)]]></title>
            <link>https://medium.com/@dillihangrae/introduction-to-parsers-part-i-ll-1-and-lr-0-2e8f1d6c9e39?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e8f1d6c9e39</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[compiler-design]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[compiler-construction]]></category>
            <category><![CDATA[parser]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Wed, 19 Mar 2025 18:41:23 GMT</pubDate>
            <atom:updated>2025-11-30T08:42:39.859Z</atom:updated>
            <content:encoded><![CDATA[<p>Compiler Design and Construction</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SRzBMQyqHsixBrjAovPDpQ.png" /></figure><h3>Abstract</h3><p>A parser is a program or component that analyzes and converts input data (like code or text) into a structured format for processing. G<strong>rammar</strong> is a set of formal rules that define the syntax of a programming language. While SDT and SDD are used for the semantics. Before the input set of symbols moves into the Semantic Phase, the parser like LL(1), and LR(0) perform the <strong>syntax analysis and various key functions. In this article, </strong>we are going to understand and learn simple examples of LL(1), and LR(0)parsers using tutorials. The basic concepts like left-most derivation, syntax tree, parsing conflicts, parser types, phases in the compiler, etc are briefly discussed.</p><h3>1. Introduction</h3><p><strong>LL(1) (Top-Down) r</strong>eads <strong>one token ahead</strong>, decides early, and expands step by step. <strong>LR(0) (Bottom-Up) r</strong>eads everything first, then groups and reduces later.</p><h3>1.1 LL(1) Parser</h3><p><strong>Top-Down Parsing (Left-to-right, Leftmost derivation, 1-token lookahead)</strong></p><ul><li><strong>L</strong> → Reads input <strong>Left to right</strong></li><li><strong>L</strong> → Produces <strong>Leftmost derivation</strong></li><li><strong>(1)</strong> → Uses <strong>1-token lookahead</strong></li></ul><h4>1.2 Left-to-Right Scanning:</h4><p>We process characters from left to right.</p><ul><li>Example: For “abc”, the scan order is <strong>a → b → c</strong>.</li></ul><h4>1.3 Right-to-Left Scanning:</h4><p>We process characters from right to left.</p><ul><li>Example: For “abc”, the scan order is <strong>c → b → a</strong>.</li></ul><h4>1.4 Leftmost Derivation:</h4><p>In <strong>leftmost derivation</strong>, we always replace the leftmost non-terminal first.</p><h4>Example:</h4><p>Grammar:</p><ul><li>S→AB</li><li>A→a</li><li>B→b</li></ul><p>Steps for <strong>leftmost derivation</strong>:</p><ol><li>Start with S.</li><li>Replace S⇒AB</li><li>Replace AB ⇒aB</li><li>Replace B with b: aB⇒ab</li></ol><h4>1.5 Rightmost Derivation:</h4><p>In the <strong>rightmost derivation</strong>, you always replace the rightmost non-terminal first.</p><h4>Example:</h4><p>Steps for <strong>rightmost derivation</strong>:</p><ol><li>Start with S.</li><li>Replace S with AB: S⇒AB</li><li>Replace B (the rightmost non-terminal) with b: AB⇒Ab</li><li>Replace A with a: Ab⇒ab</li></ol><h4>1.6 Syntax Tree for Both:</h4><p>Both derivations lead to the same tree, but the process of derivation is different.</p><pre>    S<br>   / \<br>  A   B<br>  |   |<br>  a   b</pre><p>In summary:</p><ul><li><strong>Leftmost</strong>: Replace the leftmost non-terminal first.</li><li><strong>Rightmost</strong>: Replace the rightmost non-terminal first.</li></ul><h3>2. Types of Parsers:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/0*AUbrTJU6Qd1x7g7s.jpg" /><figcaption>Retrieved from <a href="https://livebook.manning.com/book/dsls-in-action/chapter-7/">Chapter 7. External DSL implementation artifacts · DSLs in Action</a></figcaption></figure><ol><li><strong>Top-Down Parsers</strong> (Starts from the start symbol and derives input):</li></ol><ul><li><strong>Recursive Descent Parser</strong> (Uses recursive functions)</li><li><strong>LL(1) Parser</strong> (Left-to-right, Leftmost derivation with 1-token lookahead)</li></ul><p><strong>2. Bottom-Up Parsers</strong> (Starts from input and reduces to the start symbol):</p><ul><li><strong>LR Parsers</strong> (Left-to-right, Rightmost derivation in reverse)</li><li><strong>SLR (Simple LR)</strong></li><li><strong>CLR (Canonical LR)</strong></li><li><strong>LALR (Look-Ahead LR)</strong></li></ul><h3>3. Phases in Compiler</h3><ul><li><strong>Lexical Analysis</strong> → Converts source code to tokens.</li><li><strong>Syntax Analysis (Parsing)</strong> → Checks grammatical structure and builds parse tree.</li><li><strong>Semantic Analysis</strong> → Ensures meaning is correct (e.g., type-checking).</li><li><strong>Intermediate Code Generation</strong> → Converts to an intermediate form.</li><li><strong>Optimization</strong> → Improves efficiency.</li><li><strong>Code Generation</strong> → Produces machine code.</li><li><strong>Code Linking &amp; Execution</strong> → Final executable.</li></ul><h3>4. 1-Token Lookahead Concept</h3><ul><li><strong>“1-token lookahead”</strong> means <strong>before applying a rule, the parser checks the next token</strong> in the input.</li><li>The parser <strong>always looks 1 token ahead</strong> to decide <strong>which production rule to apply</strong>.</li></ul><pre>Given Grammar<br>S → a S b | ε</pre><ul><li>If <strong>next token is </strong><strong>a</strong>, use <strong>S → a S b</strong>.</li><li>If <strong>next token is </strong><strong>b</strong>, use <strong>S → ε</strong> (stop expanding).</li></ul><h4>4.1 Parsing &quot;a a b b&quot; Using Lookahead (LL(1))</h4><pre>Parsing &quot;a a b b&quot; Using Lookahead (LL(1))<br><br>Step Stack (To Process) Next Token (Lookahead) Action<br>1     S                    a                    S → a S b (because next token is a)<br>2     a S b                a                    S → a S b again (next token is still a)<br>3     a a S b b            b                    S → ε (because next token is b, stop expanding S)<br>4     a a b b              b                    S → ε (next token is still b, stop expanding S)<br>Parsed successfully!</pre><h3>5. Parsing Without Lookahead (LL(0) or LR(0) or LR(K) where K in finite number)</h3><pre>Grammar:<br>S → a S b | ε</pre><h4>5.1 LL(0) Parsing &quot;a a b b&quot; (No Lookahead)</h4><pre>Step      Stack                  Action<br>1          S                     Expand S → a S b (blindly)<br>2          a S b                 Expand S → a S b again (blindly)<br>3          a a S b b             Expand S → a S b again (blindly)<br>❌ Error    a a a S b b b        Too many a&#39;s! Parsing fails!</pre><h4>5.2 Why LL(0) Fails?</h4><ul><li><strong>No lookahead → Expands </strong><strong>S too much.</strong></li><li><strong>Doesn’t check if the next token is </strong><strong>b before expanding.</strong></li><li><strong>Too many </strong><strong>a&#39;s, so parsing fails.</strong></li></ul><h3>6. Handwritten Examples</h3><h4>6.1 LL(1) Example</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tmr6mKm2rnjnGlFyU9zWcw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s63b5-pCXGlhM6lnibfi6A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JhEDEuha9d0zLZt9iTm21A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QEjDoUjDId5y84c7JSsv6w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m2sdVMmjp1WyBjpnvRygIA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gv4M6_e6ffcCP4MY0z1F1w.jpeg" /></figure><h4>6.2 LR(0) HandWritten Example</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lFQSPPyM3iF3WB8M3wEucg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gQapYmRSc0QGKBBYMSr3uQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wh-kr9uQd7_VgMNip2b8lg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XjahK-3me_uIXZssojbQdQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_g2QNswY_NpMWn9aKXgFCA.jpeg" /></figure><h3>7. Conflict</h3><p>In compiler design, <strong>conflicts</strong> occur during the <strong>parsing</strong> phase, when the parser cannot determine the correct action.</p><h4>7.1 Shift-Reduce Conflict:</h4><p>It occurs when the parser can’t decide whether to <strong>shift</strong> (read the next token) or <strong>reduce</strong> (apply a rule).</p><pre>Example: For the grammar:<br>S → A a<br>A → b<br><br>Input: <br>b a The parser can either shift (move to a) or reduce (A → b), <br>causing a conflict.</pre><h4>7.2 Reduce-Reduce Conflict:</h4><p>Happens when multiple rules can be applied to the same input.</p><pre>Example: For the grammar:<br>S → A | B<br>A → a<br>B → a<br>Input: <br>a The parser can reduce either using A → a or B → a, causing a conflict.</pre><h3>8. Discussion</h3><p><strong>LL(1)</strong> and <strong>LR(0)</strong> are distinct parsers used to determine whether a grammar is acceptable. The <strong>LL(1) parser</strong> employs a <strong>1-token lookahead</strong> technique, computing the <strong>FIRST()</strong> and <strong>FOLLOW()</strong> sets for the grammar. It then fills the <strong>parsing table</strong> using the <strong>FIRST()</strong> sets of non-terminals. If each cell in the table contains <strong>no more than one entry</strong>, the grammar is <strong>LL(1)</strong>. If there are conflicts (i.e., multiple entries in a cell), the grammar is <strong>not LL(1)</strong>. On the other hand, the <strong>LR(0) parser</strong> constructs a <strong>Deterministic Finite Automaton (DFA)</strong> with states that track the <strong>dot pointer</strong> position during parsing. It derives the <strong>parsing table</strong> from this DFA, and if no <strong>shift-reduce</strong> or <strong>reduce-reduce</strong> conflicts are found, the grammar is <strong>LR(0)</strong>. In essence, <strong>LL(1)</strong> parses top-down using a lookahead token and checks for conflicts in its table, while <strong>LR(0)</strong> parses bottom-up using a DFA and resolves conflicts during table construction.</p><h3>Conclusion</h3><p>In conclusion, <strong>LL(1)</strong> parsing is a simple, top-down approach using a 1-token lookahead, but it can suffer from <strong>FIRST/FOLLOW</strong> conflicts, especially with <strong>left-recursive</strong> or <strong>ambiguous grammar</strong>. <strong>LR(0)</strong> parsing, a bottom-up method using a DFA, is more powerful and handles a broader range of grammars, but it can encounter <strong>shift-reduce (SR)</strong> and <strong>reduce-reduce (RR)</strong> conflicts. <strong>LL(1)</strong> and <strong>LR(0)</strong> parsers are widely used in compiler design for syntax analysis, enabling efficient parsing of programming languages with predictable grammar structures.</p><h3>Sources:</h3><ol><li>THE GATEHUB. (2020, June 25). <em>LL(1) parser | Example 1 | Top Down Parser | Compiler Design</em> [<a href="http://www.youtube.com/watch?v=en0Mptigzkw">Video</a>]. YouTube.</li><li>Gate Smashers. (2020, September 27). <em>Lec-8: LL(1) Parsing Table | Check Whether a Grammar is LL(1) or Not</em> [<a href="http://www.youtube.com/watch?v=WTxdKQmsfho">Video</a>]. YouTube.</li><li>Gate Smashers. (2020, September 30). <em>Lec-11: LR(0) Parsing Table | Check Whether a Grammar is LR(0) or not | Bottom-Up Parser</em> [<a href="http://www.youtube.com/watch?v=J4ZME5KOB-s">Video</a>]. YouTube.</li><li>THE GATEHUB. (2020, July 2). <em>LR(0) parser || Example 1 || LR(0) parsing table || Compiler Design</em> [<a href="http://www.youtube.com/watch?v=sm_hwNDzc-o">Video</a>]. YouTube.</li><li>Bhupendra Singh Saud and Indra Chaudhary, <em>Compiler Design and Construction</em>, First Edition (KEC Publication and Distribution Pvt. Ltd., 2077</li></ol><h3>Tools</h3><ol><li>OpenAI. (2023). <em>ChatGPT</em> (March 2023 version)</li><li>Google. (n.d.). <em>Google search</em>. Retrieved March 14, 2025</li></ol><p>Note: Report if any error found in the articles solved numericals.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e8f1d6c9e39" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[First() And Follow() Sets Examples]]></title>
            <link>https://medium.com/@dillihangrae/first-and-follow-sets-examples-879e5569d7f1?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/879e5569d7f1</guid>
            <category><![CDATA[compiler-design]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[parser]]></category>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Fri, 14 Mar 2025 10:43:38 GMT</pubDate>
            <atom:updated>2025-06-26T14:47:52.187Z</atom:updated>
            <content:encoded><![CDATA[<p>Compiler Design and Construction | Tutorial &amp; Implementation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JWrDX2XM1f3ANlAYrLZR-w.jpeg" /></figure><h3>Abstract</h3><p>A Set is a collection of well-defined objects/elements. Non-terminals are those sets of symbols that derive/replace another set of symbols whereas terminals cannot. Without first() and follow() sets <strong>parsers</strong> are not able to decide actions like <strong>What to do next, How to handle empty rules (ε), or, Which symbol to expect. Production rules</strong> of the grammar, <strong>non-terminals, and terminals in the grammar, as well as a</strong> systematic process to compute the sets by looking at the structure of the rules, are required to Compute the First() and Follow(). This article dives into algorithms and simple examples of computing First() and Follow().</p><h3>1. Introduction</h3><h4><strong>1.1 FIRST()</strong></h4><p><strong>The first set</strong> of a non-terminal <strong>X</strong> lists the <strong>first symbols</strong> (terminals or ε) that can start the strings made from <strong>X</strong>.</p><h4><strong>1.2 FOLLOW()</strong></h4><p>The <strong>FOLLOW set</strong> shows the symbols that can appear <strong>right after</strong> a non-terminal in a derivation.</p><p>For a given context-free grammar</p><p>S → A B C<br>A → a | ε<br>B → b<br>C → c | ε</p><p><strong>FIRST(S) = {a, ε} // corrected. FIRST(S) = {a,b} since A -&gt; a| </strong>ε<strong><br>FIRST(A) = {a, ε}<br>FIRST(B) = {b}<br>FIRST(C) = {c, ε}</strong></p><p><strong>FOLLOW(S) = {$}<br>FOLLOW(A) = {b}<br>FOLLOW(B) = {c}<br>FOLLOW(C) = {$}</strong></p><h3>Example 1:</h3><p>Consider the following simple grammar:</p><ol><li><strong>S→ABS</strong></li><li><strong>A→a</strong></li><li><strong>B→b</strong></li></ol><h4>FIRST Set:</h4><ul><li><strong>FIRST(A)</strong>:</li><li>A directly derives ‘a’ (i.e., A→a).</li><li>So, <strong>FIRST(A) = {a}</strong>.</li><li><strong>FIRST(B)</strong>:</li><li>B directly derives ‘b’ (i.e., B→b).</li><li>So, <strong>FIRST(B) = {b}</strong>.</li><li><strong>FIRST(S)</strong></li><li>S derives A first, and <strong>FIRST(A)</strong> is {a}.</li><li>So, <strong>FIRST(S) = {a}</strong>.</li></ul><h4>FOLLOW Set:</h4><ul><li><strong>FOLLOW(S)</strong>:</li><li>S is the start symbol, so <strong>FOLLOW(S) = {$}</strong> (the end-of-input marker).</li><li><strong>FOLLOW(A):</strong></li><li>A is followed by B in the production S→AB</li><li>FIRST(B)=b, so add ‘b’ to <strong>FOLLOW(A)</strong>.</li><li>So, <strong>FOLLOW(A) = {b}</strong>.</li><li><strong>FOLLOW(B)</strong>:</li><li>B is the last symbol in the production S→AB, so <strong>FOLLOW(B) = FOLLOW(S)</strong>.</li><li>Since <strong>FOLLOW(S) = {$}</strong>, <strong>FOLLOW(B) = {$}</strong>.</li></ul><h4>Summary:</h4><ul><li><strong>FIRST(S) = {a}</strong></li><li><strong>FIRST(A) = {a}</strong></li><li><strong>FIRST(B) = {b}</strong></li><li><strong>FOLLOW(S) = {$}</strong></li><li><strong>FOLLOW(A) = {b}</strong></li><li><strong>FOLLOW(B) = {$}</strong></li></ul><h3><strong>2. Compute Follow() Rules:</strong></h3><ol><li>If A is a starting symbol of given grammar G then FOLLOW(A)={$]</li><li>For every production B → α A β, where α and β are any string of grammar symbols and A is non-terminal, then everything in FIRST(β) except ε is placed on FOLLOW(A)</li><li>For every production B → α A, or a production B → α A β, FIRST(β) contains ε, then everything in FOLLOW(B) is added to FOLLOW(A)</li></ol><h3>3. Compute First() Rules:</h3><ol><li>If ‘a’ is terminal, then FIRST(a) is {a}.</li><li>If A→ε is a production, then add ε to FIRST(A).</li><li>For any non-terminal A with production rules A→α₁|α₂|α₃|…|α_n, then FIRST(A) = FIRST(α₁) U FIRST(α₂) U FIRST(α₃) U … U FIRST(α_n)</li><li>If the production rule is of the form A→B₁B₂B₃…B_n, then FIRST(A) = FIRST(B₁B₂B₃…B_n)</li></ol><h3>4. Implementation of Follow()</h3><pre>#include&lt;stdio.h&gt;<br>#include&lt;string.h&gt;<br>#include&lt;ctype.h&gt;<br><br>int n, p, i = 0, j = 0;<br>char a[10][10], Result[10];<br>char subResult[20];<br><br>void follow(char* Result, char c);<br>void first(char* Result, char c);<br>void addToResultSet(char[], char);<br><br>int main()<br>{<br>    int i;<br>    int choice;<br>    char c, ch;<br>    <br>    printf(&quot;Enter the no. of productions: &quot;);<br>    scanf(&quot;%d&quot;, &amp;n);<br>    <br>    printf(&quot;Enter %d productions\nProduction with multiple terms should be given as separate productions\n&quot;, n);<br>    for(i = 0; i &lt; n; i++)<br>        scanf(&quot;%s&quot;, a[i]);<br><br>    do<br>    {<br>        printf(&quot;Find FOLLOW of --&gt;&quot;);<br>        scanf(&quot; %c&quot;, &amp;c);<br>        follow(Result, c);<br>        <br>        printf(&quot;FOLLOW(%c) = { &quot;, c);<br>        for(i = 0; Result[i] != &#39;\0&#39;; i++)<br>            printf(&quot;%c &quot;, Result[i]);<br>        printf(&quot;}\n&quot;);<br>        <br>        printf(&quot;Do you want to continue (Press 1 to continue....)? &quot;);<br>        scanf(&quot;%d&quot;, &amp;choice);<br>        <br>    } while(choice == 1);<br>}<br><br>void follow(char* Result, char c)<br>{<br>    int k;<br>    subResult[0] = &#39;\0&#39;;<br>    Result[0] = &#39;\0&#39;;<br><br>    if(a[0][0] == c)<br>        addToResultSet(Result, &#39;$&#39;);<br>    <br>    for(i = 0; i &lt; n; i++)<br>    {<br>        for(j = 2; j &lt; strlen(a[i]); j++)<br>        {<br>            if(a[i][j] == c)<br>            {<br>                if(a[i][j+1] != &#39;\0&#39;)<br>                    first(subResult, a[i][j+1]);<br><br>                if(a[i][j+1] == &#39;\0&#39; &amp;&amp; a[i][0] != c)<br>                    follow(subResult, a[i][0]);<br>                <br>                for(k = 0; subResult[k] != &#39;\0&#39;; k++)<br>                    addToResultSet(Result, subResult[k]);<br>            }<br>        }<br>    }<br>}<br><br>void first(char* R, char c)<br>{<br>    int k, m;<br>    if(!isupper(c) &amp;&amp; c != &#39;#&#39;)<br>        addToResultSet(R, c);<br>    <br>    for(k = 0; k &lt; n; k++)<br>    {<br>        if(a[k][0] == c)<br>        {<br>            if(a[k][2] == &#39;#&#39; &amp;&amp; c == a[i][0])<br>                follow(R, a[i][0]);<br>            else if(!isupper(a[k][2]) &amp;&amp; a[k][2] != &#39;#&#39;)<br>                addToResultSet(R, a[k][2]);<br>            else <br>                first(R, a[k][2]);<br><br>            for(m = 0; R[m] != &#39;\0&#39;; m++)<br>                addToResultSet(Result, R[m]);<br>        }<br>    }<br>}<br><br>void addToResultSet(char Result[], char val)<br>{<br>    int k;<br>    for(k = 0; Result[k] != &#39;\0&#39;; k++)<br>        if(Result[k] == val)<br>            return;<br>    <br>    Result[k] = val;<br>    Result[k+1] = &#39;\0&#39;;<br>}<br><br>// Example of Interacting with the Follow() Computing Program<br>/**<br>Enter the no. of productions: 6<br>Enter 6 productions<br>Production with multiple terms should be given as separate productions<br>R=aS<br>R=(R)S<br>S=+RS<br>S=aRS<br>S=*S<br>S=ε<br>Find FOLLOW of --&gt;S<br>FOLLOW(S) = { $ ) + a * � }<br>Do you want to continue (Press 1 to continue....)? 1<br>Find FOLLOW of --&gt;R<br>FOLLOW(R) = { $ ) + a * � }<br>Do you want to continue (Press 1 to continue....)? <br>**/<br></pre><h3>5. Handwritten Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*BqSGpa4w2g0xXdxX_17NOQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*-KDmBuhdlDYPZ1nLXj203w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*8l0nfFb4QbYsuPGqtF1X0g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*xga4FxKrSdP5-aB9xtnV9w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/961/1*Hgfn91AM7X9sVWr564wF4Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/961/1*TKxZLGL9OPFgvj3HCoJdRw.png" /></figure><h3>6. Implementation of First()</h3><pre>#include &lt;stdio.h&gt;<br>#include &lt;ctype.h&gt;<br><br>void FIRST(char[], char);<br>void addToResultSet(char[], char);<br><br>int numOfProductions;<br>char productionSet[10][10];<br><br>int main()<br>{<br>    int i;<br>    char choice;<br>    char result[20];<br>    char c;<br><br>    printf(&quot;How many number of productions? : &quot;);<br>    scanf(&quot;%d&quot;, &amp;numOfProductions);<br>    getchar(); // to consume the newline character after scanf<br><br>    for(i = 0; i &lt; numOfProductions; i++) // Read production string e.g.: E=TA<br>    {<br>        printf(&quot;Enter production Number %d: &quot;, i + 1);<br>        fgets(productionSet[i], sizeof(productionSet[i]), stdin);<br>    }<br><br>    do<br>    {<br>        printf(&quot;\nFind the FIRST of: &quot;);<br>        scanf(&quot;%c&quot;, &amp;c);<br>        getchar(); // to consume the newline character after scanf<br>        FIRST(result, c); // Compute FIRST; Get Answer in &#39;result&#39; array<br>        printf(&quot;\nFIRST(%c) = { &quot;, c);<br>        for(i = 0; result[i] != &#39;\0&#39;; i++)<br>            printf(&quot;%c&quot;, result[i]);<br>        printf(&quot;}\n&quot;);<br>        printf(&quot;Press &#39;y&#39; to continue: &quot;);<br>        scanf(&quot;%c&quot;, &amp;choice);<br>        getchar(); // to consume the newline character after scanf<br>    } while(choice == &#39;y&#39; || choice == &#39;Y&#39;);<br><br>    return 0;<br>}<br><br>void FIRST(char *Result, char c)<br>{<br>    int i, j, k;<br>    char subResult[20];<br>    int foundEpsilon = 0;<br><br>    // Initialize the result array<br>    Result[0] = &#39;\0&#39;;<br><br>    // If X is terminal, FIRST(X) = {X}<br>    if (!isupper(c)) {<br>        addToResultSet(Result, c);<br>        return;<br>    }<br><br>    // If X is a non-terminal, then read each production<br>    for (i = 0; i &lt; numOfProductions; i++) {<br>        // Find production with X as LHS<br>        if (productionSet[i][0] == c) {<br>            j = 2; // Start with the right-hand side of the production<br><br>            while (productionSet[i][j] != &#39;\0&#39;) {<br>                foundEpsilon = 0;<br>                FIRST(subResult, productionSet[i][j]); // Get FIRST of current symbol<br><br>                // Add the symbols of subResult to Result<br>                for (k = 0; subResult[k] != &#39;\0&#39;; k++)<br>                    addToResultSet(Result, subResult[k]);<br><br>                // Check for epsilon ($)<br>                for (k = 0; subResult[k] != &#39;\0&#39;; k++) {<br>                    if (subResult[k] == &#39;$&#39;) {<br>                        foundEpsilon = 1;<br>                        break;<br>                    }<br>                }<br><br>                if (!foundEpsilon) break;<br>                j++; // Move to the next symbol<br>            }<br>        }<br>    }<br>}<br><br>void addToResultSet(char Result[], char val)<br>{<br>    int k;<br>    // Check if the value is already in the result set<br>    for (k = 0; Result[k] != &#39;\0&#39;; k++) {<br>        if (Result[k] == val)<br>            return;<br>    }<br>    // If not, add the value to the result<br>    Result[k] = val;<br>    Result[k + 1] = &#39;\0&#39;;<br>}<br>// S=L=R<br>// S=R<br>// L=*R<br>// L=a<br>// R=L<br><br>// How many number of productions? : 5<br>// Enter production Number 1: S=L=R<br>// Enter production Number 2: S=R<br>// Enter production Number 3: L=*R<br>// Enter production Number 4: L=a<br>// Enter production Number 5: R=L<br><br>// Find the FIRST of: S<br><br>// FIRST(S) = { *a}<br>// Press &#39;y&#39; to continue: y<br><br>// Find the FIRST of: L<br><br>// FIRST(L) = { *a}<br>// Press &#39;y&#39; to continue: y<br><br>// Find the FIRST of: *R<br><br>// FIRST(*) = { *}<br>// Press &#39;y&#39; to continue: y<br><br>// How many number of productions? : 3<br>// Enter production Number 1: S=AB<br>// Enter production Number 2: A=a<br>// Enter production Number 3: B=b<br><br>// Find the FIRST of: S<br><br>// FIRST(S) = { a}<br>// Press &#39;y&#39; to continue: y<br><br>// Find the FIRST of: A<br><br>// FIRST(A) = { a}<br>// Press &#39;y&#39; to continue: y<br><br>// Find the FIRST of: B<br><br>// FIRST(B) = { b}<br>// Press &#39;y&#39; to continue: <br></pre><h3>Conclusion</h3><p>The <strong>Follow</strong> set helps identify which symbols can appear immediately after a given non-terminal in a derivation. The <strong>First</strong> set, as we already know, identifies the terminals that can appear at the beginning of a string derived from a non-terminal. Computing Follow() is harder than First(), with the above program, it is expected to solve all types of Follow() and First() of a given grammar. <strong>First() &amp; Follow()</strong> are important and required for parsers.</p><h3>Sources</h3><ol><li>Bhupendra Singh Saud and Indra Chaudhary, <em>Compiler Design and Construction</em>, First Edition (KEC Publication and Distribution Pvt. Ltd., 2077</li><li>Compiler Design Tutorials. (2023, October 26). <em>Lec-6: Find FOLLOW() in Compiler Design | Learn First() &amp; Follow() in Easiest Way</em> <a href="https://www.youtube.com/watch?v=WUwG50xJ9vc&amp;list=PLxCzCOWd7aiEKtKSIHYusizkESC42diyc&amp;index=6"><strong>[Video]</strong></a></li><li>Compiler Design Tutorials. (2023, October 25). <em>Lec-5: Find First() in Compiler Design | First() &amp; Follow() | Compiler Design</em> <strong>[</strong><a href="https://www.youtube.com/watch?v=UXYqQ_CJsVE&amp;list=PLxCzCOWd7aiEKtKSIHYusizkESC42diyc&amp;index=5"><strong>Video</strong></a><strong>]</strong></li><li>Compiler Design Tutorials. (2023, October 27). <em>Find Follow Function | First and Follow in Compiler Design</em> <a href="https://www.youtube.com/watch?v=sWiXyhCwnsE&amp;t=592s"><strong>[Video]</strong></a>.</li></ol><h3>Tools</h3><ol><li>OpenAI. (2023). <em>ChatGPT</em> (March 2023 version)</li><li>Google. (n.d.). <em>Google search</em>. Retrieved March 14, 2025</li></ol><p>Any suggestions or errors found in the article, then DM/Comment:</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=879e5569d7f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Direct Conversion Method of Regex to DFA]]></title>
            <link>https://medium.com/@dillihangrae/direct-conversion-method-of-regex-to-dfa-80f6da15d42a?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/80f6da15d42a</guid>
            <category><![CDATA[regex]]></category>
            <category><![CDATA[dfa]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[compiler-design]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Wed, 12 Mar 2025 11:24:04 GMT</pubDate>
            <atom:updated>2025-03-12T12:31:55.041Z</atom:updated>
            <content:encoded><![CDATA[<p>Compiler Design and Construction</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5oU_NYyXQOfWb97qZxRuAg.jpeg" /></figure><h3>Abstract</h3><p>A regular expression (regex) is a formal way to describe a set of strings (a language) using a concise notation. Regex is recognized by a finite automaton (like a DFA or NFA). Regex to NFA and NFA to DFA is the traditional conversion method whereas the McNaughton-Yamada-Thompson algorithm efficiently converts regex directly to DFA. This article dives into the Algorithm and Syntax tree required for the direct conversion method of Regex to DFA.</p><h3>1. Introduction</h3><p>The McNaughton-Yamada-Thompson algorithm (often just called McNaughton-Yamada in some texts) converts a regular expression directly into a deterministic finite automaton (DFA) without explicitly constructing an intermediate nondeterministic finite automaton (NFA) as in Thompson’s construction.</p><p>Regex is widely used in computer science for pattern matching, searching, and text processing. Examples</p><ul><li>(a|b)* matches any string of as and bs (e.g., “”, “a”, “ab”, “ba”).</li><li>(a|b)*abb matches any string of as and bs ending with “abb” (e.g., “abb”, “aabb”, “baabb”).</li></ul><h3>2. Syntax Tree</h3><p>A syntax tree, also known as a parse tree or an abstract syntax tree (AST), is a tree-like representation of the structure of a sentence or expression, based on its grammatical rules.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/373/0*0WUH_Y2BgzFIQhL6.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/0*WTalqtgXI0-OZBfj.png" /></figure><pre>.Given Expression (a|b)*abb). <br>### Construction of Parse/Syntax /Abstract Syntax Tree.<br><br>       [.] (concatenation)<br>      / | \<br>    [*] a  [.] (concatenation)<br>    |     / \<br>   [|]   b   b<br>   / \<br>  a   b<br><br>Root: . (concatenation of (a|b)*, a, bb).<br>Left child: * (Kleene star).<br>Child of *: | (union of a and b).<br>Left child of |: a.<br>Right child of |: b.<br>Middle child: a.<br>Right child: . (concatenation of b and b).<br>Left child: b.<br>Right child: b.</pre><h3>3. Firstpos() ,Lastpos() and Followpos()</h3><h4>Grammar:</h4><p>A grammar is a set of rules to construct an expression or set of strings with the correct symbols placement.</p><ul><li>S → AB</li><li>A → a</li><li>B → b</li></ul><pre>    S<br>   / \<br>  A   B<br>  |   |<br>  a   b   <br><br>Given expression: a . b<br>       .<br>     /    \<br>{1}a{1} {2}b{2}<br>where a = C1 and b = C2<br>firstpos of C1 or firstpos(C1) = {1}<br>lastpos of C1 or lastpos(C1) = {1}<br>firstpos of C2 or firstpos(C2) = {2}<br>lastpos of C2 or lastpos(C2) = {2}</pre><h3>4. Algorithm</h3><h4>4.1 Augment the given expression</h4><pre>for an example we have expression like this:<br>(a|b)^*abb.<br>then the augmented form is<br>(a | b )^*. a. b . b ) See the dots concated between them</pre><h4>4.2 RE to DFA by Direct Method</h4><p>Rules for Computing the Function nullable, firstpos, and lastpos</p><pre>RE to DFA by Direct Method | Example 1 | Comiler Design<br><br>Node n                nullable(n)          firstpos(n)             lastpos(n)<br><br>A leaf labeled<br>by ɛ                  true                 Ø                      Ø<br><br>A leaf with<br>position i            false                {i}                    {i}<br><br>   *<br>  / \<br>c1  c2                nullable(c1) or      firstpos(c1) ∪         lastpos(c1) ∪<br>                      nullable(c2)         firstpos(c2)           lastpos(c2)<br><br>.<br>  / \<br>c1  c2                nullable(c1) and     if (nullable(c1))      if (nullable(c2))<br>                      nullable(c2)         firstpos(c1) ∪         lastpos(c1) ∪<br>                                           firstpos(c2)           lastpos(c2)<br>                                           else firstpos(c1)      else lastpos(c2)<br>   *<br>  / <br> c                    true                 firstpos(ci)            lastpos(ci)</pre><h4><strong>4.3. Computing </strong><strong>followpos()</strong></h4><p>To compute followpos for each position in the syntax tree. The provided code snippet outlines the algorithm:</p><pre><br><br>for (each node n in the tree)<br>{<br>    // n is a cat-node with left child c1 and right child c2<br>    if (n == c1 . c2 (concatenation-node))<br>    {<br>        for (each i in lastpos(c1))<br>        {<br>            followpos(i) = followpos(i) ∪ firstpos(c2);<br>        }<br>    }<br>    else if (n is a star-node)<br>    {<br>        for (each i in lastpos(n))<br>        {<br>            followpos(i) = followpos(i) ∪ firstpos(n);<br>        }<br>    }<br>}</pre><h3>5. Handwritten Notes</h3><p>Here the example solved handwritten examples for the above expression:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SrPiFXnZXKddmmzXpZfBig.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CCuploA4KEJ7VbVtUYb__g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F-kOCWaF-CUzHME27mb5Zw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RelgM4AGLeG4oZdwDjKPUA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6NCsK3y7wiZ6x_232zD90w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tc3cj9EsFQT4gypgbObvQQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z8aIfCmOKED-A4LFcxgkgQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hgbSp6kN0kzDkXDBneYHyA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5sC31L0xzcYT1RxuBMgJgw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MiTH2z7mxuN7qo8JvQepEg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ZhnJ67NMS2hiB4qFSdk6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7xI1Ajy9M5QKo5MLGMu0HA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7LHeWMMfNLBWwLsbPQid1w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1S3tfPXC041ECReO11NlOw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_0pI2aWOUUQv8C4_8YYIgw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J0A-hoffbDpscDPIZMjkaQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EEQCswTsExXZVlvvkCpq-w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ObLewEMfg1xByqFnpR872g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Nt_UiSuZNMS1BQYahPpIw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KZiI4yOpRKytPHrFazryEQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wWp1Pg1C7TIBeZjhX9JEwg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mwmu-H8hN3C3sSavKGWIQA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1WVJ5njz3lTYL5cDgV5LUA.png" /></figure><h3>Conclusion</h3><p>The direct method avoids Nondeterminism and ε-Transitions, Reduces Intermediate Complexity, and Efficient Pattern matching in the conversion of regex to DFA. Implementation Complexity, Less Intuitive for Humans, and limited scalability could be the cons of this method. This method serves as the theoretical foundation method to convert the given expression into a DFA. Having DFA means having a machine-computable algorithm.</p><h3>References</h3><ol><li>Compiler Design Tutorials. (2023, June 15). <em>RE to DFA Conversion — Direct Method</em> [Video]. YouTube. Retrieved from <a href="https://www.youtube.com/watch?v=p5MbSZ4nBho">URL</a></li><li>Compiler Design Tutorials. (2023, June 15). <em>Optimization of DFA using First pos , Last pos , Follow pos || Compiler Design easy trick.</em> [Video]. YouTube. Retrieved from [<a href="https://www.youtube.com/watch?v=nMwqTnBjANs&amp;t=918s">URL</a>]</li></ol><h3>Appendix</h3><h4>A. Key Concepts in Regex to DFA Conversion</h4><p>For more details, refer to the <a href="https://github.com/Dilli822/CSC_IOST/blob/main/CSC365%20Compiler%20Design%20and%20Construction/Labs/Regex%20To%20DFA%20Direct%20Method%20ChatGPT%20Chat.pdf">[Regex to DFA Direct Method ChatGPT Chat.pdf]</a></p><h4>B. Google Colab Notebook Implementation of Algorithm</h4><p>For more details, refer to the <a href="https://colab.research.google.com/drive/1Agw5Yw52YoK5ZmMDIKxrs9XuBPaN52Q8?usp=sharing">Google Colab</a></p><h3>Tools</h3><ol><li>OpenAI. (2023). <em>ChatGPT</em> (March 2023 version)</li><li>Google. (n.d.). <em>Google search</em>. Retrieved March 12, 2025</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80f6da15d42a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Relation between the Perceptron and Bayes Classifier for a Gaussian Environment]]></title>
            <link>https://medium.com/@dillihangrae/relation-between-the-perceptron-and-bayes-classifier-for-a-gaussian-environment-befd29e089ec?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/befd29e089ec</guid>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning-ai]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[bayes-theorem]]></category>
            <category><![CDATA[gaussian-distribution]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Fri, 07 Mar 2025 21:24:08 GMT</pubDate>
            <atom:updated>2025-06-04T06:51:56.705Z</atom:updated>
            <content:encoded><![CDATA[<p>Neural Network | Bayes Classifier</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YnIh9cPjrkt4gpjCDP6oSg.png" /></figure><h3>Abstract</h3><p>The Bayes Classifier is a probabilistic model based on Bayes’ Theorem used for classification, as is the Perceptron. Under the Gaussian Environment, where the data for each class is drawn from a multivariate normal distribution, the Perceptron and Bayes Classifiers share the classifying feature. This article dives into Mathematics to show the relationship between them.</p><h3>Introduction</h3><p>The <strong>Bayes Classifier</strong> (or <strong>Bayesian classifier</strong>) is a probabilistic model based on <strong>Bayes’ theorem</strong>. It classifies a new observation by calculating the posterior probabilities of each class and assigning the observation to the class with the highest posterior probability.</p><p>The <strong>Perceptron</strong> is a linear classifier that aims to separate two classes by finding a linear decision boundary (hyperplane) that best divides the data. It does this by adjusting the weights based on misclassified examples using a simple iterative algorithm.</p><h3>Perceptron</h3><p>The weight and bias are adjusted by an iterative weight-updating algorithm. The mathematical form is:</p><h4>y=sign(w^T * x + b)</h4><p>where:</p><ul><li>w is the weight vector,</li><li>x is the input feature vector,</li><li>b is the bias term.</li></ul><h3><strong>Bayes’ theorem</strong>:</h3><p>In the case of binary classification, the posterior probability for class c given observation x is:</p><p>P(c∣x)=P(x∣c) * P(c) / P(x)</p><p>where:</p><ul><li>P(x∣c) is the <strong>likelihood</strong>, the probability of observing x given the class c,</li><li>P(c) is the <strong>prior</strong> probability of class c,</li><li>P(x) is the <strong>marginal likelihood</strong> of observing x.</li></ul><h3>Normal Distribution(Gaussian Distribution)</h3><p>Named after mathematician Carl Friedrich Gauss, Gaussian Distribution is a bell-shaped curve that shows the probability distribution and how values of a variable are spread from the center(mean). Having mean, mode, and median at the center mean (μ) = 0 and standard deviation(σ) at the tail. It’s a symmetric bell curve centered at μ = 0, with σ shaping how wide or narrow the tails are!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*LdyseMvCKKZDa0maq_h-Xw.png" /></figure><h3>Multivariate Gaussian distribution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/0*RKzGtCKyhcs9Qrmc.png" /></figure><p>For <strong>multivariate Gaussian distribution</strong>, the equation extends to multiple dimensions. While the one-dimensional version deals with a single random variable, the multivariate version handles multiple random variables that may be correlated. So, it is a generalization of the one-dimensional Gaussian distribution to higher dimensions. If we are dealing with only one mountain it is Univariate Normal else it is multivariate Normal Distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PThcKd3NJx5Qu_6x8csMWA.png" /></figure><h3>Handwritten Notes</h3><p>I find it impossible to write down all the derivations without directly inserting the handwritten notes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x3M0o-txZ82Z0f6G6dWwBg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KJhuWOIJJ9-S1p8iAIMFAw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BqWEe3ja5UuNmjfXmiFnSw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f2boAlF9tcvV8zuxxuxsbg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jK3zwBWlmrD1A2I0aJ4G1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CXRGVN_o0JXXEr5LnKaw7g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ATecn1Ryjeu0VxhWPZhAJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eF13SUZQwDBxlQAzSvB-OQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pow9-T9R6QhoaFMTocT-zg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j6ALiUrtwSQWKOUqMYxJiA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C0-vo6Ald127lAL5knGxkA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4AYclz7hmnqU2SEbk30c_g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yLQRSnCN2RjzzGsrG9m9nQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*75PgQt5UyI2V7UQUA3kakA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*znf-ByalT6fu62rSYu1GCw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZNrvDOVBjww6u5wgVYrG4A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ke0TjJ8qm-OeJzR1qye9Ng.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hli0lpr4XACrQXHWZyNXZg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U8vC0oahQ22krEuEEkeO-w.png" /></figure><h3>Conclusion</h3><p>Dealing with the probabilities, misclassification cost, risk factor, inequality in the variable equation, constant terms in the risk equation with decision rule, and rejection overlapping areas, we can see the clear relation between the perceptron and the Bayes classifier. The Perceptron can mimic the Bayes Classifier’s decision boundary in a Gaussian environment under specific conditions, but it lacks the probabilistic foundation and optimality guarantees of the Bayes approach. This relation can be used for real-world applications like spam detection, medical diagnostics, or financial risk assessment, where different misclassifications carry different costs</p><h3>References:</h3><ol><li>Haykin, S. (1999). <em>Neural networks: A comprehensive foundation</em> (2nd ed.). Prentice Hall.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=befd29e089ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Perceptron Convergence Theorem]]></title>
            <link>https://medium.com/@dillihangrae/the-perceptron-convergence-theorem-c71d0efbc334?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/c71d0efbc334</guid>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 20:28:53 GMT</pubDate>
            <atom:updated>2025-06-04T06:47:28.615Z</atom:updated>
            <content:encoded><![CDATA[<p>Rosenblatt’s Perceptron | Foundation of Neural Network</p><h3>Abstract:</h3><p>The goal of the perceptron is to correctly classify the given input data into binary classes (Class 1 or Class 2). In 1958 <strong>Frank Rosenblatt</strong> developed perceptron as a linear classifier an early model for neural networks and machine learning. Threshold or hard limiter is used as an activation function output of either -1 or 1. Before that perceptron learns or adjusts its weight with some error correction (weight update) rule. This article demonstrates the mathematics proof of the perceptron convergence theorem.</p><h3>1. Introduction</h3><p>In mathematics, <strong>Convergence</strong> essentially means “getting closer and closer to a value” without necessarily reaching it (in the case of infinite limits). Convergence in the context of perceptron can be understood as the condition where the weight gets no more updates and the previous iteration weight w_i is very close to the present new weight w_i+1 such that the perceptron makes correct classification with the converged weights. Let’s see Newton-Raphson’s Method example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e9yQoazUk3Qta_oNY12DYw.png" /></figure><p>Linearly Separable Data can be defined as the collection of data points in a geometrical space which can be separated into two distinct classes if a decision boundary is drawn. The decision boundary plane/hyperplane (in 3D) and straight line (in 2D) space. Examples: Classifying the <strong>Size of the fruit</strong> (Small or Big), the <strong>Color of the fruit</strong> (Red or Orange), and so on.</p><h3>2. Convergence Theorem Statement</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*C3WAEFBJsJKbQaD_3ioAIQ.png" /></figure><p>“If the data is linearly separable, the perceptron learning algorithm will converge to a set of weights that correctly classify the training data after a finite number of updates.” (Rosenblatt, 1958)</p><p>Later, Marvin Minsky and Seymour Papert’s 1969 book, “Perceptrons,” pointed out the limitations and revealed the theorem works only for linearly separable data failing to converge for non-linearity like the XOR problem.</p><h3>3. Handwritten Notes</h3><p>I find it impossible to write down all the derivations without directly inserting the handwritten notes.</p><p>Before diving into proof let&#39;s review the mathematical form of Perceptron:</p><p>y = ( w1​⋅x1​+w2​⋅x2​+⋯+w_n​⋅x_n​ + b )</p><p>where</p><ul><li>x_n are the input values.</li><li>w_n​ are the weights.</li><li>b is the bias term.</li></ul><p>error = d_i — y_i where d_i is the desired output and y_i is the actual output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BXQ58oqh9v9qdkouE1-iqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i51HNZs77PhVphxdjow0iQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHBoIoFWUOePm_L52AQ7ng.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jYcSZNtQHd0M3QPfowu5cA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0bAJGt8cnHi_lOfRnM0k_A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FWfHH0-eFESfmigKxHD1Cg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*540zwuPqYp-ltsO78HEUZA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Bncl3WQWxbWEm7XTEuPMg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BfbIGZmTzIznyjg3WWPF2w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U1u94Cpf3bjCvWywI1nCbA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yr4356cjFHAnPfhCPULI1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MGn8wvxurzo72zHB3VxX0A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uBUd6TvIj2CvxgAvPOjjNA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L1cUe4yvv5KaIGo0kbeeYA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NssLuUePL5aRvz9Oi1FNFg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fi3YTqvIsSSvVvJfDq49SA.png" /></figure><h3>4. Intuition Behind the Use of <strong>Cauchy-Schwarz inequality (Max or Min)</strong></h3><ol><li>Apply inequality to control the growth of the weight vector:<br>∥w(k+1)∥² ≤ ∥w(k)∥² + ∥x(k)∥²</li></ol><p>2. Apply inequality to increase the margin and move the weight vector towards a good separation:<br>∥w(k)∥² ≥ min∥w∥ ∥x(k)∥²</p><h4><strong>A simplified analogy:</strong></h4><ul><li>Imagine we’re trying to build a fence to separate two types of animals — say, cows and chickens — on a farm. We want the fence to be straight and as far away from the cows and chickens as possible.</li><li><strong>Min (Margin)</strong>: The margin is how much space we give between the fence and the animals. We want the fence to be at least a certain distance away from the closest animals so that they are classified correctly. This is the <strong>min</strong>: the minimum space that must be between the animals and the fence (weight vector).</li><li><strong>Max (Bound on growth)</strong>: But we also don’t want the fence to grow so big that it extends way past the farm. We want the fence’s size to be controlled, so it doesn’t grow too large. This is the <strong>max</strong>: the maximum size the fence (weight vector) can grow.</li></ul><h3>5. Algorithm</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gq6jv8LSHoj-LCZf-eTaZw.png" /></figure><h3>Conclusion</h3><p>In the <strong>Perceptron Convergence Theorem</strong>, we’ve explored the mathematical principles and proofs behind the perceptron learning algorithm. Growth Control, Margin and Separation, Inequalities, and Convergence concepts are central to understanding how the perceptron converges to a solution. Thus, the theorem serves as the foundation of modern Neural Networks.</p><h3>References:</h3><ol><li>Rosenblatt, F. (1958). <em>The perceptron: A probabilistic model for information storage and organization in the brain</em>. Psychological Review, 65(6), 386–408.</li><li>Haykin, S. (1999). Neural networks: A comprehensive foundation (2nd ed.). Prentice Hall.</li></ol><h3>Appendix</h3><h4>A. Google Colab Notebook</h4><p>The implementation and training of the perceptron model for the AND gate, along with classification plots and convergence analysis, can be accessed via the following Google Colab link: 🔗 <a href="https://colab.research.google.com/drive/1pKPIn-c7QVh8zNboq_kfNXkQCNLGOiT2?usp=sharing">Perceptron AND Gate Implementation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c71d0efbc334" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi-Layer Perceptron Learning]]></title>
            <link>https://medium.com/@dillihangrae/multi-layer-perceptron-learning-072e021ab7ca?source=rss-2062f3579091------2</link>
            <guid isPermaLink="false">https://medium.com/p/072e021ab7ca</guid>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[xor-problem]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[multilayer-perceptron]]></category>
            <dc:creator><![CDATA[dilli_hangrae]]></dc:creator>
            <pubDate>Tue, 04 Mar 2025 20:15:35 GMT</pubDate>
            <atom:updated>2025-06-04T06:48:21.275Z</atom:updated>
            <content:encoded><![CDATA[<p>Foundation of Deep Learning | Neural Networks</p><h3>Abstract</h3><p>Perceptron Learning is the foundation of a single-layered network that can act as a classifier for linear problems. However, it fails to solve non-linear problems like XOR. Adding one or more extra layers of neurons converts the single-layered network to a Multi-Layered one, which can solve the non-linearity and make correct classification. This article dives into the introductory mathematics derivations and lab experiments to solve the XOR problem. Using one hidden layer between the input and output layer with an Activation function like Sigmoid MLP can learn the XOR Gate functions.</p><h3>1. Introduction</h3><p>Multilayer Perceptron Neural Network has stacked layer architecture. They are the Input, Hidden &amp; Output Layers. The hidden layers are so-called because they are connected only to other network units. They are hidden from the“outside world.”[1]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J9yKzWfAKe5vCMCSGFiAHA.png" /><figcaption><em>Figure 1: Diagram of a Single Hidden Layer MLP neural network (Haykin, 1999).</em></figcaption></figure><p>There can be a ’n’ number of hidden layers but only a Single Input &amp; Output layer. Notations like W_ij, σ_ij, and b_ij are used in MLP because it is a scalable architecture. There are two types of signal flows ie. activation function and error signal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ShU0Pyjr1N8XnHM9pWgHvQ.png" /><figcaption><em>Figure 2: Diagram of a More than One Hidden Layer MLP neural network (Haykin, 1999).</em></figcaption></figure><h3>2. Notations used in MLP</h3><p>For ease and standard use cases, we use notation.</p><h4>2.1 Layers &amp; Neurons</h4><ul><li>x_i​ → <strong>Inputs</strong> to the network, where i indexes the input features.</li><li>h_j​ → <strong>Neurons</strong> in the hidden layer, where j indexes the neuron in a given layer.</li><li>y_k​ → <strong>Outputs</strong>, where k indexes the output neuron.</li></ul><h4>2.2 Weights W_ij​</h4><p>Weights are the strength of the connection between neurons.</p><p><strong>Notation</strong>:</p><ul><li>W_ij​ → Weight from neuron I (previous layer) to neuron j (next layer).</li><li>Example: In a hidden layer, W_12​ is the weight from the <strong>1st input neuron</strong> to the <strong>2nd hidden neuron</strong>.</li></ul><h4>2.3 Bias b_j</h4><ul><li>Bias is a learnable scalar added to the weighted sum before activation, allowing the model to shift the decision boundary (y-intercept).</li><li>b_j​ → Bias term for neuron j in a given layer.</li><li>Example: If a hidden neuron has bias b_2​(j = 2), its activation becomes: z_2=W_12 * x_1 + W_22 * x2 + b_2</li></ul><h4>2.4. Activation Function σ</h4><ul><li><strong>Definition</strong>: A function applied to the weighted sum to introduce non-linearity.</li><li>σ(z) → Activation function applied to the weighted sum z. Here j is the hidden layer.</li><li>Example: a_ j​ = σ(W_1j * ​x_1 ​+ W_2j * ​x_2​ + b_j​)</li><li>Common functions: <strong>ReLU</strong>, <strong>Sigmoid</strong>, <strong>Tanh</strong>, <strong>Softmax</strong>.</li></ul><h4>2.5. Output Computation</h4><p>The final output layer applies a function to transform hidden layer activations:</p><p>y_k = σ_out ( Σ W_jk * a_j + b_k ). Here K is the output layer where σ_out is the activation function of the output layer.</p><h4>2.6. Forward Propagation Equation (General Form)</h4><p>We know that it is a fully connected feedforward neural network where the information is processed in the forward direction and then mathematically forward propagation.</p><p><strong>For a neuron in any layer l:</strong></p><p>z_j^(l) = Σ W_ij^(l) * a_i^(l-1) + b_j^(l)</p><p>a_j^(l) = σ(z_j^(l))</p><p>where:<br>- z_j^(l) = Weighted sum before activation<br>- a_j^(l) = Output after activation</p><h3>3. What about BackPropagation and Weight Updates?</h3><p><strong>Backpropagation</strong> is the process used to update the weights in an <strong>MLP</strong> by computing the <strong>gradient (How much the loss (error) changes when we change the weights</strong>.) of the<strong> loss function (</strong>difference between the predicted output y`​ and the true output y).<strong> with respect to the weights.</strong> This ensures that the network learns by <strong>minimizing the error.</strong></p><p><strong>Error = desired output — actual output</strong></p><p><strong>error = y — y`</strong></p><ul><li>y → Actual target value (ground truth)</li><li>y`​ → Predicted value (the output of the neural network)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*EWPmAgMgcEMPgEDN.gif" /><figcaption>Gifs from <a href="https://medium.com/analytics-vidhya/backpropagation-for-dummies-e069410fa585">Backpropagation for Dummies. All the math behind, simplest than… | by Diletta Goglia | Analytics Vidhya | Medium</a></figcaption></figure><h3>4. Handwritten Notes on Derivation of BackPropagation</h3><p>I find it impossible to write down all the derivations without directly inserting the handwritten notes.</p><p>Before diving into core maths. The learner must have prior knowledge of Chain Rule, Partial Derivative, Derivative (Power, Sum, Product Rule) , Gradient (Slope), and activation function (sigmoid).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tcI8ocJn22MvgG93SKmm9g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TuadE7n2uZBiGQqVY2RGjA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BEspYpEgwXleP1ZapqAqOA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SiB26EjIYcuGDz7CeTH19Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Op-6RJC8xOBWySq_dmW40A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h8hsr8ZVaSZ16WIXIA7ZIA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Kkd6oh7M2tbs4YqQLC8nA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h6QKwZCWSADKHcLdulL8Sw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nHJTt9rbynpRTh_85goseQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6atP0e9CZBrVsHzIjx0NFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eXnX3hubHJUbL1Ggcg4mrQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g0LMlrkHYbro9k8zJAhTvw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SlS2C7SJ-cfxg1OSR6PSOw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QjIILpdbJ7Ny_LH4U9iezw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*McqNj9qslTZ4nkD36CPy8w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nUAunEYYzGSuiPW9go-l2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*laPBklbQ17vEWk81E4LG2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fkUpT0S_80xffiCeEByQoA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XmQuT0O_Egt9GAsCKnfubg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TgT_kOFuQIR1ZQWYZjjlRg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bYS5xjSUR06oCoijq5y_eQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*toECgSsYIrmj-sNJyunk0g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hqI-jzlZEkrMZQWl9Mj-uw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ogte4ENSfHofpPD91fTpmQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZGoW6iHQ0wQWeo4WdR20WQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A5neyeetF-AC1EwOG54-hA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WI2PBzORwEt12tRy3skD0Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0NbS1rz7Ir9XHYn7oKGS_A.png" /></figure><h3>5. XOR Problem:</h3><p>A perception works by plotting a single straight decision boundary to separate different classes. But the XOR truth table is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6gZB1LFp7Hm3US0RmZOAkA.png" /></figure><p>If we plot these points on a 2D plane, you’ll see that no single straight line can separate the <strong>1s</strong> from the <strong>0s</strong>. This is why a perceptron, which relies on a linear function, fails to classify XOR correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W_qfnyDwCJ_wSi31bC1Hwg.png" /><figcaption><em>Figure 3: Diagram of a XOR MLP neural network (Haykin, 1999).</em></figcaption></figure><h3>6. Algorithm: Backpropagation for MLP Neural Network</h3><p><strong>1. Initialize Parameters:</strong><br> — Randomly initialize weights W_ih, W_ho and biases b_h, b_o.</p><p><strong>2. For each training epoch:</strong><br> 1. Forward Propagation:<br> — Compute the hidden layer input: <br> h_hidden = X W_ih + b_h<br> — Apply activation (Sigmoid) to the hidden layer: <br> a_hidden = σ(h_hidden)<br> — Compute the output layer input: <br> h_output = a_hidden W_ho + b_o<br> — Apply activation (Sigmoid) to the output layer: <br> ŷ = σ(h_output)</p><p><strong>2. Error Calculation:</strong><br> — Compute error: <br> error = y — ŷ</p><p><strong>3. Backpropagation:</strong><br> — Compute the gradient at the output layer: <br> δ_output = error ⋅ ŷ ⋅ (1 — ŷ)<br> — Compute the gradient at the hidden layer: <br> δ_hidden = δ_output ⋅ W_ho^T ⋅ a_hidden ⋅ (1 — a_hidden)</p><p><strong>4. Gradient Descent Update:</strong><br> — Update weights and biases:<br> W_ho = W_ho — η ⋅ δ_output ⋅ a_hidden^T<br> b_o = b_o — η ⋅ δ_output<br> W_ih = W_ih — η ⋅ δ_hidden ⋅ X^T<br> b_h = b_h — η ⋅ δ_hidden</p><p><strong>3. Repeat until convergence (or max epochs reached).</strong></p><h3>The intuition behind the MLP to solve the XOR Problem</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yyYBjQa4kVUqTOqIwbodnw.png" /><figcaption><em>Figure 4: Diagram of a trained XOR MLP neural network with Correct Decision Boundary(Haykin, 1999).</em></figcaption></figure><p>The hidden layer has neurons that apply weights and biases to the input values and use an activation function (like the sigmoid function). This non-linear activation allows the network to transform the inputs into a space where a linear separation is possible. A hidden layer <strong>maps the inputs to a higher-dimensional space</strong>, enabling non-linear transformations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Q3-i_iYRKT_nhiO-5HTQeQ.png" /><figcaption>Fig 5. Decision Boundary of XOR Classifier in RBF</figcaption></figure><h3>Conclusion:</h3><p>Adding hidden layers to a neural network, specifically a Multi-Layer Perceptron (MLP), enables it to solve problems that are not linearly separable, such as the XOR problem. The key to this is the <strong>non-linearity introduced by the hidden layers</strong> and their <strong>activation functions, </strong>such as the <strong>sigmoid</strong> function(0 or 1). These layers allow the network to<strong> transform the input space into a higher-dimensional space </strong>where complex decision boundaries can be learned. Each time we compute the backpropagation, we are computing the gradient and minimizing the loss function. Ultimately, we converge the weights(that we call training) that correctly classify the given XOR Input.</p><h3>References:</h3><ol><li>Bermúdez, J. L. (2014). <em>Cognitive science: An introduction to the science of the mind</em> (2nd ed.). Cambridge University Press.</li><li>Haykin, S. (1999). <em>Neural networks: A comprehensive foundation</em> (2nd ed.). Prentice Hall.</li></ol><h3>Appendix</h3><p>“The implementation of the Multi-Layer Perceptron for the XOR problem is available on Google Colab, where users can access and run the code directly. The full implementation can be found <a href="https://colab.research.google.com/drive/18GsBiGfnggtfm3Mctp-X8Or9oLE9ILfP?usp=sharing">here</a>.”</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=072e021ab7ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>