<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
      <title>Category: Theory of computation | Mitja Bezenšek</title>
      <id>http://bezensek.com/</id>
      <link href="https://bezensek.com/blog/categories/theory-of-computation/atom.xml" rel="self"/>
      <link href="https://bezensek.com/"/>
      <updated>2015-05-08T00:48:59+02:00</updated>
      <author><name>Mitja Bezenšek</name></author>
      <entry>
      <title type="text">Deterministic finite state machine minimization</title>
      <id>http://bezensek.com/blog/2015/05/08/deterministic-finite-state-machine-minimization</id>
      <link href="https://bezensek.com/deterministic-finite-state-machine-minimization/"/>
      <updated>2015-05-08T00:48:59+02:00</updated>
      <published>2015-05-08T00:48:59+02:00</published>
      <content type="html">&lt;p&gt;I have received a question about how to approach DFSM minimization in &lt;a href=&quot;https://bezensek.com/deterministic-finite-state-machine-implementation-in-c-number/&quot;&gt;one of the previous posts&lt;/a&gt;. This is an attempt to answer that question.&lt;/p&gt;
&lt;h2 id=&quot;what-is-dfsm-minimization&quot;&gt;What is DFSM minimization?&lt;/h2&gt;
&lt;p&gt;First, let us consider what does it mean for two DFSM to be &lt;strong&gt;equivalent&lt;/strong&gt;? Imagine having two DFSM that both accept the same input symbols. You then pass all the possible inputs to those two machines. If and only if the answers are the same for all the inputs then the two machines are equivalent. That is: they accept exactly the same inputs, even though their inner workings might not be the same.&lt;/p&gt;
&lt;p&gt;This means that there are many equivalent DFSM. For example all three DFSM in the below picture accept the same input which can be written as a regular expression (0+1)&lt;sup&gt;*&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://bezensek.com/_images/460ccc3df47c08e1-698.webp 698w&quot; sizes=&quot;(max-width: 540px) calc(100vw - 66px), (max-width: 760px) calc(100vw - 90px), (max-width: 898px) calc(100vw - 158px), 740px&quot;&gt;&lt;img src=&quot;https://bezensek.com/img/Equivalence.png&quot; alt=&quot;Deterministic finite state machine equivalence&quot; width=&quot;698&quot; height=&quot;163&quot; decoding=&quot;async&quot; loading=&quot;eager&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;The three DFSM vary in the number of states and also in the number of the transitions. Nonetheless, I hope you will agree with me that they all accept the same input.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;p&gt;In this example case the machines are simple. But what would happen if the machines were complex - how would we determine if they are equivalent?&lt;/p&gt;
&lt;p&gt;The answer is minimization. Minimization &lt;strong&gt;transforms&lt;/strong&gt; a machine &lt;strong&gt;into an equivalent machine&lt;/strong&gt; that has &lt;strong&gt;minimum number of states&lt;/strong&gt;.  We can then easily check if two DFSM are are equivalent. We just need to check if they have the same number of states and if all the transitions are the same.&lt;/p&gt;
&lt;h2 id=&quot;why-minimization&quot;&gt;Why minimization&lt;/h2&gt;
&lt;p&gt;Like I described in the previous paragraph you can use minimization to check the equivalence of the DFSM. Two other benefits (when using DFSM in programming) are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;performance increase due to less states and transitions (faster input checking, less memory consumed),&lt;/li&gt;
&lt;li&gt;easier reasoning about the minimized machine since it has fewer moving parts.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;two-types-of-states-to-remove&quot;&gt;Two types of states to remove&lt;/h2&gt;
&lt;p&gt;When minimizing there are &lt;a href=&quot;https://en.wikipedia.org/wiki/DFA_minimization&quot;&gt;two types of states&lt;/a&gt; that we can either completly remove or merge with other states of the machine:&lt;/p&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;unreachable states&lt;/strong&gt; are those states that are not reachable from the initial state of the DFA, for any input string,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;nondistinguishable states&lt;/strong&gt; are those that cannot be distinguished from one another for any input string.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;unreachable-states&quot;&gt;Unreachable states&lt;/h2&gt;
&lt;p&gt;First, let us look at an algorithm for finding the &lt;a href=&quot;https://en.wikipedia.org/wiki/DFA_minimization#Unreachable_states&quot;&gt;unreachable states&lt;/a&gt;. The algorithm starts from the known reachable states which are the start states - the start states are always reachable. It then expands the list of reachable states by adding all the states that can be reached from the start states. It continues this expansion using the newly added states until it cannot add any new states to the list of reachable states. The states of the DFSM that did not end up in the list of reachable states are unreachable.&lt;/p&gt;
&lt;p&gt;To me this algorithm feels like flooding the pipes: we have a system of pipes some of which are connected and some of which are not. We open the water and check which pipes did the water reach and which it did not reach.&lt;/p&gt;
&lt;p&gt;Below is the algorithm in C#&lt;sup&gt;&lt;a href=&quot;#user-content-fn-2&quot; id=&quot;user-content-fnref-2&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableStates &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; HashSet&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; newStates &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; HashSet&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;do&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; temp &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; HashSet&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; newStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;		foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	        var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Find&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                                             t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	            temp&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    temp&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ExceptWith&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    newStates &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; temp&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    reachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;UnionWith&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;newStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; while&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;newStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Count &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; unreachableStates &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;q &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The part of the algorithm that finds the possible transitions from the new states is quite inefficient. For each new state it tries all the symbols from &lt;code&gt;sigma&lt;/code&gt;. This causes us to loop over all the transitions for every symbol and every new state. A better solution is shown below. In it we are just selecting all the states that are reachable from the current state:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; newStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableFromQ &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;FindAll&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;							  .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Select&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    temp&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;UnionWith&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableFromQ&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have found all the unreachable states we can remove them (along with transitions to and from them):&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Count &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;--){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;unreachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	    unreachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	    Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Remove&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; unreachableState &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; unreachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Remove&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;unreachableState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;nondistinguishable-states&quot;&gt;Nondistinguishable states&lt;/h2&gt;
&lt;p&gt;And now for the hard part - the nondistinguishable states. Here, we will use &lt;a href=&quot;https://en.wikipedia.org/wiki/DFA_minimization#Brzozowski%27s_algorithm&quot;&gt;Brzozowski’s algorithm for minimization&lt;/a&gt;, which works like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Reversing the edges of a DFA produces a non-deterministic finite automaton (NFA) for the reversal of the original language, and converting this NFA to a DFA using the standard powerset construction (constructing only the reachable states of the converted DFA) leads to a minimal DFA for the same reversed language. Repeating this reversal operation a second time produces a minimal DFA for the original language.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To implement the described algorithm we will need the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;an algorithm that reverses a DFSM and produces a corresponding NDFSM,&lt;/li&gt;
&lt;li&gt;an algorithm for powerset construction (this algorithm converts a given NDFSM into an equivalent DFSM)&lt;/li&gt;
&lt;li&gt;an algorithm for detecting and removing unreachable states (see above).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;reversing-a-dfsm&quot;&gt;Reversing a DFSM&lt;/h3&gt;
&lt;p&gt;The algorithm for reversing a DFSM is quite easy to implement and is presented below &lt;sup&gt;&lt;a href=&quot;#user-content-fn-3&quot; id=&quot;user-content-fnref-3&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; NDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Reverse&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; delta &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                                 transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                                 transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; NDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;powerset-construction&quot;&gt;Powerset construction&lt;/h3&gt;
&lt;p&gt;Here is &lt;a href=&quot;https://bezensek.com/powerset-construction/&quot;&gt;how I implemented the powerset constructions&lt;/a&gt; algorithm in my previous post&lt;sup&gt;&lt;a href=&quot;#user-content-fn-4&quot; id=&quot;user-content-fnref-4&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; PowersetConstruction&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;NDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; ndfsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;     var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;     var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; ndfsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;     var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;     var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0 &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Join&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; ndfsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;     var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; F &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;     var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; processed &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;     var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; queue &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;     queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Enqueue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Join&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; ndfsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;     while&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Count &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; setState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Dequeue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        processed&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;CleanupState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; statesInCurrentSetState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Split&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; state &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; statesInCurrentSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;           if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;ndfsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;              F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;CleanupState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;              break&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;           }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbols &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; ndfsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Delta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;           .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; statesInCurrentSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;           .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Select&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;           .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Distinct&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbols&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;           var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableStates &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;              ndfsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Delta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;                 .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                             statesInCurrentSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;                 .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;OrderBy&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;                 Select&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;           var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableSetState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Join&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;           Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;                                    CleanupState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                                    symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;                                    CleanupState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;           if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (!&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;processed&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;              queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Enqueue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;           }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;     }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;     return&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;  private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; CleanupState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;     return&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Replace&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;/h3&gt;
&lt;p&gt;Now that we have all the pieces we can combine them into Brzozowski’s  algorithm:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; MinimizeDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; fsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reversedNDFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Reverse&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;fsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reversedDFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; PowersetConstruction&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reversedNDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;   reversedDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;RemoveUnreachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; NDFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Reverse&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reversedDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; DFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; PowersetConstruction&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;NDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;   DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;RemoveUnreachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;   return&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you check the powerset construction algorithm (or read my previous post) you can see that the implementation has a nice property that it constructs a DFSM with only reachable states. Which is why we can remove the two calls to &lt;code&gt;RemoveUnreachableStates&lt;/code&gt; method. The final algorithm:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; MinimizeDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; fsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reversedNDFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Reverse&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;fsm&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reversedDFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; PowersetConstruction&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reversedNDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; NDFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Reverse&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reversedDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;   return&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; PowersetConstruction&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;NDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And lets see how the minimization works on an example. This is how the four steps of the above minimization look like on one of the machines from the beginning .&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://bezensek.com/_images/97fcad5baecbb30b-716.webp 716w&quot; sizes=&quot;(max-width: 540px) calc(100vw - 66px), (max-width: 760px) calc(100vw - 90px), (max-width: 898px) calc(100vw - 158px), 740px&quot;&gt;&lt;img src=&quot;https://bezensek.com/img/Minimization.png&quot; alt=&quot;Deterministic finite state machine minimization&quot; width=&quot;716&quot; height=&quot;701&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;So this is it for minimization. The source code can be found at &lt;a href=&quot;https://github.com/MitjaBezensek/DFSM_Minimization&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;
&lt;section data-footnotes class=&quot;footnotes&quot;&gt;&lt;h2 class=&quot;sr-only&quot; id=&quot;footnote-label&quot;&gt;Footnotes&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;user-content-fn-2&quot;&gt;
&lt;p&gt;You might want to check &lt;a href=&quot;https://bezensek.com/deterministic-finite-state-machine-implementation-in-c-number/&quot;&gt;one of my previous articles&lt;/a&gt; to check the DFSM implementation. &lt;a href=&quot;#user-content-fnref-2&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 1&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-3&quot;&gt;
&lt;p&gt;If you read my previous article you might be wondering if those implementations will work with this reversal. The answer is no. The problem is that when we reverse a DFSM we are basically converting starting state to finish state and finish states to starting states. Which means there can be many starting states - a variant of the DFSM that my implementation does not cover. &lt;a href=&quot;#user-content-fnref-3&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 2&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-4&quot;&gt;
&lt;p&gt;With a few minor adjustements. &lt;a href=&quot;#user-content-fnref-4&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 3&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
    </entry>
