Top Banner
CS420 Introduction to the Theory of Computation Lecture 1: Introduction; nite automata Tiago Cogumbreiro CS420 Introduction; nite automata Lecture 1 Tiago Cogumbreiro 1 / 43
53

C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Jan 01, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

CS420Introduction to the Theory of Computation

Lecture 1: Introduction; �nite automata

Tiago Cogumbreiro

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 1 / 43

Page 2: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

About the courseIntructor: Tiago (蒂亚⼽) CogumbreiroClasses: Tuesday & Thursday5:30pm to 6:45pm at W-02-0158, WheatleyOf�ce hours: Tuesday & Thursday3:30pm to 5:00pm at S-3-183, Science Center

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 2 / 43

Page 3: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Course homepagecogumbreiro.github.io/teaching/cs420/f19/

(At the bottom right of my homepage.)Forum & announcements: piazza.com/umb/fall2019/cs420/homeAttendance tracking: www.estalee.comCourse code: ZS40HJDHomework assignment: https://tinyurl.com/yy4f9n4d (Blackboard)Syllabus, Slides, Video recordings

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 3 / 43

Page 4: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Course gradingCourse is divided into 3 modules (8 lessons)Each module is evaluated with a mini-test (32%)Mini-tests evaluate a single moduleEach module has a recap lessonAttendance and participation counts (4%)Tracking starts Tuesday, Sept 10Weekly homeworks(ungraded; may be used as extra credit when between grades; see syllabus)

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 4 / 43

Page 5: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

A birdseye view of CS420

5 / 43

Page 6: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

What are the limits of programs?

6 / 43

Page 7: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Limits of computationDifferent classes of machinesThe limits of each of these classesWhat properties each class enjoys

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 7 / 43

Page 8: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Limits of computationDifferent classes of machinesThe limits of each of these classesWhat properties each class enjoys

Classes of machines

Finite Automata Parse regular expressionsPushdown Automata Parse structured data (programs)

Turing Machines Any program

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 7 / 43

Page 9: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Some of what we will learnCan we write a program that checks if two regex are equivalent?

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 8 / 43

Page 10: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Some of what we will learnCan we write a program that checks if two regex are equivalent?Are two grammars equal?

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 8 / 43

Page 11: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Some of what we will learnCan we write a program that checks if two regex are equivalent?Are two grammars equal?We need to parse some data; do we need a regex or a grammar?

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 8 / 43

Page 12: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Some of what we will learnCan we write a program that checks if two regex are equivalent?Are two grammars equal?We need to parse some data; do we need a regex or a grammar?Can we know if a program terminates without running it?

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 8 / 43

Page 13: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Some of what we will learnCan we write a program that checks if two regex are equivalent?Are two grammars equal?We need to parse some data; do we need a regex or a grammar?Can we know if a program terminates without running it?Are two machines/programs equal?

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 8 / 43

Page 14: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Some of what we will learnCan we write a program that checks if two regex are equivalent?Are two grammars equal?We need to parse some data; do we need a regex or a grammar?Can we know if a program terminates without running it?Are two machines/programs equal?Can a given algorithm give an answer for all inputs?

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 8 / 43

Page 15: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

TechniquesState-machinesStructure concurrency/parallelism/User Interfaces; UML diagrams

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 9 / 43

Page 16: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

TechniquesState-machinesStructure concurrency/parallelism/User Interfaces; UML diagramsRegular expressions (regex) String matching rules

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 9 / 43

Page 17: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

TechniquesState-machinesStructure concurrency/parallelism/User Interfaces; UML diagramsRegular expressions (regex) String matching rulesGrammars Data speci�cation; Parsing data

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 9 / 43

Page 18: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

TechniquesState-machinesStructure concurrency/parallelism/User Interfaces; UML diagramsRegular expressions (regex) String matching rulesGrammars Data speci�cation; Parsing dataTuring machines Theory of computation

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 9 / 43

Page 19: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

TechniquesState-machinesStructure concurrency/parallelism/User Interfaces; UML diagramsRegular expressions (regex) String matching rulesGrammars Data speci�cation; Parsing dataTuring machines Theory of computationProofs by contradiction Formal proofs

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 9 / 43

Page 20: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

CS420Study algorithms and abstractionsTheoretical study of the boundaries of computing

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 10 / 43

Page 21: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Finite state automata

11 / 43

Page 22: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Today we will learn…Finite automata theoryState diagramImplementation of a �nite automatonFormal de�nition of a �nite automatonLanguage of a �nite automaton

