Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lfa:2022:lab06-dfa-to-regex [2022/11/11 18:41] mihai.udubasa add pictures to 7.1. dfa1 |
lfa:2022:lab06-dfa-to-regex [2022/11/19 10:34] (current) alexandra.udrescu01 |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 7. Dfa to Regex conversions ====== | + | ====== 6. Dfa to Regex conversions ====== |
- | ==== 7.1. TBD ==== | + | ==== 6.1. State elimination ==== |
Consider the following DFAs: | Consider the following DFAs: | ||
- | **DFA1** | + | ^ **DFA1** ^ **DFA2** ^ |
+ | |{{ :lfa:screenshot_2021-11-04_at_15.33.10.png?400 |}}| {{ :lfa:2022:lfa2022_lab5_ex2_4_cerinta.png?300 |}} | | ||
- | {{ :lfa:screenshot_2021-11-04_at_15.33.10.png?400 |}} | ||
- | |||
- | **DFA2** | ||
- | |||
- | {{ :lfa:2022:lfa2022_lab5_ex2_4_cerinta.png?300 |}} | ||
Convert the given DFAs to a Regex (using the state-elimination strategy). | Convert the given DFAs to a Regex (using the state-elimination strategy). | ||
Hint: is it easier to apply conversion on another DFA? | Hint: is it easier to apply conversion on another DFA? | ||
+ | |||
+ | |||
+ | |||
<hidden On what DFA should the algorithm be applied?> | <hidden On what DFA should the algorithm be applied?> | ||
It is recommended to apply the State elimination algorithm on the minimal DFA. | It is recommended to apply the State elimination algorithm on the minimal DFA. | ||
Line 38: | Line 37: | ||
Pick a state that is not initial and not final. For each way to reach its direct successors from its direct predecessors, add a new transition. Then remove the state. | Pick a state that is not initial and not final. For each way to reach its direct successors from its direct predecessors, add a new transition. Then remove the state. | ||
- | Pick **state 1**: | + | - Pick **state 1**: |
- | - in: | + | - in: |
- | * 0 --(ε)--> 1 | + | * 0 --(ε)--> 1 |
- | * 2 --(0)--> 1 | + | * 2 --(0)--> 1 |
- | - out: | + | - out: |
- | * 1 --(ε)--> 4 | + | * 1 --(ε)--> 4 |
- | * 1 --(1)--> 2 | + | * 1 --(1)--> 2 |
- | - loop: | + | - loop: |
* 1 --(0)--> 1 | * 1 --(0)--> 1 | ||
- | - Add new transitions: | + | - Add new transitions: |
- | * 2 --(00*)--> 4 | + | * 2 --(00*)--> 4 |
- | * 2 --(00*1)--> 2 | + | * 2 --(00*1)--> 2 |
- | * 0 --(0*)--> 4 | + | * 0 --(0*)--> 4 |
- | * 0 --(0*1)--> 2 | + | * 0 --(0*1)--> 2 |
- | + | * {{ :lfa:2022:lfa2022_lab6_mindfa2_3.png?300 |}} | |
- | {{ :lfa:2022:lfa2022_lab6_mindfa2_3.png?300 |}} | + | - Pick **state 3**: |
- | + | - Because state 3 has no direct successor, it can simply be removed without adding new transitions. | |
- | Pick **state 3**: | + | - Pick **state 2**: |
- | - Because state 3 has no direct successor, it can simply be removed without adding new transitions. | + | - in: |
- | + | * 0 --(0*1)--> 2 | |
- | Pick **state 2**: | + | - out: |
- | - in: | + | * 2 --(ε U 00*)--> 4 |
- | * 0 --(0*1)--> 2 | + | - loop: |
- | - out: | + | * 2 --(00*1)--> 4 |
- | * 2 --(ε U 00*)--> 4 | + | - Add new transitions: |
- | - loop: | + | * 0 --(0*1(00*1)*(ε U 00*))--> 4 |
- | * 2 --(00*1)--> 4 | + | * This transition can be written more simply: 0 --( (0*1)(0+1)*0*)--> 4 |
- | - Add new transitions: | + | * {{ :lfa:2022:lfa2022_lab6_mindfa2_4.png?300 |}} |
- | * 0 --(0*1(00*1)*(ε U 00*))--> 4 | + | |
- | * This transition can be written more simply: 0 --( (0*1)(0+1)*0*)--> 4 | + | |
- | + | ||
- | {{ :lfa:2022:lfa2022_lab6_mindfa2_4.png?300 |}} | + | |
** Step 3:** | ** Step 3:** | ||
Line 79: | Line 74: | ||
- first, we apply the minimisation algorithm | - first, we apply the minimisation algorithm | ||
- | * {{:lfa:2022:lab6_1_dfa1_1.png?500|}} | + | * {{ :lfa:2022:lab6_1_dfa1_1.png?500 |}} |
+ | - we add the new final and initial states | ||
+ | * {{ :lfa:2022:lab6_1_dfa1_2.png?500 |}} | ||
- we eliminate the state 8 | - we eliminate the state 8 | ||
- as it has no outgoing transitions, we can just remove it | - as it has no outgoing transitions, we can just remove it | ||
- | * {{:lfa:2022:lab6_1_dfa1_2.png?500|}} | + | * {{ :lfa:2022:lab6_1_dfa1_3.png?500 |}} |
- we eliminate the state 0,1 | - we eliminate the state 0,1 | ||
- in: | - in: | ||
Line 94: | Line 91: | ||
* init --(0*)--> fin | * init --(0*)--> fin | ||
* init --(0*1)--> 2 | * init --(0*1)--> 2 | ||
- | * {{:lfa:2022:lab6_1_dfa1_3.png?500|}} | + | * {{ :lfa:2022:lab6_1_dfa1_4.png?500 |}} |
- we eliminate the state 2 | - we eliminate the state 2 | ||
- in: | - in: | ||
Line 110: | Line 107: | ||
* 3 --(10)--> 3 | * 3 --(10)--> 3 | ||
* 3 --(11)--> 4 | * 3 --(11)--> 4 | ||
- | * {{:lfa:2022:lab6_1_dfa1_4.png?500|}} | + | * {{ :lfa:2022:lab6_1_dfa1_5.png?500 |}} |
- we eliminate the state 4 | - we eliminate the state 4 | ||
- in: | - in: | ||
* init --(0*11)--> 4 | * init --(0*11)--> 4 | ||
* 3 --(11)--> 4 | * 3 --(11)--> 4 | ||
- | * 6,7 --(0)--> 4 | + | * 6,7 --(1)--> 4 |
- out: | - out: | ||
- | * 4 --(1)--> 6,7 | + | * 4 --(0)--> 6,7 |
- we add: | - we add: | ||
- | * init --(0*111)--> 6,7 | + | * init --(0*110)--> 6,7 |
- | * 3 --(111)--> 6,7 | + | * 3 --(110)--> 6,7 |
- | * 6,7 --(01)--> 6,7 | + | * 6,7 --(10)--> 6,7 |
- | * {{:lfa:2022:lab6_1_dfa1_5.png?500|}} | + | * {{ :lfa:2022:lab6_1_dfa1_6.png?500 |}} |
- we eliminate the state 6,7 | - we eliminate the state 6,7 | ||
- in: | - in: | ||
- | * init --(0*111)--> 6,7 | + | * init --(0*110)--> 6,7 |
- | * 3 --(111)--> 6,7 | + | * 3 --(110)--> 6,7 |
- | * 5 --(0)--> 6,7 | + | * 5 --(1)--> 6,7 |
- out: | - out: | ||
- | * 6,7 --(1)--> 5 | + | * 6,7 --(0)--> 5 |
* 6,7 --(ε)--> fin | * 6,7 --(ε)--> fin | ||
- loop: | - loop: | ||
- | * 6,7 --(01)--> 6,7 | + | * 6,7 --(10)--> 6,7 |
- we add: | - we add: | ||
- | * init --(0*111(01)*1)--> 5 | + | * init --(0*110(10)*0)--> 5 |
- | * init --(0*111(01)*)--> fin | + | * init --(0*110(10)*)--> fin |
- | * 3 --(111(01)*1)--> 5 | + | * 3 --(110(10)*0)--> 5 |
- | * 3 --(111(01)*)--> fin | + | * 3 --(110(10)*)--> fin |
- | * 5 --(0(01)*1)--> 5 | + | * 5 --(1(10)*0)--> 5 |
- | * 5 --(0(01)*)--> fin | + | * 5 --(1(10)*)--> fin |
- | * {{:lfa:2022:lab6_1_dfa1_6.png?500|}} | + | * {{ :lfa:2022:lab6_1_dfa1_7.png?500 |}} |
- we eliminate the state 3 | - we eliminate the state 3 | ||
- in: | - in: | ||
Line 146: | Line 143: | ||
- out: | - out: | ||
* 3 --(ε|1)--> fin | * 3 --(ε|1)--> fin | ||
- | * 3 --(0|111(01)*1)--> 5 | + | * 3 --(0|110(10)*0)--> 5 |
- loop: | - loop: | ||
- | * 3 --(01)--> 3 | + | * 3 --(10)--> 3 |
- we add: | - we add: | ||
- | * init --(0*10(01)*(ε|1) )--> fin | + | * init --(0*10(10)*(ε|1) )--> fin |
- | * init --(0*10(01)*(0|111(01)*1) )-->5 | + | * init --(0*10(10)*(0|110(10)*0) )-->5 |
- | * {{:lfa:2022:lab6_1_dfa1_7.png?500|}} | + | * {{ :lfa:2022:lab6_1_dfa1_8.png?500 |}} |
- we eliminate the state 5 | - we eliminate the state 5 | ||
- in: | - in: | ||
- | * init --(0*111(01)*1|0*10(01)*(0|111(01)*1) )--> 5 | + | * init --(0*110(10)*0|0*10(10)*(0|110(10)*0) )--> 5 |
- out: | - out: | ||
- | * 5 --(0(01)*)--> fin | + | * 5 --(1(10)*)--> fin |
- loop: | - loop: | ||
- | * 5 --(0(01)*1)--> 5 | + | * 5 --(1(10)*0)--> 5 |
- we add: | - we add: | ||
- | * init --( (0*111(01)*1|0*10(01)*(0|111(01)*1) ) (0(01)*1)*0(01)*)--> fin | + | * init --( (0*110(10)*0|0*10(10)*(0|110(10)*0) ) (1(10)*0)*1(10)*)--> fin |
- | * {{:lfa:2022:lab6_1_dfa1_8.png?500|}} | + | |
- the final regex is '' | - the final regex is '' | ||
- | 0*|0*1|0*111(01)*|0*10(01)*(ε|1)|(0*111(01)*1|0*10(01)*(0|111(01)*1))(0(01)*1)*0(01)*'' | + | 0*|0*1|0*110(10)*|0*10(10)*(ε|1|110(10)*)|(0*110(10)*0|0*10(10)*(0|110(10)*0))(1(10)*0)*1(10)*'' |
</hidden> | </hidden> | ||
- | ==== 7.2. Brzozowsky's algebraic method ==== | + | |
+ | |||
+ | ==== 6.2. Brzozowsky's algebraic method ==== | ||
[[https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)|Janusz Brzozowski]] worked out a very elegant (and more computationally efficient) way to convert Dfa's to Regexes. It relies on an observation called **Arden's Lemma**: | [[https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)|Janusz Brzozowski]] worked out a very elegant (and more computationally efficient) way to convert Dfa's to Regexes. It relies on an observation called **Arden's Lemma**: | ||
Line 180: | Line 178: | ||
=== Dfa to regex conversion === | === Dfa to regex conversion === | ||
- | For each state $math[q], build an equation of the form: $math[q = c_1 q_1 \cup c_2 q_2 \ldots c_n q_n], such that: $math[\delta(q,c_i) = q_i]. Here $math[c_i\in\Sigma], thus $math[q_i] are the $math[c_i]-successors of $math[q]. Additionally, if $math[q] is a final state, and an $math[\epsilon]: $math[q = c_1 q_1 \cup c_2 q_2 \ldots c_n q_n \cup \epsilon]. | + | For each state $math[q], build an equation of the form: $math[q = c_1 q_1 \cup c_2 q_2 \ldots c_n q_n], such that: $math[\delta(q,c_i) = q_i]. Here $math[c_i\in\Sigma], thus $math[q_i] are the $math[c_i]-successors of $math[q]. Additionally, if $math[q] is a final state, add an $math[\epsilon]: $math[q = c_1 q_1 \cup c_2 q_2 \ldots c_n q_n \cup \epsilon]. |
^ ^ ^ | ^ ^ ^ | ||
Line 193: | Line 191: | ||
=== Reducing the system of equations === | === Reducing the system of equations === | ||
- | We can choose *any** equation **except** that corresponding to the initial state, and eliminate it, by exploiting **Arden's Lemma**: | + | We can choose **any** equation **except** that corresponding to the initial state, and eliminate it, by exploiting **Arden's Lemma**: |
* the solution to any equation of the form $math[q = e\cdot q \cup e'] is $math[q = e^*e']. | * the solution to any equation of the form $math[q = e\cdot q \cup e'] is $math[q = e^*e']. | ||
**Example** | **Example** | ||
- | Goind back to the previous system of equations, we can find the solution to $math[q_2] which is: $math[(A \cup B)^*]. Next, we can replace the solution to $math[q_2] in $math[q_1] which yields: | + | Going back to the previous system of equations, we can find the solution to $math[q_2] which is: $math[(A \cup B)^*]. Next, we can replace the solution to $math[q_2] in $math[q_1] which yields: |
* $math[q_1 = A q_1 \cup B(A\cup B)^*]. | * $math[q_1 = A q_1 \cup B(A\cup B)^*]. | ||
* We apply Arden's Lemma one more time and yield: $math[q_1 = A^*B(A \cup B)^*]. | * We apply Arden's Lemma one more time and yield: $math[q_1 = A^*B(A \cup B)^*]. | ||
Line 216: | Line 214: | ||
=== Exercise === | === Exercise === | ||
- | 7.2.1. Apply Brzozowsky's method to find a regex for the following DFA: | + | 6.2.1. Apply Brzozowsky's method to find a regex for the following DFA: |
^ {{ :lfa:2022:screenshot_2022-11-09_at_15.46.23.png?200 |}} ^ | ^ {{ :lfa:2022:screenshot_2022-11-09_at_15.46.23.png?200 |}} ^ | ||
+ | |||
+ | |||
+ | |||
<hidden Solution> | <hidden Solution> | ||