Informatics 1 - Computation & Logic: Tutorial 5

Similar documents
Informatics 1 - Computation & Logic: Tutorial 3

Turtle Graphics and L-systems Informatics 1 Functional Programming: Tutorial 7

Informatics 1 - Computation & Logic: Tutorial 4

The Caesar Cipher Informatics 1 Functional Programming: Tutorial 3

Homework 1 Due Tuesday, January 30, 2018 at 8pm

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

CPSC 121: Models of Computation. Module 8: Sequential Circuits

The input is a sequence of throws showing the number of pins knocked down on that throw.

Finite State Machines

Formal Definition of Computation. Formal Definition of Computation p.1/28

Automating Construction of Lexers

CSC 258 lab notes, Fall 2003

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

Finite State Machines

Lecture 2 Finite Automata

CS52 - Assignment 10

Computer Science 1 Ah

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

CS 181 EXAM #1 NAME. You have 90 minutes to complete this exam. You may state without proof any fact taught in class or assigned as homework.

Finite-State Machine (FSM) Design

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

Automata & languages. A primer on the Theory of Computation. The imitation game (2014) Benedict Cumberbatch Alan Turing ( ) Laurent Vanbever

Exercise 2: Automata Theory

Microsoft Excel 2010 Handout

Finite State Machines

Sequencing and control

Announcements! P1 part 1 due next Tuesday P1 part 2 due next Friday

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

Working with Data and Charts

ALGORITHMS EXAMINATION Department of Computer Science New York University December 17, 2007

Microsoft Office Excel Use Excel s functions. Tutorial 2 Working With Formulas and Functions

Final Exam 1, CS154. April 21, 2010

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Midterm I (Solutions) CS164, Spring 2002

Scottish Improvement Skills

(a) What is the denary equivalent of the hexadecimal number A7?

Solutions to Homework 10

Logic Informatics 1 Functional Programming: Tutorial 6

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

Theory Bridge Exam Example Questions Version of June 6, 2008

Part B Finite-state Machines

CS1800 Discrete Structures Final Version B

Objectives/Outcomes. Introduction: If we have a set "collection" of fruits : Banana, Apple and Grapes.

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

CS164: Midterm I. Fall 2003

Cambridge International Examinations Cambridge International Advanced Subsidiary Level and Advanced Level. Paper 1 May/June hour 30 minutes

COS 226 Fall 2015 Midterm Exam pts.; 60 minutes; 8 Qs; 15 pgs :00 p.m. Name:

Computer Science 236 Fall Nov. 11, 2010

Lecture Notes on Program Equivalence

CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Introduction to Lexing and Parsing

CS 3100 Models of Computation Fall 2011 Notes for L5. September 15, 2011

Design a three-input, two-output sequential digital circuit which functions as a digital locking mechanism. LOCK ALARM

a, b sum module add32 sum vector bus sum[31:0] sum[0] sum[31]. sum[7:0] sum sum overflow module add32_carry assign

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Week 5 Creating a Calendar. About Tables. Making a Calendar From a Table Template. Week 5 Word 2010

Models of Computation II: Grammars and Pushdown Automata

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Part II: Creating Visio Drawings

EE 231 Fall EE 231 Homework 8 Due October 20, 2010

Name: CS 341 Practice Final Exam. 1 a 20 b 20 c 20 d 20 e 20 f 20 g Total 207

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSE 401 Midterm Exam 11/5/10

Midterm Exam 2B Answer key

COMP 330 Autumn 2018 McGill University

Implementation of Lexical Analysis

Finite State Machines: Finite State Transducers; Specifying Control Logic

Optimizing Finite Automata

CS 61C Summer 2012 Discussion 11 State, Timing, and CPU (Solutions)

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Laboratory Exercise 7

somedata = { } somedata[ cheese ] = dairy somedata[ Cheese ] = dairy items = ( ( 3, 2 ), ( 5, 7 ), ( 1, 9 ), 0, ( 1 ) )

Lesson 9: Decimal Expansions of Fractions, Part 1

ECE 2300 Digital Logic & Computer Organization. More Sequential Logic Verilog

EXCEL 2003 DISCLAIMER:

ENTERING DATA & FORMULAS...

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

Introduction to EMACS. Harriet Borton Academic and Research Computing

ENCM 369 Winter 2019 Lab 6 for the Week of February 25

Cantor s Diagonal Argument for Different Levels of Infinity

CPSC 121 Sample Final Examination December 2013

Programming Paradigms: Overview to State Oriented

MS Office 2016 Excel Pivot Tables - notes

(Refer Slide Time: 1:43)

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

Black-box Testing Techniques

Final Examination: Topics and Sample Problems

To illustrate what is intended the following are three write ups by students. Diagonalization

Vensim PLE Quick Reference and Tutorial

Introduction to Computers & Programming

Structure and Interpretation of Computer Programs

Section 1.2 Fractions

CS314: FORMAL LANGUAGES AND AUTOMATA THEORY L. NADA ALZABEN. Lecture 1: Introduction

