data structures and algorithms 2
play

Data Structures and Algorithms 2 Marquette University 2018 Regular - PowerPoint PPT Presentation

Data Structures and Algorithms 2 Marquette University 2018 Regular Expressions and Finite State Systems Transition Diagram A man with a pet-wolf, a pet-goat, and a cabbage wants to cross a river in a boat that can only carry him and one


  1. Data Structures and Algorithms 2 Marquette University 2018

  2. Regular Expressions and Finite State Systems

  3. Transition Diagram A man with a pet-wolf, a pet-goat, and a cabbage wants to cross a river in a boat that can only carry him and one passenger. If the man leaves the wolf and the goat alone, the wolf will eat the goat. If the man leaves the goat and the cabbage alone, then the goat will eat the cabbage. How can the man transport all three possessions to the other side of the river?

  4. Transition Diagram • Introduce a set of states A man with a pet-wolf, a pet-goat, describing possible and a cabbage wants to cross a river in a boat that can only carry him and configurations one passenger. If the man leaves the wolf and the goat alone, the wolf will eat the goat. If the man leaves the • ab:cd goat and the cabbage alone, then • a and b are on the the goat will eat the cabbage. How can the man transport all three original side of the river possessions to the other side of the • c and d are on the other river? side • M — man, W — wolf, G— goat, C — cabbage

  5. Transition Diagram • Original State : MCGW • Three possible transaction: GW :MC Men moves cabbage, goat, or MC wolf to other side • Results in three di ff erent MG states, but not all of them are MCGW : CW : MG feasible because without the man, the wolf eats the goat MW and the goat eats the cabbage. CG : MW

  6. Transition Diagram • Transition diagram • States are circles • Transitions move system from one state to the other • Each movement is associated with a letter • The letter is the pet that the man selects to transport from one side of the river to the other

  7. Transition Diagrams m g MWGC : WC:MG MWC:G g m Start w c w c A man with a pet-wolf, a pet-goat, and a cabbage wants to cross a river in a boat that can only carry him and C:MWG C:MWG one passenger. If the man leaves the wolf and the goat alone, the wolf will g g g g eat the goat. If the man leaves the goat and the cabbage alone, then the goat will eat the cabbage. How MGC:W MWG:C can the man transport all three possessions to the other side of the c river? c w w g m :MWGC MG:WC G:MWC g m Finish

  8. Finite Automata • A finite automaton consists of • A finite set of states • A finite alphabet of inputs • An initial state • A set of final states • A set of transitions • Each transition is between two states and labelled with an input. Only one transition with a certain label can leave a state. • A string is accepted by a finite automaton if it corresponds to a path from the starting state to a final state

  9. Finite Automata • Formal definition • A finite automaton is a quintuple ( Q, q 0 , Q f , Σ , δ ) • where is a finite “set of states” Q • is the “start state” q 0 ∈ Q • is the “set of final or accepting states” Q f ⊂ Q • is a finite set, the “alphabet” Σ • is the “transition function” δ : Q × Σ → Q

  10. Finite Automata • A sequence of letters is processed by a series of transitions. • Assume the following automaton and the series 0001001011. 0 Start A B 0 1 1 1 1 0 C D 0

  11. Finite Automata Before processing anything of 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  12. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  13. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  14. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  15. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  16. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  17. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  18. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  19. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  20. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  21. Finite Automata 0001001011 0 Start A B 0 1 1 1 1 0 C D 0

  22. Finite Automata • We can extend this to create a mapping • Argument: • Any state • A string • Image • The state in which we end up processing the string from the state

  23. Finite Automata • Let be the set of strings with letters in the alphabet Σ ∗ Σ • is the empty string ✏ • Extend to by defining δ : Q × Σ ∗ → Q ˆ δ ∀ q ∈ Q : ˆ � ( q, ✏ ) = q ∀ a ∈ Σ ∀ q ∈ Q : ˆ δ ( q, a ) = δ ( q, a ) ∀ w ∈ Σ ∗ ∀ a ∈ Σ ∀ q ∈ Q : ˆ δ ( q, wa ) = δ (ˆ δ ( q, w ) , a )

  24. Finite Automata • Example: 1 Start A B 1 0 0 0 0 1 C D 1 • Then strings of length 2 A B C D A B C D ✏ 0 C D A B 1 B A D D

  25. Finite Automata • Example: 1 Start A B 1 0 0 0 0 1 C D 1 • First for the empty string and strings of length 1 ˆ � A B C D A B C D ✏ 0 C D A B 1 B A D D 00 A B C D 01 D C B A 10 D C B A 11 A B C D

  26. Finite Automata • Example: 1 Start A B 1 0 0 0 0 1 C D 1 • First for the empty string and strings of length 1 ˆ � A B C D A B C D ✏ 0 C D A B 1 B A D D 00 A B C D 01 D C B A 10 D C B A 11 A B C D

  27. Finite Automata ˆ δ (ˆ 1 δ ( A, 0011) = δ ( A, 001) , 1) Start A B 1 δ ( δ (ˆ = δ ( A, 00) , 1) , 1) 0 0 0 0 δ ( δ ( δ (ˆ = δ ( A, 0) , 0) , 1) , 1) 1 = δ ( δ ( δ ( δ ( A, 0) , 0) , 1) , 1) C D 1 = δ ( δ ( δ ( C, 0) , 1 , 1) δ ( δ ( δ ( B, 0) , 1) , 1) = δ ( δ ( A, 1) , 1) = δ ( B, 1) = A

  28. Finite Automata • Acceptance • A string w is accepted by a finite automaton i ff ˆ δ ( q 0 , w ) ∈ Q f • With other words, the string transitions from the starting state to an accepting state

  29. Finite Automata • Programming Finite State Machines • Easy if you can use goto and labels • Unfortunately, too many language designers decided that you should not be lead into temptation • Otherwise: • Use an enumeration data structure for the states • Express the transition function as a dictionary • Or in Java, as an array • Keep track of the current state

  30. Finite Automata • This finite automaton accepts those 1 Start A B 1 strings that have an even number of ones and an even number of zeroes 0 0 0 0 • Lemma: The automaton is in a left 1 C D state i ff it has seen an even number 1 of ones • Lemma: The automaton is in a upper state i ff it has seen an even number of zeroes • Proofs by induction on the length of a string

  31. Non-deterministic Finite Automata • Non-determinism can make it easier to design finite automata • The transition function can be multivalued • It is a function whose values are subsets of Q → 2 Q δ : Q × Σ −

  32. Non-deterministic Finite Automata 0,1 Seen 0 0 Start Seen 0 double 0,1 0 1 • Recognize all strings in with a { 0 , 1 } ∗ Seen 1 repeated 0 or a repeated 1 • Rule: A string is accepted if there is a 1 path labeled by the string from the starting state to an accepting state Seen double 1 0,1

  33. Non-deterministic Finite Automata 0,1 Seen 0 0 0,1 Start Seen 0 double 0 1 δ 0 1 Seen 1 { Start, Seen 0 } { Start, Seen 1 } Start { Seen double 0 } Seen 0 ∅ { Seen double 1 } Seen 1 ∅ 1 { Seen double 0 } { Seen double 0 } Seen double 0 { Seen double 1 } { Seen double 1 } Seen double 1 Seen double 1 0,1

  34. Non-deterministic Finite Automata • As before, extend transition function to all strings ∀ q ∈ Q : ˆ � ( q, ✏ ) = { q } ∀ a ∈ Σ ∀ q ∈ Q : ˆ � ( q, a ) = � ( q, a ) ∀ w ∈ Σ ∗ ∀ a ∈ Σ ∀ q ∈ Q : ˆ � ( q, wa ) = { p ∈ Q | ∃ r ∈ Q : r ∈ ˆ � ( q, w ) and p ∈ � ( r, a ) }

  35. Non-deterministic Finite Automata Theorem: Let L be a set accepted by a non-deterministic finite automaton. Then there exists a deterministic finite automaton that also accepts L .

  36. Non-deterministic Finite Automata • Proof sketch: • Key idea: The states of the deterministic automaton are the subsets of the non-deterministic automaton • To calculate a transition from a subset X of states, form [ δ ( q, a ) q ∈ X • Accepting states: Those subsets that contain an accepting state • Can show: A string is accepted in the NFA only if it is accepted in the DFA

  37. Non-deterministic Finite Automata 0,1 0 0 0,1 A B C 1 δ 0 1 { A } { A, B } { A, D } D { A, B } { A, B, C } { A, D } { A, D } { A, B } { A, D, E } 1 { A, B, C } { A, B, C } { A, C } { A, D, E } { A, E } { A, D, E } E { A, C } { A, B, C } { A, C } { A, E } { A, E } { A, D, E } 0,1

  38. Non-deterministic Finite Automata • Resulting DFA 0 {A,B,C} 0 {A} 0 {A,B} 1 0 1 1 0 {A,C} {A,D} 1 1 0 {A,D,E} {A,E} 1 0 1 A string without a double 0 or a double 1 needs to have alternative 0s and 1s, oscillating between the upper left three states.

  39. Non-deterministic finite automata with ε -moves • A further generalization of non-deterministic finite automata are non-deterministic automata with ε - moves • Example: Strings in whose digits only { 0 , 1 , 2 } ∗ increase. 0 1 2 A ε B ε C Start How does this automaton accept 0000222?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend