Computation Theory Dept. of Computing ITT Dublin October 14, 2010 Computation Theory
I 1 We would like a model that captures the general nature of computation Consider two simple problems: 2 Design a program that, for any given word, outputs 1 if the word is of length 4n, n 0, 0 otherwise. 3 Design a program that sorts and outputs the result of any input sequence of numbers 4 You can easily design several different programs to solve these problems. Consider their complexity in terms of memory requirements: Computation Theory
II 5 Program 1 should only need to store the numbers 0, 1, 2, 3 in its memory, regardless of the input length 6 Program 2 will need to memorise the entire input list 7 We will examine the power and limitations of programs like the first one, which use a constant amount of memory, regardless of the input length. And since memory usage is constant, this can be moved to the CPU. 8 To do this we use a model called a Finite State Machine Consider the characteristics of a simplified version of a digital computer: 9 Information is stored in binary form Computation Theory
III 10 At any instant, the internal storage is set to certain patterns of bits... The state of the computer 11 Since the computer contains a finite amount of storage, there is a finite number of possible states 12 Actions are synchronised by an internal clock. On a pulse, input can be read, which can change some of the storage locations, thus changing the state of the machine to a new state 13 What that new state is will depend on what the input was, as well as the previous state 14 If these two factors are known, the change is predictable and non-random Computation Theory
IV 15 Because the contents of storage cells are available as output, the state of the machine determines its output 16 In this way, the machine produces a sequence of outputs in response to a sequence of inputs Computation Theory
FSM Definition The Finite State Machine is a model that captures the characteristics of the computer Definition (Finite State Machine) M = [S, I, O, f s, f o ] is a finite state machine if: 1 S is a finite set of states, 2 I is a finite set of input symbols (the input alphabet), 3 O is a finite set of output symbols (the output alphabet), 4 and f s and f o are functions where f s : S I S and f o : S O. 5 The machine is always initialised to begin in a fixed starting state s o. Computation Theory
FSM Example I We need to define the three sets and two functions involved: S = s 0, s 1, s 2 I = 0, 1 O = 0, 1 Because f s and f o act on finite domains, they can be defined by a state table (next slide) What would the effect be of an input sequence 01101? The FSM can also be defined by a state graph: Each state of the FSM and its output are shown as the label of a node Computation Theory
FSM Example II The next state function is given by directed arcs of the graph, each arc showing the input symbol required to produce the particular state change Present State Next State Output on Input 0,1 s 0 s 1, s 0 0 s 1 s 2, s 1 1 s 2 s 2, s 0 1 Computation Theory
Exercise 1 A FSM M1 is given by the following state graph (Fig. 1). Give the state table for M1 Figure: Finite State Machine, graph form Computation Theory
Exercise 2 A FSM M2 is given by the following state table. 1 Give the state graph for M2 2 What output corresponds to an input sequence of 2110? Present State Next State Output on Input 0,1,2 s 0 s 0, s 1, s 1 0 s 1 s 1, s 0, s 0 1 Computation Theory
A machine can be built which recognises when its input matches a certain description. What function does the following FSM perform? Figure: Computation Theory
1 To design a machine having an output of 1 exactly when the input string received to that point ends in 101. 2 Case 1: to handle an input sequence consisting of just 101... Figure: 3 Case 2: to extend the machine to handle any input sequence ending in 101 The machine states represent the ability to remember past inputs. Computation Theory
To avoid writing down outputs, we designate states of a FSM with an output of 1 as final states. They are denoted with a double circle Definition (FSM Recognition) A finite state machine M with input alphabet I recognises a subset S of I if M, beginning in state s 0 and processing an input string α, ends in a final state if and only if α S I is the set of all strings over I, the input alphabet. Computation Theory
Set Recogonition What are the Sets Recognised by the following FSMs? Figure: Computation Theory
I We need a compact way to describe the sets we have just seen We describe these using Regular Expressions, each describing a particular set Regular expressions over the input alphabet I are: the symbol (a symbol representing the empty set) and the symbol λ (a symbol which represents the set {λ} containing the empty string) the symbol i for any i I the expressions (AB), A B, and (A) if A and B are regular expressions Computation Theory
II Any set represented by a regular expression is a regular set: represents the empty set λ represents the set containing only the empty string i represents the set {i} containing any one symbol from the input alphabet if A and B are regular expressions, the following are regular sets: (A) represents the set of all concatenations of members of A AB the set of all elements of the form αβ where α is any member of A, and β is any member of B A B, the set of all elements of A and B. Computation Theory
I Informally: An element in AB is an item from A followed by an item from B An element in A B is a single item from either AorB An element in (A) is zero or more repetitions of elements from A λ is a member of the set represented by A, for any A Some examples: 0 10: Any number (including none) of zeros, or the string 10 Computation Theory
II 1 0(01) : Any number (including none) of ones, followed by a single zero, followed by any number (including none) of 01 pairs 0 1 : A single zero, or any number (including none) of ones (0 1) : Any string of zeros or ones (including l) 11(10) 11) (00 ): A nonempty string of pairs of ones interspersed with any number (including none) of 10 pairs, followed by at least one zero. Computation Theory
Equivalent Regular Expressions A regular set may be recognised by more than one regular expression: The set of all strings of 0 and 1 can be described by (0 1), but also by the regular expression ((0 1 ) (01) ) It is difficult to decide in general whether two regular expressions represent the same set. Computation Theory
Definition () Any set recognised by a FSM is regular, and ny regular set can be recognised by some FSM outlines the limitations and capabilities of FSMs: Regular sets are exactly the sets FSMs are capable of recognising It follows that if a set is not regular, there is no FSM which can recognise it Ex: The set S = {0 n 1 n n 0}, where 0 n is the string containing n 0 s. So this is the set of strings which have a specific number of 0 s followed by the same number of 1 s. Computation Theory
I Give a regular expression for each of the following: The set of all strings of 0s and 1s beginning with 0 and ending with 1 The set of all strings of 0 s and 1 s having an odd number of 0 s The set of all strings of a s and b s where each a is followed by two b s Does the given string belong to the given regular set? 01110111 (1 01) (11 0 ) 011100101 01 10 (11 0) Computation Theory
II A paragraph of English text is to be scanned and the number of words beginning with the word con counted. Design a FSM that will output a 1 each time such a word is encountered. The input alphabet I is equal to the keys on a keyboard, and O is {0, 1}. To represent blank, use the character β, and to simplify your FSM, you can use the input symbol I {m} to represent any character not equal to m. Construct a FSM to describe the behaviour of a VCR: Whenever a tape is inserted into the VCR, the machine automatically turns on and plays the tape. At the end of the tape, the machine turns off. To program the VCR, you must manually turn it on and select the menu function; when you are finished you turn the machine off, but its timer is set. At Computation Theory
III the appropriate time, the machine records, then at the appropriate time, the machine turns itself off. Computation Theory