Basic Concepts and Formal Language theory

65
Basic Concepts and Formal Language theory Unit –I By T.H. Gurav June 18, 2022

description

Basic Concepts and Formal Language theory. Unit –I By T.H. Gurav. Elements of Formal Language. Symbols Alphabets Strings Languages. Alphabet. An alphabet is a finite, non-empty set of symbols We use the symbol ∑ (sigma) to denote an alphabet Examples: Binary: ∑ = {0,1} - PowerPoint PPT Presentation

Transcript of Basic Concepts and Formal Language theory

Page 1: Basic Concepts  and Formal Language theory

Basic Concepts and Formal Language theory

Unit –IBy

T.H. Gurav

April 20, 2023

Page 2: Basic Concepts  and Formal Language theory

Elements of Formal Language

SymbolsAlphabetsStringsLanguages

April 20, 2023

Page 3: Basic Concepts  and Formal Language theory

Alphabet

An alphabet is a finite, non-empty set of symbolsWe use the symbol ∑ (sigma) to denote an alphabetExamples: Binary: ∑ = {0,1} All lower case letters: ∑ = {a,b,c,..z} Alphanumeric: ∑ = {a-z, A-Z, 0-9} DNA molecule letters: ∑ = {a,c,g,t} …

April 20, 2023

Page 4: Basic Concepts  and Formal Language theory

Strings

A string or word is a finite sequence of symbols chosen from ∑

Empty string is (or “epsilon”)

Length of a string w, denoted by “|w|”, is equal to the number of (non- ) characters in the string

E.g., x = 010100 |x| = 6 x = 01 0 1 00 |x| = ?

xy = concatentation of two strings x and y

April 20, 2023

Page 5: Basic Concepts  and Formal Language theory

Kleen Closure ( ∑* )Given an alphabet ∑ , we wish to define a

language in which any string of letters from ∑ is a word/string, even null string.

This language we shall call the closure of the alphabet.

Positive Closure (∑+)

April 20, 2023

Page 6: Basic Concepts  and Formal Language theory

Languages

L is a said to be a language over alphabet ∑, only if L ∑* this is because ∑* is the set of all strings (of all possible length

including 0) over the given alphabet ∑Examples:

1. Let L be the language of all strings consisting of n 0’s followed by n 1’s:

L = {,01,0011,000111,…}2. Let L be the language of all strings of with equal number of 0’s

and 1’s: L = {,01,10,0011,1100,0101,1010,1001,…}

Definition: Ø denotes the Empty language

Let L = {}; Is L=Ø?

NOApril 20, 2023

Page 7: Basic Concepts  and Formal Language theory

Definition 1. A set is a group of objects . The objects in a set are called the elements, or members, of the set.

Example 1

The set of positive integers less than 100 can be denoted as {1,2,3,…99}

Definition 2. Two sets are equal if and only if they have the same elements.

