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.

419 lines
9.5 KiB

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