Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005.

Similar presentations


Presentation on theme: "1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005."— Presentation transcript:

1 1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005

2 2 Content Finite Automata, FA Deterministic Finite Automata, DFA Nondeterministic Automata NFA NFA  DFA Equivalence

3 3 Finite Automata FA

4 4 There is no formal general definition for "automaton". Instead, there are various kinds of automata, each with it's own formal definition. has some form of input has some form of output has internal states, may or may not have some form of storage is hard-wired rather than programmable Generally, an automaton

5 5 Finite Automaton Input String Output String Finite Automaton

6 6 Finite Accepter Input “Accept” or “Reject” String Finite Automaton Output

7 7 Nodes = States Edges = Transitions An edge with several symbols is a short-hand for several edges: FA as Directed Graph

8 8 Deterministic Finite Automata DFA

9 9 Deterministic there is no element of choice Finite only a finite number of states and arcs Acceptors produce only a yes/no answer DFA

10 10 Transition Graph initial state final state “accept” state transition abba -Finite Acceptor Alphabet =

11 11 Initial Configuration Input String

12 12 Reading the Input

13 13

14 14

15 15

16 16 Output: “accept”

17 17 Rejection

18 18

19 19

20 20

21 21 Output: “reject”

22 22 Another Example

23 23

24 24

25 25

26 26 Output: “accept”

27 27 Rejection

28 28

29 29

30 30

31 31 Output: “reject”

32 32 Formal definitions Deterministic Finite Accepter (DFA) : set of states : input alphabet : transition function : initial state : set of final states

33 33 Input Aplhabet

34 34 Set of States

35 35 Initial State

36 36 Set of Final States

37 37 Transition Function

38 38

39 39

40 40

41 41 Transition Function

42 42 Extended Transition Function

43 43

44 44

45 45

46 46 Observation: There is a walk from to with label

47 47 Recursive Definition