<entry>
      <title type="text">Powerset construction in C#</title>
      <id>http://bezensek.com/blog/2015/04/30/powerset-construction</id>
      <link href="https://bezensek.com/powerset-construction/"/>
      <updated>2015-04-30T15:58:59+02:00</updated>
      <published>2015-04-30T15:58:59+02:00</published>
      <content type="html">&lt;p&gt;In the last two articles I have implemented the &lt;a href=&quot;https://bezensek.com/deterministic-finite-state-machine-implementation-in-c-number/&quot;&gt;deterministic finite state machine&lt;/a&gt; (DFSM) and the &lt;a href=&quot;https://bezensek.com/non-deterministic-finite-state-machine-implementation-in-c-number/&quot;&gt;non-deterministic finite state machine&lt;/a&gt; (NDFSM). As you probably all know the two variants are equivalent in terms of power; that is everything that can be expressed with one can also be expressed with the other. This may come as a surprise when you first hear about it, but you are soon presented with the &lt;strong&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Powerset_construction&quot;&gt;powerset construction&lt;/a&gt;&lt;/strong&gt; which converts a NDFSM into a DFSM and thus proves one part of the equivalence. The other part is trivial since DFSM is only a limited NDFSM.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h2 id=&quot;powerset-construction-algorithm&quot;&gt;Powerset construction algorithm&lt;/h2&gt;
