Docsity

Finite State Automata: Understanding FSA Components and Construction - Prof. Eric Torng, Study notes of Computer Science

Toggle sidebar
bg1
1
1
Module 12
• Computation and Configurations
– Formal Definition
– Examples
2
Definitions
• Configuration
– Functional Definition
•Given the original program and the current
configuration of a computation, someone should
be able to complete the computation
– Contents of a configuration for a C++ program
• current instruction to be executed
• current value of all variables
• Computation
– Complete sequence of configurations
3
Computation 1
1 int main(int x,y) {
2 int r = x % y;
3 if (r== 0) goto8;
4 x = y;
5 y = r;
6 r = x % y;
7 goto 3;
8 return y; }
Input: 10 3
• Line 1, x=?,y=?,r=?
• Line 2, x=10, y=3,r=?
• Line 3, x=10, y=3, r=1
• Line 4, x=10, y=3, r=1
• Line 5, x= 3, y=3, r=1
• Line 6, x=3, y=1, r=1
• Line 7, x=3, y=1, r=0
• Line 3, x=3, y=1, r=0
• Line 8, x=3, y=1, r=0
• Output is 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52

Partial preview of the text

Download Finite State Automata: Understanding FSA Components and Construction - Prof. Eric Torng and more Study notes Computer Science in PDF only on Docsity!

1

Module 12

• Computation and Configurations

– Formal Definition

– Examples

2

Definitions

• Configuration

  • Functional Definition
    • Given the original program and the current configuration of a computation, someone should be able to complete the computation
  • Contents of a configuration for a C++ program
    • current instruction to be executed
    • current value of all variables

• Computation

  • Complete sequence of configurations

3

Computation 1

1 int main(int x,y) { 2 int r = x % y; 3 if (r== 0) goto 8; 4 x = y; 5 y = r; 6 r = x % y; 7 goto 3; 8 return y; } Input: 10 3

  • Line 1, x=?,y=?,r=?
  • Line 2, x=10, y=3,r=?
  • Line 3, x=10, y=3, r=
  • Line 4, x=10, y=3, r=
  • Line 5, x= 3, y=3, r=
  • Line 6, x=3, y=1, r=
  • Line 7, x=3, y=1, r=
  • Line 3, x=3, y=1, r=
  • Line 8, x=3, y=1, r=
  • Output is 1

4

Computation 2

int main(int x,y) { 2 int r = x % y; 3 if (r== 0) goto 8; 4 x = y; 5 y = r; 6 r = x % y; 7 goto 3;

8 return y; }

Input: 53 10

  • Line 1, x=?,y=?,r=?
  • Line 2, x=53, y=10, r=?
  • Line 3, x= 53, y=10, r=
  • Line 4, x=53, y=10, r=
  • Line 5, x=10, y=10, r=
  • Line 6, x=10, y=3, r=
  • Line 7, x=10, y=3, r=
  • Line 3, x=10, y=3, r=
  • ...

5

Computations 1 and 2

  • Line 1, x=?,y=?,r=?
  • Line 2, x=53, y=10, r=?
  • Line 3, x= 53, y=10, r=
  • Line 4, x=53, y=10, r=
  • Line 5, x=10, y=10, r=
  • Line 6, x=10, y=3, r=
  • Line 7, x=10, y=3, r=
  • Line 3, x=10, y=3, r=
  • ...
  • Line 1, x=?,y=?,r=?
  • Line 2, x=10, y=3,r=?
  • Line 3, x=10, y=3, r=
  • Line 4, x=10, y=3, r=
  • Line 5, x= 3, y=3, r=
  • Line 6, x=3, y=1, r=
  • Line 7, x=3, y=1, r=
  • Line 3, x=3, y=1, r=
  • Line 8, x=3, y=1, r=
  • Output is 1

6

Observation

int main(int x,y) { 2 int r = x % y; 3 if (r== 0) goto 8; 4 x = y; 5 y = r; 6 r = x % y; 7 goto 3; 8 return y; }

  • Line 3, x= 10, y=3, r=
    • Program and current configuration - Together, these two pieces of information are enough to complete the computation - Are they enough to determine what the original input was? - No! - Both previous inputs, 10 3 as well as 53 10 eventually reached the same configuration (Line 3, x=10, y=3, r=1)

10

Complexity Theory

• In complexity theory, we differentiate problems

by how hard a problem is to solve

  • Remember, all problems in REC are solvable

• Which problem is harder and why?

  • Max:
    • Input: list of n numbers
    • Task: return largest of the n numbers
  • Element
    • Input: list of n numbers
    • Task: return any of the n numbers

REC RE -

REC

All languages

  • RE

11

Resource Usage *

• How do we formally measure the hardness of a

problem?

• We measure the resources required to solve input

instances of the problem

• Typical resources are?

• We need a notion of size of an input instance

  • Obviously larger input instances require more resources to solve

12

Poly Language

Class *

Informal Definition: A problem L 1 is easier than problem L 2 if problem L 1 can be solved in less time than problem L 2.

Poly: the set of problems which can be solved in polynomial time (typically referred to as P, not Poly)

Major goal: Identify whether or not a problem belongs to Poly

Poly (^) Rest of REC

REC RE -

REC

All languages

  • RE

13

Working with Poly

Poly Rest of REC

• How do you prove a problem L is in Poly?

• How do you prove a problem L is not in

Poly?

– We are not very good at this.

– For a large class of interesting problems, we

have techniques (polynomial-time answer-

preserving input transformations) that show a

problem L probably is not in Poly, but few

which prove it.

14

Examples

• Shortest Path Problem

• Input

  • Graph G
  • nodes s and t

• Task

  • Find length of shortest path from s to t in G

• Longest Path Problem

• Input

  • Graph G
  • nodes s and t

• Task

  • Find length of longest path from s to t in G

Poly Rest of REC

Which problem is provably solvable in polynomial time?

15

Automata Theory

• In automata theory, we will define new models of

computation which we call automata or grammars

  • Finite State Automata (FSA)
  • Context Free Grammars (CFG)

• Key concept

  • FSA’s and CFG’s are restricted models of computation
    • FSA’s and CFG’s cannot solve all the problems that C++ programs can
  • We then identify which problems can be solved using FSA’s and CFG’s

REC RE -

REC

All languages

  • RE

19

String Searching

• Input

  • String x
  • String y

• Tasks

  • Return location of y in string x
  • Does string y occur in string x?

• Can you identify

applications of this

type of problem in real

life?

• Try and develop an

efficient solution to

this problem.

20

String Searching II

• Input

  • String x
  • pattern y

• Tasks

  • Return location of y in string x
  • Does pattern y occur in string x?

• Pattern

  • [anything].html
  • $EN4$$

21

String Searching

• We will show an easy way to solve these

string searching problems

• In particular, we will show that we can

solve these problems in the following

manner

– Write down the pattern

– The computer automatically turns this into a

program which performs the actual string

search

22

Module 14

• Regular languages

– Inductive definitions

– Regular expressions

  • syntax
  • semantics

23

Regular Languages

(Regular Expressions)

24

Regular Languages

• New language class

  • Elements are languages

• We will show that this language class is identical

to LFSA

  • Language class to be defined by Finite State Automata (FSA)
  • Once we have shown this, we will use the term “regular languages” to refer to this language class

28

Regular Expressions

• How do we describe a regular language?

– Use set notation

  • {aa, bb, ab, ba}*
  • {a}{a,b}*{b}

– Use regular expressions R

  • Inductive def of regular languages and regular expressions on page 72
  • (aa+bb+ab+ba)*
  • a(a+b)*b

29

R and L(R) *

• How we interpret a regular expression

– What does a regular expression R mean to us?

  • aaba represents the regular language {aaba}
  • φ represents the regular language {}
  • aa+bb represents the regular language {aa, bb}

– We use L(R) to denote the regular language

represented by regular expression R.

30

Precedence rules

• What is L(ab+c*)?

– Possible answers:

  • {a}({b} union {c}*}
  • ({a}{b,c})*
  • ({ab} union {c})*
  • {ab} union {c}*

– Must know precedence rules

    • first, then concatenation, then +

31

Precedence rules continued

• Precedence rules similar to those for

arithmetic expressions

– ab+c 2

  • (a times b) + (c times c)
  • exponentiation first, then multiplication, then addition
  • Think of Kleene closure as exponentiation, concatenation as multiplication, and union as addition and the precedence rules are identical

32

Regular expressions are strings *

