30
Finite State Machines Concepts DFA NFA

Finite State Machines Concepts DFA NFA

Embed Size (px)

DESCRIPTION

Finite State Machines Concepts DFA NFA. Graphs and More. Finites state machines are directed graphs. Nodes-states, Edges-input. Each city represents a state you can be in. Take and edge on some desire(input) Imagine you are doing a food tour of the country, starting in Atlanta - PowerPoint PPT Presentation

Citation preview

Finite State Machines

ConceptsDFANFA

Graphs and More

• Finites state machines are directed graphs.

Nodes-states, Edges-input

• Each city represents a state you can be in.

• Take and edge on some desire(input)

• Imagine you are doing a food tour of the country, starting in Atlanta

• You want to eat…Perogies, Philly Cheese Steak, Cuban

Edges as input

• The food transitions are along the pathway.

• Certain food tours are allowed, others just don’t work out!

• Valid: Perogies, Philly Cheese Steak, Miami Cuban

• Invalid: Perogies, Jambalaya, Cuban

• Same food can be found in several places, and assume there is a finite set of foods – the alphabet

• There are also a finite set of cities that you can be in, or go to – the states

• Add restriction that for health reasons, the last food you eat has to be Cuban! ( ACCEPTING vs REJECT states)

• What kind of questions can we answer!

Finite State Machines (FSMs)

• What are the problems we are trying to solve? Acceptor – takes an input and tells you

whether the input matches some specifications. (Are you in or are you out)

Transducers – working thru input data to modify the behavior of some process.

• Key limitation - memory size is fixed Independent of input size.

Input String FSM {Yes, No}

The FSM model

• Inputs: Strings built from a fixed alphabet

• Machine: A directed graph Nodes: states of the machine Edges: transitions from one state to another

• Special states Start Final or accepting

1 20 a a

bb a,b

Input String FSM {Yes, No}

FSM Decider Example

• Which strings of as and bs are accepted?

• Input alphabet {a, b}

• States {q0, q1, q2}

• Start state q0

• Final states {q2}

1 20a ab

b a,b

The soda machine – concretely

The soda machine – abstractly

• Input alphabet $0.25 Return-coin

• States Ready-for-coins 25 50 Empty …

Making the DFA model precise

Input alphabet

• S State set

• q0 S Initial state

• F S Final states

:S S Transition function

M = (, S, q0, F, )

Building FSMs

• An FSM is a directed graph How large is the input alphabet? How many states? How fast must it run?

• How to minimize space?

• Representations Matrix Array of lists Hashtable

• Overlapping hashtable Switch statement

a b

0 1 1

1 2 3

2 1 0

3 2 3

4 1 4

Representing the machine

• Matrix representation

• Why?

• Running Time?

a b

0 1 0

1 2 0

2 2 2

Process machines

• Coke machine

• Speech recognition – add probability to the transitions

• TCP protocol

• Parsers (HTML)

TCP Protocol ( don’t worry …)

Simple DFA example

• HTML Parser – Is it valid HTML?

• Accepts strings that end in 1 , = {0,1}

• Create a machine that accepts string which contain the sub-string “001” 0010, 1001 are in. 11, 0000 are out

• Working it out… Skip all 1’s, perk up when you see a 0. ( you may have just come upon the substring ). Skip back if you see a 1 too early. 1. Seen no symbols of the pattern 2. Just seen a 0 3. Just seen a 00 4. Just seen the entire pattern 001

Regular Expressions

• Regular expression.

• Way to describe sets of strings

• Examples –

• (“|” – OR)

• (“*” – 0 or more, “+” – one or more) (01)* (a|b)*ab this | that | theother 00*11*22* = 0+1+2+

Languages and FSMs

• A language that can be DECIDED by an FSM is called regular. Examples (accepts only these and no other)

• a(a|b)*b

Easiest way to test whether a language is regular is to see if you can create a machine!

L = {a(a|b)*b}

Problems types

• What about… a(a|b)*b

• What does this mean? | - OR * - 0 or more

Problems types

• Ends with… (a+b)*ab

• Begins with… ab(a+b)*

• Contains substring ‘w1’ (a+b)*w1(a+b)*

• Even / odd numbers of some character or both!

• Leverage the power of the guess

Enhancing the model - NFA

• Non-determinism Allow the machine to be in more than

one state at once On a given input, you could follow

multiple paths If there EXISTS a pathway to an ACCEPT

state to deal with the given input, ACCEPT

Examples:• 001 sub-string• a(a|b)*b

The Idea of Non-determinism

• An NFA can be in more than one state at a time

• 3 ways of looking at it Coins/fingers on every state you can be

at. A tree of outcomes, put a lemming on

each path, if any of them end on a final state, we ACCEPT

Oracle approach. Someone is telling you the right way to go

More examples

• “[.?!][]\"')]*\\($\\|\t\\| \\)[ \t\n]*” [.?!][]\"')]*($|<tab>|��)[�<tab><C-j>]* Emacs regexp:

• Any of . ? ! followed by• Zero or more of ] “ ‘ ) followed by• Any of end-of-line, tab, two spaces followed by• Zero or more of space, tab, newline

Big Question

• Are there languages L that can be accepted by NFAs but not DFAs?

• Are they more “powerful”?

Big Question

• Are there languages L that can be accepted by NFAs but not DFAs?

No!

What have we gained?

• Can we solve harder problems? We have assumed we can solve problems by only

parsing the data once… What about the ability to back-track? Turing Machines…

( If I only had a memory…)

What can’t you do

• Counting (on an unbounded set) L = {0n1n | n>0} It needs to remember the number of 0’s

it has seen. Since the number of 0’s is not limited we would have to track an infinite set of states.

Violates our memory condition… We can do L = {0515}

What can’t you do

• Counting (on an unbounded set) L = {0n1n | n>0} It needs to remember the number of 0’s

it has seen. Since the number of 0’s is not limited we would have to track an infinite set of states.

Violates our memory condition… We can do L = {0515}

NEED MORE POWER

End