&lt;p&gt;Like I discussed in my &lt;a href=&quot;https://bezensek.com/non-deterministic-finite-state-machine-implementation-in-c-number/&quot;&gt;previous blog post&lt;/a&gt;, the only difference between the DFSM and the NDFSM is that the DFSM does not allow multiple transitions for a given state and symbol. If we wish to transform a NDFSM into a DFSM we need to remove these multiple transitions. We can achieve this by combining all the states that we can reach with a given symbol from a given state into a new state which is a set of all the reachable states.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://bezensek.com/_images/a83f5c1854d235cb-510.webp 510w&quot; sizes=&quot;(max-width: 540px) calc(100vw - 66px), (max-width: 760px) calc(100vw - 90px), (max-width: 898px) calc(100vw - 158px), 740px&quot;&gt;&lt;img src=&quot;https://bezensek.com/img/PowersetConstruction.png&quot; alt=&quot;Powerset construction&quot; width=&quot;510&quot; height=&quot;160&quot; decoding=&quot;async&quot; loading=&quot;eager&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;As you can see on the left side of the above picture we can reach states &lt;code&gt;q1&lt;/code&gt; and &lt;code&gt;q2&lt;/code&gt; from the state &lt;code&gt;q0&lt;/code&gt; using the symbol &lt;code&gt;a&lt;/code&gt;. This means that the state &lt;code&gt;q0&lt;/code&gt; has multiple transitions defined for the symbol &lt;code&gt;a&lt;/code&gt; and we wish to get rid of these multiple transitions. The powerset construction combines states &lt;code&gt;q1&lt;/code&gt; and &lt;code&gt;q2&lt;/code&gt; into a new state that is the set of these two states as can be seen on the right side of the picture. The new state is denoted as &lt;code&gt;{q1, q2}&lt;/code&gt;. When we do this transformation for the combined states, like the &lt;code&gt;{q1, q2}&lt;/code&gt;, we need to look at the transitions for all the states that make this combined state, in this case &lt;code&gt;q1&lt;/code&gt; and &lt;code&gt;q2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The good news is that this does exactly what we need: it gets rid of the multiple transitions. The bad news though is that in the worst case the number of states explodes and we end up with the states that are all the possible subsets of our original set of states.&lt;/p&gt;
&lt;p&gt;Ouch.&lt;/p&gt;
&lt;p&gt;So how would we approach this constructions programmatically? Probably the easiest way would be to keep a queue of states that we wish to process. The initial queue would only contain the starting states. We would then add all the possible states that are reachable from the starting states. If there are multiple transitions from a state with a given symbol we would create a new &lt;strong&gt;combined state&lt;/strong&gt; like described in the previous paragraph and add it to the queue. For the symbols that only have one transition we would just add the ending state of the transition to the queue. We would then remove a state from the queue and repeat the described algorithm until we empty the queue.&lt;/p&gt;
&lt;p&gt;Determining the finish states of the DFSM machine is easy. The finish states will be all the finish states of the original NDFSM plus the combined states that have a least one of the finish states as one of their elements.&lt;/p&gt;
&lt;p&gt;During the execution of the algorithm we would of course need to keep track of the already processed states and the transitions between them so that we can construct our NDFSM.&lt;/p&gt;
&lt;p&gt;Here is one possible implementation:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Convert&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;NDFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; nd&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; nd&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0 &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; nd&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; F &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; processed &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; 	var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; queue &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	while&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Count &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	   var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; setState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Dequeue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	   processed&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	   Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;		var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; statesInCurrentSetState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Split&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    	foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; state &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; statesInCurrentSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	    	if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;nd&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	        	F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	        	break&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	       }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbols &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; nd&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Delta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	       .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; statesInCurrentSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	       .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Select&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	       .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Distinct&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	    foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbols&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	       var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableStates &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	          nd&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Delta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	             .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	                         statesInCurrentSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	             .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;OrderBy&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	             .&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Select&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;	       var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableSetState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Join&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	       Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; reachableSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	       if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (!&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;processed&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;	          queue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Enqueue&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;reachableSetState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	       }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This algorithm was actually easier to implement than I thought. I thought that I would need to generate the powerset and then all the possible transitions between all the states. I took a different approach which only goes through the reachable states and only generates the states of the powerset that are actually needed. The algorithm turned out much simpler and on top of that it also produces a better results since the unreachable states are pruned away!&lt;/p&gt;
&lt;p&gt;The source code is available at &lt;a href=&quot;https://github.com/MitjaBezensek/PowersetConstruction&quot;&gt;Github&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
<entry>
      <title type="text">Non-deterministic Finite State Machine implementation in C#</title>
      <id>http://bezensek.com/blog/2015/04/12/non-deterministic-finite-state-machine-implementation-in-c-number</id>
      <link href="https://bezensek.com/non-deterministic-finite-state-machine-implementation-in-c-number/"/>
      <updated>2015-04-12T22:35:00+02:00</updated>
      <published>2015-04-12T22:35:00+02:00</published>
      <content type="html">&lt;p&gt;&lt;a href=&quot;https://bezensek.com/deterministic-finite-state-machine-implementation-in-c-number/&quot;&gt;Last time&lt;/a&gt; I implemented the deterministic finite state machine (DFSM). This time I will do the same for the non-deterministic (NDFSM) one. I chose the variation without the &lt;code&gt;epsilon&lt;/code&gt; transitions and will leave that for exercise if someone might find it intriguing.&lt;/p&gt;
