1 / 141

Deterministic Finite Automata Machine

Deterministic Finite Automata Machine. More Examples of DFAs Simple examples 0*1* vs 0 n 1 n Mod Counting Examples Contains Substring (NFA vs DFA) Any Finite Language Integer mod 7 Adding to Integers Calculator Syntax Processor. Read-Once and Bounded Memory Loop Invariants

lel
Télécharger la présentation

Deterministic Finite Automata Machine

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deterministic Finite AutomataMachine More Examples of DFAs Simple examples 0*1* vs 0n1n Mod Counting Examples Contains Substring (NFA vs DFA) Any Finite Language Integer mod 7 Adding to Integers Calculator Syntax Processor Read-Once and Bounded Memory Loop Invariants Constant Memory Code with Simple Loop Deterministic Finite Automata Path Through Graph DFA "knowing" and distinguishable strings Nondeterministic Finite Automata DFA vs TM (Extended) Regular Expressions Parsing with Regular Expressions Jeff Edmonds York University ECS 2001 Lecture 2

  2. Read-Once & Bounded Memory • The input arrives as a stream.One character at a time.It cannot be reread. • Eg: • simple iterative algorithms • simple mechanical or electronic devices like elevators and calculators • simple processes like the job queue of an operating system • simple patterns within strings of characters. • simple languages of strings {01,100,01110,… }

  3. Read-Once & Bounded Memory l=40, x=3, y=2 • The input arrives as a stream.One character at a time.It cannot be reread. • What do you have to rememberso that in the end you can answer the question? • The memory is bounded! Loop Invariant:

  4. Read-Once & Bounded Memory Many different equivalent models: • Loop Invariant • What is remembered at top of loop • Code with simple loop • Action each character read • Deterministic Finite Automata (DFA) • Focuses on transitions between states • Path through DFA • Each path represents a string • Nondeterministic Finite Automata (NFA) • Get help from a fairy god mother. • Regular Expressions • Representing string patterns • Extended Regular Expressions • Also allow set intersection and set complement Compilably Equivalent (a*b  a*bb) 0*1* (a*b  a*bb) 0*1*

  5. Read-Once & Bounded Memory Many different equivalent models: • Loop Invariant • What is remembered at top of loop • Code with simple loop • Action each character read • Deterministic Finite Automata (DFA) • Focuses on transitions between states • Path through DFA • Each path represents a string • Nondeterministic Finite Automata (NFA) • Get help from a fairy god mother. • Regular Expressions • Representing string patterns • Extended Regular Expressions • Also allow set intersection and set complement Compilably Equivalent (a*b  a*bb) 0*1* (a*b  a*bb) 0*1*

  6. Read-Once & Bounded MemoryLoop Invariant Given a string, find the longest block of 1’s 00101111001100011111000011001 Alg reads the digits one at a time and remembers enough about what has been read so that it does not need to reread anything.

  7. Largest block so far. Read-Once & Bounded MemoryLoop Invariant Given a string, find the longest block of 1’s 00101111001100011 1 1 1 When it has read this much,what does it remember? Read the next character & re-determine the largest block so far.

  8. Read the next character & re-determine the largest block so far& current largest. Read-Once & Bounded MemoryLoop Invariant Given a string, find the longest block of 1’s 00101111001100011 1 1 1 When it has read this much,what does it remember? • Largest block so far. • Size of current block.

  9. Read-Once & Bounded MemoryLoop Invariant A loop invariant is an assertion that must be true about the state of the data structure every time the algorithm is at the top of the loop.

  10. Solution Extra Read-Once & Bounded MemoryLoop Invariant The input consists of an array of objects We have read in the first i objects. We will pretend that this prefix is the entire input. We have a solution for this prefix plus some additional information

  11. Solution Extra Read-Once & Bounded MemoryLoop Invariant The input consists of an array of objects • Do not worry about the entire computation. next

  12. Solution Solution Extra Extra Read-Once & Bounded MemoryLoop Invariant The input consists of an array of objects We read in the i+1st object. We will pretend that this larger prefix is the entire input. We extend the solution we have to one for this larger prefix. (plus some additional information)

  13. Read-Once & Bounded MemoryLoop Invariant • By Induction the computation will always keep the loop invariant true!

  14. Solution Exit Read-Once & Bounded MemoryLoop Invariant The input consists of an array of objects In the end, we have read in the entire input. The LI gives us that we have a solution for this entire input.

  15. Exit Exit Exit 0 km Exit 79 km 75 km 79 km to school Exit Loop Invariant

  16. 52,23,88,31,25,30,98,62,14,79 23,31,52,88 Insertion Sort The input consists of an array of integers Solution We have read in the first i objects. We will pretend that this prefix is the entire input. We have a solution for this prefix

  17. 23,25,31,52,88 52,23,88,31,25,30,98,62,14,79 23,31,52,88 Insertion Sort The input consists of an array of integers We read in the i+1st object. We will pretend that this larger prefix is the entire input. We extend the solution we have to one for this larger prefix.

  18. If you are describing an alg to a friend, is this what you say? Do you think about an algorithm as a sequence of actions? Do you explain it by saying:“Do this. Do that. Do this”? Do you get lost about where the computation is and where it is going? What if there are many paths through many ifs and loops? How do you know it worksfor every pathon every input? A Sequence of Actions Max( a,b,c ) m = a if( b>m ) m = bendif if( c>m ) m = cendif return(m)

  19. “preCond: Input has 3 numbers.” “postCond: return max in {a,b,c}” A Sequence of Actions Max( a,b,c ) m = a At least tell me what the algorithm is supposed to do. • Preconditions:Any assumptions that must be true about the input instance. • Postconditions:The statement of what must be true when the algorithm/program returns. if( b>m ) m = bendif if( c>m ) m = cendif return(m)

  20. “preCond: Input has 3 numbers.” “postCond: return max in {a,b,c}” A Sequence of Actions Max( a,b,c ) m = a How can you possibly understand this algorithm without knowing what is true when the computation is here? if( b>m ) m = bendif if( c>m ) m = cendif return(m)

  21. “preCond: Input has 3 numbers.” “postCond: return max in {a,b,c}” A Sequence of Actions Max( a,b,c ) m = a “assert: m is max in {a}” How can you possibly understand this algorithm without knowing what is true when the computation is here? if( b>m ) m = bendif “assert: m is max in {a,b}” if( c>m ) m = cendif Tell me! “assert: m is max in {a,b,c}” return(m)

  22. vsA Sequence of Assertions Max( a,b,c ) m = a if( b>m ) m = bendif if( c>m ) m = cendif return(m) A Sequence of Actions “preCond: Input has 3 numbers.” “assert: m is max in {a}” “assert: m is max in {a,b}” It is helpful to have different ways of looking at it. “assert: m is max in {a,b,c}” “postCond: return max in {a,b,c}”

  23. Fixed/Constant vs Arbitrary/Finite l=40, x=3, y=2 • The input arrives as a stream.One character at a time.It cannot be reread. • What do you have to rememberso that in the end you can answer the question? • The memory is bounded! Loop Invariant:

  24. Fixed/Constant vs Arbitrary/Finite Given a string, find the longest block of 1’s l=40, x=3, y=2 00101111001100011 1 When it has read this much,what does it remember? • Largest block so far. • Size of current block. • How much memory is need on an input of length n? • A count up to n, takes log n bits of memory. • Too much. Memory can’t grow with input!

  25. Fixed/Constant vs Arbitrary/Finite Max( a,b,c ) m = a if( b>m ) m = bendif if( c>m ) m = cendif return(m) “preCond: Input has 3 numbers.” “assert: m is max in {a}” “assert: m is max in {a,b}” • How much memory is need on an input of length n? • A index up to n, takes log n bits of memory. • Too much. Memory can’t grow with input! “assert: m is max in {a,b,c}” “postCond: return max in {a,b,c}”

  26. Fixed/Constant vs Arbitrary/Finite • Given the needs of the problem at hand,a programmer can give her constant memory program • as many variables and • as a big of a (finite) range for each variable • as she wants. • But these numbers are fixed/constant. • If K(J,I) is these numbers program J has on input I, • then this function can depend on J, • but can’t depend on I • (or on |I|) • "regular language L, •  constant memory program J,  an integer k, • "inputs I, • K(J,I) k

  27. Fixed/Constant vs Arbitrary/Finite I come up with a regular language L • "regular language L, •  constant memory program J,  an integer k, • "inputs I, • K(J,I) k

  28. Fixed/Constant vs Arbitrary/Finite I write a constant memory program J and give it k=1,000,000,000,000,000 variables, each with range 0..1,000,000,000,000,000! • "regular language L, •  constant memory program J,  an integer k, • "inputs I, • K(J,I) k

  29. Fixed/Constant vs Arbitrary/Finite I write a constant memory program J and give it k=1,000,000,000,000,000 variables, each with range 0..1,000,000,000,000,000! Wow. That’s not fair.With more memory, it can memorize more of the input and then can have any power to determine the answer. • "regular language L, •  constant memory program J,  an integer k, • "inputs I, • K(J,I) k

  30. Fixed/Constant vs Arbitrary/Finite I write a constant memory program J and give it k=1,000,000,000,000,000 variables, each with range 0..1,000,000,000,000,000! I will let him use any amount of memory,but this fixed J must work for all inputs. • "regular language L, •  constant memory program J,  an integer k, • "inputs I, • K(J,I) k

  31. Fixed/Constant vs Arbitrary/Finite I write a constant memory program J and give it k=1,000,000,000,000,000 variables, each with range 0..1,000,000,000,000,000! If he uses more memory,I will give him a bigger input I. Hee Hee Hee J must still solve the problem. • "regular language L, •  constant memory program J,  an integer k, • "inputs I, • K(J,I) k

  32. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. a = 001011 Length = |a|= 6 > 3 Therefore not in language L. a = 011 # of 1’s = 2 is even Therefore not in language L. a = 001 Length = |a|= 3  3 # of 1’s = 1 is odd Therefore in language L.

  33. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. Alg reads the digits one at a time and remembers enough about what read so that it does not needto reread anything.

  34. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. l =length = |a|  {0,1,2,3,more} # of 1’s is r  {even,odd} a = 001 When it has read this much,what does it remember?

  35. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. l =length = |a|  {0,1,2,3,more} # of 1’s is r  {even,odd} l =0, r = even a =  With the empty string read, establish the empty string.

  36. Read the next character & re-determine the largest block so far& current largest. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. l =length = |a|  {0,1,2,3,more} # of 1’s is r  {even,odd} l =0, r = even a =  lt= lt-1+1 = 1, rt = rt-1 = even a = 0

  37. Read the next character & re-determine the largest block so far& current largest. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. l =length = |a|  {0,1,2,3,more} # of 1’s is r  {even,odd} l =0, r = even a =  lt= lt-1+1 = 1, rt = rt-1 = even a = 0 lt= lt-1+1 = 2, rt = rt-1 +1 = odd a = 01

  38. Read the next character & re-determine the largest block so far& current largest. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. l =length = |a|  {0,1,2,3,more} # of 1’s is r  {even,odd} l =0, r = even a =  lt= lt-1+1 = 1, rt = rt-1 = even a = 0 lt= lt-1+1 = 2, rt = rt-1 +1 = odd a = 01 lt= lt-1+1 = 3, rt = rt-1 = odd a = 010

  39. Read the next character & re-determine the largest block so far& current largest. Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. l =length = |a|  {0,1,2,3,more} # of 1’s is r  {even,odd} l =0, r = even a =  lt= lt-1+1 = 1, rt = rt-1 = even a = 0 lt= lt-1+1 = 2, rt = rt-1 +1 = odd a = 01 lt= lt-1+1 = 3, rt = rt-1 = odd a = 010 lt= lt-1+1 = more, rt = rt-1 +1 = even a = 0101

  40. Exit Read-Once & Bounded MemoryLoop Invariant L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. l =length = |a|  {0,1,2,3,more} # of 1’s is r  {even,odd} l =0, r = even a =  lt= lt-1+1 = 1, rt = rt-1 = even a = 0 lt= lt-1+1 = 2, rt = rt-1 +1 = odd a = 01 lt= lt-1+1 = 3, rt = rt-1 = odd a = 010 lt= lt-1+1 = more, rt = rt-1 +1 = even a = 0101 Once the string is all read in,determine if in L or not. a  L

  41. Read-Once & Bounded Memory Many different equivalent models: • Loop Invariant • What is remembered at top of loop • Code with simple loop • Action each character read • Deterministic Finite Automata (DFA) • Focuses on transitions between states • Path through DFA • Each path represents a string • Nondeterministic Finite Automata (NFA) • Get help from a fairy god mother. • Regular Expressions • Representing string patterns • Extended Regular Expressions • Also allow set intersection and set complement Compilably Equivalent (a*b  a*bb) 0*1* (a*b  a*bb) 0*1*

  42. Code with Simple Loop Loop Invariant that must always be true when at top of loop. L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. Gives a picture of what will be remembered about the prefix read so far.

  43. Exit Read the next character & maintain the Loop Invariant. Code with Simple Loop Establish the Loop Invariant for the empty string. L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. Once the input string is all read in,determine if it is in L or not.

  44. Code with Simple Loop Loop Invariant that must always be true when at top of loop. Gives a picture of what will be remembered about the prefix read so far. Temporary variables need not be mentioned.

  45. Exit Read the next character & maintain the Loop Invariant. Code with Simple Loop Establish the Loop Invariant for the empty string. Once the input string is all read in,determine if it is in L or not.

  46. Read-Once & Bounded Memory Many different equivalent models: • Loop Invariant • What is remembered at top of loop • Code with simple loop • Action each character read • Deterministic Finite Automata (DFA) • Focuses on transitions between states • Path through DFA • Each path represents a string • Nondeterministic Finite Automata (NFA) • Get help from a fairy god mother. • Regular Expressions • Representing string patterns • Extended Regular Expressions • Also allow set intersection and set complement Compilably Equivalent (a*b  a*bb) 0*1* (a*b  a*bb) 0*1*

  47. Deterministic Finite Automaton (DFA) L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. Temp variables need not be considered. Build a state node for each “state” that the loop invariant says the computation might be in.ie for every setting of the variables. Give the states meaningful names.

  48. For each state node & for each character, have an edge to the state node to transition to. Deterministic Finite Automaton (DFA) Establish the Loop Invariant for the empty stringby specifying the start state. L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. δ(qcurrent , cnext read) = qnext

  49. Exit Deterministic Finite Automaton (DFA) L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. Accept states are those at whichthe DFA accepts the input if in this state when the string ends.

  50. Exit Deterministic Finite Automaton (DFA) L = {a Î {0,1}* | a has length at mostthree and the number of 1'sis odd }. Dead state can be useful if prefix of input read will never lead to an accept

More Related