Transcription:

Informatics - Computation & Logic: Tutorial 5 Computation: Introduction to Finite State Machines Week 7: 3 October - 4 November 206 Please attempt the entire worksheet in advance of the tutorial, and bring with you all work, including (if a computer is involved) printouts of code and test results. Tutorials cannot function properly unless you do the work in advance. You may work with others, but you must understand the work; you can t phone a friend during the exam. Assessment is formative, meaning that marks from coursework do not contribute to the final mark. But coursework is not optional. If you do not do the coursework you are unlikely to pass the exams. Attendance at tutorials is obligatory; please let your tutor know if you cannot attend.

A finite state machine (FSM), also called a finite state automaton (FSA) is an abstract model of computation that can be used to design sequential logic circuits and computer programs. For example, FSMs can represent the behaviour of devices such as vending machines, elevators or traffic lights. They may take as input, for example characters from an alphabet, numbers or more abstract symbols, e.g. coins or button presses. Finite state machines are often represented as a graph. Circles represent the states in which the machine can be. There is a finite number of states and the machine can only be in one state at a time (this is called the current state). A change from one state to another is called a transition. Possible transitions are represented by arrows on the graph. If a transition is triggered by particular input to the system, it is represented as an annotation next to the arrow. The state in which the machine starts operation is called the initial state and in this tutorial it is represented by pointing to it with an arrowhead. One or more of the states can be marked as an accepting state. This is a state which should be reached after processing an input sequence that the machine is designed to recognize as valid, for example a state in which an appropriate amount of money was inserted into a vending machine, which should then dispense a drink. If an input sequence causes the machine to end in an accepting state, we say that input sequence is accepted, else it is rejected. Initial Accepting For more information you can refer to the Wikipedia article on FSMs, on which this introduction was based. You may find it useful to refer to the FSM Workbench question set which accompanies this tutorial at homepages.inf.ed.ac.uk/s020995/tutorial5. The link will take you to FSM simulations corresponding to each question. You can also use this site to build FSMs on your own and experiment with various inputs. A brief reference card is given at the end of this document. 2

. Consider this finite state machine. (a) The machine accepts the sequence. Give two other sequences that it will accept. (empty string),, (b) What do the sequences that this machine accepts have in common? All accepted strings consist of n, where n mod 3 = 0. (c) When will this machine be in state c? The machine will be in state c when n mod 3 = 2. 2. Like decimal numbers, binary numbers can be sorted into odd and even by looking at only the least significant digit. For example 2 = 00 is even, 9 = 00 is odd. (a) Design a finite state machine over the alphabet {0,} which accepts only those sequences that form an odd binary number. 0 0 (b) For each input sequence in the table below, record whether the sequence is accepted or rejected by your machine. 3

Input Is Accepted? 0 N Y 000 Y Y 0000 N 00 Y N (c) What changes would you make to your machine to make recognise even numbers? Make the start state the only accepting state. (d) What changes would you make to your machine to recognise the same sets for binary numbers presented in reverse order, with the least significant bit coming first? Odd 4

Even 3. This finite state machine could be used as part of a vending machine. It accepts any sequence of coins that add up to 40p or more. 0p 40p (a) How does this machine deal with input of more than 40p? After 40p has been reached, more money can be input but it will not alter the recorded total (b) Modify the machine to also allow 0p coins. 5

0p, 0p 40p 0p 0p 0p 0p, 0p 30p 4. The FSM below models the control logic of a chip & PIN card payment terminal. It represents a single transaction, ending in an accepting state if the transaction is successful. Wrong PIN S S2 S3 S4 Insert Card Correct PIN Take Card Take Card S5 (a) Note that the transaction will fail if the card is removed too early. Modify the machine so that the transaction will also fail if the wrong PIN is entered three times. S6 S7 S8 Wrong PIN Wrong PIN Correct PIN Wrong PINCorrect PIN S S2 S3 S4 Insert Card Correct PIN Take Card Take Card S5 6

(b) This machine only verifies the PIN. Modify it to represent the process of checking with the bank for approval. The machine should only accept if the transaction is approved. The modified machine should take inputs Transaction Approved and Transaction Rejected. S6 S7 S8 Wrong PIN Wrong PIN Correct PIN Wrong PINCorrect PIN S S2 S3 S9 S4 Insert Card Correct PIN Transaction Approved Take Card Transaction Rejected Take Card S5 S0 It could be argued that there should be Take Card transitions to S5 from every state other than S, S5, and S9. As shown, the behaviour of the machine is undefined if the card is removed early. 7

