Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lfa:2022:lab03-dfa-regexp [2022/10/07 19:40] alexandra.udrescu01 |
lfa:2022:lab03-dfa-regexp [2022/10/28 22:42] (current) alexandra.udrescu01 |
||
---|---|---|---|
Line 30: | Line 30: | ||
$ B = \{ 1^n \mid n \geq 1 \} $ | $ B = \{ 1^n \mid n \geq 1 \} $ | ||
\\ | \\ | ||
- | $ AB = ? $ \\ $ BA = ? | + | $ AB = ? $ \\ $ BA = ? $ |
<hidden> | <hidden> | ||
Line 70: | Line 70: | ||
A* = {ε} (epsilon is always part of Kleene star) | A* = {ε} (epsilon is always part of Kleene star) | ||
- | B*= {ε} (epsilon is always part of Kleene star) U {(1^n)} U {(1^2n)} U {(1^3n)} U ... | + | B*= {ε} (epsilon is always part of Kleene star) U {$ 1^n $} U {$ 1^{2n} $} U {$ 1^{3n} $} U ... |
- | So basically B = L( (1^n)* ) | + | So basically B = L( ($1^n$)* ) |
</hidden> | </hidden> | ||
Line 78: | Line 78: | ||
**3.1.4** | **3.1.4** | ||
- | $math[A = \{ 0^n 1^n 2^m \mid m \geq n \geq 1 \}] | + | $math[A = \{ 0^n 1^n 0^m \mid m \geq n \geq 1 \}] |
\\ | \\ | ||
$ B = \{ 0^n \mid n \geq 1 \} $ | $ B = \{ 0^n \mid n \geq 1 \} $ | ||
Line 87: | Line 87: | ||
- | $math[AB = \{ 0^n 1^n 2^m 0^k \mid m \geq n \geq 1, k \geq 1 \}] | + | $math[AB = \{ 0^n 1^n 0^{m+k} \mid m \geq n \geq 1, k \geq 1 \}]. Deci $math[AB = A]. |
- | Note that the n in the definition of language A **is different** from the n in in the definition of B, they are **independent** when used in defining diffrent sets/languages. However, when n is used several times in the definition of one language, such as the 2 times it appears in langauge A, it is **the same** value. | + | Note that the n in the definition of language A **is different** from the n in in the definition of B, they are **independent** when used in defining different sets/languages. However, when n is used several times in the definition of one language, such as the 2 times it appears in language A, it is **the same** value. |
- | $math[BA = \{ 0^{(n+k)} 1^n 2^m \mid m \geq n \geq 1, k \geq 1 \}] | + | $math[BA = \{ 0^{(n+k)} 1^n 0^m \mid m \geq n \geq 1, k \geq 1 \}]. Equivalently: $math[BA = \{0^x 1^y 0^z \mid x \geq y\geq 1 \text{ and } z \geq y \geq 1 \}] |
</hidden> | </hidden> | ||
Line 117: | Line 117: | ||
</hidden> | </hidden> | ||
+ | |||
**3.2.2.** Write a regular expression for $ L = \{ \omega \text{ in } \text{{0,1}} ^* \text{ | EVERY sequence of two or more consecutive zeros appears before ANY sequence of two or more consecutive ones} \} $ | **3.2.2.** Write a regular expression for $ L = \{ \omega \text{ in } \text{{0,1}} ^* \text{ | EVERY sequence of two or more consecutive zeros appears before ANY sequence of two or more consecutive ones} \} $ | ||
+ | |||
<hidden> | <hidden> | ||
Line 131: | Line 133: | ||
</hidden> | </hidden> | ||
+ | |||
**3.2.3.** Write a DFA for $ L(( 10 \cup 0) ^* ( 1 \cup \epsilon )) $ | **3.2.3.** Write a DFA for $ L(( 10 \cup 0) ^* ( 1 \cup \epsilon )) $ | ||
+ | |||
<hidden> | <hidden> | ||
{{:lfa:2022:lfa2022_lab_3.2.3.png?300|}} | {{:lfa:2022:lfa2022_lab_3.2.3.png?300|}} | ||
</hidden> | </hidden> | ||
+ | |||
**3.2.4.** Write a regular expression which generates the accepted language of A. Then try to find the most simple and easy to understand way to write it. | **3.2.4.** Write a regular expression which generates the accepted language of A. Then try to find the most simple and easy to understand way to write it. | ||
{{:lfa:graf1.png?400|}} | {{:lfa:graf1.png?400|}} | ||
+ | |||
<hidden> | <hidden> | ||
Line 147: | Line 153: | ||
Let's see what words are accepted: | Let's see what words are accepted: | ||
* ab*ab (when we don't loop on state 1) | * ab*ab (when we don't loop on state 1) | ||
- | * ab*(<a way to leave state 2 and return back to it>)*ab => ab*(aab*)ab | + | * ab*(<a way to leave state 2 and return back to it>)*ab => ab*(aab*)*ab |
* anything that repeats the previous expression several times | * anything that repeats the previous expression several times | ||
* ε (the initial state is also a final state) | * ε (the initial state is also a final state) | ||
* from the previous 2 observations => we can use Kleene star | * from the previous 2 observations => we can use Kleene star | ||
So, the regex is: | So, the regex is: | ||
- | > ( ab*(aab*)ab )* | + | > ( ab*(aab*)*ab )* |
<note> | <note> | ||
- | For now, we will try to determine the equivalence between a regex and a DFa intuitively. But this is not the actual correct approach. | + | For now, we will try to determine the equivalence between a regex and a DFA intuitively. But this is not the actual correct approach. |
- | We will learn later (and you can review this exercise with the future knowledge) that once we find a regex intuitively, we should check that the DFA and the regex are actually equivalent by transforming the regex into an NFA and then checking for non-distinguishable states OR using DFA to regex conversion algorithms. (ask your TA about this if you want more detail right now, or wait until you're actually learning for the exam and reviewing all the courses) | + | We will learn later (and you can review this exercise with the future knowledge) that once we find a regex intuitively, we should check that the DFA and the regex are actually equivalent by transforming the regex into an NFA and then checking for non-distinguishable states OR we can use a DFA to regex conversion algorithm. (ask your TA about this if you want more details right now or wait until you're actually learning for the exam and reviewing all the courses) |
</note> | </note> | ||
Line 164: | Line 170: | ||
</hidden> | </hidden> | ||
- | **3.2.5.** Describe as precisely as possible the language generated by $math[((0(1 \cup 0)(1 \cup 0)) \cup 100)1(0(1 \cup 0)(1 \cup 0)) \cup 100)1((0(1 \cup 0)(1 \cup 0)) \cup 100)0)*] | + | |
+ | **3.2.5.** Describe as precisely as possible the language generated by $math[((0(1 \cup 0)(1 \cup 0)) \cup 100)1((0(1 \cup 0)(1 \cup 0)) \cup 100)1(((0(1 \cup 0)(1 \cup 0)) \cup 100)0)*] | ||
(hint: BCD) | (hint: BCD) | ||
+ | |||
<hidden> | <hidden> | ||
Line 176: | Line 184: | ||
Ugly, yes. But let's see the language of this little regex: | Ugly, yes. But let's see the language of this little regex: | ||
- | 0001 | + | 0001, 0011, 0101, 0111, 1001 |
- | 0011 | + | What are these in BCD? |
- | 0101 | + | 1, 3, 5, 7, 9. |
- | 0111 | + | So, we got so far BCD numbers that start with an odd digit, BUT this pattern repeats twice. So, we have BCD numbers starting with 2 odd digits. |
- | 1001 | + | NEXT: |
- | What are these in BCD? | + | ( ( 0 ( 1 U 0 ) ( 1 U 0 ) ) U 100 ) 0 )* |
- | 1 | + | We have a Kleene star, so something repeats any number of times. |
+ | And, inside this Kleene star, there is ( 0 (1 U 0) (1 U 0) ) U 100 )0 that accepts the language: | ||
- | 3 | + | 0000, 0010, 0100, 0110, 1000. |
- | 5 | + | So, this ugly regex encodes BCD numbers that start with exactly 2 odd digits which are followed by 0 or more even digits. |
+ | </hidden> | ||
- | 7 | ||
- | 9. | + | ===== 3.3 Regex Equivalence ===== |
- | So, we got so far BCD numbers that start with an odd digit, BUT this pattern repeats twice. So BCD numbers starting with 2 odd digits. | + | Are the following regex pair equivalent? |
- | NEXT: | + | ** 3.3.1 ** |
- | ( ( 0 ( 1 U 0 ) ( 1 U 0 ) ) U 100 ) 0 )* | + | $ E1 = ab|a|b $ |
+ | \\ | ||
+ | $ E2 = (a|\epsilon)(b|\epsilon) $ | ||
- | We have a Kleene star, so something repeats any number of times. | ||
- | And, inside this Kleene star, there is ( 0 (1 U 0) (1 U 0) ) U 100 )0 that accepts the language: | ||
- | 0000 | + | <hidden><note important> |
+ | We can observe that E2 accepts ε, while E1 does not so they are not equivalent. | ||
+ | \\ | ||
+ | Another approach is to compute the language of each expression (since they are finite) and check if they are equivalent. | ||
+ | </note></hidden> | ||
- | 0010 | ||
- | 0100 | + | ** 3.3.2 ** |
- | 0110 | + | $ E1 = a(b|c)(d|e)|abb|abc $ |
+ | \\ | ||
+ | $ E2 = ab(b|c|d|e)|acd|ace $ | ||
- | 1000. | ||
- | So this ugly regex encodes BCD numbers that start with exactly 2 odd digits which are follwed by 0 or more even digits. | + | <hidden><note important> |
- | </hidden> | + | Since both E1 and E2 have a finite language, we could just compute the language and check if they are equivalent. |
+ | Language is L = {abb, abc, abd, abe, acd, ace}, therefore they are equivalent. | ||
+ | </note></hidden> | ||
+ | |||
+ | |||
+ | ** 3.3.3 ** | ||
+ | |||
+ | $ E1 = (a\mid b)^*aa^* \mid \epsilon $ | ||
+ | \\ | ||
+ | $ E2 = (a\mid ba)^*(b\mid ba)^* $ | ||
+ | |||
+ | |||
+ | <hidden><note important> | ||
+ | Both E1 and E2 have an infinite language, so comparing them is not an option. | ||
+ | |||
+ | We can see that for example E2 accepts b, while E1 does not accept it, so the expressions are not equivalent. | ||
+ | |||
+ | Fun fact: E1 was proposed by a student as a solution for 3.2.2 last year, while E2 is the actual solution. | ||
+ | </note></hidden> | ||
+ | |||
+ | |||
+ | ** 3.3.4 ** | ||
+ | |||
+ | $ E1 = ((ab^*a)^+b)^* $ | ||
+ | \\ | ||
+ | $ E2 = (a(b\mid aa)^*ab)^* $ | ||
+ | |||
+ | |||
+ | <hidden><note important> | ||
+ | Both E1 and E2 have an infinite language, so comparing them is not an option. | ||
+ | |||
+ | We can try looking for words that are accepted by one and not by the others, but we can't easily find such words. ! This does not mean they are equivalent ! | ||
+ | |||
+ | We should transform each expression into its min DFA and check if they are the same (number of states, transitions, alphabet, initial/final states) (renaming of states might be needed). | ||
+ | |||
+ | Plot twist: They are the same. | ||
+ | |||
+ | The purpose of this exercise is to understand how to approach regex equivalence, not how to solve this given comparison per | ||
+ | se. | ||
+ | </note></hidden> | ||
- | ===== Exercitii extra HARD - click here if you want nightmares ===== | + | ==== Conclusion ==== |
<hidden><note important> | <hidden><note important> | ||
What have we learned today? | What have we learned today? |