48 48      ,,,,,,* ),,(*,* q bq baq baq baq abq          

49 49 Languages Accepted by DFAs Take DFA Definition: The language contains all input strings accepted by = { strings that drive to a final state}

50 50 Example accept Alphabet =

51 51 Another Example accept Alphabet =

52 52 Formally For a DFA Language accepted by : alphabet transition function initial state final states

53 53 Observation Language accepted by Language rejected by

54 54 More Examples accept trap state Alphabet =

55 55 = { all strings with prefix } accept Alphabet =

56 56 = { all strings without substring } Alphabet =

57 57 Regular Languages All regular languages form a language family A language is regular if there is a DFA such that

58 58 Example is regular The language Alphabet =

59 59 Nondeterministic Automata NFA

60 60 Nondeterministic there is an element of choice: in a given state NFA can act on a given string in different ways. Several start/final states are allowed. -transitions are allowed. Finite only a finite number of states and arcs Acceptors produce only a yes/no answer NFA

61 61 Two choices Alphabet = Nondeterministic Finite Accepter (NFA)

62 62 First Choice

63 63 First Choice

64 64 First Choice

65 65 “accept” First Choice

66 66 Second Choice

67 67 Second Choice

68 68 Second Choice No transition: the automaton hangs

69 69 Second Choice “reject”

70 70 Observation An NFA accepts a string if there is a computation of the NFA that accepts the string

71 71 Example is accepted by the NFA:

72 72 Lambda Transitions

73 73

74 74

75 75 (read head doesn’t move)

76 76

77 77 “accept” String is accepted

78 78 Language accepted:

79 79 Another NFA Example Alphabet =

80 80

81 81

82 82

83 83 “accept”

84 84 Another String Alphabet =

85 85

86 86

87 87

88 88

89 89

90 90

91 91 “accept”

92 92 Language accepted Alphabet =

93 93 Another NFA Example Alphabet =

94 94 Language accepted

95 95 Formal Definition of NFA Set of states, i.e. Input alphabet, i.e. Transition function Initial state Final states

96 96 Transition Function

97 97

98 98

99 99

100 100 Extended Transition Function (Utvidgad övergångsfunktion)

101 101

102 102

103 103 Formally if and only if there is a walk from to with label

104 104 The Language of an NFA Alphabet =

105 105

106 106

107 107

108 108

109 109 Formally The language accepted by NFA (final state) where and there is some (at least one) is:

110 110

111 111 NFA  DFA Equivalence

112 112 Equivalence of NFAs and DFAs Accept the same languages? YES! NFAs  DFAs ? Same power?

113 113 We will prove: Languages accepted by NFAs Languages accepted by DFAs NFAs and DFAs have the same computation power!

114 114 Languages accepted by NFAs Languages accepted by DFAs Step 1 Proof Every DFA is also an NFA A language accepted by a DFA is also accepted by an NFA

115 115 Languages accepted by NFAs Languages accepted by DFAs Step 2 Proof Any NFA can be converted to an equivalent DFA A language accepted by an NFA is also accepted by a DFA

116 116 Procedure NFA to DFA 1. Initial state of NFA: Initial state of DFA:

117 117 Example NFA DFA Step 1

118 118 Procedure NFA to DFA 2. For every DFA’s state Compute in the NFA Add transition

119 119 Example NFA DFA Step 2

120 120 Procedure NFA to DFA Repeat Step 2 for all letters in alphabet, until no more transitions can be added.

121 121 Example NFA DFA Step 3

122 122 Procedure NFA to DFA 3. For any DFA state If some is a final state in the NFA Then is a final state in the DFA

123 123 Example NFA DFA Step 4

124 124 Theorem Take NFA Apply procedure to obtain DFA Then and are equivalent :

125 125 Languages accepted by NFAs Languages accepted by DFAs We have proven (proof by construction): Regular Languages END OF PROOF

126 126 Nondeterministic vs. Deterministic Automata

127 127 Formal Definition of NFA Set of states, i.e. Input alphabet, i.e. Transition function Initial state Final (accepting) states NFA is a mathematical model defined as a quintuple:

128 128 Deterministic Finite Automata A deterministic finite automaton (DFA) is a special case of a nondeterministic finite automaton in which 1. no state has an -transition, i.e. a transition on input, and 2. for each state q and input symbol a, there is at most one edge labeled a leaving q.

129 129 STATE INPUT SYMBOL ab {0, 1} - {0} {2} {3} Transition table for the finite automaton above A nondeterministic finite automaton b 0 start 1 a 2 bb 3 a Example

130 130 NFA accepting aa* + bb* 0 start 1 a 2 a 3 b 4 b Example

131 131 NFA accepting (a+b)*abb 0 start 1 a 2 bb b aa a b 3 a Example

132 132 NFA recognizing three different patterns. (a) NFA for a, abb, and a*b +. (b) Combined NFA. Example 4 1 start a 2 3 a 65 b b 7 b 8 b a 4 1 a 2 3 a 65 bb 7 b 8 b a 0

133 133 Ways to think of nondeterminism always make the correct guess “backtracking” (systematically try all possibilities) For a particular string, imagine a tree of possible state transitions: q0q0 q3q3 q0q0 q4q4 q2q2 q1q1 a a a a b a

134 134 Advantages of nondeterminism an NFA can be smaller, easier to construct and easier to understand than a DFA that accepts the same language useful for proving some theorems good introduction to nondeterminism in more powerful computational models, where nondeterminism plays an important role

135 135 Space and time taken to recognize regular expressions: - NFA more compact but take time to backtrack all choices - DFA take place, but save time AUTOMATONSPACETIME NFA DFA O(|r|) O(2 |r| ) O(|r|  |x|) O(|x|) Determinism vs. nondeterminism (Where r is regular expression, and x is input string)

136 136 Equivalent automata Two finite automata M 1 and M 2 are equivalent if L(M 1 ) = L(M 2 ) that is, if they both accept the same language.

137 137 Equivalence of NFAs and DFAs To show that NFAs and DFAs accept the same class of languages, we show two things: –Any language accepted by a DFA can also be accepted by some NFA (As DFA is a special case of NFA) –Any language accepted by a NFA can also be accepted by some (corresponding, specially constructed) DFA

138 138 Proof strategy To show that any language accepted by a NFA is also accepted by some DFA, we describe an algorithm that takes any NFA and converts it into a DFA that accepts the same language. The algorithm is called the “subset construction algorithm”. We can use mathematical induction (on the length of a string accepted by the automaton) to prove the DFA that is constructed accepts the same language as the NFA.

139 139 Converting NFA to DFA Subset Construction

140 140 Subset construction Given a NFA constructs a DFA that accepts the same language The equivalent DFA simulates the NFA by keeping track of the possible states it could be in. Each state of the DFA is a subset of the set of states of the NFA -hence, the name of the algorithm. If the NFA has n states, the DFA can have as many as 2 n states, although it usually has many less.

141 141 Steps of subset construction The initial state of the DFA is the set of all states the NFA can be in without reading any input. For any state {q i,q j,…,q k } of the DFA and any input a, the next state of the DFA is the set of all states of the NFA that can result as next states if the NFA is in any of the states q i,q j,…,q k when it reads a. This includes states that can be reached by reading a, followed by any number of -moves. Use this rule to keep adding new states and transitions until it is no longer possible to do so. The accepting states of the DFA are those states that contain an accepting state of the NFA.

142 142 Example Here is a NFA that we want to convert to an equivalent DFA

143 143 {0,1} The start state of the DFA is the set of states the NFA can be in before reading any input. This includes the start state of the NFA and any states that can be reached by a -transition. NFA DFA

144 144 {0,1} a b {2} For start state {0,1}, make transitions for each possible input, here a and b. Reading b from start {0,1}, we reach state {2}. Means from either {0}, or {1} we reach {2}. NFA DFA

145 145 For state {2}, we create a transition for each possible input, a and b. From {2}, with b we are either back to {2} (loop) or we reach {1}- see the little framed original NFA. So from {2}, with b we end in state {1, 2}. Reading a leads us from {2} to {0} in the original NFA, which means state {0, 1} in the new DFA. {0,1} {1,2} {2} NFA DFA

146 146 For state {1, 2}, we make again transition for each possible input, a and b. From {2} a leads us to {0}. From {1} with a we are back to {1}. So, we reach {0, 1} with a from {1,2}. With b we are back to {1,2}. At this point, a transition is defined for every state-input pair. {0,1} {1,2} {2} DFA NFA

147 147 The last step is to mark the final states of the DFA. As {1} was the accepting state in NFA, all states containing {1} in DFA will be accepting states: ({0, 1} and {1, 2}). {0,1} {1,2} {2} DFA NFA

148 148 Subset Construction Algorithm

149 149 Subset Construction States of nondeterministic M´ will correspond to sets of states of deterministic M Where q 0 is start state of M, use {q 0 } as start state of M´. Accepting states of M´ will be those state-sets containing at least one accepting state of M.

150 150 Subset Construction (cont.) For each state-set S and for each s in alphabet of M, we draw an arc labeled s from state S to that state-set consisting of all and only the s- successors of members of S. Eliminate any state-set, as well as all arcs incident upon it, such that there is no path leading to it from {q 0 }.

151 151 The power set of a finite set, Q, consists of 2 |Q| elements The DFA corresponding to a given NFA with Q states have a finite number of states, 2 |Q|. If |Q| = 0 then Q is the empty set, | P(Q)| = 1 = 2 0. If |Q| = N and N  1, we construct subset of a given set so that for each element of the initial set there are two alternatives, either is the element member of a subset or not. So we have 2 · 2 · 2 · 2 · 2 · 2 · 2…. ·2 = 2 N N times

152 152 From an NFA to a DFA Subset Construction Operation Description - closure(s) - closure(T) Move(T,a) Set of NFA states reachable from an NFA state s on -transitions along Set of NFA states reachable from some NFA state s in T on - transitions along Set of NFA states reachable from some NFA state set with a transition on input symbol a

153 153 From an NFA to a DFA Subset Construction Initially, -closure (s 0 ) is the only states in D and it is unmarked while there is an unmarked state T in D do mark T; for each input symbol a do U:= e-closure(move(T,a)); if U is not in D then add U as an unmarked state to D Dtran[T,a]:=U; end(for) end(while)

154 154 Ett exempel på en dugga 1. Antag att M är följande NFA - Ge övergångsfunktionen för M - Använd algoritmen för delmängdskonstruktion för att skapa motsvarande DFA - Ge ett reguljärt uttryck för L (M) - Minimera automaten

155 Givet är följande NFA M - Skapa en reguljär grammatik för M som genererar L(M) - Ge ett reguljärt uttryck för L (M)

156 Reguljärt eller inte? -Mängden av alla strängar på formen xwx R där x och w är strängar över alfabetet  = {0,1}. -Språket L = {a n : n inte är ett primtal}. DUGGA 1: 21 april (F6) [13:15-15:00 Lambda]


Download ppt "1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005."

Similar presentations


Ads by Google