<?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 Markbackcs on Medium]]></title>
        <description><![CDATA[Stories by Markbackcs on Medium]]></description>
        <link>https://medium.com/@markbackcs?source=rss-ffb161a8f50f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*iROIpjci8LlVEusj</url>
            <title>Stories by Markbackcs on Medium</title>
            <link>https://medium.com/@markbackcs?source=rss-ffb161a8f50f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:16:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@markbackcs/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[Every NFA Question Feels Different. Here’s One Method That Works for All of Them]]></title>
            <link>https://medium.com/@markbackcs/every-nfa-question-feels-different-heres-one-method-that-works-for-all-of-them-f996b068c687?source=rss-ffb161a8f50f------2</link>
            <guid isPermaLink="false">https://medium.com/p/f996b068c687</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[automata]]></category>
            <category><![CDATA[theory-of-computation]]></category>
            <category><![CDATA[education]]></category>
            <dc:creator><![CDATA[Markbackcs]]></dc:creator>
            <pubDate>Mon, 12 Oct 2026 20:43:44 GMT</pubDate>
            <atom:updated>2026-10-12T20:54:45.938Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step way to build any NFA, worked through on a real exam-style question</p><figure><img alt="NFA diagram: question phrases like “begins with a” and “contains bcb” flowing into one NFA built from two pieces" src="https://cdn-images-1.medium.com/max/1024/1*1x804uKSs5r9OgeCxoPc8A.png" /></figure><p>You read the question. You understand every word. And you still have no idea where to start.</p><blockquote>“Construct an NFA that accepts all strings that begin with a and end with c, or contain bcb followed by an odd number of a’s.”</blockquote><p>You’ve seen worked solutions, and they make sense when someone else draws them. Then the next question looks completely different, and you’re back to a blank page.</p><p>If that’s you, here’s the good news. NFA questions only look different. Underneath, nearly all of them are made from the same few small pieces, glued together in the same few ways. Once you can spot the pieces, you stop starting from zero every time.</p><p>That’s what this article is about. We’ll go through the method one step at a time, then use it on the exact question above.</p><h3>First, what an NFA actually is</h3><p>An automaton is a small machine. It reads a string one letter at a time, and at the end it says yes (accept) or no (reject).</p><p>It’s made of four things:</p><ul><li><strong>States</strong>: the circles. The machine is always sitting in one of them.</li><li><strong>Arrows</strong>: each one has a letter on it. Reading that letter moves you along that arrow.</li><li><strong>A start state</strong>: where you begin. It has an arrow pointing into it from nowhere.</li><li><strong>Accepting states</strong>: the double circles. If you finish reading on one of these, the answer is yes.</li></ul><p>An NFA (non-deterministic finite automaton) gets two extra freedoms:</p><ol><li>One letter can have several arrows leaving the same state, or none at all.</li><li>It can have ε arrows (empty arrows). You can follow those for free, without reading a letter.</li></ol><p>The simplest way to picture it: an NFA is allowed to guess. If even one possible path ends on an accepting state, the string is accepted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QcO8vK-3Owt60TiGdUDomQ.png" /><figcaption>An NFA for strings over {a, b} that contain “aabb”. The start state has two arrows for a: the machine can stay, or guess that “aabb” starts right here</figcaption></figure><p>You can see the guessing in the picture. From q0, reading an a can keep you looping, or move you forward to q1. The NFA tries both at once. That freedom is exactly why NFAs are easier to build than DFAs, as you’ll see in a moment.</p><h3>The method: Split, Build, Glue, Test</h3><p>Every question goes through the same four steps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QcO8vK-3Owt60TiGdUDomQ.png" /></figure><ol><li><strong>Split</strong> the sentence into small conditions.</li><li><strong>Build</strong> a tiny machine for each condition.</li><li><strong>Glue</strong> the tiny machines together.</li><li><strong>Test</strong> it with a few strings.</li></ol><p>Let’s take them one at a time.</p><h3>Step 1: Split the sentence</h3><p>Before drawing anything, look for the joining words. They tell you how the pieces connect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QcO8vK-3Owt60TiGdUDomQ.png" /></figure><ul><li><strong>“or”</strong> means two separate machines. The string only has to match one of them.</li><li><strong>“followed by”</strong> or <strong>“then”</strong> means one machine runs first, and the next one carries on from where it stopped.</li></ul><p>One thing to watch for is the word <strong>“and”</strong>. In “begins with a and ends with c”, the “and” is describing one string, from its first letter to its last. So you build it as one machine, not two.</p><h3>Step 2: Build each piece from a pattern</h3><p>Most exam questions are built from just a handful of patterns. Learn them once and you’ll keep recognising them.</p><p><strong>“Begins with a”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QcO8vK-3Owt60TiGdUDomQ.png" /></figure><p>The first arrow out of the start state reads a. If the string starts with anything else, there’s no arrow to follow, and that path dies.</p><p><strong>“Ends with c”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6hOubYET4RQHO6kP_hT_Rg.png" /></figure><p>The last arrow, the one going into the accepting state, reads c.</p><p><strong>“Anything in between”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u8A7xrRDAH2GXi9GAhuX8g.png" /></figure><p>This is a state with a loop back to itself on every letter. It lets the machine read whatever comes, for as long as it needs.</p><p><strong>“Contains bcb”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YdeU7zTLLLAA5NtM4uwqag.png" /></figure><p>Loop on anything, read b, c, b in a row, then loop on anything again. The first loop covers whatever comes before “bcb”, and the last loop covers whatever comes after.</p><p><strong>“An odd number of a’s”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pu87f5xzyt66gaDykWqHjg.png" /></figure><p>Two states that swap every time you read an a. One means “even so far”, the other means “odd so far”. You start on even, because zero a’s is even, and you accept on odd.</p><h3>Why the guessing matters</h3><p>Look at “ends with c” again, joined with “begins with a” and “anything in between”. To accept, the machine has to know which c is the last one, and while it’s reading, it can’t know that yet.</p><p>An NFA doesn’t need to know. It guesses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9XncB4plM3xUHPxvrDAZEw.png" /></figure><p>Take the string “abcc”. At the first c, one path jumps to q2, but there’s still a c left to read and no arrow to follow, so that guess dies. The other path stays on the loop, waits for the final c, and then jumps to q2. That path ends on an accepting state, so the string is accepted.</p><p>You never had to work out “is this the last c?” yourself. The NFA tries every possibility, and one right guess is enough.</p><h3>Step 3: Glue the pieces</h3><p>Once each piece is built, there are only two ways to join them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yhnKtbbCHVEP93taw-U0hA.png" /></figure><p><strong>For “or” (union):</strong> add one new start state, and draw an ε arrow from it to the start of each piece. The machine can slip into either piece for free, so a string is accepted if it fits either one.</p><p><strong>For “followed by” (concatenation):</strong> draw an ε arrow from each accepting state of the first piece to the start of the second piece. Then turn those old accepting states into normal states. Only the second piece’s accepting states count now, because the string isn’t finished until the second part is done too.</p><p>Those two glues build almost every question you’ll see.</p><h3>Step 4: Test before you hand it in</h3><p>This is the step that saves the most marks, and it takes about a minute.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x4qNo5WHVb-nMA5IzkM9xw.png" /></figure><p>Pick a short string that should pass, a longer one that should pass, one that should fail, and the empty string if the question makes it matter. Trace each one through your machine. The “should fail” one matters most. If it gets accepted, you’ve found the mistake before your marker does.</p><h3>Now let’s solve the real question</h3><blockquote>“All strings that begin with a and end with c, <strong>or</strong> contain the substring bcb followed immediately by an odd number of a’s.”</blockquote><p><strong>Split.</strong> There’s an “or”, so we have two pieces:</p><ul><li>Piece 1: begins with a and ends with c</li><li>Piece 2: contains bcb, then an odd number of a’s</li></ul><p><strong>Build piece 1.</strong> This is three patterns in a row: “begins with a”, then “anything in between”, then “ends with c”. You’ve already seen this exact machine in the guessing example.</p><p><strong>Build piece 2.</strong> I’ll name this piece’s states p0, p1, p2… instead of q, so the two pieces don’t share names when we glue them together. Names are just labels, so any letters work as long as each state is different.</p><p>One honest note: this part of the question is a little ambiguous. I’m reading it as “the string ends with bcb and then an odd number of a’s”. If anything could come after, the “odd” part wouldn’t actually change anything. If your professor words it differently, just adjust the end.</p><p>So it’s “anything” first, then b, c, b, then the odd-a’s pattern on the end.</p><p><strong>Glue with “or”.</strong> A new start state S, with ε arrows into both pieces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yMFL54wFlBjLZ3-PbAo_9Q.png" /><figcaption>The full NFA. The top row is piece 1, the bottom row is piece 2, and S lets the machine try both.</figcaption></figure><p><strong>Test.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jAkn2OrTgdzzQ75bY06MUQ.png" /></figure><ul><li>“abcc” goes through piece 1: it starts with a, and its last letter is c. Accepted.</li><li>“cbcbaaa” goes through piece 2: it reads bcb, then three a’s, and three is odd. Accepted.</li><li>“bcbaa” fails both: piece 2 ends after two a’s, which is even, and piece 1 can’t even start without an a. Rejected.</li></ul><p>And that’s it. Nothing here was invented on the spot. We spotted the patterns, built them, glued them, and checked them.</p><h3>The mistakes that cost the most marks</h3><ul><li><strong>Forgetting the new start state</strong> when gluing with “or”. Without it, you’ve drawn two machines, not one.</li><li><strong>Leaving old accepting states as accepting</strong> after gluing with “followed by”.</li><li><strong>Mixing up odd and even.</strong> Check where you start: zero a’s is even.</li><li><strong>Only testing strings that should pass.</strong> That proves almost nothing. Always test one that should fail.</li></ul><h3>Try these yourself</h3><p>Use Split, Build, Glue, Test. The answers are below, but give each one a real try first.</p><ol><li>Strings over {a, b} that <strong>end with ab or contain bb</strong>.</li><li>Strings that are <strong>one b followed by an even number of a’s</strong>, and nothing else.</li><li>Strings over {a, b} that <strong>contain aba or have an even length</strong>.</li></ol><p><strong>Answers (short version)</strong></p><ol><li>“Or” gives two pieces. For “ends with ab”: a loop on anything, then a, then b into an accepting state. For “contains bb”: a loop, then b, b, then a loop. Glue them with a new start state and ε arrows.</li><li>Just one piece. The start state reads b into an “even” state, and two states swap on a. Zero a’s counts as even, so the state right after b is accepting.</li><li>“Or” gives two pieces: the “contains aba” pattern, and two states that swap on every letter (even length and odd length), accepting on even. Glue with ε arrows.</li></ol><h3>The takeaway</h3><p>Every NFA question feels different until you notice it’s the same handful of patterns, joined with “or” and “followed by”. Split the sentence, build the small pieces, glue them, and always test one string that should fail.</p><p>I lost marks on automata myself before this clicked, and that’s a big part of why I made a set of animated lessons that build <strong>DFAs and NFAs</strong> step by step, one arrow at a time. The lesson on DFAs and NFAs is free if <strong>seeing the machines move helps you</strong>: markbackcs.com/courses/automata/finite-automata/dfa-and-nfa</p><p>And if a question still has you stuck, leave it in the <strong>responses below</strong>. I’m happy to walk through it with you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f996b068c687" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>