{1,3,5}.set as same theis ,5,5,5}{1,3,3,3,5Set

3 Example

Sets

April 20, 2023

Page 8: Basic Concepts  and Formal Language theory

Cartesian Product

The Cartesian product of two sets is defined as:

AB = {(a, b) | aA bB}

Example: A = {x, y}, B = {a, b, c}AB = {(x, a), (x, b), (x, c), (y, a), (y, b), (y, c)}

April 20, 2023

Page 9: Basic Concepts  and Formal Language theory

Set Operations

Union: AB = {x | xA OR xB}

Example: A = {a, b}, B = {b, c, d} AB = {a, b, c, d}

Intersection: AB = {x | xA AND xB}

Example: A = {a, b}, B = {b, c, d} AB = {b}

April 20, 2023

Page 10: Basic Concepts  and Formal Language theory

Set Operations

Two sets are called disjoint if their intersection is empty, that is, they share no elements:AB =

The difference between two sets A and B contains exactly those elements of A that are not in B:A-B = {x | xA xB}

Example: A = {a, b}, B = {b, c, d}, A-B = {a}A-B = {a}

April 20, 2023

Page 11: Basic Concepts  and Formal Language theory

Set Operations

The complement of a set A contains exactly those elements under consideration that are not in A: -A = U-A

Example: U = N, B = {250, 251, 252, …} -B = {0, 1, 2, …, 248, 249}

April 20, 2023

Page 12: Basic Concepts  and Formal Language theory

xy

z

A function

x

Not a function

Functions and Relations

Let A and B be the sets. A function f from A to B is an assignment of exactly one element of B to each element of A. We write f(a)=b if b is the unique element of B assigned by the function f to the element a of A. If f is a function from A to B, we write f : A B.

April 20, 2023

Page 13: Basic Concepts  and Formal Language theory

Relations

Let A and B be the sets. A relation R from A to B is a subset of A X BFrom the defination , relation R is a set of pairs.If (a,b) ɛ R , we say a has a relation R with b, denoted as aRb.Functions can be consider as relations, but relations are more general than functions.

April 20, 2023

Page 14: Basic Concepts  and Formal Language theory

R is an equivalence relation if for any pair (x,y) of R

xRx for all x (reflexivity)

If xRy then yRx (symmetry)

If xRy and yRz, then xRz. (transitivity)

We usually use to denote equivalence relation.

Example : if A= {a,b} then

Reflexive relation = {(a,a),(b,b)}

Transitive relation = {(a,b) ,(b,a),

(a,a)}

Symmetric relation = {(a,b),(b,a)}

Properties of Relation

April 20, 2023

Page 15: Basic Concepts  and Formal Language theory

Transitive Closure (R+)Reflexive and Transitive Closure(R*) of a relation R

Eg: R ={(1,2),(2,3),(3,4),(5,4)}Solution : for R+ we will compute R2 ,R3,R4,….R2 = RoR = {(1,2),(2,3),(3,4),(5,4)} o {(1,2),(2,3),(3,4),(5,4)}

= {(1,3),(2,4)}R3 = R2 oR = {(1,3),(2,4)}o{(1,2),(2,3),(3,4),(5,4)} = {(1,4)}R4 = R3 oR = {(1,4)}o{(1,2),(2,3),(3,4),(5,4)}

=ФR5 = Ф

April 20, 2023

Page 16: Basic Concepts  and Formal Language theory

R+ = R U R2 U R3

R+ = {(1,2),(2,3),(3,4),(5,4)} {(1,2),(2,3),(3,4),(5,4)} U {(1,3),(2,4)} {(1,3),(2,4)} U {(1,4)} {(1,4)} R+ = {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4),(5,4)}R* = R+ U { (1,1),(2,2),(3,3),(4,4),(5,5)}

= {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4),(5,4)} U { (1,1),(2,2),(3,3),(4,4),(5,5)}

={(1,2),(1,3),(1,4),(2,3),(2,4),(3,4),(5,4) ,(1,1),(2,2),(3,3),(4,4),(5,5)}

April 20, 2023

Page 17: Basic Concepts  and Formal Language theory

Example 1

v7v1v2

v5v4v9

v8

v3e1 e3

e4

e5

e7

e6

e2

e8

e9

e10

A directed graph

(digraph)

Example 2

v7v1v2

v5v4v9

v8

v3e1 e3

e4

e5

e7

e6

e2

e8

e9

e10

An undirected graph

GraphsA Graph is a construct of two sets, denoted as G=(V,E) where V={v1,v2,v3…,vn} is a set of vertices and E={e1,e2,…em} is a set of edges. Each eage is pair from V

April 20, 2023

Page 18: Basic Concepts  and Formal Language theory

Induction

If we have a propositional function P(n), and we want to prove that P(n) is true for any natural number n, we do the following:

1. Show that P(0) is true. (basis step)2. Show that if P(n) then P(n + 1) for any nN. (inductive step)3. Then P(n) must be true for any nN. (conclusion)April 20, 2023

Page 19: Basic Concepts  and Formal Language theory

Induction

1 + 2 + … + n = n (n + 1)/2

1. Show that P(0) is true.(basis step)

For n = 0 we get 0 = 0. True.

April 20, 2023

Page 20: Basic Concepts  and Formal Language theory

Induction

2. Show that if P(n) then P(n + 1) for any nN. (inductive step)

1 + 2 + … + n = n (n + 1)/21 + 2 + … + n + (n + 1) = n (n + 1)/2 + (n + 1)

= (n + 1) (n/2 + 1) = (n + 1) (n + 2)/2 = (n + 1) ((n + 1) + 1)/2

April 20, 2023

Page 21: Basic Concepts  and Formal Language theory

Induction

3. Then P(n) must be true for any nN. (conclusion)

1 + 2 + … + n = n (n + 1)/2 is true for all nN.

End of proof.

April 20, 2023

Page 22: Basic Concepts  and Formal Language theory

Introduction to Automata Theory

April 20, 2023

