theory of computation (fall 2014): lecture 02: deterministic finite automata; how dfas work; dfa...

50
Theory of Computation Vladimir Kulyukin www.vkedco.blogspot.com Deterministic Finite Automata

Upload: vladimir-kulyukin

Post on 24-Jun-2015

122 views

Category:

Science


2 download

TRANSCRIPT

Page 1: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Theory of Computation

Vladimir Kulyukin

www.vkedco.blogspot.com

Deterministic Finite Automata

Page 2: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Outline

● Blocks World Problem: A DFA for a Robotic Camera & Arm

● How DFAs Work● DFA Transition Function● Formal Languages Accepted by DFAs● Sample DFA Problems● Sample DFA Implementation

Page 3: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Blocks World Problem

Page 4: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

A B

Suppose we want to design a control system for a robotic camera & arm unit that should build two-block towers: a on b or b on a.

Blocks World Problem

Page 5: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

A

B

Goal State 01: A on B

Page 6: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

B

A

Goal State 02: B on A

Page 7: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Robotic Arm & Camera Operations

• puton(X, Y) – put A on top of Y• clear(X) – verify that X's top is clear• on(X, Y) – verify that X is on top of Y

Page 8: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Finite Automaton for Blocks World Problem

on(A, T)on(B, T)clear(A)clear(B)

on(A, B)on(B, T)clear(A)

on(B, A)on(A, T)clear(B)

puton(A, B)

puton(A, T)

puton(B, A)

puton(B, T)

Page 9: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Back to Automata & Languages

The robot control for our problem can be solved with a finite state machine, i.e., it is a regular language.

Page 10: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Deterministic Finite Automata

• A DFA can be informally defined as a directed graph whose nodes are states and whose edges are transitions on specific symbols

• A DFA has a unique start state and a set (possibly empty) of final or accepting states

• A DFA processes the input string one symbol at a time. When the last symbol is read, the DFA reaches a state which is either final or not. If the state is final, the DFA accepts (recognizes) the string. If the state is not final, the DFA rejects the string

Page 11: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

DFA: Formal Definition

states. (final) accepting ofset theis

state;start theis

;: function;n transitioa is

alphabet;an is

states; ofset finite a is

:where

,,,,, i.e. tuple,-5 a is DFA A

0

0

F

Qq

QQ

Q

FqQMM

Page 12: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Example 1: DFA M

q1q0

a

b

b

a

q0 is the start state.q1 is the final state.

Page 13: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Example 1: DFA M

01

100010

1

10

0

,

;,;,;, .4

; .3

;, .2

;, 1.

where,,,,,

qbq

qaqqbqqaq

qF

ba

qqQ

FqQM

Page 14: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFAs Work

Page 15: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Example: DFA M

a b

q0 q1 q0

q1 q1 q0

The transition function, δ, can be represented as a table:

Page 16: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFA M Works

q0

b a a b a

tape.on the

symbolleftmost thebefore placed is head The

Page 17: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFA M Works

q0

b a a b a

right. cell one move and in remain , read ,In 00 qbq

Page 18: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFA M Works

q0

b a a b a

right. cell one move and into go , read ,In 10 qaq

Page 19: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFA M Works

q1

b a a b a

right. cell one move and in remain , read ,In 11 qaq

Page 20: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFA M Works

q1

b a a b a

right. cell one move and into go , read ,In 01 qbq

Page 21: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFA M Works

q0

b a a b a

right. cell one move and into go , read ,In 10 qaq

Page 22: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

How DFA M Works

q1

b a a b a

final. is because

ecognized,accepted/r is Thus,

1q

baaba

Page 23: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Observations on DFA Definition

● Q may not be empty: a DFA must have at least one state, i.e., the start state

● δ is a function, i.e., it must be for every state in Q and every symbol in Σ

● F may be empty, in which case the DFA accepts the empty language

● F can be equal to Q, in which case the DFA accepts Σ*

Page 24: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

δ* :DFA Transition Function

Page 25: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

The δ* Function

axqxaq

qq

,,, .2

;, .1

:statesFA tostrings maps

**

*

*

Page 26: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Back to Example DFA M

Fqaqabq

abbqabbq

abbqabbqbbaq

bba

100

00*

0*

0*

0*

,,,

,,,,,,,

,,,,,,

accepted?it Is . isinput theSuppose

