CSE 311: Foundations of Computing highlights Fall 2014 • DFAs ≡ Regular Expressions Lecture 26: Pattern matching, Halting problem – No need to know details of NFAs→RegExpressions • Method for proving no DFAs for languages {0 n 1 n : n ≥ 0}, – e.g. {Binary palindromes} pattern matching string s = x y x x y x y x y y x y x y x y y x y x y x x pattern p = x y x y y x y x y x x • Given – a string, s s s, of n s n n n characters – a pattern, p p p p, of m m characters m m – usually m m m<<n m n n n • Find – all occurrences of the pattern p p p p in the string s s s s • Obvious algorithm: – try to see if p p p p matches at each of the positions in s s s s stop at a failed match and try the next position
string s = x y x x y x y x y y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x x y x y y x y x y x x x y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x x y x y x y x y x x x y x y y x y x y x x x y x y x y y x y x y x x
string s = x y x x y x y x y y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x x y x y x y x y x x x y x y x y x y y x y x y y x y x y y x y x y x x x x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x x y x y x y x y x x x y x y x y x y y x y x y y x x x y x y y x y x y x x x y x y y x y x y x x x y x y y x y x y x x x x y x y y x y x y x x
string s = x y x x y x y x y y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x x y x y x y x y x x x y x y x y x y y x y x y y x x x y x y y x y x y x x x y x y y x y x y x x x x x y x x y x x y x y y x y x y x x x x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x x y x y x y x y x x x y x y x y x y y x y x y y x x x y x y y x y x y x x x y x y y x y x y x x x x x y x x y x x x x x x y x y y x y x y x x x y x y y x y x y y x y x y x x
string s = x y x x y x y x y y x y x y x y y x y x y x x string s = x y x x y x y x y y x y x y x y y x y x y x x x y x y x y x y x Lots of wasted work x y x y x y y x x y x y y x y x y x x x Worst-case time x y x O( mn ) x x x y x y y x x y x y y x y x y x x x y x y y x y x y x x better pattern matching via finite automata building a DFA for the pattern • Build a DFA for the pattern (preprocessing) of size pattern p =x y x y y x y x y x x O(m m m) m – Keep track of the ‘longest match currently active’ – The DFA will have only m m+1 states m m • Run the DFA on the string n n n n steps • Obvious construction method for DFA will be O(m m 2 m m 2 2 ) 2 but can be done in O(m m m) time. m • Total O(m+n m+n m+n m+n) time
preprocessing the pattern preprocessing the pattern pattern p =x y x y y x y x y x x pattern p =x y x y y x y x y x x preprocessing the pattern preprocessing the pattern pattern p =x y x y y x y x y x x pattern p =x y x y y x y x y x x
generalizing Languages and Machines! All Are there Are there Are there Are there • Can search for arbitrary combinations of patterns things Java things Java things Java things Java Java – Not just a single pattern can’t do? can’t do? can’t do? can’t do? – Build NFA for pattern then convert to DFA ‘on the fly’. Context-Free Compare DFA constructed above with subset construction for the obvious NFA. Binary Palindromes Regular DFA 0* NFA Regex Finite {001, 10, 12} An Assignment Too Simple for 142. Follow Up Question Students should write a Java program that… Students should write a Java program that… Students should write a Java program that… Students should write a Java program that… What does this program do? What does this program do? What does this program do? What does this program do? – Prints “Hello” to the console – Eventually exits _(__,___,____){___/__<=1?_(__,___+1,___ _):!(___%__)?_(__,___+1,0):___%__==___ / GradeIt, GradeIt GradeIt GradeIt , , PracticeIt , PracticeIt, etc. need to grade the PracticeIt PracticeIt , etc. need to grade the , etc. need to grade the , etc. need to grade the __&&!____?(printf("%d\t",___/__),_(__,_ students. students. students. students. __+1,0)):___%__>1&&___%__<___/__?_( __,1+ ___,____+!(___/__%(___%__))):___<__*__ How do we write that grading program? ?_(__,___+1,____):0;}main(){_(100,0,0);}
Sneak Peak Some Notation and Starting Ideas It turns out the simple autograder is We’re going to be talking about Java code a impossible to write… lot. CODE( P ) will mean “the code of the program P” And we’ll prove it! So, consider the following function: public String P(String x) { return new String(Arrays.sort(x.toCharArray()); } What is P ( CODE ( P ))? “ “ ((()))..;AACPSSaaabceeggghiiiilnnnnnooprrrrrrrrrrrsssttttttuuwxxyy{} ” “ “ The Halting Problem The Halting Problem Given: - Given: Given: Given: - - - CODE( P ) for any program P Given: Given: Given: Given: - - - CODE( P ) for any program P - - input x - input x Output: Output Output Output : : : true if P halts on input x Output Output Output Output: : : : true if P halts on input x false if P does not halt on input x false if P does not halt on input x It turns out that it isn’t possible to write a It turns out that it isn’t possible to write It turns out that it isn’t possible to write It turns out that it isn’t possible to write a a a program that solves the Halting Problem. program that solves the Halting Problem. program that solves the Halting Problem. program that solves the Halting Problem.
public static void D ( x ) { Proof by contradiction if ( H ( x , x ) == true) { Does D (CODE( D )) halt? while (true); /* don’t halt */ } • Suppose that H H is a Java program that solves the H H else { return; /* halt */ Halting problem. Then we can write this program: } } public static void D ( x ) { H solves the halting problem implies that if ( H ( x , x ) == true) { H(CODE( D ),x) is true iff D (x) halts, H(CODE( D ),x) is false iff not while (true); /* don’t halt */ Suppose D (CODE( D )) halts . } Then, we must be in the second case of the if. else { So, H( CODE( D ) , CODE( D )) is false return; /* halt */ Which means D ( CODE( D )) doesn’t halt } } Suppose D (CODE( D )) doesn’t halt . Then, we must be in the first case of the if. • Does D D D (CODE( D D D )) halt? D D So, H( CODE( D ) , CODE( D )) is true . Which means D ( CODE( D )) halts . public static void D ( x ) { public static void D ( x ) { if ( H ( x , x ) == true) { if ( H ( x , x ) == true) { while (true); /* don’t halt */ while (true); /* don’t halt */ Does D (CODE( D )) halt? Does D (CODE( D )) halt? } } else { else { return; return; /* halt */ /* halt */ } } } } H solves the halting problem implies that H solves the halting problem implies that H(CODE( D ),x) is true iff D (x) halts, H(CODE( D ),x) is false iff not H(CODE( D ),x) is true iff D (x) halts, H(CODE( D ),x) is false iff not Suppose D (CODE( D )) halts . Suppose D (CODE( D )) halts . Then, we must be in the second case of the if. Then, we must be in the second case of the if. So, H( CODE( D ) , CODE( D )) is false So, H( CODE( D ) , CODE( D )) is false Which means D ( CODE( D )) doesn’t halt Which means D ( CODE( D )) doesn’t halt Suppose D (CODE( D )) doesn’t halt . Suppose D (CODE( D )) doesn’t halt . Then, we must be in the first case of the if. Then, we must be in the first case of the if. So, H( CODE( D ) , CODE( D )) is true . So, H( CODE( D ) , CODE( D )) is true . Which means D ( CODE( D )) halts . Which means D ( CODE( D )) halts .
public static void D ( x ) { That’s it! if ( H ( x , x ) == true) { Does D (CODE( D )) halt? while (true); /* don’t halt */ } else { • We proved that there is no computer return; /* halt */ } program that can solve the Halting Problem. } – There was nothing special about Java H solves the halting problem implies that H(CODE( D ),x) is true iff D (x) halts, H(CODE( D ),x) is false iff not Suppose D (CODE( D )) halts . • This tells us that there is no compiler that Then, we must be in the second case of the if. can check our programs and guarantee to So, H( CODE( D ) , CODE( D )) is false find any infinite loops they might have. Which means D ( CODE( D )) doesn’t halt Suppose D (CODE( D )) doesn’t halt . Then, we must be in the first case of the if. So, H( CODE( D ) , CODE( D )) is true . Contradiction! Which means D ( CODE( D )) halts . What’s next? • We showed: If some “hypothetical” subroutine H existed that solved the Halting Problem then it would let us build a program D that cannot possibly exist – We will use the same idea to show that programs solving other problems are impossible, but we now will be able to use that H cannot exist • A key piece of the proof was considering what a program does when given its own code as input – This was inspired by a method to compare the sizes of infinite sets call diagonalization that we will study next class
Recommend
More recommend