Page 23: Basic Concepts  and Formal Language theory

Theory of computationTheory of Computation aims at studying “How and how efficiently problems can be solved on a model of computation, using an algorithm”.

The field is divided into three major branches: • automata theory, • Computability theory and • computational complexity theory.

April 20, 2023

Page 24: Basic Concepts  and Formal Language theory

Automata

Automaton = an abstract computing device(Note: A “device” need not even be a physical hardware!)Automata theory is theory of models. Working of every process can be represented by means of model.

April 20, 2023

Page 25: Basic Concepts  and Formal Language theory

Finite Automata

An automaton with a set of statesstates, and its “control” moves from state to state in response to external “inputs” is called a finite automaton. FA, provides the simplest model of a computing device.It has a central processor of finite capacity

April 20, 2023

Page 26: Basic Concepts  and Formal Language theory

Components of Finite Automata

April 20, 2023

left-end

x1 x2 x3 x4 x5 ……. xn

input: x

.

.

.

.Accept / Final state

current state

initial state

Finite control (program)

Movable tape head (Read Only)

…. no right-end

Page 27: Basic Concepts  and Formal Language theory

Components of Finite Automata

April 20, 2023

The components of FA model are Input tape, Read control and finite control.

The input tape is divided into number of cells. Each cell can hold one i/p symbol.

The read head reads one symbol at a time and moves ahead.

Finite control acts like a CPU. Depending on the current state and input symbol read from the input tape it changes state.

Page 28: Basic Concepts  and Formal Language theory

Finite Automaton – Formal DefinitionAn automaton is represented formally by a 5-tuple

(Q,Σ,δ,q0,F), where:Q is a finite set of states.Σ is a finite set of symbols, called the alphabet of the automaton.q0  is the start state, that is, the state of the automaton before any input has been processed, where q0∈ Q.F is a set of states of Q (i.e. F⊆Q) called accept states.δ is the transition function, that is, δ: Q × Σ → Q.

For elements q, r Q; a Σ; the formula δ(q,a) = {r} specifies the next state (r) by M after reading symbol ‘a’ while in state q.

April 20, 2023

Page 29: Basic Concepts  and Formal Language theory

How does an FA work?

At the beginning, an FA is in the start state (initial state) its tape head points at the first cell

For each move, FA reads the symbol under its tape head changes its state (according to the transition function) to

the next state determined by the symbol read from the tape and its current state

move its tape head to the right one cell

April 20, 2023

Page 30: Basic Concepts  and Formal Language theory

When does an FA stop working?

When it reads all symbols on the tape

Then, it gives an answer if the input is

in the specific language: Answer “YES” if its last state is a final state

Answer “NO” if its last state is not a final state

April 20, 2023

Page 31: Basic Concepts  and Formal Language theory

Alternative ways of FA representation1. Transition Diagram

1q 2qa

Current State

Input Symbol

Next State

April 20, 2023

Page 32: Basic Concepts  and Formal Language theory

Example-

The circles indicates the statesIf accepting state is marked with double circleThe arrows pointing from a state q indicates how

to move on reading a character when current state is q

0,1

0,1

Start

q0 q1

April 20, 2023

Page 33: Basic Concepts  and Formal Language theory

April 20, 2023

Transition Table

state For all Tape Symbols (a)

qi qj

Where: qi is the current stateqj is the next statea is the current symbol

Page 34: Basic Concepts  and Formal Language theory

Acceptability of a string by FA

A string ‘s’ is accepted by a FA if (q0,x)=q for some qЄF.

Eg : Q = { q0,q1,q2,q3} ∑ = {0,1} F = {q0} Transition function is given as : States i/p 0 1 q0 q2 q1 q1 q3 q0 q2 q0 q3 q3 q1 q2

<Q,∑,,q0,F>

Page 35: Basic Concepts  and Formal Language theory

Solution : Give entire sequence of states for the i/p string 110101

(q0,110101) = (q1,10101)= (q0,0101)= (q2,101)= (q3,01)= (q1,1)= (q0,^)=q0

Hence q0 1 q1 1 q0 0 q2 1 q3 0 q1 1 q0

Thus string is accepted.

April 20, 2023

Page 36: Basic Concepts  and Formal Language theory

Extended Transition function to Strings

It is slightly cumbersome to use a transition function for every i/p symbol. This problem can be resolved using a transition function for the whole string.

This extended transition function is defined as δ :Q X ∑*Q

April 20, 2023

Page 37: Basic Concepts  and Formal Language theory