5. Consider a hot drinks machine. The machine takes and 50p coins. It sells tea for 50p and coffee for 70p. (a) Design a FSM that could be used to control this machine. After a successful sale the FSM should be in an accepting state. The machine only needs to model a single transaction. 0p 50p 50p C Coffee Tea 50p 70p Tea T 50p,50p Tea 40p 60p The label on the transition from the 50p state to the 70p state could also allow a 50p input. (b) Consider your answer to part (a). How does your machine handle over-payment? Would it be possible to design a FSM that gives correct change? The machine ignores payment over 70p. No finite state machine can give correct change for all possible inputs, as this requires the machine to count arbitrarily high. However, by adding more states it would be possible to give change up to some fixed limit. 6. The designer of the this machine was attempting to create a system to accept strings with matching opening and closing brackets. 8

) ) S S2 S3 ( ( { } } S4 S5 { (a) Give a sequence of matching brackets that the machine does not accept. ((())) or {{{}}} or {()}. (b) What does each state of the machine represent? That is, for each state what do the sequences that end in that state have in common? These are hard to describe with regular expressions, as parentheses are special characters. In the next two bulleted paragraphs, we use ( ) { } as literals and use [ ] to group regular expressions S We introduce two patterns of matched parentheses to describe the two branches of the FSM. Let P = [([( )] )] be a pattern for strings containing only ( ), and let Q be the corresponding pattern for strings using { }, that is, Q = [{[{ }] }] S accepts the language L = (P Q). S2 accepts L followed by and extra (, plus any number of ( ) pairs. S3 accepts any string accepted by S2, plus an extra (. S4 is similar to S2. It accepts a string in L plus an extra {, plus any number of { } pairs. S5 accepts any string accepted by S4, plus an extra {. (c) Is it possible to design a finite state machine that will accept all possible sequences of matching brackets? Justify your answer. No this requires counting the number of open brackets. To do this for all possible inputs would require an infinite number of states. 9

(d) For each state in the machine, there are input symbols which do not correspond to any transition. What do you think the machine should do if it received input that did not correspond to a transition? There are (at least) two conventions for missing inputs. When we are modelling systems such as a coffee machine, we often use the convention that a missing transition signifies that the corresponding action that cannot happen. When we consider NFA the definition of a trace means that we interpret missing transitions differently. If we construct the corresponding DFA using the subset construction, then the missing transition is replaced by a transition to the empty set, which corresponds to a black hole state, from which there is no escape. The ambiguity of how to handle unexpected input arises as it is often undesirable to draw transitions for all possible inputs to each state, as this can make diagrams difficult to read. For machines modelling control systems, such as the vending machine in question 5, the convention is for machines to remain in their current state when they receive unexpected input. For string processing applications, such as bracket matching, the convention is to reject sequences that contain unexpected input by moving to a black hole state. 0

7. This is a variation on Question 2. (a) Design a finite state machine over the alphabet 0, which accepts only those sequences that form binary number divisible by 3? For example your machine should accept 0, 000, 0,, 0, 00, and 00, which represent 0, 0, 3, 3, 6, 9 and 2. Hint: design a machine with three states s0, s, s2, where the machine is in state s2 whenever n mod 3 = 2, i.e.where n, the number represented by the binary sequence seen, is 2 more than some multiple of 3. If a binary string s represents the number n, then s represents 2n +, while s0 represents 2n. The three states keep track of n mod 3. Answer: (b) What changes would you make to your machine to make recognise numbers not divisible by 3? Answer: (c) What changes would you make to your machine to recognise the numbers divisible by 3 for binary numbers presented in reverse order, i.e. with the least significant bit coming first? The surprising answer is that we don t need to change anything. The machine for recognising 0 mod 3 can run backwards as well as forwards if we reverse the direction of every arrow we get the same machine. This means that if the machine accepts a given binary string then it also accepts that string in reverse order. So, we have discovered that a binary string represents a number divisible by 3 iff its reverse represents a number divisible by 3! We can see this algebraically. If n is even, then 2x n = mod 3, while if n is odd then 2x n = mod 3. The value v is the sum Thanks to Andrew Ranicki for this argument.

of these contributions mod 3. Reversing a string of odd length takes odd bits to oddd bits, and even to even. Reversing a string of even length swaps odd and even bits, so changes the sign of each contribution. In either case, if the sum is 0 mod 3 it is unchanged by the reversal. Understanding the operation of the machine on the reversed string is more complex. Adding a zero at the start of a binary number doesn t change its value, v. However, the effect of adding a to a string of length n is to move from v to v + 2 n. So if we add two successive s then v mod 3 doesn t change. Using these observations, you can check that the machine is: in S0 when v = 0 mod 3 in S when either n is even and v = mod 3, or n is odd and v = mod 3; in S2 when either n is even and v = mod 3, or n is odd and v = mod 3; Using the FSM Workbench The workbench provides tools for creating, editing, and simulating finite state machines. The diagram shows the function of each tool. You can toggle each tool on/off by clicking it. When no tools are active you can drag the states of your FSM to rearrange the layout. Provide input to the machine Start new input sequence 0 Reset Save as SVG Add transition Toggle initial Delete states/transitions Add State 0 0 0 0 Edit names & transitions Toggle accepting S S2 0 0 0 0 S3 S4 This tutorial exercise sheet was written by Matthew Hepburn and Dagmara Niklasiewicz, with additions from Michael Fourman. Send comments to Michael.Fourman@ed.ac.uk 2