Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

388 lines
9.4 KiB

  1. This a very high level review post that I am making for myself and other people reviewing CS Theory.
  2. If you want to lean more about content in this blog post I recommend cracking open a text book-- I know, gross.
  3. This hastily thrown together post will cover how to solve typical problems relating to topics covered by my second CS Theory exam.
  4. # Myhill-Nerode Theorem
  5. ## Definition
  6. L is regular if and only if it has a finite index. The index is the maximum number of elements that are pairwise distinguishable.
  7. Two strings are said to be pairwise distinguishable if you can append something to both of the strings and it makes one string
  8. accepted by the language and the other string rejected.
  9. The size of an index set X equals the number of equivalence classes it has and the minimum number of states required to represent it
  10. using a DFA. Each element in the language is accepted by only one equivalence class.
  11. ## Problem Approach
  12. Prove that language L is regular.
  13. 1) Define a set X which is infinite in size - this doesn't have to be in the language.
  14. 2) Make a general argument that show that each element in X is pairwise distinguishable.
  15. Pick any two elements x, y in X and show that if you append z to them one is accepted by the language and
  16. the other is not in the language.
  17. ## Example
  18. Prove the following language is non-regular:
  19. $$
  20. L={ww^r | w \in {0,1}^*}
  21. $$
  22. answer:
  23. 1)
  24. $$
  25. X = {(01)^i | i \geq 0}
  26. $$
  27. Pick any 2 elements of X and show pairwise distinguishable
  28. $$
  29. x = (01)^i, y = (01)^j | i \neq j
  30. $$
  31. suppose we pick
  32. $$
  33. z = (10)^i\\
  34. xz \in L\\
  35. yz \notin L
  36. $$
  37. # DFA minimization algorithm
  38. Types of Problems:
  39. - Prove DFA is minimal
  40. - Minimize the DFA
  41. The argument for DFA minimization comes from the Myhill-Nerode theorem. Given
  42. a DFA, if you can form a set of strings which represent each state and they are all
  43. pairwise distinguishable, then the DFA is minimal with that many states.
  44. ## Prove DFA is minimal
  45. For these types of problems you construct a table and show that each state is pairwise distinguishable.
  46. To show pairwise distinguishably you have to show that there exists a string where if appended to one element
  47. makes it accepted by the language but pushes the other string out of the language.
  48. ### Example
  49. Prove the following DFA is minimal.
  50. ![DFA Example](media/CSTHEORY/DFAMinimalProof.png)
  51. Find a set of strings which represent the minimal path to each state in the DFA.
  52. $$
  53. X = \{\epsilon, b, bb, ba\}
  54. $$
  55. Show that each state is pairwise distinguishable.
  56. ![DFA Example](media/CSTHEORY/DFAMinimalTable.png)
  57. ## Minimize the DFA
  58. To use the concept of being indistinguishable to minimize a DFA, you can use a table to keep track which
  59. states are distinguishable from each other. The states which are not indistinguishable can
  60. be combined. To solve one of these problems you start by creating a table which compares each of the
  61. states in the DFA. You then go through and mark the states which are indistinguishable -- start with
  62. the ones with different accepting statuses. Then you continue marking off states where if you transition with
  63. a symbol on the DFA you are distinguishable and the other state is non-distinguishable according to the table.
  64. ### Example
  65. Minify the Following DFA:
  66. ![DFA Example](media/CSTHEORY/DFAMinification.png)
  67. After marking the states with different accepting criteria as being distinguishable you get this table:
  68. ![Half Complete Table](media/CSTHEORY/MinificationTable.svg)
  69. After looping through all pairs and marking them on the table if there exists symbol which results in one state
  70. to be distinguishable and one to be indistinguishable you get this table:
  71. ![Min TableTable](media/CSTHEORY/MinTable2.svg)
  72. According to the table you are able to combine {D, A, B}, {C, F}, and {E, G}.
  73. Minimal DFA:
  74. ![Min DFA](media/CSTHEORY/MinimalDFA.svg)
  75. # Pumping lemma for regular languages
  76. The pumping lemma cannot prove that a language is regular, however, you can use it
  77. to show that some languages are non-regular. This theory gets at the idea that if
  78. a regular language is long enough/infinite, it will have a state somewhere which is
  79. repeated on the path that accepts the string.
  80. The accepted strings can be divided into three parts:
  81. - Symbols leading up to the loop
  82. - Symbols which complete a loop and come back to start of loop
  83. - Symbols at the end of the string
  84. ![Min DFA](media/CSTHEORY/PumpingLemmaTheory.svg)
  85. To Show that a language L is not regular using pumping lemma:
  86. - Use a proof by contradiction
  87. - Assume L is regular
  88. - Choose a representative string S which is just barely in the language and is represented in terms of p.
  89. - Express S = xyz such that |xy| < p and y > 0
  90. - Show that you can pump y some amount of times such that it is not in the language.
  91. - This contradicts the pumping lemma.
  92. - The assumption that L is regular is wrong.
  93. - L must not be regular.
  94. ## Example
  95. Show that the following language is non-regular.
  96. $$
  97. {0^n1^n | n \geq 0}
  98. $$
  99. Proof by contradiction
  100. Assume that L is regular.
  101. Let p be the pumping length associated with L
  102. $$
  103. S = o^p1^p
  104. $$
  105. S is valid since
  106. $$
  107. |s| \geq p, S \in L
  108. $$
  109. For any valid decomposition
  110. S = xyz
  111. such that |xy| <= p and |y| > 0
  112. Consider:
  113. $$
  114. xy^2z
  115. $$
  116. By the pumping lemma this should be in the language but it is not. Therefore our assumption that the
  117. language is regular is false.
  118. ![Min DFA](media/CSTHEORY/PumpingLemmaExample.svg)
  119. # Context-free grammars, closure properties for CFLs
  120. The context-free grammars are a super-set of the regular languages. This means that CFG's can represent
  121. some non-regular languages and every regular language. Contest-free Languages are defined by Context-Free Grammars and accepted using
  122. Push-down Automata machines.
  123. Context Free Grammars are Represented using:
  124. - **Terminals** = Set of symbols in that language
  125. - **Variables** = Set of symbols representing categories
  126. - **Start Symbol** = Variable which you start with- written on top
  127. - **Substitution Rules** = Set of rules that recursively define the language.
  128. ## Example 1
  129. Grammar G:
  130. $$
  131. A \rightarrow 0A1 \\
  132. A \rightarrow B \\
  133. B \rightarrow \# \\
  134. $$
  135. This grammar describes the following language:
  136. $$
  137. L = \{0^k\#1^k | k \geq 0\}
  138. $$
  139. ## Example 2
  140. Give CFG for non-Palindromes
  141. $$
  142. S \rightarrow aXb | bXa | aSa | bSb | ab | ba \\
  143. X \rightarrow aX | bX | a | b \\
  144. $$
  145. In this example, the S rule recursively applies itself until something that is not a palindrome is added.
  146. Once you exit the S state, you can finish by appending anything to the middle of the string.
  147. ## Example 3
  148. Give CFG for the following language:
  149. $$
  150. \{a^ib^jc^kd^l | i+k = j + l\}
  151. $$
  152. $$
  153. S \rightarrow aSd | XYZ \\
  154. X \rightarrow aXb | \epsilon\\
  155. Y \rightarrow bYc | \epsilon\\
  156. Z \rightarrow cZd | \epsilon
  157. $$
  158. ## Closure
  159. CFLs are closed under union, concatenation, and Kleene star.
  160. - Union: Create a new starting variable which goes to either branch.
  161. -Kleene Star: We can repeatedly concat the derivations of the string. However, we also need to make sure that epsilon occurs in the string.
  162. - Concatenation: From start variable we force the concatenation of two variables representing each state sub CFG
  163. # Parse trees, ambiguity
  164. Parse Trees are simply graphical means to illustrate a deviation of a string from a grammar.
  165. The root of the tree will be the start variable, interior nodes are other variables. Leaf nodes are
  166. terminal symbols.
  167. A CFG is said to be ambiguous if there is at least one string with two or more distinct derivations.
  168. Leftmost and rightmost derivations are not ambiguous.
  169. To remove ambiguity try to force order or break CFG into cases.
  170. # Chomsky Normal Form
  171. Useful form for CFGs since they allow you to easily identify if a string is in a language.
  172. Form:
  173. $$
  174. A \rightarrow BC\\
  175. A \rightarrow a\\
  176. S \rightarrow \epsilon
  177. $$
  178. Convert CFG to CNF (Chomsky Normal Form).
  179. - Add new start variable
  180. - Remove Epsilon rules (multi-step process)
  181. - Remove unit rules
  182. - Convert to A -> BC, A -> a
  183. ## Example
  184. Convert the following CFG to a CNF
  185. $$
  186. S \rightarrow ASA | aB\\
  187. A \rightarrow B | S\\
  188. B \rightarrow b | \epsilon
  189. $$
  190. Step 1: Add new start variable.
  191. $$
  192. S_0 \rightarrow S\\
  193. S \rightarrow ASA | aB\\
  194. A \rightarrow B | S\\
  195. B \rightarrow b | \epsilon
  196. $$
  197. Step 2: Remove epsilon rules.
  198. $$
  199. S_0 \rightarrow S\\
  200. S \rightarrow ASA | aB | a\\
  201. A \rightarrow B | S | \epsilon\\
  202. B \rightarrow b
  203. $$
  204. $$
  205. S_0 \rightarrow S\\
  206. S \rightarrow ASA | aB | SA | AS | S | a\\
  207. A \rightarrow B | S\\
  208. B \rightarrow b
  209. $$
  210. Step 3: Remove unit rules
  211. (Remove A -> B)
  212. $$
  213. S_0 \rightarrow S\\
  214. S \rightarrow ASA | aB | SA | AS | a\\
  215. A \rightarrow b | S\\
  216. B \rightarrow b
  217. $$
  218. (Remove A -> S)
  219. $$
  220. S_0 \rightarrow S\\
  221. S \rightarrow ASA | aB | SA | AS | a\\
  222. A \rightarrow b | ASA | aB | SA | AS | a\\
  223. B \rightarrow b
  224. $$
  225. (Remove S0-> S)
  226. $$
  227. S_0 \rightarrow ASA | aB | SA | AS | a\\
  228. S \rightarrow ASA | aB | SA | AS | a\\
  229. A \rightarrow b | ASA | aB | SA | AS | a\\
  230. B \rightarrow b
  231. $$
  232. # Push-Down automata
  233. These are NFAs but they also have a stack. This allows us to solve any problem which
  234. can be represented with a CFG.
  235. The stack has it's own alphabet. The dollar symbol typically represents the empty stack.
  236. With each transition you can examine the stack, push to the stack and move to a new state.
  237. ## Example
  238. $$
  239. L = \{a^n\#b^n\}
  240. $$
  241. ![](media/CSTHEORY/PDAExample.svg)
  242. # Construction to convert CFG to a PDA
  243. Basic idea: use stack to hold progressive derivations of a string using rules of grammar.
  244. ## Example
  245. Convert the following CFG to a PDA:
  246. $$
  247. S \rightarrow aTb | b \\
  248. T \rightarrow Ta | \epsilon
  249. $$
  250. ![](media/CSTHEORY/PDAConstruction.svg)