Section 1.1

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 12 / 43

Page 23: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Decision problemWe will study Decision Problems: yes/no answerThe set of inputs the problems answers yes are called the formal language

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 13 / 43

Page 24: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Finite Automataa.k.a. �nite state machine

14 / 43

Page 25: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

A turnstile controllerAllows one-directional passage. Opens when the front sensor is triggered. It shouldremain open while any sensor is triggered, and then close once neither is triggered.

States: open, closeInputs: front, rear, both, neither

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 15 / 43

Page 26: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Each state must have exactly onetransition per element of the alphabet (allstates must have same transition count)

De�nition

Graph-based diagramNodes: called states; annotated with aname (Distinct names!)Edges: called transitions; annotatedwith inputsInitial state has an incoming edge (onlyone)Accepted nodes have a double circle(zero or more)Multiple inputs are comma separated

State Diagram

In the example: Two states: open, close. State close is an accepting state. State close is also the initial state

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 16 / 43

Page 27: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Front, Neither]

State Diagram: example 1

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 17 / 43

Page 28: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Front, Neither]

State Diagram: example 1

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 18 / 43

Page 29: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Front, Neither]

State Diagram: example 1

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 19 / 43

Page 30: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Rear, Front, Rear, Neither, Rear]

State Diagram: example 2

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 20 / 43

Page 31: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Rear, Front, Rear, Neither, Rear]

State Diagram: example 2

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 21 / 43

Page 32: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Rear, Front, Rear, Neither, Rear]

State Diagram: example 2

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 22 / 43

Page 33: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Rear, Front, Rear, Neither, Rear]

State Diagram: example 2

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 23 / 43

Page 34: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Rear, Front, Rear, Neither, Rear]

State Diagram: example 2

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 24 / 43

Page 35: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Input: [Rear, Front, Rear, Neither, Rear]

State Diagram: example 2

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 25 / 43

Page 36: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

The controller of a turnstileState transition

close open close close closeopen open open open close

from enum import *

class State(Enum): Open = 0; Close = 1

class Input(Enum): Neither = 0; Front = 1; Rear = 2; Both = 3

def state_transition(old_st, i): if old_st �� State.Close and i �� Input.Front: return State.Open if old_st �� State.Open and i �� Input.Neither: return State.Close return old_st

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 26 / 43

Page 37: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

An automatonAn automaton receives a sequence of inputs, processes them, and outputs whether itaccepts the sequence.

Input: a string of inputs, and an initial stateOutput: accept or reject

Implementation example

def automaton_accepts(inputs): st = State.Close for i in inputs: st = state_transition(st, i) return st is State.Close

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 27 / 43

Page 38: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

An automaton acceptance examples�>� automaton_accepts([])True�>� automaton_accepts([Input.Front, Input.Neither])True�>� automaton_accepts([Input.Rear, Input.Front, Input.Front])False�>� automaton_accepts([Input.Rear, Input.Front, Input.Rear, Input.Neither, Input.Rear])True

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 28 / 43

Page 39: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Creating an Automaton libraryclass FiniteAutomaton: def __init__(self, states, alphabet, transition_func, start_state, accepted_states): assert start_state in states assert all(x in states for x in accepted_states) self.states = states self.alphabet = alphabet self.transition_func = transition_func self.start_state = start_state self.accepted_states = accepted_states

def accepts(self, inputs): st = self.start_state for i in inputs: assert i in self.alphabet st = self.transition_func(st, i) assert st in self.states return st in self.accepted_states # We accept now multiple states

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 29 / 43

Page 40: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Finite automaton library example�>� a = FiniteAutomaton(State, Input, state_transition, State.Close, [State.Close])�>� a.accepts([])True�>� a.accepts([Input.Front, Input.Neither])True�>� a.accepts([Input.Rear, Input.Front, Input.Front])False�>� a.accepts([Input.Rear, Input.Front, Input.Rear, Input.Neither, Input.Rear])True

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 30 / 43

Page 41: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Strings

31 / 43

Page 42: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

AlphabetLet represent a �nite set of some elements.

Examplesbits:

vowels: or, perhaps

Σ

Σ = {0, 1}Σ = {a, e, i, o, u} Σ = {a, e, i, o, u, y}

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 32 / 43

Page 43: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

StringA string (also known as a word) over an alphabet is a �nite and possibly empty sequenceof elements of .

Examples, , are strings of

, , are all strings of

ΣΣ

