Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Notes 

Similar presentations


Presentation on theme: "Lecture Notes "— Presentation transcript:

1 Lecture Notes  http://iir.ruc.edu.cn/~pwwang/ITC.jsp

2 Classical Theory Algorithm Decidability Efficiency (P vs. NP) decidable efficient inefficient

3 Introduction to the Theory of Computation Part I: Automata

4 1.1 Finite Automata  Deterministic FA  Non-Deterministic FA  NFA = DFA  -NFA  -NFA = NFA

5 Finite Automata  The simplest computational model, with very limited memory, but highly useful  Example: a finite automaton modeling an on/off switch offon push Start

6 State Diagram of FA start state accept state transitions states M1: 1 0 q1q3q2 1 0 0,1

7 M1 Cont ’ d on input “ 0110 ”, the machine goes: q1  q1  q2  q3  q3 = “ reject ” 1 0 q1q3q2 1 0 0,1

8 M1 Cont ’ d on input “ 101 ”, the machine goes: q1  q2  q1  q2 = “ accept ” 1 0 q1q3q2 1 0 0,1

9 Strings and Languages  Alphabet: a finite set of symbols,  A string over an alphabet is a finite sequence of symbols from the alphabet  A language is a set of strings.

10 M1 Cont ’ d 010: 11: 0110100: 010000010010: What is the language accepted by M1? accept reject accept 1 0 q1q3q2 1 0 0,1 Accepts all strings without two consecutive 1’s.

11 Formal Definition of FA  A finite automaton is defined by a 5- tuple (Q, Σ, , q 0, F) Q: finite set of states Σ: finite alphabet : transition function, : Q x Σ  Q, takes a state and input symbol as arguments, and returns a state q 0 Q: start state FQ: set of accept states

12 M1 ’ s Formal Definition  M1 = (Q, Σ, , q 0, F), where Q = {q1, q2, q3} Σ = {0, 1} (q1,0)=q1, (q1,1)=q2, (q2,0)=q1, (q2,1)=q3, (q3,0)=q3, (q3,1)=q3 q1 is the start state F = {q1,q2} q1 q2 q3 01 q1q2 q1q3 1 0 q1q3q2 1 0 0,1

13 Formal Definition of Computation  A DFA M accepts input w if w can be written as where and sequences of states exist that satisfy the following conditions: 1. 2.For, we have 3.r m is an accept state. Computations,Derivations and Proofs

14 Languages recognized by FA  If A is the set of all strings that machine M accepts, then A is called the language of M and write L(M)=A. We say that M recognizes A or M accepts A.  Two machines are equivalent if they recognize the same languages. If machine M accepts no strings, then L(M) is the empty language.

15 Designing Finite Automata  Given some language, design a FA that recognizes it.  Pretending to be a FA, You see the input symbols one by one You have finite memory, i.e. finite set of states, so remember only the crucial information (finite set of possibilities) about the string seen so far.