• Let L be a regular language over the alphabet Σ

  • A regular expression R for L is just a string over the alphabet Σ union {(, ), +, *, φ, λ}.
  • The set of legal regular expressions is itself a language over the alphabet Σ union {(, ), +, } - φ, aaba are strings in the language of legal reg. exp. - )(, a are strings NOT in the language of legal reg. exp.

33

Semantics *

• We give a regular expression R meaning when we

interpret it to represent L(R).

  • aaba is just a string
  • we interpret it to represent the language {aaba}.

• We do similar things with arithmetic expressions

  • 10+7 2 is just a string
  • We interpret this string to represent the number 59

37

Finite State Automata

New Computational Model

38

Tape

• We assume that you have already seen

FSA’s in CSE 260

– If not, review material in reference textbook

• Only data structure is a tape

– Input appears on tape followed by a B character

marking the end of the input

– Tape is scanned by a tape head that starts at

leftmost cell and always scans to the right

39

Data type/States

• The only data type for an FSA is char

• The instructions in an FSA are referred to as

states

• Each instruction can be thought of as a

switch statement with several cases based

on the char being scanned by the tape head

40

Example program

1 switch(current tape cell) {

case a: goto 2 case b: goto 2 case B: return yes

2 switch (current tape cell) {

case a: goto 1 case b: goto 1 case B: return no;

41

New model of computation

  • FSA M=(Q,Σ,q 0 , A ,δ)
    • Q = set of states = {1,2}
    • Σ = character set = {a,b}
      • don’t need B as we see below
    • q 0 = initial state = 1
    • A = set of accepting (final) states = {1} - A is the set of states where we return yes on B - Q-A is set of states that return no on B
    • δ = state transition function

1 switch(current tape cell) { case a: goto 2 case b: goto 2 case B: return yes } 2 switch (current tape cell) { case a: goto 1 case b: goto 1 case B: return no; }

42

Textual representations of δ *

1 switch(current tape cell) { case a: goto 2 case b: goto 2 case B: return yes } 2 switch (current tape cell) { case a: goto 1 case b: goto 1 case B: return no; }

a b

δ(1,a) = 2, δ(1,b)=2,

δ(2,a)=1, δ(2,b) = 1

{(1,a,2), (1,b,2), (2,a,1), (2,b,1)}

46

Computing with FSA’s

47

Computation Example *

a

a

a

b

b

b

Input: aabbaa

48

Computation of FSA’s in detail

• A computation of an FSA M on an input x

is a complete sequence of configurations

• We need to define

– Initial configuration of the computation

– How to determine the next configuration given

the current configuration

– Halting or final configurations of the

computation

49

  • Given an FSA M and an input string x, what is the initial configuration of the computation of M on x? - (q 0 ,x) - Examples - x = aabbaa - (1, aabbaa) - x = abab - (1, abab) - x = λ - (1, λ)

Initial Configuration

a

a

a

b

b

b

FSA M

50

  • (1, aabbaa) ├M (1, abbaa)
    • config 1 “yields” config 2 in one step using FSA M
  • (1,aabbaa) ├ (^) M (2, baa)
    • config 1 “yields” config 2 in 3 steps using FSA M
  • (1, aabbaa) ├ (^) M (2, baa)
    • config 1 “yields” config 2 in 0 or more steps using FSA M
  • Comment:
    • ├ (^) M determined by transition function δ
    • There must always be one and only one next configuration - If not, M is not an FSA

Definition of ├M

a

a

a

b

b

b

3

FSA M

51

  • Halting configuration
    • (q, λ)
    • Examples
      • (1, λ)
      • (3, λ)
  • Accepting Configuration
    • State in halting configuration is in A
  • Rejecting Configuration
    • State in halting configuration is not in A

Halting Configurations *

a

a

a

b

b

b

FSA M

55

  • L(M) or Y(M)
    • The set of strings M accepts
      • Basically the same as Y(P) from previous unit
    • We say that M accepts/decides/recognizes/solves L(M)
      • Remember an FSA will not loop or crash
    • What is L(M) (or Y(M)) for the FSA M above?
  • N(M)
    • Rarely used, but it is the set of strings M rejects
  • LFSA
    • L is in LFSA iff there exists an FSA M such that L(M) = L.

L(M) and

LFSA *

b b b

a

a

a

FSA M

56

LFSA Unit Overview

• Study limits of LFSA

– Understand what languages are in LFSA

  • Develop techniques for showing L is in LFSA

– Understand what languages are not in LFSA

  • Develop techniques for showing L is not in LFSA

• Prove Closure Properties of LFSA

• Identify relationship of LFSA to other

language classes

57

Comparing language classes

Showing LFSA is a subset of REC,

the set of solvable languages

58

LFSA subset REC

• Proof

– Let L be an arbitrary language in LFSA

– Let M be an FSA such that L(M) = L

  • M exists by definition of L in LFSA

– Construct C++ program P from FSA M

– Argue P solves L

– L is solvable

59

Visualization

LFSA

REC

FSA’s (^) C++ Programs

L L

M P

•Let L be an arbitrary language in LFSA •Let M be an FSA such that L(M) = L •M exists by definition of L in LFSA •Construct C++ program P from FSA M •Argue P solves L •There exists a program P which solves L •L is solvable

60

Construction

• The construction is an algorithm which

solves a problem with a program as input

– Input to A: FSA M

– Output of A: C++ program P such that P solves

L(M)

– How do we do this?

Construction Algorithm

FSA

M

Program P