Extended Transition function to Strings

Language of DFA : DFA defines a language which contains all the strings that reach to the final state of DFA starting with the start state.If δ is transition function then δ is extended transition function to strings. δ gives resultant o/p state ‘q’ from state q0 by i/p of string w. δ is defined as :

1.δ(q0,ε)=q0;2.δ(q,w)= δ(δ ( q,w1),wn-1) where w=w1w2….wn

Page 38: Basic Concepts  and Formal Language theory

Determinism and Non-determinism

A Finite Automaton may be ‘Deterministic’ or ‘Non-Deterministic’:

Deterministic Automaton means that the machine cannot be in more state than one at any point of time.

Non-Deterministic Automaton means that the machine can be in two or more states at any point of time.

April 20, 2023

Page 39: Basic Concepts  and Formal Language theory

DFAThe FA is called as Deterministic FA ,if there is only one path for a specific i/p from current state to next state.Definition: DFA is mathematically represented as a 5-uple(Q,∑,δ,q0, F)The transition function is a function in Q × ∑ → Q

1. a finite set of states (often denoted Q)2. a finite set of symbols (alphabet)3. a transition function that takes as argument a

state and a symbol and returns a state (often denoted )

4. a start state often denoted q05. a set of final or accepting states (often denoted F) We have q0 Є Q and F Є Q

Page 40: Basic Concepts  and Formal Language theory

Example : Password

When does an automata accepts a word ? It reads a word and accepts it if it stops in an accepting state.

Only word then is accepted q5 is stuck or dead state

Page 41: Basic Concepts  and Formal Language theory

NFASimilar to DFANondeterministic move

On reading an input symbol, the automaton can choose to make a transition to one of selected states.

Without reading any symbol, the automaton can choose to make a transition to one of selected states or not.

0

0

Page 42: Basic Concepts  and Formal Language theory

NFA definition A nondeterministic finite automaton M is a five-tuple M = (Q, , , q0, F), where:1. Q is a finite set of states of M2. is the finite input alphabet of M3. : Q 2Q (power set of Q), is the state

transition function mapping a state-symbol pair to a subset of Q

4. q0 is the start state of M5. F Q is the set of accepting states or final

states of M

Page 43: Basic Concepts  and Formal Language theory

DFA vs NFA DFA NFA

1 For Every symbol of the alphabet, there is only one state transition in DFA

1 We do not need to specify how does the NFA react according to some symbol.

2 DFA can not use Empty String transition

2 NFA can use Empty String transition.

3 DFA can be understood as one machine.

3 NFA can be understood as multiple little machines computing at the same time.

4 DFA will reject the string if it end at other than accepting state.

4 If all of the branches of NFA dies or rejects the string, we can say that NFA reject the string.

Page 44: Basic Concepts  and Formal Language theory

NFA to DFA conversion Subset ConstructionSubset Construction method :

Given an NFA with states Q, inputs Σ, transition function δN, state state q0, and final states F, construct equivalent DFA with:

states 2Q (Set of subsets of Q). Inputs Σ. Start state {q0}. Final states = all those with a member of F.

Page 45: Basic Concepts  and Formal Language theory

Eg:Construct a DFA equivalent to N=({q0,q1},{0,1},δ,q0,{q0})

δ is given in following table:

Solution : the states in DFA are subsets of {q0,q1} ie Ф ,{q0},{q1},{q0,q1}Initial state : {q0} Final states : {q0} and {q0,q1} ... As q0 present in these sets

State 0 1

q0 q0 q1

q1 q1 q0,q1

Page 46: Basic Concepts  and Formal Language theory

δ can be constructed as bellow :

Draw Transition diagram :

State 0 1

q0 q0 q1

q1 q1 q0,q1

{q0,q1} {q0,q1} {q0,q1}

Page 47: Basic Concepts  and Formal Language theory

NFA with -transitions

NFA that allows the transition of an empty string from a state

Jumping to a state is possible even without input

Situation: strings from a lang with 1st any no. of a’s then any no. of b’s and then any no. of c’s.

Page 48: Basic Concepts  and Formal Language theory

NFA with -transitionsA nondeterministic finite automaton with -transitions (or

-NFA) is a five-tuple M = (Q, , , q0, F), where:

1. Q is a finite set of states of M

2. is the finite input alphabet of M

3. : Q ( + ) power set of Q, is the state transition function

mapping a state-symbol pair to a subset of Q

