automata theory, computability and complexity

64
Automata Theory, Computability and Complexity Mridul Aanjaneya Stanford University June 26, 2012 Mridul Aanjaneya Automata Theory 1/ 64

Upload: others

Post on 05-Dec-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Automata Theory, Computability and Complexity

Automata Theory, Computability and Complexity

Mridul Aanjaneya

Stanford University

June 26, 2012

Mridul Aanjaneya Automata Theory 1/ 64

Page 2: Automata Theory, Computability and Complexity

Course Staff

• Instructor: Mridul AanjaneyaOffice Hours: 2:00PM - 4:00PM, Gates 206 (Mon).

• Course Assistant: Sean KandelOffice Hours: 6:00PM - 8:00PM, Gates 372 (Tue/Thu).

• Textbook: Michael T. Sipser, Introduction to the Theory ofComputation (second edition).

• Website: http://www.stanford.edu/class/cs154/

• Discussion: Lore (http://www.lore.com/)Access code: R4ML4Z

Mridul Aanjaneya Automata Theory 2/ 64

Page 3: Automata Theory, Computability and Complexity

Course Requirements

• Homework

• Midterm

• Final (optional)

Note:

• Final grade will be computed from Homework (60%) and bestof Midterm/Final (40%).

• Projected grade will be out one week before the Final, takingHomework (60%) and Midterm (40%).

Can skip Final if satisfied with grade.

• Can collaborate on homeworks, but separate write-up perperson and must mention collaborators.

• 4 homeworks, 2 free late days, use them however you want.

No late days for final homework.

Mridul Aanjaneya Automata Theory 3/ 64

Page 4: Automata Theory, Computability and Complexity

Why Study Automata?

• Regular expressions:

are used in many systems, e.g., UNIX.DTD’s describe XML tags with RE like format.

• Finite automata model protocols, electronic circuits.

Theory is used in model-checking.

• Context free languages:

are used as syntax descriptors for PL’s, parsers (YACC).have an important role in describing natural languages.find use in procedural modeling.

Mridul Aanjaneya Automata Theory 4/ 64

Page 5: Automata Theory, Computability and Complexity

Course Outline

• Deterministic/Nondeterministic finite automata.

• Regular expressions.

• Decision/closure properties of regular languages.

• Context-free languages. Parse trees. Normal forms.

• Pushdown automata. Equivalence of CFG’s and PDA’s.

• Pumping lemma for CFL’s. Properties of CFL’s.

• Enumerations, Turing machines.

• Undecidable problems. NP-completeness.

• Satisfiability. Cook’s theorem.

Mridul Aanjaneya Automata Theory 5/ 64

Page 6: Automata Theory, Computability and Complexity

Automata, Computability and Complexity

Main Question:

What are the fundamental capabilities and limitations of computers?

Dates back to the 1930s.

Different interpretations in all three areas.

Mridul Aanjaneya Automata Theory 6/ 64

Page 7: Automata Theory, Computability and Complexity

Complexity

Question

Why are some problems hard and others easy?

• Subset sum: Given a set of integers, does any non-emptysubset of them add up to zero?

• So far only a classification according to hardness.

• Can help alter the root of difficulty, settle for an approximatesolution, some problems are hard only in the worst case.

Mridul Aanjaneya Automata Theory 7/ 64

Page 8: Automata Theory, Computability and Complexity

Computability

Question

Which problems are solvable?

• Halting problem: Given an arbitrary computer program,decide if it finishes or continues running forever.

• Computability and complexity are closely related.

Mridul Aanjaneya Automata Theory 8/ 64

Page 9: Automata Theory, Computability and Complexity

Automata

• Deals with different models of computation.

Mridul Aanjaneya Automata Theory 9/ 64

Page 10: Automata Theory, Computability and Complexity

Preliminaries: Mathematical Induction

Mridul Aanjaneya Automata Theory 10/ 64

Page 11: Automata Theory, Computability and Complexity

Preliminaries: Mathematical Induction

Example

Prove that 12 + 22 + 32 + . . .+ n2 is n(n+1)(2n+1)6 .

• Basis (n=1): 1(1+1)(2·1+1)6 = 2·3

6 = 1 = 12.

• Induction: Let P(n) := 12 + . . .+ n2 is n(n+1)(2n+1)6 .

P(n + 1) = 12 + . . .+ n2 + (n + 1)2 = n(n+1)(2n+1)6 + (n + 1)2

= (n + 1){

n(2n+1)+6n+66

}= (n + 1)

{2n2+7n+6

6

}= (n + 1)

{2n2+4n+3n+6

6

}= (n+1)(n+2)(2n+3)

6

• Since P(1) is true and P(n)⇒ P(n + 1), we conclude thatP(n) is true for all n ≥ 1.

Mridul Aanjaneya Automata Theory 11/ 64

Page 12: Automata Theory, Computability and Complexity

Preliminaries: Graphs

• Undirected graph

Mridul Aanjaneya Automata Theory 12/ 64

Page 13: Automata Theory, Computability and Complexity

Preliminaries: Graphs

• Degree of a vertex

Mridul Aanjaneya Automata Theory 13/ 64

Page 14: Automata Theory, Computability and Complexity

Preliminaries: Graphs

• Subgraph

Mridul Aanjaneya Automata Theory 14/ 64

Page 15: Automata Theory, Computability and Complexity

Preliminaries: Graphs

• Path

Mridul Aanjaneya Automata Theory 15/ 64

Page 16: Automata Theory, Computability and Complexity

Preliminaries: Graphs

• Cycle

Mridul Aanjaneya Automata Theory 16/ 64

Page 17: Automata Theory, Computability and Complexity

Preliminaries: Graphs

• Directed graph

Mridul Aanjaneya Automata Theory 17/ 64

Page 18: Automata Theory, Computability and Complexity

Preliminaries: Pigeonhole Principle

Pigeonhole Principle

If there are n + 1 pigeons and n pigeonholes, then some pigeonholemust contain at least two pigeons.

Mridul Aanjaneya Automata Theory 18/ 64

Page 19: Automata Theory, Computability and Complexity

Preliminaries: Pigeonhole Principle

Generalized Pigeonhole Principle

If there are kn + 1 pigeons and n pigeonholes, then some pigeonholemust contain at least k + 1 pigeons.

Mridul Aanjaneya Automata Theory 19/ 64

Page 20: Automata Theory, Computability and Complexity

Preliminaries: Pigeonhole Principle

Example 1

Given twelve integers, show that two of them can be chosen whosedifference is divisible by 11.

Example 2

Twenty-five crates of apples are delivered to a store. The apples areof three different sorts, and all the apples in each crate are of thesame sort. Show that among these crates there are at least ninecontaining the same sort of apple.

Example 3

Show that in any group of five people, there are two who have anidentical number of friends within the group.

Mridul Aanjaneya Automata Theory 20/ 64

Page 21: Automata Theory, Computability and Complexity

Alphabet

• An alphabet is any finite set of symbols.

Some examples include: ASCII, {0,1} (binary), {a,b,c}.• The set of strings over an alphabet Σ is the set of lists, each

element of which is a member of Σ.

Note: Strings are shown with no commas, e.g., abc, 01101.

• Σ∗ denotes this set of strings.

• ε stands for the empty string (string of length 0).

Example:

• For the alphabet Σ = {0, 1}, the set of strings in Σ∗ is:

{ε,0,1,00,01,10,11,000,001,010,011,100,101,110,111,. . .}• For the alphabet Σ = {a, b, c}, the set of strings in Σ∗ is:

{ε,a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,. . .}

Mridul Aanjaneya Automata Theory 21/ 64

Page 22: Automata Theory, Computability and Complexity

Alphabet

• An alphabet is any finite set of symbols.

Some examples include: ASCII, {0,1} (binary), {a,b,c}.• The set of strings over an alphabet Σ is the set of lists, each

element of which is a member of Σ.

Note: Strings are shown with no commas, e.g., abc, 01101.

• Σ∗ denotes this set of strings.

• ε stands for the empty string (string of length 0).

Example:

• For the alphabet Σ = {0, 1}, the set of strings in Σ∗ is:

{ε,0,1,00,01,10,11,000,001,010,011,100,101,110,111,. . .}• Subtlety: 0 as a string, and 0 as an alphabet look the same.

Context determines the type.

Mridul Aanjaneya Automata Theory 22/ 64

Page 23: Automata Theory, Computability and Complexity

Languages

• A language is a subset of Σ∗ for some alphabet Σ.

• Example 1: The set of strings over {0,1} with no twoconsecutive 1’s.

{ε,0,1,00,01,10,000,001,010,100,101,0000,0001,0010,0100,0101,1000,1001,1010,. . .}

• Example 2: The set of strings over {a,b,c} with unequalnumber of a’s and b’s.

{a,b,aa,ac,bb,bc,ca,cb,aaa,bbb,aab,baa,aba,aac,caa,aca,bbc,cbb,bcb,bba,abb,bab,cca,ccb,cac,cbc,bcc,acc,. . .}

Mridul Aanjaneya Automata Theory 23/ 64

Page 24: Automata Theory, Computability and Complexity

Finite Automata

• Informally, finite automata are finite collections of states withtransition rules for going from one state to another.

• There is a start state and (one or more) accept states.

Representation: Simplest representation is often a graph.

• Nodes denote states, and arcs indicate state transitions.

• Labels on arcs denote the cause of transition.

q q1 2

1

10

0

• The above automaton only accepts binary strings ending in 1.

Mridul Aanjaneya Automata Theory 24/ 64

Page 25: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 25/ 64

Page 26: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 26/ 64

Page 27: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 27/ 64

Page 28: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 28/ 64

Page 29: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 29/ 64

Page 30: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 30/ 64

Page 31: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 31/ 64

Page 32: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 32/ 64

Page 33: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 33/ 64

Page 34: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 34/ 64

Page 35: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 35/ 64

Page 36: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 36/ 64

Page 37: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 37/ 64

Page 38: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 38/ 64

Page 39: Automata Theory, Computability and Complexity

An example

• 01100011100101

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 39/ 64

Page 40: Automata Theory, Computability and Complexity

An example

• Accept!

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 40/ 64

Page 41: Automata Theory, Computability and Complexity

An example

• Exercise: Verify that 000111010110 is rejected.

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 41/ 64

Page 42: Automata Theory, Computability and Complexity

Deterministic Finite Automata

Definition

A DFA is a 5-tuple (Q,Σ, δ, q0,F ) consisting of:

• A finite set of states Q,

• A set of input alphabets Σ,

• A transition function δ : Q × Σ→ Q,

• A start state q0, and

• A set of accept states F ⊆ Q.

The transition function δ:

• Takes two arguments, a state q and an alphabet a.

• δ(q,a) = the state the DFA goes to when it is in state q andthe alphabet a is received.

Mridul Aanjaneya Automata Theory 42/ 64

Page 43: Automata Theory, Computability and Complexity

Graph representation of DFA’s

• Nodes correspond to states.

• Arcs represent transition function.

Arc from state p to state q labeled by all those input symbolsthat have transitions from p to q.

• Incoming arrow from outside denotes start state.

• Accept states indicated by double circles.

q q1 2

1

0

0

q3

1

0,1

• Accepts all binary strings without two consecutive 1’s.

Mridul Aanjaneya Automata Theory 43/ 64

Page 44: Automata Theory, Computability and Complexity

Example

• 011

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 44/ 64

Page 45: Automata Theory, Computability and Complexity

Example

• 011

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 45/ 64

Page 46: Automata Theory, Computability and Complexity

Example

• 011

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 46/ 64

Page 47: Automata Theory, Computability and Complexity

Example

• 011

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 47/ 64

Page 48: Automata Theory, Computability and Complexity

Example

• 011

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 48/ 64

Page 49: Automata Theory, Computability and Complexity

Transition table for DFA’s

0 1→ q∗1 q1 q2

q∗2 q1 q3

q3 q3 q3

• A row for each state, a column for each alphabet.

• Accept states are starred.

• Arrow for the start state.

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 49/ 64

Page 50: Automata Theory, Computability and Complexity

Extended transition function

• Effect of a string on a DFA can be described by extending δto a state and a string.

• Induction on length of the input string.

• Basis: δ(q,ε)=q

• Induction: δ(q,wa)=δ(δ(q,w),a)

w is a string, a is an input alphabet.Convention: w,x,y,. . . are strings, a,b,c,. . . are alphabets.

• For a DFA, extended δ is computed for state q and inputsa1a2. . .an by following a path starting at q and selecting arcswith labels a1,a2,. . .,an in turn.

Mridul Aanjaneya Automata Theory 50/ 64

Page 51: Automata Theory, Computability and Complexity

Example using transition table

0 1→ q∗1 q1 q2

q∗2 q1 q3

q3 q3 q3

δ(q2, 011) = δ(δ(q2, 01), 1) = δ(δ(δ(q2, 0), 1), 1)

= δ(δ(q1, 1), 1) = δ(q2, 1) = q3

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 51/ 64

Page 52: Automata Theory, Computability and Complexity

Language of a DFA

• Automata of all kinds define languages.

• If A is an automaton, L(A) is its language.

• For a DFA A, L(A) is the set of strings labeling paths fromthe start state to an accept state.

• Formally, L(A) = set of strings w such that δ(q0,w) is in F.

• Example: 01100011100101 is in L(A), where A is:

q q1 2

1

10

0

Mridul Aanjaneya Automata Theory 52/ 64

Page 53: Automata Theory, Computability and Complexity

Proofs of set equivalence

Question

How to prove that two different sets are in fact the same?

• Here, T is the set of strings of 0’s and 1’s with no consecutive1’s and L = {w | M accepts w}, where M is:

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 53/ 64

Page 54: Automata Theory, Computability and Complexity

Proofs of set equivalence

Question

How to prove that two different sets are in fact the same?

• In general, to show T = L, we need to prove two parts:

If w is in L, then w is in T, i.e., L ⊆ T.If w is in T, then w is in L, i.e., T ⊆ L.

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 54/ 64

Page 55: Automata Theory, Computability and Complexity

Part 1: L ⊆ T

• To prove: If w ∈ L, then w has no consecutive 1’s.

• Proof is induction on length of w.

• Important trick: Expand the inductive hypothesis to be moredetailed than you need.

Mridul Aanjaneya Automata Theory 55/ 64

Page 56: Automata Theory, Computability and Complexity

Part 1: L ⊆ T (Inductive Hypothesis)

1 If δ(q1,w)=q1, then w has no consecutive 1’s and does notend in 1.

2 If δ(q1,w)=q2, then w has no consecutive 1’s and ends in asingle 1.

• Basis: |w| = 0, i.e., w = ε.1 holds as ε has no 1’s at all.2 holds vacuously, since δ(q1,ε) is not q2.

• Note: p ⇒ q is always true, if p is false.

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 56/ 64

Page 57: Automata Theory, Computability and Complexity

Part 1: L ⊆ T

1 If δ(q1,w)=q1, then w has no consecutive 1’s and does notend in 1.

2 If δ(q1,w)=q2, then w has no consecutive 1’s and ends in asingle 1.

• Inductive step: Assume IH is true for strings shorter than w,where |w| ≥ 1.• Because w is not empty, we can write w = xa, where a is the

last alphabet of w and x is the string that precedes.• IH is true for x.

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 57/ 64

Page 58: Automata Theory, Computability and Complexity

Part 1: L ⊆ T

1 If δ(q1,w)=q1, then w has no consecutive 1’s and does notend in 1.

2 If δ(q1,w)=q2, then w has no consecutive 1’s and ends in asingle 1.

• Inductive step: Assume IH is true for strings shorter than w,where |w| ≥ 1.

1 for w is δ(q1,w) = q1.

• Since δ(q1,w) = q1, δ(q1,x) must be q1 or q2 and a must be 0.

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 58/ 64

Page 59: Automata Theory, Computability and Complexity

Part 1: L ⊆ T

1 If δ(q1,w)=q1, then w has no consecutive 1’s and does notend in 1.

2 If δ(q1,w)=q2, then w has no consecutive 1’s and ends in asingle 1.

• Inductive step: Assume IH is true for strings shorter than w,where |w| ≥ 1.

2 for w is δ(q1,w) = q2.

• Since δ(q1,w) = q2, δ(q1,x) must be q1 and a must be 1.

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 59/ 64

Page 60: Automata Theory, Computability and Complexity

Part 2: T ⊆ L

• To prove: If w has no consecutive 1’s, then w ∈ L.

• We prove the contrapositive.

• Note: p ⇒ q is equivalent to ¬q ⇒ ¬p.

• To prove (contrapositive): If w /∈ L, then w has no 11’s.

Mridul Aanjaneya Automata Theory 60/ 64

Page 61: Automata Theory, Computability and Complexity

Part 2: T ⊆ L

• Simple induction on length of w.

• The only way w is not accepted is if it gets to q3.

• The only way to get to q3 is if w = x1y.

• If δ(q1,x) = q2, then surely x = z1 (from Part 1: L ⊆ T).

• ⇒ w = z11y!

q q1 2

1

0

0

q3

1

0,1

Mridul Aanjaneya Automata Theory 61/ 64

Page 62: Automata Theory, Computability and Complexity

Regular languages

Definition

A DFA M = (Q,Σ, δ, q0,F ) accepts w if there exists a sequence ofstates r0, r1, . . . , rn in Q with three conditions:

• r0 = q0

• δ(ri ,wi+1) = ri+1 for i = 0, . . . , n − 1, and

• rn ∈ F

• Condition 1 says that M starts in the start state q0.

• Condition 2 says that M follows δ between two states.

• Condition 3 says that last state is an accept state.

• We say that M recognizes L if L = {w | M accepts w}.

Mridul Aanjaneya Automata Theory 62/ 64

Page 63: Automata Theory, Computability and Complexity

Regular languages

• A language L is regular if it is the language of some DFA.

Note: the DFA must accept only the strings in L.

• Some languages are not regular (more later).

Intuitively, DFA’s are memoryless.

Mridul Aanjaneya Automata Theory 63/ 64

Page 64: Automata Theory, Computability and Complexity

A regular language

Example

Let L = {w | w ∈ {0,1}∗ and w, viewed as a binary integer, isdivisible by 5.}

q q0

1

2

0

1

1

0

10

1

0

0

q q

q

1 3

4

Mridul Aanjaneya Automata Theory 64/ 64