Page 27: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Languages Accepted by DFAs

Page 28: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

DFA String Acceptance

FxqFqQM

x

, ifonly and if ,,,,

DFA aby ecognizedaccepted/r is stringA

0*

0

*

Page 29: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

DFA’s Language: L(M)

.,|Then

.,,,,such that DFA a be Let

0**

0

FxqxML

FqQMM

Page 30: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Regular Languages

A language L is regular if and only if there exits a DFA M such that L(M) = L.

Page 31: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Sample DFA Problems

Page 32: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Typical DFA Problems

● Given a DFA M and a set of strings, determine which strings are in the language of M, i.e. L(M)?

● Given a DFA M, state in the set former notation, state L(M)?

● Draw a DFA diagram given the formal definition of the DFA

Page 33: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Problem 01

q0 q1

a

a

b

Which of the following strings are in L(M)?1. b2. ε3. ab4. abba5. ababaaaaba

b

Page 34: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Solution 01

Fqababaaaabaq

Fqaqabqabbq

abbaq

ababq

aabbqabbaq

MLab

Fqbqbaqabq

ML

MLb

10*

0111

0*

0*

0*

0*

110*

0*

, .5

,,,,,,

,,,,

,,,

,,, .4

So, .,,,, .3

.2

.1

Page 35: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Problem 02

q0 q1

a,b

a,b

What is the language accepted by this DFA M?

Page 36: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Solution 02

.,12 and ,|

:answer Formal

odd. islength whose,over strings All

:answer Informal

* NnnxbaxxML

ba

Page 37: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Problem 03

q0 q1

ba

a

b

What is the language accepted by this DFA M?

Page 38: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Solution 03

*, and |

:answer Formal

in end that ,over strings All

:answer Informal

bayyaxxML

aba

Page 39: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Problem 04

odd is in s' ofnumber |, .2

,| 1.

:it acceptsthat

DFA a draw languages, following theofeach For

*

*

xabax

baxx

Page 40: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Solution 04.1

q0

a,b

Page 41: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Solution 04.2

q0 q1

b b

a

a

Page 42: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Problem 05

01

01

10

10

1010

,

;,

;,

;,

,,,,,,,

:DFA thisDraw

qbq

qaq

qbq

qaq

qqbaqq

Page 43: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Solution 05

q0 q1

a,b

a,b

Page 44: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Sample DFA Implementation

Page 45: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

A DFA: Odd Numbers of Zeros

q0 q1

0

0

q2

any symbol other than 0any symbol other than 0

Page 46: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Java Implementation: Part 1

import java.io.*;

public class OddNumZeros {

   private static final int q0 = 0;   private static final int q1 = 1;   private static final int q2 = 2; // this is the sink

   private int state; // current state …

}

Page 47: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Java Implementation: Part 2

static private int delta(int s, char c) {       switch (s) {           case q0: switch (c) {               case '0': return q1;               default: return q2; // go to the sink           }           case q1: switch (c) {               case '0': return q0;               default: return q2; // go to the sink           }           default: return q2; // go to the sink       }}

Page 48: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Java Implementation: Part 3

public void deltaStar(String instr) {       char c;       for(int i = 0; i < instr.length(); i++) {           c = instr.charAt(i);           state = delta(state, c);       }   }

   public boolean isInputAccepted() {       return state == q1;   }

Page 49: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

Java Implementation: Part 4public void reset() { state = q0; }

public static void main(String[] args) throws IOException {

       OddNumZeros dfa = new OddNumZeros();       BufferedReader in =               new BufferedReader(new InputStreamReader(System.in));

       String instr = in.readLine();       while ( instr != null )       {           dfa.reset();           dfa.deltaStar(instr);           if ( dfa.isInputAccepted() ) {               System.out.println(instr + " accepted");           }           else {               System.out.println(instr + " rejected");           }           instr = in.readLine();       }

}

Page 50: Theory of Computation (Fall 2014): Lecture 02: Deterministic Finite Automata; How DFAs Work; DFA Transition Functions; Formal Languages Accepted by DFAs; Sample DFA Problems; Sample

References & Reading Suggestions

● A. Brooks Weber. Ch. 2 Formal Language: A Practical Introduction, Franklin, Beedle & Associates, Inc

● Davis, Weyuker, Sigal. Ch. 9. Computability, Complexity, and Languages, 2nd Edition, Academic Press