cse 311 foundations of computing i lecture 21 finite state machines autumn 2011 cse 3111

18
CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 Autumn 2011 CSE 311 1

Upload: jennifer-rosemary-bryan

Post on 13-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

CSE 311 Foundations of Computing I

Lecture 21Finite State Machines

Autumn 2011

Autumn 2011 CSE 311 1

Page 2: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Announcements

• Reading assignments– 7th Edition, Sections 13.3 and 13.4– 6th Edition, Section 12.3 and 12.4– 5th Edition, Section 11.3 and 11.4

• HW 8 is on-line

Autumn 2011 CSE 311 2

Page 3: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Last lecture highlights

Autumn 2011 CSE 311 3

Let R be a relation on a set A. There is a path of length n from a to b if and only if (a,b) Rn

Let R be a relation on a set A. The connectivity relation R* consists of the pairs (a,b) such that there is a path from a to b in R.

Transitive-Reflexive closure: Add the minimum possible number of edges to make the relation transitive and reflexive

The transitive-reflexive closure of a relation R is the connectivity relation R*

Page 4: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Finite state machines

StatesTransitions on inputsStart state and final statesThe language recognized by a machine is the set of strings that reach a final state

Autumn 2011 CSE 311 4

s0 s2 s3s1111

1

0,1

0

0

0State 0 1

s0 s0 s1

s1 s0 s2

s2 s0 s3

s3 s3 s3

Page 5: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Applications of Finite State Machines (a.k.a. Finite Automata)

• Implementation of regular expression matching in programs like grep

• Control structures for sequential logic in digital circuits

• Algorithms for communication and cache-coherence protocols– Each agent runs its own FSM

• Design specifications for reactive systems– Components are communicating FSMs

Autumn 2011 CSE 311 5

Page 6: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Applications of Finite State Machines (a.k.a. Finite Automata)

• Formal verification of systems– Is an unsafe state reachable?

• Computer games– FSMs provide worlds to explore

• Minimization algorithms for FSMs can be extended to more general models used in– Text prediction– Speech recognition

Autumn 2011 CSE 311 6

Page 7: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

What language does this machine recognize?

Autumn 2011 CSE 311 7

s0

s2 s3

s1

1

1

1

1

0

0

0

0

Page 8: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

3 Bit Shift register

Autumn 2011 CSE 311 8

001 011

111

110

101010000

100

1

11 0 1

1

1

1

000 1

0

0

00

Page 9: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Autumn 2011 CSE 311 9

001 011

111

110

101010000

100

1

11 0 1

1

1

1

000 1

0

0

00

10

00 01 10 11

11

1

0

0 0

0 0 0 01

1

1

1

Page 10: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Design a DFA that accepts strings with a 1 three positions from the end

Autumn 2011 CSE 311 10

Page 11: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Autumn 2011 CSE 311 11

How does the size of a DFA to recognize “10th character is a 1” compare with the size of a DFA to recognize “10th character from the end is 1”?

Page 12: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Strings over {0, 1, 2}*

M1: Strings with an even number of 2’s

M2: Strings where the sum of digits mod 3 is 0

Autumn 2011 CSE 311 12

s0 s1

t0 t2

t1

Page 13: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Recognize strings with an even number of 2’s and a mod 3 sum of

0

Autumn 2011 CSE 311 13

s0t0

s1t0

s1t2

s0t1

s0t2

s1t1

Page 14: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

State machines with output

Autumn 2011 CSE 311 14

Input Output

State L R

s1 s1 s2 Beep

s2 s1 s3

s3 s2 s4

s4 s3 s4 Beep

S3 S4S1 S2

R

L

R

L

R

L

L

R

“Tug-of-war”

Page 15: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Vending Machine

Autumn 2011 CSE 311 15

Enter 15 cents in dimes or nickelsPress S or B for a candy bar

Page 16: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Vending Machine, Version 1

Autumn 2011 CSE 311 16

0 5 10 15

D D

N N N, D

B, S

Basic transitions on N (nickel), D (dime), B (butterfinger), S (snickers)

Page 17: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Vending Machine, Version 2

Autumn 2011 CSE 311 17

0’ B 5 10

15

Adding output to states: N – Nickel, S – Snickers, B – Butterfinger

15’ N

0

0” S

N

N

N

N

N

B

D

D

D

D

D B

S

S

Page 18: CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111

Vending Machine, Final Version

Autumn 2011 CSE 311 18

0’ B 5 10

15

Adding additional “unexpected” transitions

15’ N

0

0” S

N

N

N

N

N

B

D

D

D

D

D B

S

S

15”

DS

B

B,S

B,S

B,S

B,S B,S

N

N

N

D

D

D