4. q0 is the start state of M

5. F Q is the set of accepting states or final states of M

Page 49: Basic Concepts  and Formal Language theory

Epsilon closure

In an NFA M, let q Q

ECLOSE(q) represents all states r that can be reached

from q using only -transitions including itself.

Recursive definition for ECLOSEo If (q, ) is empty, ECLOSE(q) = {q}

o Else, Let (q, ) = {r1, r2,…, rn}.

ECLOSE(q) = ECLOSE(ri) {q}

Page 50: Basic Concepts  and Formal Language theory

Example : find -closure for the following NFA with .

Solution : 1. -closure (q0) = {q0,q1,a2} means self state +

reachable states .2. -closure(q1) = {q1,q2} 3. -closure(q2)={q2}

q0 q2q1

a cb

Page 51: Basic Concepts  and Formal Language theory

Steps to convert -NFA to NFA

If -NFA(Q, , , q0, F) is given then we need to find NFA (Q, , ’’, q0, F’).

If (0,1) then find ’’(q,0) and ’’(q,1) for all q ε Q.

Steps to find ’’(q,0)1. Find -closure of q. Outcome is set of states. Let that set is P

Find (pi,0) for all pi ε P i>=1Take union of all states obtained from step 2.Find -closure of all states obtained in step 3.

Same steps are repeated for ’’(q,1) for i/p alphabet 1.

Page 52: Basic Concepts  and Formal Language theory

Final states

NFA M’ = (Q, , ’’, q0, F’) such that F’ is described as follows

F’ = F plus all states q such that ECLOSE(q) contains a state in F

Page 53: Basic Concepts  and Formal Language theory

Converting -NFA into DFA

Step1 : first convert NFA into NFA

Step 2: then convert NFA into DFA by sub-state construction method.

Page 54: Basic Concepts  and Formal Language theory

Minimization of FA

The minimization of FA means reducing no. of states in FA.We need to find equivalent states and then represent them by one representative state.

Find equivalent classes ( ie groups of equivalent states)Step 1: Divide FA’s states into two groups

a. accepting group b. rejecting groupStep 2: Find out if states in groups go to same group on given

i/p or create new group.Step 3: Repeat step 2 till no new groups are created.

Page 55: Basic Concepts  and Formal Language theory

Finite AutomatonSome Applications Software for designing and checking the behavior

of digital circuits

Lexical analyzer of a typical compiler

Software for scanning large bodies of text (e.g.,

web pages) for pattern finding

Software for verifying systems of all types that

have a finite number of states (e.g., stock market

transaction, communication/network protocol)April 20, 2023

Page 56: Basic Concepts  and Formal Language theory

Mealy and Moore Machines

Theory of Computation

Page 57: Basic Concepts  and Formal Language theory

Specialized FA

DFA used to recognize a language. Need: But computers often have to do more useful function of performing calculations and conveying results. Solution: Need m/c that have output capability.

Page 58: Basic Concepts  and Formal Language theory

Melay and Moore models

Moore Machines : output depend only on the present state.

Mealy Machines : output depends on the present state and the present input.

Page 59: Basic Concepts  and Formal Language theory

Primitive State Diagram, Moore

state/output

input

A/0

C/0 D/0

B/1

etc.

off

on

off

Page 60: Basic Concepts  and Formal Language theory

Definition of a Moore Machine

It is a six tuple (Q,∑,Δ,δ,λ,q0) where 1.Q = A finite set of states 2.∑ = Alphabet of input letters3.Δ = Alphabet of output letters4.δ = Transition function ∑ x Q into Q

A unique one for each letter and each state

5.λ = Output Table . Maps Q into Δ6.q0 = is the start state.

Page 61: Basic Concepts  and Formal Language theory

Primitive State Diagram, Mealy

state

input/output

A

C D

B

etc.

off/1

on/0

off /0

Page 62: Basic Concepts  and Formal Language theory

TG for Mealy m/c

Q Σ0 1

State o/p State o/p         

         

Page 63: Basic Concepts  and Formal Language theory

Definition of a Mealy Machine

It is a six tuple (Q,∑,Δ,δ,λ,q0) where all the symbols have the same meaning as Moore machine except λ.

λ is the output function mapping ∑x Q into Δ

Page 64: Basic Concepts  and Formal Language theory

Interconversion

Mealy to Moore Moore to mealy

April 20, 2023

Page 65: Basic Concepts  and Formal Language theory

Thanking you…

April 20, 2023