&lt;p&gt;The only difference between the DFSM and NDFSM is that while the DFSM can only have a single transition for a given state and input symbol the NDFSM is not limited in that regard. It can have multiple transitions for a given state and input symbol.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;p&gt;With multiple possible next moves we get the non-deterministic way of operating. This is usualy depicted as a “magic wand” that correctly decides which transition to choose when multiple options are available. Unfortunatelly for us programmers we don’t have that magic wand. We need to try all the possible options before we can answer wheater the NDFSM accepts the input or not. My first thought when seeing something like this is &lt;a href=&quot;https://en.wikipedia.org/wiki/Backtracking&quot;&gt;backtracking&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;pick one of the possible transitions,&lt;/li&gt;
&lt;li&gt;if it leads to the solution great,&lt;/li&gt;
&lt;li&gt;if not backtrack and try something else.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But let us not get ahead of ourselves. Let us start with something simpler.&lt;/p&gt;
&lt;h2 id=&quot;valid-transitions&quot;&gt;Valid transitions&lt;/h2&gt;
&lt;p&gt;With the before discussed difference in mind I looked at the DFSM implementation to see what will I need to change (If you didn’t read the &lt;a href=&quot;https://bezensek.com/deterministic-finite-state-machine-implementation-in-c-number/&quot;&gt;previous article&lt;/a&gt; this would be a good time, since I will mostly skip the parts that will remain the same and only focus on the differences. And even if you have already read it, it might help to have another window opened with the &lt;a href=&quot;https://github.com/MitjaBezensek/DFSM/blob/master/FSM_Simple/DeterministicFSM.cs&quot;&gt;source code&lt;/a&gt; of the solution.).&lt;/p&gt;
&lt;p&gt;One of the first things I noticed was the logic that checks the validity of transitions. We no longer need to check if a similar transition has already been defined, so we can remove that part:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; ValidTransition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;  Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All the other validation logic can remain the same. Which brings us to the biggest change: the &lt;code&gt;Accepts&lt;/code&gt; method.&lt;/p&gt;
&lt;h2 id=&quot;non-deterministic-accept&quot;&gt;Non-deterministic Accept&lt;/h2&gt;
&lt;p&gt;Like I already mentioned above the non-determinism often looks like magic. This kind of behavior is usually implemented by recursive backtracking. Let us look at a high level overview of the method:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;find all the posible transitions from the current state using the current symbol,&lt;/li&gt;
&lt;li&gt;recursively select one transition and try to accept the remaining input by using that transition,&lt;/li&gt;
&lt;li&gt;if we managed to read the whole input and ended up in a final state, accept the input,&lt;/li&gt;
&lt;li&gt;if the state is not final or no transitions are possible, backtrack and select a different transition.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First, we need a method that will start our recursive calls. The logic here is a bit different than in the deterministic case where we could easily determine if the input is not accepted:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;there was no valid transition for current state and symbol symbol or&lt;/li&gt;
&lt;li&gt;we read all the input and ended up in a non finite state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the non-deterministic case we need to do that for all the possible transitions from a given state and symbol. And we should start this from the starting state and the whole input:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Success&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;Trying to accept: &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; StringBuilder&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;())){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Failure&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;Could not accept the input: &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we need to write the recursive function that checks if there is any sequence of steps that leads from the current state to the final state using the specified input:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; StringBuilder&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; steps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Length &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transitions &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; GetAllTransitions&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;currentState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transitions&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;            var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentSteps &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; StringBuilder&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;steps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Substring&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentSteps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;                return&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;currentState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Success&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;Successfully accepted the input &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;                               &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;in the final state &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;                               &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt; with steps:&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; steps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If there are more symbols to read we find all the possible transitions for the current state and current symbol. We then recursively try each of these transitions by calling the &lt;code&gt;Acecepts&lt;/code&gt; method on the &lt;code&gt;EndState&lt;/code&gt; of the transition with the remaining input. The remaining input here is the current input except for the first symbol which we used in the transition. We also need to add the current transition to the steps. For that I created a new &lt;code&gt;StringBuilder&lt;/code&gt; or we would add some transitions that did not lead to the solution in the &lt;code&gt;foreach&lt;/code&gt; loop.&lt;/p&gt;
&lt;p&gt;The recursion stops when we either tried all the transitions and none of them worked. Or if we came to the end of the input in which case we accept the input only if we stoped in a finish state.&lt;/p&gt;
&lt;p&gt;The only thing remaining is the &lt;code&gt;GetAllTransitions&lt;/code&gt; method which simply goes through all transitions and finds the ones with the specified start state and symbol:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; IEnumerable&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; GetAllTransitions&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;FindAll&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                         t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;While the difference between the DFSM and NDFSM is quite small on paper their algorithms are quite different. Reading the input in the case of DFSM proceeds in linear fashion, we are always reducing the number of symbols we still need to read. But for NDFSM this is not the case. We might read all the input but not finish in the end state and then backtrack back to the begining with whole input left to read. This also makes the NDFSM algorithm perform much slower.&lt;/p&gt;
&lt;p&gt;Also the implementation above is not really the best when it comes to performance. We are calling the &lt;code&gt;GetAllTransitions&lt;/code&gt; method over and over again. A better way would be to store the possible transitions so that we don’t have go through all the transitions every time. But I’ll leave this to the readers (as well as implementing &lt;a href=&quot;https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton_with_%CE%B5-moves&quot;&gt;&lt;code&gt;epsilon&lt;/code&gt; transitions&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;You can find the &lt;a href=&quot;https://github.com/MitjaBezensek/NDFSM&quot;&gt;source code&lt;/a&gt; for NDFSM on github.&lt;/p&gt;
</content>
    </entry>
<entry>
      <title type="text">Deterministic finite state machine implementation in C#</title>
      <id>http://bezensek.com/blog/2014/08/13/deterministic-finite-state-machine-implementation-in-c-number</id>
      <link href="https://bezensek.com/deterministic-finite-state-machine-implementation-in-c-number/"/>
      <updated>2014-08-13T21:36:58+02:00</updated>
      <published>2014-08-13T21:36:58+02:00</published>
      <content type="html">&lt;p&gt;While I was working at the &lt;a href=&quot;https://www.fri.uni-lj.si/en/&quot;&gt;Faculty of Computer and Information Science&lt;/a&gt; I have implemented many of the standard algorithms from the famous Cormen’s &lt;a href=&quot;https://www.amazon.com/gp/product/0262033844/ref=as_li_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0262033844&amp;amp;linkCode=as2&amp;amp;tag=bezensek-20&amp;amp;linkId=EL57QKBNM5BB373B&quot;&gt;Introduction to Algorithms&lt;/a&gt;. Since I was a part of the Laboratory for algorithms and data structures that is to be expected. However, I have never implemented many of the great results from the Theory of Computation.&lt;/p&gt;
&lt;p&gt;This is why I decided to implement the finite state machine and the Turing machine in C# as an exercise. In this blog post I will start with the easier of the two, the &lt;a href=&quot;https://en.wikipedia.org/wiki/Automata_theory&quot;&gt;finite state machine (FSM)&lt;/a&gt;.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h2 id=&quot;programming-by-wishful-thinking&quot;&gt;Programming by wishful thinking&lt;/h2&gt;
&lt;p&gt;I will not bore you with the theory, since you can read all about it in the &lt;a href=&quot;https://en.wikipedia.org/wiki/Automata_theory&quot;&gt;wikipedia article&lt;/a&gt;, which explains the logic behind FSM better than I ever could. So I will just start with &lt;a href=&quot;https://dsoguy.blogspot.com/2007/01/programming-by-wishful-thinking.html&quot;&gt;programming by wishful thinking&lt;/a&gt;. This is how I would like to consume an implementation of a deterministic FSM (DFSM):&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-comment, #62635d);font-style:italic&quot;&gt;    // Declaration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q1&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q2&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q1&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q1&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q2&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q2&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q1&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0 &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; F &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;q2&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; dFSM &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DeterministicFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see I define the five components that make a DFSM: the states of the machine &lt;code&gt;Q&lt;/code&gt;, the alphabet &lt;code&gt;Sigma&lt;/code&gt;, the list of transitions &lt;code&gt;Delta&lt;/code&gt;, the start state &lt;code&gt;Q0&lt;/code&gt;, and the set of finish states &lt;code&gt;F&lt;/code&gt;. I then construct the machine by passing the components to the constructor.&lt;/p&gt;
&lt;p&gt;I expect the upper definition to construct the FSM that is visible on the image below. As you can see the machine accepts all the words with an even number of “a” characters.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://bezensek.com/_images/0663846c13aa62b2-383.webp 383w&quot; sizes=&quot;(max-width: 540px) calc(100vw - 66px), (max-width: 760px) calc(100vw - 90px), (max-width: 898px) calc(100vw - 158px), 740px&quot;&gt;&lt;img src=&quot;https://bezensek.com/img/FSM.png&quot; alt=&quot;Finite state machine&quot; width=&quot;383&quot; height=&quot;73&quot; decoding=&quot;async&quot; loading=&quot;eager&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;After constructing the machine we wish to test if some input is a part of the machine’s language:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-comment, #62635d);font-style:italic&quot;&gt;    // Asking the machine if certain inputs are a part of it&apos;s language&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;aa&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I want the &lt;code&gt;Accept&lt;/code&gt; method to print out if the machine accepted the input or not. Also I would like to see the steps that led to the finish state if the input was accepted.&lt;/p&gt;
&lt;h2 id=&quot;transitions&quot;&gt;Transitions&lt;/h2&gt;
&lt;p&gt;With that, I had all the specification I needed, so I started by implementing the first thing the compiler complained about: the  &lt;code&gt;Transition&lt;/code&gt; object.&lt;/p&gt;
&lt;p&gt;The transitions define how we can move between the states. So the &lt;code&gt;Transition&lt;/code&gt; class needs to hold the information about the &lt;code&gt;StartState&lt;/code&gt;, the &lt;code&gt;Symbol&lt;/code&gt; and the &lt;code&gt;EndState&lt;/code&gt; of the transition. Appart from that I added a &lt;code&gt;ToString()&lt;/code&gt; method that will be used later on.&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; private&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Token &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; private&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; EndState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; private&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; startState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; token&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; endState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; startState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            Token &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; token&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            EndState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; endState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; override&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; ToString&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Format&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;({0}, {1}) -&amp;gt; {2}&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;deterministic-finite-state-machine&quot;&gt;Deterministic finite state machine&lt;/h2&gt;
&lt;p&gt;Now that we have the &lt;code&gt;Transition&lt;/code&gt;, we can start implementing the machine. Here is how the constructor might look like:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; DeterministicFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; readonly&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; readonly&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Sigma &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; readonly&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; readonly&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; F &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; List&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; DeterministicFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;IEnumerable&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; IEnumerable&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;                                IEnumerable&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;                                IEnumerable&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            Q &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            Sigma &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;            AddTransitions&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;            AddInitialState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;            AddFinalStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I just copied the list of states and the alphabet to the internal properties, but for the other three components we need some basic validation. For the transitions we want to verify that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;they connect the states that are defined in &lt;code&gt;Q&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;they use only symbols from &lt;code&gt;Sigma&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is an additional constraint with deterministic machine and that is: each state can only have one transition for a given symbol.&lt;/p&gt;
&lt;p&gt;Considering all the requirements this is how the &lt;code&gt;AddTransitions&lt;/code&gt; method might look like:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; AddTransitions&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;IEnumerable&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transitions&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transitions&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;ValidTransition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; ValidTransition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;  Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;   &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                Sigma&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;                !&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;TransitionAlreadyDefined&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;     private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; TransitionAlreadyDefined&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;Transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Any&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                              t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;     }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The validations for the start state and the finish states are much simpler. We only need to check if they are defined in Q:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; AddInitialState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            Q0 &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; AddFinalStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;IEnumerable&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; finalStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; finalState &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; finalStates&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        			 finalState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;finalState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;))){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;finalState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;accepting-the-inputs&quot;&gt;Accepting the inputs&lt;/h2&gt;
&lt;p&gt;That brings us to the meat of our DFSM implementation: the &lt;code&gt;Accepts&lt;/code&gt; method. We will need to do the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;read the input symbol by symbol,&lt;/li&gt;
&lt;li&gt;try to find a transition from the current state that uses the current symbol,&lt;/li&gt;
&lt;li&gt;move to the next state defined by the found transition,&lt;/li&gt;
&lt;li&gt;if we have read all the symbols and have ended up in one of the finish states we can accept the input,&lt;/li&gt;
&lt;li&gt;in all other cases (no transition possible, ending up in non finish states) we don’t accept the input.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The upper pseudo algorithm is implemented below. The main logic is in the foreach loop that reads the next symbol and tries to transition to the next state. If the transition is successful it records the current step in the steps variable. I have also added a simple &lt;code&gt;ConsoleWriter&lt;/code&gt; class that deals with simple print outs to the console.&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt; Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Q0&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; steps &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt; StringBuilder&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        foreach&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;ToCharArray&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;())&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-type, #176d76)&quot;&gt;            var&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; Delta&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Find&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;StartState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                                             t&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;Symbol &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Failure&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;No transitions for current state and symbol&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;                ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Failure&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;steps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;               return&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            currentState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; transition&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;EndState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            steps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Append&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;transition &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;currentState&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;            ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Success&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;Accepted the input with steps:&lt;/span&gt;&lt;span style=&quot;color:var(--code-constant, #895b13)&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; steps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-keyword, #9f3e2b)&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Failure&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;Stopped in state &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; currentState &lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;         					   &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt; which is not a final state.&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        ConsoleWriter&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Failure&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;steps&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I have omitted some additional validations and the code behind the &lt;code&gt;ConsoleWriter&lt;/code&gt; for the sake of brevity, but the full source code can be found at &lt;a href=&quot;https://github.com/MitjaBezensek/DFSM&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Building the DFSM was quite fun! I have already implemented the non-deterministic one as well and will present it in one of the coming posts, and for sure I’ll do the Turing machine as well.&lt;/p&gt;
&lt;p&gt;Before I say goodbye let me show you a test run of the DFSM on the following inputs:&lt;/p&gt;
&lt;pre class=&quot;astro-code dev-notes&quot; style=&quot;background-color:var(--code-background, #e8e9e3);color:var(--code-foreground, #292a28); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;aa&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;aaa&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--code-foreground, #292a28)&quot;&gt;    dFSM&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--code-function, #2465aa)&quot;&gt;Accepts&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-string, #286446)&quot;&gt;aaaa&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--code-punctuation, #62635d)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://bezensek.com/_images/382e091b3983832b-407.webp 407w&quot; sizes=&quot;(max-width: 540px) calc(100vw - 66px), (max-width: 760px) calc(100vw - 90px), (max-width: 898px) calc(100vw - 158px), 740px&quot;&gt;&lt;img src=&quot;https://bezensek.com/img/Output.png&quot; alt=&quot;Finite state machine output&quot; width=&quot;407&quot; height=&quot;327&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
</content>
    </entry>
    </feed>