16 Example  Σ ={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L. What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities.

17 Example  Σ ={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L. What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities. Assign a state to each possibility. q even q odd

18 Example  Σ ={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L. What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities. Assign a state to each possibility. Assign the transitions from one possibility to another upon reading a symbol. q even q odd 0 1 1 0

19 Example  Σ ={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L. What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities. Assign a state to each possibility. Assign the transitions from one possibility to another upon reading a symbol. Set the start and accept states. q even q odd 0 1 1 0

20 Exercise  Σ ={0,1}, L = {w | w has even number of 0s and even number of 1s}, design a FA to recognize L. What to remember? How many possibilities?

21 Exercise q 00 q 01 q 10 q 11 0 0 00 1 1 1 1

22 Example 1.9  Σ ={0,1}, L = {w | w contains 001 as a substring}, design a FA to recognize L. four possibilities: 1.haven ’ t just seen any symbols of 001 --- q 2.have just seen a 0 --- q 0 3.have just seen a 00 --- q 00 4.have seen the entire pattern 001 --- q 001

23 Example 1.9 qq0q0 q 00 q 001 1 0 1 1 0 0 0, 1

24 1.1 Finite Automata  Deterministic FA  Non-Deterministic FA  NFA = DFA  -NFA  -NFA = NFA

25 Nondeterministic Finite Automata  Deterministic: At any point when the machine is in a state with an input symbol, there is a unique next state to go.  Non-Deterministic: There can be more than one next state for each state-input pair.  Example: an automaton that accepts all strings ending in 01. q0q0 q1q1 q2q2 10 0, 1 M2:

26 How an NFA computes? q0q0 q0q0 q0q0 q0q0 q0q0 q0q0 q1q1 q1q1 q1q1 q2q2 q2q2 00101 (die) Input: q0q0 q1q1 q2q2 10 0, 1

27 Formal Definition of NFA  An NFA can be in several states at once, or viewed in another way, it can “ guess ” which state to go next.  Formally, an NFA is a 5-tuple N = (Q, Σ, , q 0, F), where all is as DFA, but : Q x Σ  P (Q) is a transition function from Q x Σ to the power set of Q.

28 M2 ’ s Formal Definition  M2 = (Q, Σ, , q 0, F), where Q = {q 0, q 1, q 2 } Σ = {0, 1} (q 0,0)={q 0,q 1 }, (q 0,1)={q 0 }, (q 1,1)={q 2 } q 0 is the start state F = {q 2 } q0q0 q1q1 q2q2 10 0, 1 q0q0 q1q1 q2q2 01 {q 0,q 1 }{q 0 }  {q 2 } 

29 Formal Definition of Computation  A NFA M accepts input w if w can be written as where and sequences of states exist that satisfy the following conditions: 1. 2.For, we have 3.r m is an accept state.

30 1.1 Finite Automata  Deterministic FA  Non-Deterministic FA  NFA = DFA  -NFA  -NFA = NFA

31 Equivalence of NFA and DFA  NFA ’ s are usually easier to “ program ” in.  Surprisingly, for each NFA there is an equivalent (recognizes the same language) DFA.  But the DFA can have exponentially many states.

32 Subset Construction  Given an NFA N=(Q N, Σ,  N, q 0, F N ), we will construct an DFA D=(Q D, Σ,  D, {q 0 }, F D ), such that L(D) = L(N).  Subset construction: Q D = {S | S  Q N }, i.e. power set of Q N  D (S, a) = ∪ p  S  N (p, a) F D = {S | S ∩ F ≠ , S ∈ Q D }

33 Construct DFA from M2  Subset construction: 01  {q 0 }{q 0,q 1 }{q 0 } {q 1 }  {q 2 }  {q 0,q 1 } {q 0,q 2 } {q 0,q 1 }{q 0 } {q 1,q 2 }  {q 2 } {q 0,q 1,q 2 }{q 0,q 1 }{q 0,q 2 } {q 0,q 1 } 10 1 {q 0 } {q 0,q 2 } 0 0 1 8 possible subsets, 3 accessible:

34 Example NFA  Design an NFA to accept strings over alphabet {1, 2, 3} p q r s t 1, 2, 3 11 22 33 1, 2 1

35 Equivalent DFA  32 possible subsets, 15 accessible:  DFA is much larger than NFA 123 ppqprps pqpqtprps pqt prps prpqrprtps prtpqrprtps pqsprspst pqsprspst prspqrsprstpst prstpqrsprstpst pqspqstprspst pqst prspst pqrpqrtprtps pqrt prtps pqrspqrstprstpst pqrst prstpst

36 1.1 Finite Automata  Deterministic FA  Non-Deterministic FA  NFA = DFA  -NFA  -NFA = NFA

37 Finite Automata with - Transitions  Allow  to be a label on arcs.  Formally the transition function  of an -NFA is from Q x Σ ∪ {} to P (Q).  Nothing else changes: acceptance of w is still the existence of a path from the start state to an accept state with label w. But  can appear on arcs, and means the empty string (i.e., no visible contribution to w).

38 Example of an -NFA qr s  1 0 0  1 “001” is accepted by the path q  s  r  q  r  s with label 0  01  = 001

39 1.1 Finite Automata  Deterministic FA  Non-Deterministic FA  NFA = DFA  -NFA  -NFA = NFA

40 Elimination of -Transitions  -transitions are a convenience, but do not increase the power of FA's. To eliminate - transitions: 1.Compute the transitive closure of the -arcs only. 2.If a state p can reach state q by -arcs, and there is a transition from q to r on input a (not ), then add a transition from p to r on input a. 3.Make state p an accept state if p can reach some accept state q by -arcs. 4.Remove all -transitions.

41 -CLOSURE  -CLOSURE(q): all states reachable from q by a sequence  …  q-CLOSURE(q); p-CLOSURE(q) and r(p, )  r-CLOSURE(q)

42 Example 1.-CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s} qr s 1 0 0 1  

43 Example 1.-CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s} 2.Add (s, 0)={q}, (r, 1)={q} qr s 1 0 0, 1  

44 Example 1.-CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s} 2.Add (s, 0)={q}, (r, 1)={q} 3.Add r into F as an accept state q rs 1 0 0, 1  

45 Example 1.-CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s} 2.Add (s, 0)={q}, (r, 1)={q} 3.Add r into F as an accept state 4.Remove (s, )={r}, (r, )={s} q rs 1 0 0, 1

46 Summary of Finite Automata  DFA, NFA, and -NFA are all equivalent.  -NFA  NFA  DFA  -elimination subset construction

47 Assignment 1  Exercise 1.3  Exercise 1.5: (c)(d)  Exercise 1.12


Download ppt "Lecture Notes "

Similar presentations


Ads by Google