[] [0, 0] [0, 1, 0, 0] Σ = {0, 1}[a, a, e] [a, e, i] [u, a, i, e, e, e, e] Σ = {a, e, i, o, u}

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 33 / 43

Page 44: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

ExamplesLet .

NotesThe string type is a parametric type. Thetype of strings is parametric on the typeof the alphabet, much like a list isparametric on the type of its contents.Unlike programmers, mathematiciansfavour short notations over moreverbose names, so is preferred over

.

In this course we use the word type andset as synonyms.

String typeWe use to denote the type of a string, whose elements are strings over alphabet .Σ⋆ Σ

Σ = {0, 1}[] ∈ Σ⋆

[0, 0] ∈ Σ⋆

[0, 1, 0, 0] ∈ Σ⋆Σ⋆

String⟨Σ⟩

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 34 / 43

Page 45: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Formally de�ning a stringDe�ning a string

The empty string , also represented as

Adding one element to a string written as

We will learn that is known as grammar.

w ::= [] ∣ c :: w

[] ϵ

c w c :: w

w ::= [] ∣ c :: w

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 35 / 43

Page 46: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Formally de�ning a stringWe use the following notation to represent a string

We may also omit the brackets and commas when there is no ambiguity

[c , c , ..., c ] ≡1 2 n c :1 : c :2 : ⋯ :: c :n : []

[c , c , c ] =1 2 3 c c c1 2 3

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 36 / 43

Page 47: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Operations on stringsLength

We are de�ning the lenght function by branches. Each branch depends on the pattern ofthe argument.

Example

Show that .

Proof. The proof follows by applying the de�nition of the length function.

∣ϵ∣ = 0∣c :: w∣ = 1 + ∣w∣

∣[1, 2]∣ = 2

∣1 :: 2 :: []∣ = 1 + ∣2 :: []∣ = 1 + 1 + ∣[]∣ = 1 + 1 + 0 = 2□

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 37 / 43

Page 48: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Operations on stringsConcatenation

Attaches two strings together in a new string.

Formalization of the usual intuition of string concatenation.

Example

ϵ ⋅ w = w

c :1 : w ⋅1 w =2 c :1 : (w ⋅1 w )2

aba ⋅ ca = a :: ba ⋅ ca = a :: (ba ⋅ ca) = a :: b :: (a ⋅ ca) = a :: b :: a :: ([] ⋅ ca) =abaca

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 38 / 43

Page 49: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

ExponentThe exponent concatenates copies of the same string.

Example

n

w =0 []w =n+1 w ⋅ wn

ab =3 ababab

ab =1 ab

ab =0 [] = ϵ

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 39 / 43

Page 50: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Pre�xDe�ning predicates by cases.

How do we read this?

The notation means if happens, then we can conclude .

def prefix(p, w): if len(p) �� 0: return True # Rule 1 if len(p) < len(w): return False return p[0] �� w[0] and prefix(p[1�], w[1�]) # Rule 2

ϵ prefix w c :: w  prefix c :: w1 2

w  prefix w1 2

QP P Q

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 40 / 43

Page 51: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Languages

41 / 43

Page 52: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

LanguageA language is a set of strings of type , formally .

Examples is a language that only contains the empty string

is a language that only contains a string with a single character

is a language that only contains string

is a language whose strings' last character is 1

is a language whose strings' sizes are even numbers

L Σ⋆ L ⊆ Σ⋆

{ϵ}{[c]} c

{[1, 1, 1]} [1, 1, 1]{w ∣ w ∈ Σ ∧⋆  ends with 1}{w ∣ w ∈ Σ ∧⋆ ∣w∣ is even}

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 42 / 43

Page 53: C S 4 2 0 - cogumbreiro.github.ioC S 4 2 0 ☽ I n t ro d u c t ion ; n ite a utom a t a ☽ L e ctu re 1 ☽ Tia go C ogum breiro 3 / 43. C o u r s e g rad in g C o u r s e is d iv

Operations on languagesUnion:

Intersection:

Subtraction: .In the book, . Note that

Complementation:

L ∪M = {w ∣ w ∈ L ∨ w ∈M}L ∩M = {w ∣ w ∈ L ∧ w ∈M}L−M = {w ∣ w ∈ L ∧ w ∈/ M}L−M = L ∖M L−M = L ∩M

=L {w ∣ w ∈/ L} = Σ −⋆ L

CS420  ☽  Introduction; �nite automata  ☽  Lecture 1  ☽  Tiago Cogumbreiro 43 / 43