finite state machines concepts dfa nfa. graphs and more finites state machines are directed graphs

30
Finite State Machines Concepts DFA NFA

Upload: dorcas-howard

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

Finite State Machines

ConceptsDFANFA

Page 2: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

Graphs and More

• Finites state machines are directed graphs.

Page 3: 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

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

Page 4: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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!

Page 5: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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}

Page 6: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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}

Page 7: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 8: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

The soda machine – concretely

Page 9: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

The soda machine – abstractly

• Input alphabet $0.25 Return-coin

• States Ready-for-coins 25 50 Empty …

Page 10: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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, )

Page 11: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 12: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

Representing the machine

• Matrix representation

• Why?

• Running Time?

a b

0 1 0

1 2 0

2 2 2

Page 13: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

Process machines

• Coke machine

• Speech recognition – add probability to the transitions

• TCP protocol

• Parsers (HTML)

Page 14: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

TCP Protocol ( don’t worry …)

Page 16: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 17: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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+

Page 18: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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}

Page 19: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

Problems types

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

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

Page 20: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 21: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 22: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 24: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 25: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

Big Question

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

• Are they more “powerful”?

Page 26: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

Big Question

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

No!

Page 27: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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…)

Page 28: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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}

Page 29: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

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

Page 30: Finite State Machines Concepts DFA NFA. Graphs and More Finites state machines are directed graphs

End