Computation Engineering Applied Automata Theory and Logic

359
1

Transcript of Computation Engineering Applied Automata Theory and Logic

Page 1: Computation Engineering Applied Automata Theory and Logic

1

Page 2: Computation Engineering Applied Automata Theory and Logic

Computation Engineering:applied automata theory and logic

–DRAFT–March 28, 2005

Ganesh Gopalakrishnan

Page 3: Computation Engineering Applied Automata Theory and Logic

Contents

i

Page 4: Computation Engineering Applied Automata Theory and Logic

ii CONTENTS

Page 5: Computation Engineering Applied Automata Theory and Logic

List of Figures

iii

Page 6: Computation Engineering Applied Automata Theory and Logic

iv LIST OF FIGURES

Page 7: Computation Engineering Applied Automata Theory and Logic

List of Tables

v

Page 8: Computation Engineering Applied Automata Theory and Logic

vi LIST OF TABLES

Page 9: Computation Engineering Applied Automata Theory and Logic

Chapter 1

Introduction

Welcome to Computation Engineering - the discipline of applying engineer-ing principles to model, analyze, and construct computing systems. Thehuman society is, more than ever, reliant on computing systems operatingcorrectly within automobile control systems, medical electronic devices, tele-phone networks, mobile robots, farms, nuclear powerplants, etc, etc. Withso much entrusted to computers, how can we ensure that all these computersare being built and operated in a manner responsible to all flora and fauna?How do we avoid potential disasters - such as mass attacks due to malev-olent “viruses,” or an avionic computer crashing and rebooting mid-flight?On a deeper, more philosophical note, what exactly is computation? Canwe mathematically characterize those tasks that computers are capable ofperforming, and those they are incapable of performing? The subject-matterof this book is really about seeking answers to these deep questions usingautomata theory and mathematical logic.

Computation Science and Computation Engineering

We distinguish computer scientist from a computation engineer. We de-fine those people who seek an in-depth understanding of the phenomenon ofcomputation to be a computer scientist. We define those people to seek toefficiently apply computer systems to solve real-world problems to be com-putation engineers1. The distinction is, in a sense, similar to that between achemist and a chemical engineer. This book will expose you to computer

1We prefer to call the latter activity computation engineering as opposed to computerengineering because the term “computer engineer” is, unfortunately, applied nowadays to

1

Page 10: Computation Engineering Applied Automata Theory and Logic

2 CHAPTER 1. INTRODUCTION

science through automata theory and logic. It will show you how computa-tion engineers use automata theory and logic as the engineering mathemat-ics, much like ‘traditional engineers’ apply differential and integral calculus tobuild better-engineered products such as bridges, automobiles, and airplanes.

What is ‘Computation?’

By the way of popular analogy2, we can offer one possible feeble answer: “ifit involves a computer, a program running on a computer, and numbers goingin and out, then computation is likely happening.” Such an answer invites abarrage of additional questions “what is a computer?”, “what is a program?”,etc. There are other tricky situations to deal with, as well. Consider anotherplace where computation seems to be happening: within our very body cells.Thanks to modern advances in genetics3, we are now able to understand themind-boggling amount of “string processing” that occurs within our cells -in the process of transcribing the genetic code (which resembles assemblycode in a strange programming language), doing all that wonderful stringmatching and error correction, resulting in the synthesis of proteins. Is thatalso computation?

The short answer is that we cannot have either a comprehensive or apermanent definition of what ‘computation’ means. Unless we employ theprecise language offered by mathematics, all discussions, philosophical, emo-tionally charged, or otherwise, are bound to lead nowhere. One must buildformal models that crystallize the properties observed in real-world comput-ing systems, study these models, and then answer questions about computingand computation in terms of the models. The abstraction must also be atthe right level - otherwise, we will end-up modeling a computer as a mindlesselectronic oscillator that hauls bits around4.

Given all this, it is indeed remarkable that computer science has beenable to capture the essence of computing in terms of a single formal device- the so called Turing machine. A Turing machine is a simple device thathas finite-state control that interacts with an unbounded5storage tape (or,

people with a hardware bias in their outlook. In this book, we carry no such bias.2“If it walks like a duck and quacks like a duck, then it is a duck.”3An excellent reference on the subject is “Cartoon Guide to Genetics,” by Larry Gonick

and Mark Wheelis.4A similar end-result should we abstract the music of Mozart as a sound pressure

waveform.

Page 11: Computation Engineering Applied Automata Theory and Logic

3

equivalently, a finite-state control that interacts with two unbounded stacks,as we shall show very soon). In fact, several other formal devices - suchas the Lambda Calculus, Thue Systems, etc. - were proposed around thesame time as Turing machines. All these devices were also formally shownto be equivalent to Turing machines. This caused Alonzo Church to putforth his (by now famous) thesis “All effectively computable functions canbe reduced to one of these models.” In Chapter 10 we shall study Turingmachines in great detail; Chapter 4 gives a glimpse of how the LambdaCalculus, essentially through the fixed-point theory6, provides a formal modelfor computer programs.

A minimalist approach

To cut a long story short, the power of a computational device is determinedby two essential ingredients, namely: (i) how it is set up to carry out thevarious steps in an algorithm, and (ii) how it is set up to record informa-tion from previous steps in an algorithm (the “history recording device”).The former is called finite-state control – essentially, it consists of the “stepnumber” of an algorithm being currently executed and which steps may fol-low the current step. For example, we may be currently at step 34, andthe ensuing steps might be step 43 (under certain conditions) and step 54(under other conditions). As for the history recording device, since we aredealing with mathematical models of computations, we allow the possibilityof recording an arbitrary amount of information from previous steps. Thisis similar to how Real numbers are allowed to have infinite precision (e.g.,π = 3.1415926 . . .).

With the above conventions, we adopt a ‘minimalist’ approach in order tostudy the power of machines. We always assume the presence of a finite-statecontrol. We then vary the amount, as well as organization of the recordingdevice. It turns out that we can vary the recording device in just three stepsand cover three very interesting families of machines. A finite-state controldevice coupled with zero stacks will be called a deterministic finite automaton(DFA). At first glance, a DFA appears to be a terribly impoverished form ofa computer. It is basically a computer in which all programs must executein the confines of a finite amount of memory. However, as we shall soon

5‘Unbounded’ means ‘of infinite capacity’ or ‘infinitely sized’ or ‘without any bounds.’6The main person behind the use of fixed-point theory in programming is Dana Scott.

As one of Perlis’s epigrams goes “Scott is the Church of the Lattice Way Saints.”

Page 12: Computation Engineering Applied Automata Theory and Logic

4 CHAPTER 1. INTRODUCTION

demonstrate, a DFA is surprisingly powerful.We shall then proceed to couple exactly one unbounded stack with a

finite-state control device, thus obtaining something more powerful than aDFA. We either obtain a DPDA (deterministic push-down automaton) or aNPDA (non-deterministic push-down automaton), depending on whether weallow non-determinism, or not. This difference is quite significant, as we shalldiscuss later in Chapter 9. Even as powerful as they are, neither a DPDA nora DPDA possess the same power as a traditional computer. However, thisgap is closed simply by providing one more unbounded stack! The momenta finite-state control device has access to two unbounded stacks, it becomesa full-fledged computer! All computers, starting from the humble low-endcomputer of a Furbee doll or a digital wrist-watch - through all varieties ofdesktop and laptop computers - all the way to monstrously powerful com-puters, say, the IBM Blue Gene/L [?] computer planned for inaugurationat the Lawrence Livermore National Laboratory in California in year 2005 -can be modeled in terms of a finite state control coupled to just two stacks.(The prevailing convention is to study these two-stack devices in terms of anequivalent structure called Turing machines, as we shall later see.)

We will also later examine whether the power of computational devicesvaries with other recording devices employed: how about a single bounded,but rewriteable tape? How about a queue instead of a stack? We will findthat the answers are indeed remarkably surprising.

Models versus reality

Here is an important point of confusion we wish to avoid early. We assumethat all these ‘regular computers’ - whether inside a Furbee doll or the BlueGene computer - has unbounded main memory. Any computer that hasonly finite memory is nothing but a DFA. Even so, it is possible to viewa finite memory computer as a Turing machine. This is because virtuallyall programs that we run on computers do so within the confines of finitememory. But still, the formal model underlying computers must allow foreven those programs that consume an unbounded amount of memory. Thisis entirely similar to the use of Real numbers in studying number theory;while most numbers are “well behaved,” and are in fact rational numbers,there are also those irrational numbers such as π or e that one has to dealwith. Therefore, the underlying formal model of numbers - Real numbers -must be powerful-enough to deal with all these kinds of numbers.

Page 13: Computation Engineering Applied Automata Theory and Logic

5

Both ‘Real real numbers’ and ‘computers with infinite memory’ are fig-ments of imagination; they do not exist “in the real world.” Yet, we cannotdo without them in the study of the respective disciplines! There are severalmore things that do not exist, and yet are indispensable to mankind - com-plex numbers, the Dirac delta function (which is a pulse of infinite height,zero width, and unit area), etc. View Turing machines at the same level asone of these abstractions.

How do we measure the power of computers?

In comparing computers, we cannot go by any subjective measure such astheir absolute speed - what is an ultra-fast computer today might be a snail-paced computer tomorrow. Therefore, a wrist-watch computer, the Furbeedoll computer, a multiprocessor, and a Turing machine are all equivalent inpower (again assuming infinite memory). How is this equivalence preciselydefined? Again, computer science offers a remarkably simple, but profoundanswer. Computers are used to solve problems; therefore we can deem twocomputers to be equivalent if they can solve the same class of problems -ignoring the actual amount of time taken. Problem solving, in turn, can bemodeled in terms of deciding membership in languages. We now define someof these notions briefly (details appear later).

Consider the problem of programming a computer to tell whether twoarbitrary C programs are equivalent. How do we model this as a problemof deciding membership in a language? A formal language is defined tobe a set of strings, a set of pairs of strings, etc. For the purpose of ourcurrent example, let us employ a language S that consists of all pairs ofequivalent C programs7. This is a mathematically precise notion - eithertwo C programs are equivalent in functionality, or they are not. Also, eachC program is, after all, a string. Thus, the set S will consist of all pairsof equivalent C programs. Now, given an arbitrary pair of C programs,say x, where x = 〈c1, c2〉, imagine askng whether x belongs to S. In otherwords, we are asking whether the programs c1 and c2 are equivalent. Now,if we can program a Turing machine to answer whether x ∈ S or not, andalso similarly program a watch/doll computer or a multiprocessor to providesimilar answers, then these devices are equivalent - but only so far as solving

7Having equivalent functionality essentially means “effect the same input/output map-ping.” Later, we shall make this notion also precise through the notion of language equiv-alence.

Page 14: Computation Engineering Applied Automata Theory and Logic

6 CHAPTER 1. INTRODUCTION

the equivalence of C programs goes. Now, if, for every problem, we haveidentical capabilities - either both these devices can be programmed to do thejob, or they both cannot be - then we can conclude that a Turing machine,a watch/doll computer, and a multiprocessor are identical in power. The“capstone” result of computability theory is to state and prove this result.We shall provide this proof in Chapter 12.

The notion of membership in languages is far-reaching. It helps us in twoessential ways:

• As we just now saw, it helps us answer whether two formal models ofcomputation are equivalent.

• It helps us answer the solvability of particular problems by particularformal models of computation.

To illustrate the latter point, consider the following types of questions:

• Given the language P =

The set of all 〈x, y〉 where x and y are descriptions of DPDAsthat behave exactly the same. (These descriptions are coded-up in the form of strings.)

Now, given two DPDAs a and b, can we program a Turing machine totell whether the pair 〈a, b〉 is a member of language P?

• Another question is: given P , a, and b as above, can we program aDPDA to tell whether the pair 〈a, b〉 is a member of language P?

Some of these questions are very difficult to answer, indeed. The first of thetwo questions above was posed in 1966, and answered in the affirmative onlyin 1996 [?]! The answer to the second question is “no.”

While common-sense seems to suggest that in general we should not aska “lesser device” (such as a PDA) to answer questions pertaining to a moresuperior device (such as a TM), we shall later see some surprising results. Forinstance, it is possible to ‘teach’ NPDAs to answer certain difficult questionspertaining to TMs, if we can allow the NPDAs to “peek inside” the TMs, andobtain snapshots of each and every step the TM is making. This technique,called the computation history method, will also be the subject of Chapter 12.

Page 15: Computation Engineering Applied Automata Theory and Logic

7

Complexity Theory

Well, you guessed it right: in studying and comparing computing devices,we really cannot ignore time or resources entirely. They do matter! Butthen, one will ask “how do we measure quantities such as time, space, andenergy?” It is very easy to see that using metrics such as minutes andhours is entirely unsatisfactory. First of all, such measures are closely tied tosubjective factors such as the clock speed of computers, the pipeline depth,etc. With advances in technology, computations that took hours a few yearsago can be performed in seconds nowadays. They are also too non robust; wewill have to define what kind of computer - a Furbee computer or Blue Gene -is used as the standard reference! There are deeper worries. Einstein’s resultshave mathematically predicted that if one carries an ultra-fast computer onan ultra-fast moving spacecraft, it will wind up slowing down to computeultra-slow. In other words, the Blue Gene traveling close to the speed oflight can be slower than a Furbee computer that is stationary8! The deeperpoint is that we need to get away from arbitrary and non-robust measuresof time complexity.

Again Turing machines come to our rescue! We define a unit of time tobe one step of a Turing machine that is running the particular program orcomputation. We then define time in terms of the O() notation, which relatesrun-time to input lengths. Our time-scale now becomes quite general, andindependent of the underlying details. Some examples of time complexityaccording to our newly adopted time-scale may look as follows:

• In one example, if n is the length of the input on which a Turingmachine program operates, it may take O(n2) amount of time for theprogram to terminate and return its results.

• In another example, it may take an exponential amount of Turing ma-chine steps before the results are delivered.

• In yet another example, on some inputs it may take a polynomialamount of time steps, but on other inputs, it may take an infinitenumber of Turing machine steps!

You read it right - yes, our approach is to first distinguish between infiniteand finite numbers of steps, and if the latter, to detail whether the finite

8Now, traveling close to the speed of light is a relativistic notion - you may be “sittingperfectly still” on Galaxy #1 while Galaxy #2 may be traveling at the speed of light awayfrom you; observers on the latter will scoff at your Blue Gene computer!

Page 16: Computation Engineering Applied Automata Theory and Logic

8 CHAPTER 1. INTRODUCTION

number is polynomial, exponential, logarithmic, etc. - as a function of theinput length. Things that take an infinite number of steps are classified underthe heading “undecidable” while things that take a finite number of steps areclassified under the heading “decidable;” for decidable membership queries,we then talk about their associated complexity.

To sum up, time complexity is defined in terms of the number of Turingmachine steps it takes to decide membership of strings in languages. Simi-larly, space complexity is defined in terms of the number of Turing machinetape cells consumed while computing something. In these discussions, itwill, of course, be kept in mind that a Turing machine tape cell - space -is reusable - whereas time is not reusable. This simple fact actually has aprofound impact on complexity theory, as we shall see later; since we canreuse space but not time, space complexity figures tend to be lower.

Following similar principles as above, various researchers have studiedvarious other complexity measures, such as energy complexity, communica-tion complexity, wiring complexity9(the number of wires we need to stringwithin a computer), etc. We refer the interested reader to the references atthe end of this book.

Types of Deciders

From the “time-scale” defined above, it must be clear that there are manyoutcomes to membership queries in languages - some membership queriescan be answered by Turing machines taking, in the worst case, an infiniteamount of time, while some others can be finitely answered, perhaps takinga polynomial amount of time. What is the spectrum of answers one canobtain? Again, we go by a few examples to get an overall idea.

Suppose someone offers us a set S and asks us to build a “decider box”BS say, using one or more Turing machines, such that box BS can now beemployed to decide membership of candidate strings x, y, z, etc., within S.Suppose BS has two lights - green and red - where the green light is lit incase x is a member of S, while the red light is lit in case x is not a memberof S. The spectrum of possibilities are the following:

• For certain languages S, we cannot build the decider, BS. We cannoteven build a semi-decider - a ‘halfBS

.’ A semi-decider is guaranteed to

9Before the advent of telephone switches, the New York sky was filled with criss-crossingtelephone cables.

Page 17: Computation Engineering Applied Automata Theory and Logic

9

shine the green light in finite time in case x is a member of S. It ishowever not guaranteed to shine the red light in finite time in case x isnot a member of S.

Formally speaking, for this example at hand, we assert that“the decision problem of whether x ∈ S is neither TuringRecognizable (TR) nor co-Turing Recognizable (co-TR).”

• In some cases, we can build a semi-decider. Therefore, at least forx ∈ S, we can obtain an answer in finite time. However, for x /∈ S,halfBS

can still loop for some of the x.

Formally, “the decision problem of whether x ∈ S is TR butnot co-TR.”

• We can in fact build the decider BS that halts whether x ∈ S or not.However, we care not to make a statement about how long (in numberof TM steps) BS will take before it shines a light.

Formally, “the decision problem of whether x ∈ S is bothTR and co-TR.” (Being both TR and co-TR is tantamountto being decidable. If either not TR or not co-TR, we say“undecidable10.”)

• We can build the decider BS, but alas, in the worst case, it takes k|x|

units of time to get a light to shine (for some constant k). Here, |x|stands for the length of x.

Formally, “the decision problem of whether x ∈ S is decidablein exponential-time.”

• We can build the decider BS, but we don’t know whether it will takek|x| units of time or |x|k units of time to get a light to shine11. We dohowever know that these are the upper and lower bounds.

Formally, “the decision problem of whether x ∈ S is decidablein time complexity NP-hard.” Sometimes we will be able toalso say “both NP-hard and in NP” which is equivalent to“NP-complete.”

10Undecidable languages are often undesirable.11For a fixed constant k, k|x| and |x|k are widely different in magnitude. Try to calculate

these quantities for k = 2 and |x| = 100.

Page 18: Computation Engineering Applied Automata Theory and Logic

10 CHAPTER 1. INTRODUCTION

NP-hard, NP-complete etc. are names of complexity classes that allowus to confess our ignorance; in practice, they mean “don’t know ifpolynomial or exponential.”

• We can build a decider that runs in polynomial time, i.e., |x|k unitsof time. In this book, once we know something is we will probe nofurther. This is because we adopt the point of view that “polynomialis good; please don’t ask for anything more.” As you can imagine, moreadvanced books will keep probing further along various complexity axes(logarithmic time, space complexity, energy complexity, etc.) and evenask more detailed questions such as “Okay, it is polynomial, but is itthe kind that can be easily parallelized12?”

To sum up, the questions of decision and complexity are very naturally mod-eled using Turing machines.

Automata Theory and Computing

In this book, we approach the above ideas through automata theory. Whatis Automata Theory? Should its meaning be cast in concrete, or should itevolve with advances in computer science and computer applications?

In this book, we take a much broader meaning to the term ‘automatatheory.’ We will use it to refer to a comprehensive list of closely relatedtopics, including

• finite and infinite automata (together called “automata”),

• mathematical logic,

• computability and complexity (TR, co-TR, NP-complete, etc),

• formal proofs,

• the use of automata to decide the truth of formal logic statements,

• automata on infinite words to model the behavior of reactive systems,and last but not least

• applications of the above topics in formal verification of systems.

Automata theory is a ‘living and breathing’ branch of theory - not ‘fossilizedknowledge.’ It finds day-to-day applications in numerous walks of life, in theanalysis of computing system behavior. In this book, we present a number oftools to help understand automata theoretic concepts and also to illustrate

12Or is it the kind that can be easily paralyzed?

Page 19: Computation Engineering Applied Automata Theory and Logic

11

how automata theory can be applied in practice. We illustrate the use ofautomata to describe many real-life finite-state systems, including games,puzzles, mathematical logic statements, programming language syntax, errorcorrecting codes, and combinational and sequential digital circuits. We alsoillustrate tools to describe, compose, simplify, and transform automata13.

Why “mix-up” automata and mathematical logic?

We believe that teaching automata theory hand-in-hand with mathemati-cal logic allows us to not cover concepts pertaining to formal languages andmachines, but also illustrate the deep connections that these topics haveto the process of formalized reasoning - proving properties about comput-ing systems. Formalized reasoning about computing systems is escalatingin importance because of the increasing use of computers in safety criticaland life critical systems such as medical equipment, automobiles, telephonenetworks, financial applications, mobile robots, automatic drug delivery sys-tems implanted in the body of patients, powerplant control, to name a few.The software and hardware used in life and resource critical applications ofcomputers are becoming so incredibly complex that testing these devices forcorrect operation has become a major challenge. For instance, while per-forming 100 trillion tests sounds more than adequate for many systems, itis simply inadequate for most hardware/software systems to cover all possi-ble behaviors. As real-life experience has shown, despite being “thoroughlytested,” it is possible to have radiation therapy equipment end-up runningincorrect software causing death to patients using them, instead of curingthem. It turns out that in order to prove that even the simplest of com-puting systems operate correctly, one has to examine so many inputs andtheir corresponding outputs for correctness. In many cases, it will take sev-eral thousands of centuries to finish complete testing. For example, if such acomputer has the equivalent of 256 binary inputs and 1 megabyte of internalstorage, one has to study the behavior of such a device on 210

6+256 vectors.(An input and its corresponding output are together called a test vector.) At1 micro-second per vector, this will take about 210

6+220 days.

13Contrary to the advise of P.G. Wodehouse, one of my heroes, I’ve “laced” this bookwith several footnotes, hoping to ‘break the ice,’ and make you believe that you are notreading a theory book. Hopefully I can hide behind Alonzo Church, another of my heroes,who is generous with footnotes!

Page 20: Computation Engineering Applied Automata Theory and Logic

12 CHAPTER 1. INTRODUCTION

Why verify? Aren’t computers “mostly okay?”

The human society is crucially dependent on software for carrying out anincreasing number of day-to-day activities. The presence of bugs in softwareis hardly noticed till, say, one’s PalmPilot hangs, when one pokes its resetbutton and moves on, with some bewilderment. The same is the case withmany other computers that we use; in general, the mantra seems to be, “resetand move on14!” However, this surely cannot be a general design paradigm(imagine a machine getting stuck in an infinite reboot loop if the problemdoes not clear following a reset). In the modern context, one truly has toworry about the logical correctness of software and hardware because therehave been many “close calls,” some real disasters, and countless dollars arebeing wasted in verifying products before they are released. In 2004, the MarsSpirit rover’s computer malfunctioned when the number of allowed open filesin Flash memory were exceeded. This caused a shadow of uncertainty to hangover the project for a few days, with scientists wasting their time finding acure for the problem. In 2003, the BMW car company had to recall manymodel 745i cars, fearing engine malfunction due to a software bug [?]. In1996, Ariane-5, a rocket costing $2B, self-destructed following an arithmeticoverflow error that initially caused the rocket nozzles to be pointed incorrectly[?]. In 1987, a radiation therapy machine called Therac-25 actually causedthe death of several patients [?]. In addition, there are many deep-seatedproblems that do not make the headlines. The root cause of many bugs isambiguous and/or inconsistent specifications for digital system componentsand subsystems. The human society is paying a huge price for this sloppiness.Software built based on such specifications is very likely to be flawed, hard todebug, and impossible to systematically test15. As an example from recentpractice, the specification document of a widely used computer bus calledthe ‘PCI’ [?] was shown to be internally inconsistent [?, ?].

The ability to write precise as well as unambiguous specifications is centralto using them correctly and testing them reliably. Such formal methods forhardware and software have already been widely adopted. Organizations suchas Intel, IBM, Microsoft, Sun, HP, JPL, NASA, and NSA employ hundreds

14Even the act of hitting the reset button has an automata-theoretic explanation: movethe machine from a state that it was ‘not supposed to reach’ to its initial state!

15It will be like designing boilers without engineering specifications or calculations - andsuch boilers did frequently explode. Computer Science is still in the era of “explodingboilers.”

Page 21: Computation Engineering Applied Automata Theory and Logic

13

of formal verification specialists. Unfortunately, there are many areas wherethe power of formal methods has not been demonstrated. This book hopesto expose students to the basics of formal methods so that they will be ableto participate in as well as contribute to the formal methods revolution thatis happening.

Verifying computing systems using automaton models

How does automata theory help ensure that safety critical systems are cor-rect? First of all, it helps create abstract models for systems. Many systemsare so complex that each vector in such systems consists of thousands, if notmillions, of variables. If one considers going through all the 21000 assignmentsfor the bits in the vector, the process will last thousands of millenia. If oneruns such tests for several days, even on the fastest available computer, andemploying a good randomization strategy in vector selection, one would stillhave covered only some arbitrary set of behaviors of the system. Vast ex-panses of its state-space would be left unexamined. A much more effectiveway to approach this problem in practice is to judiciously leave out most ofthe bits from vectors, and examine the system behavior exhaustively over allthe remaining behaviors. (In many cases, designers can decide which bitsto leave out; in some cases, computer-based tools can perform this activity.)That is, by leaving out the right set of bits, we end up covering an abstractmodel fully. Experience has repeatedly shown that verifying systems in boththese ways - by first subjecting it to a ‘good whipping’ by running a wholeslew of random vectors, followed by running through its abstracted modelexhaustively - reveals far more errors than either technique applied in isola-tion.

It turns out that abstracted models of most systems are finite automata.There are a number of techniques being developed that can represent aswell as manipulate very large finite automata16. These techniques helpminimize the degree to which systems have to be abstracted before they canbe exhaustively verified. This, in turn, means that the risk of accidentallymissing an error due to overly heavy abstractions is also reduced.

16The ability to represent and manipulate very large automata is also central to manyother areas of computer science such as representing very large dictionaries of terms fornatural language processing, for parsing, etc.

Page 22: Computation Engineering Applied Automata Theory and Logic

14 CHAPTER 1. INTRODUCTION

Automaton/Logic connection

Automata theory includes the very deep connections that finite-state ma-chine have with mathematical logic. Mathematical logic helps precisely andunambiguously describe both system requirements and system behaviors. Theimportance of precision and clarity in system descriptions cannot be over-stated. Many system description documents rival Los Angeles telephonedirectories in their size, containing a very large number of subtle assertionsthat tax the human mind17. Each person reading such a document comesup with a different understanding. While engineers are incredibly smart andare able to correct their misunderstandings more often than not, they stillwaste their time poring over lengthy prose fragments that have highly vari-able information content. It has been widely shown that formal statementsin mathematical logic can be very effectively used to capture the precisionmissing in text-based documents. Please note that we are not talking aboutreplacing text-based documents using formal mathematical logic based spec-ifications, but rather supplementing them.

Another problem with natural-language based system descriptions is thatengineers using them cannot effectively answer “what if” questions about sce-narios that are not explicitly discussed. An example from the I/O systemworld is “What if I/O transaction x is allowed to overtake transaction y?Does it cause wrong answers to be returned? Does it cause system dead-locks?” The number of potentially interesting “what if” questions pertain-ing to any real system are extremely numerous. A specification documentcannot hope to have all such questions and their answers listed in a docu-ment18. Instead, what is absolutely needed (to have this capability) is to beable to state the specification of the I/O bus in a precise language (such asmathematical logic) and let the engineer pose his/her questions also in math-ematical logic (we refer to these as putative or challenge queries). Then, usingthe power of mathematical logical reasoning systems, we can mechanicallydecide or deduce the consequences of the what-if questions. If an engineerposes a conjecture, the system can try to either show that the conjecture istrue, or show why it is false by creating a witness for when the conjecturecan be false. The ability to decide - provide an answer, without looping, for

17Engineers trying to implement drivers for modern computing system input/outputbusses are known to complain of nausea and dizziness after trying to read lengthy descrip-tions of modern busses!

18Doing so would result in not just a telephone directory, but an entire city library!

Page 23: Computation Engineering Applied Automata Theory and Logic

15

all possible putative queries is, of course, a luxury enjoyed when we employsimpler mathematical logics. Unfortunately, simpler mathematical logics areoften not as expressive, and we will then have to deduce, using partly man-ual steps and partly automatic (decidable) steps, the answer. This topic -expressiveness versus decidability - is now further discussed.

Decidability and Complexity

Automata theory (as we describe) helps obtain powerful connections betweenmathematical logic statements and decision algorithms - algorithms to auto-matically answer putative queries. For many highly expressive logics such ashigher order logic, obtaining such answers can be formally shown to be un-decidable. However, as just pointed out, even such logics are highly useful inpractice, as they help write succinct and clear descriptions of system behav-ior. Many logics (such as first-order logic) are such that they have associatedsemi-decision procedures: for all conjectures that are true, these proceduresare capable of discovering the “yes” answer. However, for conjectures thatare false, these procedures may go into an infinite loop while trying to tacklethe decision problem. (In short, they have associated halfBS

boxes.) Sincethe user usually has no a priori knowledge of what to expect regarding thetruth or falsity of a typical first-order logic formula, they have no better op-tion available than to submit their formula to the semi-decision procedure,‘and hope for the best.’

It is an amazing feat of human intelligence (captured in concepts such aslogic and decidability) that we can formally prove that there are limits to thecapabilities of algorithms! Equally amazing is the fact that we can formallyprove, for certain logics, that no procedure to decide truth that also avoidslooping can exist. Through such studies, one can identify the “right” logicfor a class of applications. Such logics have the right amount of expressivepower while having procedures that obtaining conclusive answers in the mostnumber of cases. In fact, going down the hierarchy of logics, one can soonfind logics for which decision algorithms exist - algorithms that, for everyconjecture, either return a ‘yes’ or a ‘no’ - never going into a loop. However,the run-time that these algorithms take can be very high! In some cases,the run-time grows with the “towers of two” complexity: for a conjecture

formula of length (size) n, the run-time is proportional to 2222

...22

where the

Page 24: Computation Engineering Applied Automata Theory and Logic

16 CHAPTER 1. INTRODUCTION

height19of this “tower” is n. Finally, for some of the simplest logics in thisspectrum, called zero-th order or propositional logic, the run-time can still beexponential. In fact, it is not known whether the run-time can be polynomial.This is the much celebrated “P versus NP” problem that remains, despitedecades of research, an open problem. Automata theory allows us to studythis fascinating array of topics in a cohesive manner, paving the foundationsfor machine assisted reasoning in every walk of computing.

Avoiding wasted work through the use of automata

The gist of the discussion so far is that automata theory is an importantbranch of the engineering mathematics behind building computer systems.Engineering is concerned with cost/effort minimization. Automata are em-ployed to abstract away from actual designs that are too detailed and com-plicated to model in their entirety. As explained earlier, it is remarkable thatvirtually all computing systems can be modeled using one of three principlevarieties of automata: deterministic finite automata (DFA), push-down au-tomata (PDA), or Turing machines (TM). In these models, we also abstractaway from details such as absolute time, using a very coarse scale to measuretime. Often, we measure the runtimes of computations using a binary scale:either the computation takes a finite amount of time, or it takes an infiniteamount of time.

What is the point of creating such whole-scale abstractions? The reasonis quite simple to state, but nevertheless profound in effect: If we can provethat there cannot be a decider for some task, then there is no point wastingeveryone’s time in pursuit of an efficient solution. On the other hand, ifwe prove that an algorithm does exist, we can switch to a much less severeabstraction. We can then safely deploy “armies of CS students” in pursuit ofan efficient algorithm. In this sense, automata theory helps researchers andengineers avoid wasted work, and channel their energies in those areas whereit actually makes sense to do so.

So, the next time your boss asks you to produce a C-grammar equivalencechecker that checks the equivalence between any two arbitrary C-grammarfiles (say, written in the language Yacc or Bison) and takes no more than “asecond per grammar-file line,” don’t waste your time coding - simply prove

19You may wish to evaluate this quantity for “towers of various heights.” It has beenobserved that in practice this complexity can often be avoided.

Page 25: Computation Engineering Applied Automata Theory and Logic

17

that this task is impossible20!

Solving one implies solving all

There is another sense in which automata theory helps avoid work. In manyof these cases, researchers have found that while we cannot actually solvea given problem, we can gang up or “club together” thousands of problemssuch that the ability to solve any one of these problems gives us the ability tosolve all of these problems. Often the solvability question will be whether it istractable to solve the problems - i.e., solve in polynomial time. In many cases,we simply are interested in solvability - without worrying about the amountof time. In these cases also, repeated work is avoided by grouping a collectionof problems into an equivalence class and looking for a solution to only oneof these problems; this solution can be easily modified to solve thousands ofpractical problems. This is the motivation behind studying NP-completenessand related notions. We will also study the famous Halting problem andproblems related to it by taking this approach of clubbing problems togetherthrough the powerful idea of mapping reductions.

Really use all that theory?

Results in automata theory are the fruits of tireless toil by thousands ofresearchers for more than a century. Automata theory and logic are part ofthe enduring heritage of computer science - as opposed to computer hardwareand software that keep becoming obsolete at a rapid rate. Will people reallyuse “all that theory?” Why in fact bother to ensure software quality whenit may be more profitable to take “the path of least resistance,” and sellsomething that is erroneous (and later also sell a “software patch”)? Thissarcastic sentence is meant to call attention to the fact that computer scienceoften struggles to evolve amidst all that commercial hoopla. However, it isgratifying that research funding agencies as well as large corporations havetaken a long-term perspective towards the growth of the discipline. As NancyLeveson points out, “the emergence of an engineering discipline should not bepredicated on accumulated evidence in the form of disasters.” Unfortunately,computer science, has had more than enough of these in our opinion. Many ofthe disasters of computer science are not crashes of rockets or chips (although

20You may wish to state that the difference between Bison and Yacc is that one cannotwash one’s face in a Yacc!

Page 26: Computation Engineering Applied Automata Theory and Logic

18 CHAPTER 1. INTRODUCTION

such disasters have happened21), but rather, disasters of the following nature:

• software engineering projects that more often fail and/or slip theirschedule than not;

• software structures that keep getting reinvented rather than reused;

• software systems that cannot be upgraded because of poor documen-tation.

Bibliographic references at the end of this chapter have some pointers forfurther reading. If computer science were to become a proper engineeringdiscipline, it is essential that theories such as automata theory and logic beused to erect firm and secure foundations on which to base system design,develop coding standards, and documentation methods to avoid the abovelisted disasters which currently plague computer system design.

A mind-expanding subject

Last but not least, the study of automata theory is a challenging, mindexpanding endeavor. It exposes students to a variety of mathematical models,and helps build confidence in them, thus encouraging them to be creative, totake chances, and to tread new ground while designing computing systems.Through constant practice, it helps develop the ability to study proofs, andsee flaws in them. In the same manner, it helps designers be better at readingand following through complex system descriptions, giving them the abilityto uncover many flaws by reading and deeply thinking about the correctnessof system operation. The ability to characterize one’s own designs preciselyand formally is a very satisfying experience. It gives the engineers’ pridein their creation an extra boost by giving them the satisfaction that theyhave precisely, unambiguously, and most usefully described their designs. Itmakes them aware that future users or modifiers of the system will not cursethem for having created an inscrutable object, but rather sing praises as tohow clear and clean the design is. Value judgements such as “easy” and“difficult” are simply a reflection of how clear (or otherwise) something isdescribed. Merely by virtue of being described in a crystal-clear manner,

21The Ariane rocket, worth $2B, was lost because of incorrect version of software run-ning. Intel lost $0.5B due to a floating-point bug in their Pentium II microprocessor.

Page 27: Computation Engineering Applied Automata Theory and Logic

19

a complex system will suddenly end up being perceived as a “neat” and“elegant” system!

Automata theory demands a lot from you!

It is impossible to learn automata theory “in a hurry.” One simply has to givethe subject its dues by working very hard. I have witnessed many studentswho miss some lectures of automata theory, do not take any extra effortto catch-up promptly, and try to reappear in class only to find themselvesutterly lost. While the subject is quite simple and intuitive in hindsight,to get to that stage is simply not easy. The strongest advice I have is tobe regular, diligent, patient, want to learn the subject, and approach it withgusto, sorting out problems/misunderstandings with the highest promptness.Some problems may take days or even weeks to solve. If we tell you to beginsolving them the instant it is assigned to you, we really mean it. Unlike manyother topics in applied computer science, it is almost impossible to “pull anall-nighter” and get anywhere, except emerge feeling mush-brained.

To summarize, the best advice I have to offer is to attempt problems assoon as they are assigned, and allow enough time for the problems to ‘gestate’in your minds. After repeatedly trying and failing, you will be able to carrythe problems in your minds. You may end up solving some of them in theshower22.

A brief foray into history

Let us take a historical perspective, to look back into how this subject wasviewed by two of the originators of this subject - Michael Rabin and DanaScott - in their 1959 paper (that, incidentally, is cited in their ACM Turingaward citation [?, ?]):

Turing machines are widely considered to be the abstract pro-totype of digital computers; workers in the field, however, havefelt more and more that the notion of a Turing machine is toogeneral to serve as an accurate model of actual computers. It iswell known that even for simple calculations, it is impossible togive an a priori upper bound on the amount of tape a Turing

22You may wish to warn your family members that you may one day bolt out of theshower, all soaking wet, shouting “Eureka!”

Page 28: Computation Engineering Applied Automata Theory and Logic

20 CHAPTER 1. INTRODUCTION

machine will need for any given computation. It is precisely thisfeature that renders Turing’s concept unrealistic.

In the last few years, the idea of a finite automaton has appearedin the literature. These are machines having only a finite numberof internal states that can be used for memory and computation.The restriction of finiteness appears to give a better approxima-tion to the idea of a physical machine. [...]. Many equivalentforms of the idea of finite automata have been published. Oneof the first of these ws the definition of “nerve-nets” given byMcCulloch and Pitts. [...]

In short, Rabin and Scott observe that the theory of Turing Machines, whileall encompassing, is “too heavy-weight” for day-to-day studies of computa-tions. They argue that perhaps finite automata are the right model of mostreal-world computations. From a historical perspective, the more complexmachine form (Turing machine) was proposed much before the much simplermachine form (finite automata). All this is clearly not meant to say thatTuring machines are unimportant. Rather, the message is that a more bal-anced view of the topics studied under the heading of automata will help onebetter appreciate how this area came about, and how the priorities will shiftover time. In the same manner as Scott and Rabin anticipate important newdirections, it is quite likely that models such as quantum computing [?] ormodels more suitable for real-numbers [?] might play an increasing role infuture automata theory books.

Disappearing Formal Methods

In the article “disappearing formal methods [?],” John Rushby points outhow throughout the history of engineering various new technologies had been(over) advertised, till they became widely accepted and taken for granted.For instance, many olden day radios had a digit (‘6’ or ‘8’) boldly written ontheir aluminum cases, advertising the fact that they actually employed 6 or8 (as the case may be) transistors. Centuries ago, differential and integralcalculus were widely advertised as the “magic” behind studying planetarymotions, as well as building everyday objects such as bridges. Today, nobodyhangs a sign-board on a bridge proclaiming “this bridge has been designedusing differential and integral calculus!” In the same vein, the term formalmethods is nowadays used as a “slogan” to call attention to the fact that we

Page 29: Computation Engineering Applied Automata Theory and Logic

21

are really using ideas based on logic and automata theory to design products,as opposed to previously when we used no such disciplined approach. Whilethe explicit advertisement of the technology behind modern developments isinevitable, in the long run when such applications are well understood, wewould no longer be pointing out explicitly that we are actually designing,say, floating-point units, using higher order logic.

Said another way, in a future book on automata theory, perhaps onecould then dispense with all these motivational remarks that have preventedus from heading towards the core of this book. Luckily, that is preciselywhere we are headed now.

Page 30: Computation Engineering Applied Automata Theory and Logic

22 CHAPTER 1. INTRODUCTION

Page 31: Computation Engineering Applied Automata Theory and Logic

Chapter 2

A Quick Overview

Please read this chapter! Take 10 minutes for a quick tour of the book(Section 2.1). This tour will give you a good perspective of the whole book.Follow this tour by taking a background assessment test (Section 2.2). Thistest will expose you to an assortment of topics related to the material in thisbook (many of these topics will be gone over in detail later in the book).

Please take the quizzes on “formal language intuitions,” “computabilityintuitions,” and “complexity intuitions.” I have been time and again pleas-antly surprised by the fact that even on the very first day of classes moststudents are able to answer these questions! It will encourage you immenselyto know that these are precisely the kinds of questions taken up for discussionin the rest of this book!

2.1 A Ten Minute Tour

Chapter 3 begins with the quintessentially important topics of sets, functions,and relations. After going through details such as expressing function signa-tures, the difference between partial and total functions, we briefly examinethe topic of computable functions - functions that computers can hope torealize within them. We point out important differences between the termsprocedure and algorithm, briefly touching on the 3x+1 problem - a four-lineprogram that confounds scientists despite decades of intense research. Inorder to permit you to discuss functions concretely, we introduce the lambdanotation. A side benefit of our introduction to Lambda calculus is that youwill be able to study another formal model of computation besides Turing

23

Page 32: Computation Engineering Applied Automata Theory and Logic

24 CHAPTER 2. A QUICK OVERVIEW

machines (that we shall study later).

Chapter 4 goes through the concept of cardinality of sets, which, in itselfis extremely mentally rewarding, and also reinforces the technique of proof bycontradiction. It also sets the stage for defining fine distinctions such as ‘alllanguages,’ of which there are “uncountably many” members, and ‘all Turingrecognizable languages,’ of which there are only “countably many” members.Chapter 5 discusses important classes of binary relations, such as reflexive,transitive, preorder, symmetric, anti-symmetric, partial order, equivalence,identity, universal, equivalence, and congruence (modulo operators).

The best way to study the material in Chapters 3, 4, and 5 is throughrepeated visits. Read the chapter carefully now, attempting all the assignedproblems, and as many extra ones that you can do. Later in a chapter whenyou see one of the notions introduced in this chapter being used, revisit thischapter and read over it again. Try coming up with your own examples thatillustrate the material in this chapter.

In Chapter 6, you will obtain an intuitive introduction to mathematicallogic. We have written this chapter with an eye towards helping you readdefinitions involving the operators if, if and only if (iff), and quantifiers forall, and there exists. The full import of definitions ‘laden with’ ifs and iffs,and the quantifiers is by no means readily apparent - and so, to cultivatesufficient practice is essential. You will see proof by contradiction discussedat a ‘gut level’ - we encourage you to play the game of Mastermind, where youcan apply this technique quite effectively to win pretty much every time1.You will see the quantifiers ∀ and ∃ discussed through ‘horse tails.’ We discussvarious principles of induction, including arithmetic, complete, noetherian,and structural. We formally prove the equivalence between the arithmeticand complete induction principles. We help you assemble these ideas inyour mind by stating and proving the pigeon-hole principle using proof bycontradiction and induction (an exercise courtesy of [?]). We then discussproofs themselves - given the ease of writing incorrect proofs and the elusivenature of correct and well-described proofs, this is a topic that merits a fewpages. Our message is: Write proofs clearly and lucidly - that way, in caseyou go wrong, others can spot the mistake and help you.

Chapter 7 studies the topic of recursion at some depth. Given that abook on automata theory is ‘ridden with definitions,’ one has to understand

1Assuming, of course, that your annoying sibling is not breaking into your concentrationby humming a cacophony or munching something near your ear.

Page 33: Computation Engineering Applied Automata Theory and Logic

2.1. A TEN MINUTE TOUR 25

carefully when these definitions make sense, when they end-up being ‘totalnonsense’ owing, say, to being circular, or not uniquely defining something.Chapter 7 is about the use of Lambda calculus to make sense of recursion.We present to you the ‘friendliest foray into fixed-point theory’ that we canmuster, encouraging you to play with your calculators, followed by experi-ments involving xeroxing your face on a xerox machine! Hopefully, these intu-itive introductions will help you appreciate the simple but profound messagebehind recursion - that it is about solving for the variable (usually a func-tion) being defined. Later, when we discuss context-free grammars, we cantest this concept by viewing context-free grammars as recursive equationsthat attempt to define a language. In fact, we will see that these recursiveequations have multiple “solutions,” but we can uniquely identify those thatare minimal.

In Chapter 8, we begin discussing the notions of strings and languages.We hope that the background material of the earlier chapters will makethe study of this chapter ‘a breeze.’ Please, however, pay special atten-tion2to “five most confused objects of automata theory,” namely ε, φ, ε,φ, and the equation φ∗ = ε. We discuss the notion of concatenation,exponentiation, ‘starring,’ complementation, reversal, homomorphism, andprefix-closure applied to languages.

In Chapter 9, we discuss machines, languages, and deterministic finiteautomata. We construct Deterministic Finite Automata (DFA) for severalexample languages. One problem asks you to build a DFA that scans anumber presented in any number-base b either most significant digit-first, orleast significant digit-first, and shine its green light exactly at those momentswhen the number scanned so far equals 0 in some modulus k. We will havemore occasion to examine the true “power” of DFAs in Chapters 13 and ??.Chapter 9 closes-off with a brief study of the limitations of DFA.

Chapter 10 discusses the kind of DFAs used for hardware design. It hasbeen my experience in the past that many students take courses on hardwaredesign and courses on “traditional automata theory” without integrating thematerial in their minds. This chapter tries to bridge this gap by showingthat with a slightly different (and perhaps a bit more utilitarian) perspectiveon the topic, we can connect what we have learnt to some of the practices inhardware design.

2Once you understand the fine distinctions between these objects, you will detect afaint glow around your head.

Page 34: Computation Engineering Applied Automata Theory and Logic

26 CHAPTER 2. A QUICK OVERVIEW

Chapter 11 continues these discussions, now examining the crucially im-portant concept of non-determinism. It also discusses regular expressions -a syntactic means for describing regular languages. The important topic ofNon-deterministic Finite Automaton (NFA) to DFA conversion is also exam-ined.

Automata theory is a readily usable branch of computer science theory.While it is important to obtain a firm grasp of its basic principles using paper,pencil, and the human brain, it is quite important that one use automatedtools, especially while designing and debugging automata. To paraphraseProf. Dana Scott, computer-assisted tools are most eminently used as thetelescopes and microscopes of learning - to see farther, to see closer, and tosee clearer than the human mind alone can discern. We demonstrate the useof grail tools to generate and verify automata. We close-off with a powerfulconcept - ultimate periodicity - that pretty-much sums up what DFA arecapable of.

In Chapter 12, we discuss operations that combine regular languages,most often yielding new regular languages as a result. We discuss the con-version of NFA to DFA - an important algorithm both theoretically and inpractice. We also discuss the notion of ultimate periodicity which crystallizesthe true power of DFAs in terms of language acceptance.

In Chapter 13, we will begin discussing binary decision diagrams (BDD),which are nothing but minimized DFA for the language of satisfying assign-ments (viewed as sequences, as well will show) for given Boolean expres-sions. The nice thing about studying BDDs is that it helps reinforce notonly automata-theoretic concepts but also concepts from the area of formallogic. It teaches you a technique widely used in industrial practice, and alsopaves the way to your later study of the theory of NP-completeness.

In Chapter 14, we discuss the ‘Pumping’ Lemma. We point out the notionof incomplete and complete Pumping Lemmas - the former can help disprovethat certain languages are regular, while the latter can be used to eitherdisprove or prove that certain languages are regular. While we present thebasic ideas behind the Pumping Lemma as a two-person game, we present theactual application of the lemma in first-order logic, including the refutationproof achieved using it.

In Chapter 15 we present the idea of context-free languages. Context-freelanguages are generated by a context-free grammar that consists of productionrules. By reading production rules as recursive equations, we can actuallysolve for the context-free language being defined! The fixed-point theory of

Page 35: Computation Engineering Applied Automata Theory and Logic

2.1. A TEN MINUTE TOUR 27

Chapter 7 will be the basis for solving the recursive equations. We will alsohave occasion to prove, via induction, that context-free productions are soundand complete - that they do not generate a string outside of the language,but do generate all strings within the language. The important notions ofambiguity and inherent ambiguity will also be studied.

Chapter ?? will introduce our first infinite state automaton variety - thepush-down automaton (PDA) - a device that has a finite-control and exactlyone unbounded stack. We will study a method to inter-convert PDA to CFGand vice versa.

In Chapter ??, we study Turing machines (TM). Turing machines willbe contrasted with other machine varieties such as random-access machines(which ‘normal’ computers are). Important notions, such as instantaneousdescriptions (ID) are introduced in this chapter. Several Turing machinesimulators are available on the web - you are encouraged to download andexperiment with them. In this chapter, we will also introduce linear boundedautomata (LBA) which are TMs where one may deposit new values only inthat region of the tape where the original input was presented. We will showthat the notion of context-sensitivity is precisely captured by LBAs.

Chapter 18 discusses some of the most profound topics in computer sci-ence - the halting problem, the notion of semi decision procedures, and thenotion of algorithms. With the introduction of unbounded store, many deci-sion problems will become formally undecidable. In a large number of cases,it no longer will be possible to predict what the machine will do. For exam-ple, it will become harder or impossible to tell whether a machine will haltwhen started on a certain input, whether two machines are equivalent, etc.In this chapter, we will formally state and prove many of these undecidabilityresults.

Chapter 19 sets the stage to discuss the theory of NP completeness. Ittouches on a number of topics in mathematical logic that will help you betterappreciate all the nuances. We briefly discuss a “Hilbert style” axiomatiza-tion of propositional logic, touching on soundness and completeness onceagain. We discuss basic definitions including satisfiability, validity, tautol-ogy, and contradiction. We discuss the various “orders” of logic includingzeroth-order, first-order, and higher order. We briefly illustrate that the va-lidity problem of first-order logic is only semi decidable by reduction fromthe Post Correspondence problem. We illustrate how to experiment withmodern SAT tools. We also cover related ideas such as 6=-sat, 2-sat, andsatisfiability-preserving transformations.

Page 36: Computation Engineering Applied Automata Theory and Logic

28 CHAPTER 2. A QUICK OVERVIEW

Chapter ?? discusses the notion of polynomial-time algorithms, compu-tational complexity, and the notion of NP-completeness. We shall examinea class of problems for which decision is guaranteed, but comes at a heavyprice - that of potentially paying an exponential time and/or space cost. Wewill also briefly examine the notion of P-Space completeness, and contrast itwith NP-completeness.

In Chapter ??, we introduce a small subset of first-order logic that enjoysthe following remarkable property: given a formula in this logic, we can builda finite automaton such that the automaton has an accepting run if and onlyif the formula is satisfiable. The neat thing about this technique is that itreinforces the automaton/logic connection introduced in the previous chap-ter. It also illustrates the use of automata in reasoning about parameterizedhardware: hardware systems defined for a whole range of sizes (for exampleN -bit adders for any value of N).

In Chapter ??, we introduce the so called omega automata. An omegaautomaton is a finite state automaton that accepts only infinitely long strings.(This means that all accepting runs have a suffix that fall within a cycle.) Theexact accepting condition is that the accepting run visit some accept statean infinite number of times. Such an unusual acceptance criterion turns outto be actually very useful. For example, suppose you have to model thesituation of an elevator in a building that perpetually evades the floor youare on. You can model this situation by treating all other floors as “acceptstates”; the elevator then infinitely visits these accept states, evading thestate represented by your floor. Omega automata are important in temporallogic based verification of systems.

Chapter ?? will review the material presented in the book. It will reiteratethe important results, proof techniques, and principles presented throughoutthe book. It will be very interesting to take stock of some of the importantrecurring themes throughout the book. They are: These include:

• proofs by contradiction;

• the use of mapping reductions to reduce known difficult problems tonew problems;

• the subtle use of mathematical logic assertions, in particular the use ofif and only if assertions;

• the technique of modeling problem solving in terms of language mem-bership checking; and

• various induction techniques.

Page 37: Computation Engineering Applied Automata Theory and Logic

2.2. BACKGROUND ASSESSMENT 29

The book attempts to provide an Appendix containing a list of conceptscovered, a number of exercises, and some suggestions for further reading.

2.2 Background Assessment

This book assumes that you have done basic courses on discrete structures,programming, software, and hardware design. Below, we offer a list of self-assessment problems from a variety of topics. Later chapters will most likelyvisit these topics again.Basic Syntax Later chapters will examine context-free grammars and pars-ing. Here are two simple problems to get your mind going in that regard.

1. Parenthesize the following expression according to the rules of stan-dard precedence for arithmetic operators, given that stands for unaryminus:

1 ∗ 2− 3− 4/ 5

2. Convert the above expression to Reverse Polish form.

3. Briefly describe an algorithm to convert infix expressions to ReversePolish.

Sets Sets are fundamental mathematical data structures.

1. What is the set described by the expression 1, 2, 3∩1, 2∪2, 4, 5?∩ has higher precedence than ∪

2. What is the symmetric difference of 1, 2, 3 and 2, 4, 5?

3. How many elements are there in the following set:

φ ∪ φ ∪ 2, φ

φ denotes the empty set. Also, sets may contain other sets.

Undirected Graphs Graphs are central to computing. We shall look atmany decision problems pertaining to graphs later on.

1. Draw an undirected graph of five nodes named a, b, c, d, e such thatevery pair of nodes have an edge between them. (Such graphs arecalled “cliques” - the above being a 5-clique.)

Page 38: Computation Engineering Applied Automata Theory and Logic

30 CHAPTER 2. A QUICK OVERVIEW

2. What is the number of edges in an n-clique?

3. Which n-cliques are planar (for what values of n)?

4. What is a Hamiltonian cycle in an undirected graph?

Directed Graphs

1. Draw a directed graph G with nodes being the subsets of set 1, 2, andwith an edge from node ni to node nj if either ni ⊆ nj or |ni| = |nj|.|S| stands for the size or cardinality of set S.

2. How many strong components are there in the above graph? A strongcomponent of a graph is a subset of nodes that are pairwise connected(reachable from one another).

3. What is the asymptotic time complexity of telling whether a directedgraph has a cycle?

Relations

1. Give two examples (each) of a binary relation and a ternary relationover Natural numbers. Natural numbers, or Nat, are 0, 1, 2, . . ..

2. View the above graph G as a binary relation RG. Write down the pairsin this relation. A relation is written as

〈a0, b0〉, 〈a1, b1〉, . . .

where 〈ai, bi〉 are the pairs in the relation.

Functions

1. Is RG a functional relation? Why, or why not? A functional relationis one for which for one ‘input’, at-most one ‘output’ is produced. Inother words, if 〈ai, bj〉 and 〈ai, bk〉 are in the relation, bj = bk.

2. Define the terms “domain” and “range” of functions. What are the do-mains and ranges of the sin function? How about for the tan function?

3. Point out at least two key differences between the sin and tan functions.

Induction

Page 39: Computation Engineering Applied Automata Theory and Logic

2.2. BACKGROUND ASSESSMENT 31

1. Prove by induction that the sum of 1 through N is N(N + 1)/2.

2. Obtain an expression for the total number of nodes in a balanced k-arytree. Prove this result by induction.

Recursion

1. Write a recursive program (pseudo-code will do) to traverse a binarytree in postorder.

2. Write a recursive program to solve the Towers of Hanoi problem.

3. Describe a recursive descent parser for arithmetic expressions.

4. Describe a recursive descent parser for Reverse Polish expressions.

Boolean Algebra

1. Simplify x + ¬xy. Here, + stands for or; sometimes we will use ∨instead.

2. Simplify the following propositional logic formula:

¬(a¬b)⇒ ¬(a¬b).

x ⇒ y, or “x implies y” means ¬x ∨ y. Expressions of the form a.bstand for a ∧ b; they will often be abbreviated as ab.

Propositional Logic

1. Describe what the following terms mean:

(a) axiom

(b) rule of inference

(c) theorem

(d) satisfiable

(e) proof

(f) tautology

Page 40: Computation Engineering Applied Automata Theory and Logic

32 CHAPTER 2. A QUICK OVERVIEW

2. Show that the following is a tautology:

(¬x⇒ ¬y)⇔ (y ⇒ x)

Recall that x ⇔ y or “x equals y” or “x if and only if y” or “x iff y”means (x⇒ y) ∧ (y ⇒ x).

Combinatorics

1. A Hamiltonian cycle in a graph with respect to a given node n is a tourthat begins at n, visits all other nodes exactly once, returning to n. Ina 5-clique, how many distinct Hamiltonian cycles exist? How about inan n-clique?

2. How many different ways can n pigeons occupy n − 1 pigeon holes?Assume that the pigeon holes are undistinguished (they don’t have anidentity; so, if you put two pigeons in hole 1 and one pigeon in hole 2,that’s the same as putting two pigeons in hole 2 and one in hole 1).

Data Structures

1. What is a Hash table? When are they used?

2. What is the complexity of checking whether two undirected graphs areisomorphic?

3. Suppose we write a program that traverses a “tape” of n cells, num-bered 1 through n. The program performs n traversals of the tape, withthe ith traversal sequentially examining elements i through n. What isthe runtime of such a program in the Big-O notation?

Algorithms

1. Let k = 2. Compute xk (polynomial) kx (exponential) complexitygrowth for x = 1, 2, 5, 10, 50 and 100.

2. Compute 2222

...22

(i times) for various i. (Note how to read this tower:begin with 2, and keep taking ‘2previous’.)

3. Present a recursive algorithm to mirror a binary tree “along the yaxis.” First, write a solution outline as follows: “Mirroring an emptytree results in itself. Mirroring a non-empty tree rooted at node n withleft sub-tree L and right sub-tree R results in a tree rooted at n withleft sub-tree . . ..” Then provide the pseudo-code.

Page 41: Computation Engineering Applied Automata Theory and Logic

2.2. BACKGROUND ASSESSMENT 33

4. Present the pseudo-code of an algorithm to reverse a directed graphrepresented by an adjacency matrix. A directed graph is reversed byreversing all its edges.

Software Design

1. Write pre and post conditions for a Binary search routine to search fora given item in an array of size N , returning the item if present, and−1 if the item is not present. (Assume that none of the items enteredinto the array are −1.)

2. What is the purpose of writing requirements specifications before de-signing a software system? How are the requirements specificationsused after the system has been designed and coded-up?

3. Propose a method to test a Binary-search routine so that all its pathsand “corner cases” are covered a reasonable number of times.

Hardware Design

1. Minimize the Boolean expression (¬a + ab)c + ac using a Karnaughmap.

2. A field-programmable gate-array realizes Boolean functions via mul-tiplexors. How many Boolean functions can you build with a 4-to-1multiplexor that has two select inputs?

Formal Language Intuitions If you can solve the following problems, youare well on your way to understanding the material in Chapter 15 already:

1. Suppose you are given a set of strings of the following form

Lijk = aibjck | i, j, k ≥ 0 and if odd(i) then j = k

In other words, if an odd number of as are seen at first, then an equalnumber of js and ks must be seen later. Provide the pseudo-code ofan algorithm that uses exactly one unbounded stack and a fixed set ofother variables (the total number of bits in these variables is arbitrary- but must be fixed once chosen), and filters out all those strings notin Lijk.

Page 42: Computation Engineering Applied Automata Theory and Logic

34 CHAPTER 2. A QUICK OVERVIEW

2. Answer the above question if the language is changed to

Lijk1 = bjckai | i, j, k ≥ 0 and if odd(i) then j = k

3. Answer the above question if the language is changed to

Lijk1 = aibjckdl | i, j, k, l ≥ 0 and if odd(i) then j = k else k = l

4. Answer the above question if the language is changed to

Lijk1 = bjckdlai | i, j, k, l ≥ 0 and if odd(i) then j = k else k = l

Complexity Intuitions

1. Suppose you walk into a class-room and find a large number of stu-dents seated, patiently awaiting your arrival. Your task is to determinewhether k of them know each other pair-wise. You must accomplishthis task by asking some randomly chosen k of them the question “doyou all know each other,” and then patiently listening to each one ofthe k answer “yes” or “no.” If there are indeed a subset k of suchpeople in the class who know each other, and if this is your lucky day(so your random choice was perfect), how much time would you taketo determine the answer?

2. Suppose the task were changed to “find out whether there exist k who,mutually, do not know each other.” Your approach is still required tobe to pick a random k of them and ask “do any of you know each otherpairwise.” In the best case, how long would you take to answer thisquestion? Assume a class size of N >> k.

Just for fun I heard this one on NPR radio in the “Car Talk” show. A manand his wife went to a party where a total of 52 people attended. The partywent well, with everyone mingling and shaking hands (the exact number ofsuch handshakes, nor who shook whose hands is not known; also, a personshaking his/her own hands does not count as a handshake). However, whilereturning from the party, the man told the wife “Gee, what a nice party!”The wife replied “Yes! And, I know for fact that there were at least twopeople (let’s call them A and B) who shook the same number of hands.”Prove this statement for 52, and then prove it for any N , stating all stepsclearly.

Page 43: Computation Engineering Applied Automata Theory and Logic

2.2. BACKGROUND ASSESSMENT 35

One more for good measure! This was narrated to me by one of myfaculty colleagues. Someone starts somewhere on earth, walks 10 miles south,then 10 miles west, and finally 10 miles north and returns to where he startedfrom. Where (all) could he have started such a journey from?

Page 44: Computation Engineering Applied Automata Theory and Logic

36 CHAPTER 2. A QUICK OVERVIEW

Page 45: Computation Engineering Applied Automata Theory and Logic

Chapter 3

Mathematical Preliminaries

In this chapter, we introduce many of the fundamental mathematical ideasused throughout the book. We first discuss sets, which help organize “things”into meaningful unordered collections. We then discuss functions which helpmap things to other things. Next, we discuss relations that relate things.We provide a concrete syntax, that of Lambda Expressions, for writing downfunction definitions. We then present ways to “count” infinite sets througha measure known as cardinality.

3.1 Numbers

We will refer to various classes of numbers. The set Nat, or natural numbersrefers to whole numbers greater than or equal to zero, i.e., 0, 1, 2, . . .. The setInt, or integers refers to whole numbers, i.e., 0, 1,−1, 2,−2, 3,−3 . . .. Theset Real, or real numbers refers to both rational as well as irrational numbersincluding 0.123,

√2, π, 1, −2, . . ..

Exercise 3.1 Prove that√2 is irrational. Hint: The methods in Section 6.1.3.

3.2 Sets

A set is a collection of things. For example, A = 1, 2, 3 is a set containingthree natural numbers. The order in which we list the contents of a set doesnot matter. Thus, A = 3, 1, 2 is the same set as above. A set cannot have

37

Page 46: Computation Engineering Applied Automata Theory and Logic

38 CHAPTER 3. MATHEMATICAL PRELIMINARIES

duplicate elements. Thus, B = 3, 1, 2, 1 is not a set1.A set containing no elements at all is called the empty set, written , or

equivalently φ. A set may also consist of a collection of other sets, as in

P = , 1, 2, 3, 1, 2, 1, 3, 2, 3, 1, 2, 3.

The above set P has a special status: it contains every subset of set A. P isin fact the powerset of A. We will have more to say about powersets soon.

3.2.1 Defining Sets

Sets are specified using the set comprehension notation

S = x ∈ D | p(x).

Here, S includes all x from some universe D such that p(x) is true. p(x)is a Boolean formula called characteristic formula. p by itself is called thecharacteristic predicate. We can leave out D if it is clear from the context.Examples:

Set A, described earlier, can be written as

A = x | x = 1 ∨ x = 2 ∨ x = 3.

For any set D,x ∈ D | true = D.

Notice from this example that the characteristic formula can simply betrue, or for that matter false.

For any D,x ∈ D | false = φ.

The powerset, P , introduced earlier can also be written as

P = x | x ⊆ 1, 2, 3.

Notice that we must use ⊆ to allow the set 1, 2, 3 to be a member ofP . If we use ⊂, we cannot include 1, 2, 3.

1An unordered collection with duplicates, such as B, is called a multi-set or bag. Wedo not discuss this notion any further in this book.

Page 47: Computation Engineering Applied Automata Theory and Logic

3.2. SETS 39

Exercise 3.2 What is the set defined by

P = x ∈ Nat | 55 < 44.

Exercise 3.3 What is the set defined by

P = x ∈ Nat | nor(false, false).

The next two sections illustrate that care must be exercised in writingset definitions. The brief message is that by writing down a collection ofmathematical symbols, one does not necessarily obtain something that iswell defined. Sometimes, we end up defining more than one thing withoutrealizing it (the definitions admit multiple solutions).

3.2.2 Avoid Contradictions

What characteristic formulae are allowed in set definitions? In most cases,we will be using simple ones that choose the elements from the domain us-ing straight-forward Boolean formulas. In this section we illustrate a fewinstances of making poor choices of characteristic formulae that result in theset in question either not being defined, or not being defined uniquely.

The first example has historical significance in set theory. It illustrateswhat is known as the Russell’s Paradox. It stems from allowing expressionssuch as x ∈ x and x /∈ x inside characteristic formulae. Consider somearbitrary domain D. Define a set S as follows:

S = x ∈ D | x /∈ x.

Now, the expression x /∈ x reveals that x itself is a set. Since S is a set, wecan now ask “is S a member of S?”

• If S is a member of S, it cannot be in S, because S cannot contain setsthat contain themselves.

• However, if S is not a member of S, then S must contain S2!

The proposition S ∈ S must produce a definite answer - true or false. How-ever, both answers led to a contradiction.

2Contradictions are required to be complete, i.e., apply to all possible cases. Forexample, if S /∈ S does not result in a contradiction, that, then, becomes a consistentsolution. In this example, we fortunately obtain a contradiction in all the cases.

Page 48: Computation Engineering Applied Automata Theory and Logic

40 CHAPTER 3. MATHEMATICAL PRELIMINARIES

We can better understand this contradiction as follows. Suppose S ∈ S.This allows us to conclude that S /∈ S. In other words, (S ∈ S)⇒ (S /∈ S) istrue. In other words, ¬(S ∈ S) ∨ (S /∈ S), or (S /∈ S) ∨ (S /∈ S), or (S /∈ S)is true. Likewise, (S ∈ S) ⇒ (S /∈ S). This allows us to prove (S ∈ S)true. Since we have proved S ∈ S as well as S /∈ S, we have proved theirconjunction, which is false! With false proved, anything else can be proved(since false ⇒ anything is ¬(false) ∨ anything, or true). Thus, once acontradiction is arrived at, virtually anything else can be proved.

Russell’s paradox is used to conclude that a “truly universal set” – a setthat contains everything – cannot exist. Here is how such a conclusion isdrawn. Notice that set S, above, was defined in terms of an arbitrary setcalled D. Now, if D were to be a set that contains “everything,” a set suchas S must clearly be present inside D. However, we just argued that S mustnot exist, or else a contradiction will result. Thus, a set containing everythingcannot exist, for it will lack in at least S. This is the reason why the notionof a universal set is not an absolute notion. Rather, a universal set specificto the domain of discourse is defined each time. This is illustrated below inthe section devoted to universal sets. In practice, we disallow sets suchas S by banning expressions of the form x ∈ x, etc. Fortunately, thetypes of elements we allow as members of various sets automatically preventsuch expressions in most cases. For instance, if s is a string, then s ∈ s is anill-typed expression.

3.2.3 Ensuring uniqueness of definitions

When a set is defined, it must be uniquely defined. In other words, wecannot have a definition that does not pin down the exact set being talkedabout. To illustrate this, consider the “definition” of a set

S = x ∈ D | x ∈ S,where D is some domain of elements. In this example, the set being defineddepends on itself. The circularity, in this case, leads to S not being uniquelydefined. For example, if we select D = Nat, and plug in S = 1, 2 on bothsides of the equation, the equation is satisfied. However, it is also satisfiedfor S = φ, S = 3, 4, 5. Thus, by virtue of the above circular definition, wecannot pin down exactly what S is.

The message here is that one must avoid using purely circular definitions.However, sets are allowed to be defined through recursion. Chapter 5 explains

Page 49: Computation Engineering Applied Automata Theory and Logic

3.2. SETS 41

how recursion is understood, and how sets can be uniquely defined eventhough “recursion seems like circular definition.”

Operations on Sets

Sets support the usual operations such as membership, union, intersection,subset, powerset, cartesian product, and complementation. x ∈ A means x isa member of A. The union of two sets A and B, written A∪B, is a set suchthat x ∈ (A∪B) if and only if x ∈ A or x ∈ B. In other words, x ∈ (A∪B)implies that x ∈ A or x ∈ B. Also, x ∈ A or x ∈ B implies that x ∈ (A∪B).Similarly, the intersection of two sets A and B, written A ∩ B, is a set suchthat x ∈ (A ∩B) if and only if x ∈ A and x ∈ B.

A proper subset A of B, written A ⊂ B, is a subset of B different fromB. A ⊆ B, read ‘A is a subset of B’, means that A ⊂ B or A = B. Notethat the empty set has no proper subset.

Subtraction, Universe, Complementation, Symmetric Difference

Given two sets A and B, set subtraction, ‘\’, is defined as follows:

A \B = x | x ∈ A ∧ x /∈ B.

Set subtraction basically removes all the elements in A that are in B. Forexample, 1, 2 \ 2, 3 is the set 1. 1 survives set subtraction because itis not present in the second set. The fact that 3 is present in the second setis immaterial, as it is not present in the first set.

For each type of set, there is a set that contains all the elements of thattype. Such a set is called the universal set. For example, consider the set ofall strings over some alphabet, such as a, b. This is universal set, as far assets of strings are concerned. We can write this set as

Sigmastar = ε, a, b, aa, ab, ba, bb, aaa, aab, . . ..

Here, ε is the empty string, commonly written as "", a and b are strings oflength 1, aa, ab, ba, and bb are strings of length 2, and so on. While discussingnatural numbers, we can regard Nat = 0, 1, 2, . . . as the universe.

Universal sets help define the notion of complement of a set. Consider theuniversal set (or “universe”) Sigmastar of strings over some alphabet. Thecomplement of a set of strings such as a, ba is ‘Sigmastar \ a, ba′. If we

Page 50: Computation Engineering Applied Automata Theory and Logic

42 CHAPTER 3. MATHEMATICAL PRELIMINARIES

now change the alphabet to, say, a, the universal set of strings over thisalphabet is

Sigmastar1 = ε, a, aa, aaa, aaaa, . . ..For this set, the complement of a set such as a, aaa is the set that containsstrings of as such that the number of occurrences of as is neither one northree.

Given two sets A and B, their symmetric difference is defined to be (A \B) ∪ (B \ A). Thus, if A = 1, 2, 3 and B = 2, 3, 4, 5, their symmetricdifference is the set 1, 4, 5.

For any alphabet Σ and its corresponding universal set Sigmastar, thecomplement of the empty set φ is Sigmastar. One can think of φ as theempty set with respect to every alphabet.

Types versus Sets

The word type will be used to denote a set together with its associated oper-ations. For example, the type natural number, or Nat, is associated with theset 0, 1, 2, . . . and operations such as successor, +, etc. φ is an overloadedsymbol, denoting the empty set of every type. When we use the word “type,”most commonly we will be referring to the underlying set, although strictlyspeaking types are “sets plus their operations.”

Numbers as Sets

In mathematics, it is customary to regard natural numbers themselves assets. Each natural number essentially denotes the set of natural numbersbelow it. For example, 0 is represented by , or φ, as there are no naturalnumbers below 0. 1 is represented by 0, or (more graphically) , theonly natural number below 1. Similarly, 2 is the set 0, 1, 3 the set 0, 1, 2,and so on. This convention comes in quite handy in making formulas morereadable, by avoiding usages such as

∀i : 0 ≤ i ≤ N − 1 : ..something..

and replacing them with

∀i ∈ N : ..something..

Notice that this convention of viewing sets as natural numbers is exactlysimilar to how numbers are defined in set theory textbooks such as [?]. We

Page 51: Computation Engineering Applied Automata Theory and Logic

3.3. CARTESIAN PRODUCT AND POWERSET 43

are using this convention simply as a labor-saving device while writing downdefinitions. We do not have to fear that we are suddenly allowing sets thatcontain other sets. If we turn our attention back to the discussion on Russell’sparadox, for instance, and take D to be the set of Natural numbers, theassertion x /∈ x evaluates to false. This is because no natural number(viewed as a set) contains itself - it only contains all natural numbers strictlybelow it in value. For instance 3 = 0, 1, 2 does not contain 3 itself. In thiscase, set S defined on Page 39 becomes the empty set φ, and the propositionS ∈ S is false. Hence, no contradiction results.

Exercise 3.4 Formally define the set S of divisors of 64. Either show theset explicitly, or define it using comprehension.

Exercise 3.5 Formally define the set S of divisors of 67,108,864. Eithershow the set explicitly (!), or define it using comprehension.

Exercise 3.6 What is the set defined by x | x ≥ 0 ∧ prime(x) ∧ x ≤ 10?

Exercise 3.7 What is the set defined by

x | (x ∈ 13) ∧ composite(x) ∧ x ≥ 1?A composite number is one that is not prime.

Exercise 3.8 What is the set defined by x | x ∈ 25 ∧ square(x)?square(x) means x is the square of a natural number.

Exercise 3.9 What is the set S = x | (x ⊂ N) ∧ (23 = 24).?

3.3 Cartesian Product and Powerset

The cartesian product operation ‘×’ helps form sets of tuples of elementsover various types. The terminology here goes as follows: ‘pairs3’ are ‘two-tuples,’ ‘triples’ are ‘three-tuples,’ ‘quadruples’ are ‘four-tuples,’ and so on.After 5 or so, you are allowed to say ‘n-ple’ - for instance, ‘37-ple’ and soon. For example, the set int× int denotes the sets of pairs of all integers.Mathematically, the former set is

int× int = 〈x, y〉 | x ∈ int ∧ y ∈ int.3In some circles, the word ‘couple’ is used instead of ‘tuple.’

Page 52: Computation Engineering Applied Automata Theory and Logic

44 CHAPTER 3. MATHEMATICAL PRELIMINARIES

Given two sets A and B, the cartesian product of A and B, writtenA×B is defined to be the set

〈a, b〉 | a ∈ A ∧ b ∈ B.

We can take cartesian product of multiple sets also. Thus, in general, thecartesian product of n sets Ai, i ∈ n results in a set of “n-tuples”

A0 × A1 × . . . An−1 = 〈a0, a1, . . . , an−1〉 | ai ∈ Ai for every i.

If one of these sets Ai is φ, the cartesian product results in φ, because it isimpossible to “draw” any element out of Ai in forming the n-ples.

Here are some examples of cartesian products:

• 2, 4, 8 × 1, 3 × 100 =〈2, 1, 100〉, 〈2, 3, 100〉, 〈4, 1, 100〉, 〈4, 3, 100〉, 〈8, 1, 100〉, 〈8, 3, 100〉.

• Sigmastar1× Sigmastar1 = 〈x, y〉 | x and y are strings over a.

In taking the cartesian product of n sets Ai, i ∈ n, it is clear that ifn = 1, we get the set A0 back. For example, if A0 = 1, 2, 3, the 1-arycartesian product of A0 is itself. Note that A0 = 1, 2, 3 can also be writtenas A0 = 〈1〉, 〈2〉, 〈3〉 because, in classical set theory [?], 1-tuples such as〈0〉 are the same as the item without the tuple sign, in this case 0.

It is quite common to take the cartesian product of different types ofsets. For example, the set int× bool denotes the sets of pairs of integers andBooleans. An element of the above set is 〈22, true〉, which is a pair consistingof one integer and one boolean.

3.3.1 Powersets and Characteristic Sequences

The powerset of a set S is the set of all its subsets. As is traditional, wewrite 2S to denote the powerset of S. In symbols,

2S = x | x ⊆ S.

This “exponential” notation suggests that the size of the powerset is 2 raisedto the size of S. We can argue this to be the case using the notion ofcharacteristic sequences. Take S = 1, 2, 3 for example. Each subset ofS is defined by a bit-vector of length three. For instance, 000 represents φ(include none of the elements of S); 001 represents 3; 101 represents 1, 3,

Page 53: Computation Engineering Applied Automata Theory and Logic

3.3. CARTESIAN PRODUCT AND POWERSET 45

and 111 represents S. These “bit vectors” are called characteristic sequences.All characteristic sequences for a set S are of the same length, equal to thesize of the set, |S|. Thus, the number of characteristic sequences for a finiteset S is exponential in |S|.

Exercise 3.10 Take S = Nat, which contains an infinite number of ele-ments. How many elements are there in the powerset of S? Clearly it alsocontains an infinite number of elements. But is it the “same kind of infin-ity?” Think for five minutes and write down your current thoughts in aboutfour sentences (we shall later revisit this issue).

Exercise 3.11 The set Odd of odd numbers is a proper subset of Nat. Thus,Odd appears “smaller” than Nat - yet, both sets contain an infinite numberof elements? How can this be? Is the ‘infinity’ that measures the size of Odda ‘smaller infinity’ than that which measures the size of Nat? Again, expressyour current thoughts in about four sentences.

Exercise 3.12 Let E be the set of even natural numbers. Express the setE × 2E using set comprehension.

Exercise 3.13 An undirected graph G is a pair 〈V,E〉, where V is the setof vertices and E is the set of edges. For example, a triangular graph overV = 0, 1, 2 is

〈0, 1, 2, 〈0, 1〉, 〈1, 2〉, 〈0, 2〉〉.(It is clear that any of the edges, for example 〈0, 2〉 could have been presented‘reversed’, for example 〈2, 0〉. However, by saying upfront ‘undirected graph,’we are avoiding having to point this arbitrariness out.)

Now, this question is about cliques. A triangle is a 3-clique. A clique isa graph where every pair of nodes have an edge between them4. We showedyou, above, how to present a 3-clique using set-theoretic notation.

Present the following n-cliques over the nodes i ∈ n in the same set-theoretic notation. Also draw a picture of each resulting graph:

1. 1-clique, or a point.

2. 2-clique, or a straight-line.

3. 4-clique.

4. 5-clique.

Page 54: Computation Engineering Applied Automata Theory and Logic

46 CHAPTER 3. MATHEMATICAL PRELIMINARIES

3.4 Functions and Signature

A function is a mathematical object that expresses how items called “inputs”can be turned into other items called “outputs.” A function maps its domainto its range; thus, the inputs of a function belong to its domain and theoutputs belong to its range. The domain and range of a function arealways assumed to be non-empty. The expression “f : TD → TR” iscalled the signature of f , denoting that f maps the domain of type TD tothe range of type TR. Writing signatures down for functions makes it veryclear as to what the function “inputs” and what it “outputs.” Hence, this isa highly recommended practice. As a simple example, + : int × int → intdenotes the signature of integer addition.

Function signatures must attempt to capture their domains and rangesas tightly as possible. Suppose we have a function g that accepts subsets of1, 2, 3, and outputs 4 if given 1, 2, and outputs 5 given anything else.How do we write the signature for g? Theoretically speaking, is correctto write-down the signature as 2Nat → Nat; however, in order to providemaximum insight to the reader, one must write the signature as

21,2,3 → 4, 5.

If you are unsure of the exact domain and range, try to get as tight aspossible. Remember, you must help the reader.

Exercise 3.14 Write a function signature for the sin and tan functions.Choose either degrees or radians for the input angles.

Exercise 3.15 Decipher the signature given below by writing down four dis-tinct members of the domain and the same number from the range of thisfunction. Here, X stands for “don’t care,” which we add to bool. For yourexamples, choose as wide a variety of domain and range elements as possible,to reveal your detailed understanding of the signature:

(int ∪ −1)× 2int × bool → 22bool∪X × int.

Exercise 3.16 Write a function signature for the function 1/(1 − x) forx ∈ Nat.

4In the real world, “clique”s are known to form among groups of people who decide tounfairly favor “the insiders.”

Page 55: Computation Engineering Applied Automata Theory and Logic

3.5. THE λ NOTATION 47

The image of a function is the set of range points that a function actuallymaps onto. For function f : TD → TR,

image(f) = y ∈ TR | ∃x ∈ TD : y = f(x).

3.5 The λ notation

The lambda calculus was invented by Alonzo Church5as a formal representa-tion of computations. Church’s Thesis tells us that the lambda-based evalu-ation machinery, Turing machines, as well as other formal models of compu-tation (Post systems, Thue’ systems, ...) are all formally equivalent. Formalequivalences between these systems have all been worked out by the 1950s.

More immediately for the task at hand, the lambda notation provides aliteral syntax for naming functions. First, let us see in the context of numbershow we name them. The sequence of numerals (in programming parlance theliteral) ‘1’ ‘9’ ‘8’ ‘4’ names the number 1984. We do not need to give alternatenames, say Fred, to such numbers! A numeral sequence such as 1984 suffices.In contrast, during programming one ends up giving such alternate names,typically derived from the domain of discourse. For example,

function Fred(x) return 2;

Using Lambda expressions, one can write such function definitions withoutusing alternate names. Specifically, the Lambda expression λx.2 capturesthe same information as in the above function definition. We think of stringssuch as λx.2 as a literal (or name) that describes functions. In the same vein,using Lambda calculus, one name for the successor function is λx.(x + 1)6,another name is λx.(x+2− 1), and so on. We think of Lambda expressionsas irredundant names for functions (irredundant because redundant stringssuch as “Fred” are not stuck inside them). In Chapter 5, we show that thisidea of irredundant names can be used even with recursive functions.

Here are the only two rules pertaining to it that you need to know.

5When once asked how he chose λ as the delimiter, Church replied “Eenie meenie myniemo!”

6You may be baffled that I suddenly use “23” and “+” as if they were Lambda terms.As advanced books on Lambda calculus show [?], such quantities can also be encodedas Lambda expressions. Thus, anything that is effectively computable—computable by amachine—can be formally defined using only the Lambda calculus.

Page 56: Computation Engineering Applied Automata Theory and Logic

48 CHAPTER 3. MATHEMATICAL PRELIMINARIES

• The Alpha rule, or “the name of the variable does not matter.” Thus,λx.(x+1) is the same as λy.(y+1). This process of renaming variablesis called alpha conversion. Plainly spoken, the Alpha rule simply saysthat, in theory7, the formal parameters of a function can be namedhowever one likes.

• The Beta rule, or “here is how to perform a function call.” A function isapplied to its argument by writing the function name and the argumentname in juxtaposition. Thus, (λx.(x+1)) 2 says “feed” 2 in place of x.The result is obtained by substituting 2 for x in the body (x + 1). Inthis example, 2+1, or 3 results. This process of simplification is calledbeta reduction.

The formal arguments of Lambda expressions associate to the right. Forexample, as an abbreviation, we allow cascaded formal arguments of the form(λxy.(x + y)), as opposed to writing it in a fully paranthesized manner asin (λx.(λy.(x + y))). In addition, the arguments to a Lambda expressionassociate to the left. Given these conventions, we can now illustrate thesimplification of Lambda expressions. In particular,

(λzy.(λx.(z + x))) 2 3 4

can be simplified as follows (we show the bindings introduced during reduc-tion explicitly):

= (λzy.(λx.(z + x))) 2 3 4

= (using the Beta rule) (λz = 2 y = 3.(λx.(z + x)))4.

= (λx.(2 + x))4

= (using the Beta rule) (λx = 4.(2 + x))

= 2 + 4

= 6

The following additional examples shed further light on Lambda calculus:

• (λx.x) 2 says apply the identity function to argument 2, yielding 2.

• (λx.x) (λx.x) says “feed the identity function to itself.” Before per-forming beta reductions here, we are well-advised to perform alphaconversions to avoid confusion. Thus, we turn (λx.x) (λx.x) into(λx.x) (λy.y) and then apply beta reduction to obtain (λy.y), or theidentity function back.

7In practice, one chooses mnemonic names.

Page 57: Computation Engineering Applied Automata Theory and Logic

3.6. TOTAL, PARTIAL, 1-1, AND ONTO FUNCTIONS 49

• As the Lambda calculus seen so far does not enforce any “type check-ing”, one can even feed (λx.(x+1)) to itself, obtaining (after an alphaconversion) (λx.(x+1))+1. Usually such evaluations then get “stuck”as we cannot add a number to a function.

Exercise 3.17 Express the successor function over Nat using the Lambdanotation.

Exercise 3.18 Express the function that sums 1 through N using the Lambdanotation.

Exercise 3.19 Simplify (λzy.(λx.(z + ((λv.(v + x))5)))) 2 3 4

Exercise 3.20 A half-wave rectifier receives a waveform at its input andproduces output voltage as follows. When fed a positive voltage on the input,it does not conduct below 0.7 volts (effectively producing 0 volts). When feda positive voltage above 0.7 volts, it conducts, but diminishing the output by0.7 volts. When fed a negative voltage, it produces 0 volts, except when fed avoltage below -100 volts, when it blows up in a cloud of smoke (causing theoutput to be undefined). View the functionality of this rectifier as a functionthat maps input voltages to output voltages. Describe this function usingthe Lambda notation. You can assume that ifthenelse and numbers areprimitives in Lambda calculus.

3.6 Total, Partial, 1-1, and onto functions

Functions that are defined over their entire domain are total. An example ofa total function is λx.2x, where x ∈ Nat. A partial function is one undefinedfor some domain points. For example, λx.(2/x) is a partial function, as it isundefined for x = 0.

The most common use of partial functions in computer science is to modelprograms that may go into infinite loops for some of their input values. Forexample, the recursive program over Nat,

f(x) = if (x = 0) then 1 else f(x)

terminates only for x = 0, and loops for all other values of x. Viewedas a function, it maps the domain point 0 to the range point 1, and is

Page 58: Computation Engineering Applied Automata Theory and Logic

50 CHAPTER 3. MATHEMATICAL PRELIMINARIES

undefined everywhere else on its domain. Hence, function f can be naturallymodeled using a partial function. In the Lambda notation, we can write fas λx.if (x = 0) then 1. Notice that we use an “if-then” which leaves the“else” case undefined.

One-to-one (1-1) functions f are those for which every point y ∈ image(f)is associated with exactly one point x in TD. A function that is not 1-1is ‘many-to-one.’ One-to-one functions are also known as injections. Anexample of an injection is the predescessor function pred : Nat → Natdefined as follows:

λx.if (x > 0) then (x− 1).

We have pred(1) = 0, pred(2) = 1, and so on. This function is partial,because it is undefined for 0.

Onto functions f are those for which image(f) = TR. Onto functions arealso known as surjections. While talking about the type of the range, we sayfunction f maps into its range-type. Thus, onto is a special case of into whenthe entire range is covered. One-to-one, onto, and total functions areknown as bijections. Bijections are also known as correspondences.Examples: We now provide some examples of various types of functions.In all these discussions, assume that f : Nat→ Nat.

• An example of a one-to-one (1-1) function (“injection”) is f = λx.2x

• An example of a many to one function is f = λx.(x mod 4)

• An example of an onto function is f = λx.x

• An example of a partial function is f = λx.if even(x) then (x/2)

• An example of a bijection is f = λx.if even(x) then (x+1) else (x−1).All bijections f : TD → TR where TD = TR = T are the same type, arepermutations over T .

• Into means not necessarily onto. A special case of into is onto.

• Partial means not necessarily total. A special case of partial is total.

For any given one-to-one function f , we can define its inverse to be f−1.This function f−1 is defined at all its image points. Thus, whenever f isdefined at x,

f−1(f(x)) = x

. For f : TD → TR, we have f−1 : TR → TD. Thus, if f is onto, thenf−1 is total—defined everywhere over TR. To illustrate all this, consider thepredescessor function, pred. The image of this function is Nat. Hence, pred

Page 59: Computation Engineering Applied Automata Theory and Logic

3.6. TOTAL, PARTIAL, 1-1, AND ONTO FUNCTIONS 51

is onto. Hence, while pred : Nat→ Nat is not total, pred−1 : Nat→ Nat istotal, and turns out to be the successor function, succ.

Given a bijection f with signature TD → TR, for any x ∈ TD, f−1(f(x)) =

x, and for any y ∈ TR, f(f−1(y)) = y. This shows that if f is a bijection

from TD to TR, f−1 is a bijection from TR to TD. For this reason, we tend to

call f a bijection between TD and TR - given the forward mapping f , theexistence of the backward mapping f−1 is immediately guaranteed.

Exercise 3.21 Provide one example of a bijection from Nat to Nat.

Exercise 3.22 Point out which of the following functions can exist, andwhich cannot. Provide reasons for functions that cannot exist, and examplesfor functions that can exist.

1. A partial 1-1 and onto function from Nat to Nat.

2. A partial 1-1 and onto function from Int to Nat.

3. A 1-1 into function from Nat to Nat.

4. A 1-1 into, but not onto, function from Nat to Nat.

5. A bijection from Int to Real.

6. A bijection from a set to its powerset. (Recall that we cannot have φas either the domain or range of a function.)

7. A many-to-one function from the powerset of a set to the set.

8. An into map from a set to its powerset.

Exercise 3.23 Describe a bijection from the set ε to the set φ. Here, εis the empty string.

Exercise 3.24 Think about the following question, writing your thoughts ina few sentences in case you cannot definitely answer the question: can therebe a bijection between Int and 2Int? How about a finite subset, F , of Int,and 2F? How about an infinite subset, I, of Int, and 2I?

Composition of functions

The composition of two functions f : A→ B and g : B → C, written g f ,is the function λx . g(f(x)).

Page 60: Computation Engineering Applied Automata Theory and Logic

52 CHAPTER 3. MATHEMATICAL PRELIMINARIES

3.7 Computable functions

Computational processes map their inputs to their outputs, and thereforeare naturally modeled using functions. For instance, given two matrices, acomputational process for matrix multiplication yields the product matrix.All those functions whose mappings may be obtained through a mechanicalprocess are called computable functions, or effectively computable functions, oralgorithmically computable functions. For practical purposes, another equiv-alent definition of a computable function is one whose definition can be ex-pressed in a general-purpose programming language. By ‘mechanical process’we mean a sequence of elementary steps, such as bit manipulations, that canbe carried out on a machine. Such a process must be finitary, in the sensethat for any input for which the function is defined, computational processproducing the mapping must be able to read the input in finite time, andyield the output in finite time. Thus, functions that map truly infinite ob-jects (such as the entire set Nat, the entire set Even of even numbers, or atruly infinite string of 1’s, written 1ω) to some output cannot be computable.Chapter 3 discusses the notion of a ‘machine’ a bit more in detail; for now,think of computers when we refer to a machine.

Non-computable functions also exist. These are genuine mathematicalfunctions, albeit those whose mappings cannot be obtained using a machine.In Section 4.1, based on cardinality arguments, we shall show that non-computable functions do exist. We hope that the intuitions we have providedabove will allow you to answer the following problems intuitively. The mainpoint we are making in this section is that just because a function “makessense” mathematically doesn’t necessarily mean that we can code it up as acomputer program!

Exercise 3.25 Do you see any problems calling a function g “computable”if g were to accept a subset of Nat, and output 4 if given 1, 2, and output 5given any other subset of Nat? How about a variant of this problem with anyother proper subset of Nat specified in the second case? Hint: think aboutwhat all “anything else” can be.

Exercise 3.26 Which of the following functions are computable

1. A function that inverts every bit of an infinite string of bits.

2. A function that inverts every bit of a finite (but arbitrarily long) stringof bits.

Page 61: Computation Engineering Applied Automata Theory and Logic

3.8. ALGORITHM VERSUS PROCEDURE 53

3. A function that outputs a 1 when given π, and 0 when given any otherReal number. (Recall that π is not 22/7, or 3.14, or even 3.1415926.In fact, π is not a Rational number.)

3.8 Algorithm versus Procedure

An algorithm is an effective procedure, where the word ‘effective’ means ‘canbroken down into elementary steps that can be carried out on a computer.’The term algorithm is reserved to those procedures that come with a guar-antee of termination on every input. If such a guarantee is not provided,we must not use the word ‘algorithm’ but instead use the word procedure.While this is a simple criterion one can often apply, sometimes it may notbe possible to tell whether to call something an algorithm or a procedure.Consider the celebrated “3x+1 problem,” also known as “Collatz’s problem”captured by the following program:

function three_x_plus_one(x)

if (x==1) then return 1;

if even(x) then three_x_plus_one(x/2);

else three_x_plus_one(3x+1);

Thus, given 3, the three_x_plus_one function obtains 10,5,16,8,4,2,1

and halts. Will this function halt for all x? Nobody knows! It is stillopen whether this function will halt for all x in Nat [?]! Thus, if someonewere to claim that the above program is their actual implementation of analgorithm (not merely a procedure) to realize the constant function λx.1, noteven the best mathematicians or computer scientists living today know howto either confirm or to refute the claim! That is, nobody today is able toprove or disprove that the above program will halt for all x, yielding 1 as theanswer 8.

Exercise 3.27 Does there exist a procedure that, given a C program P andits input x answers whether P halts on x? Does there exist an algorithm forthis purpose?

Exercise 3.28 Can there be an algorithm that, given two C programs, checkthat they have identical functionality (over all their inputs)?

8Our inability to deal with “even a three-line program” perhaps best illustrates Dijk-stra’s advice on the need to be humble programmers [?].

Page 62: Computation Engineering Applied Automata Theory and Logic

54 CHAPTER 3. MATHEMATICAL PRELIMINARIES

Exercise 3.29 Can there be an algorithm that, given two Yacc grammar files(capturing context-free grammar productions) checks whether the grammarsencode the same language or not? (Yacc is a tool to generate parsers.) Writeyour ‘best guess’ answer for now; this problem will be formally addressed later.

3.9 Relations

A relation R over a set S is a subset of S. Usually, S is a set of k-tuples, andso R is a k-ary relation. Also, usually k = 2, and then R is a binary relation.For example, given Nat × Nat, we can define the (binary) relation < to bethe set

< = 〈x, y〉 | x, y ∈ Nat and x < y.Of course, the very same relational symbol is commonly used to define abinary relation over Int, the set of positive and negative numbers, as well asReal.

For the sake of uniformity, we allow k = 1 also, giving rise to unaryrelations. Thus, odd can be viewed as a unary relation

odd = x | x ∈ Nat and x is odd,

or, equivalently,

odd = x | x ∈ Nat and ∃y ∈ Nat : x = 2y + 1.

Much like we defined binary relations, we can define ternary relations (or3-ary relations), 4-ary relations, etc. An example of a 3-ary relation over Natis between, defined as follows:

between = 〈x, y, z〉 | x, y, z ∈ Nat ∧ (x ≤ y) ∧ (y ≤ z).

Given a binary relation R over set S, define the domain of R to be

domain(R) = x | ∃y : 〈x, y〉 ∈ R,

and the codomain of R to be

codomain(R) = y | ∃x : 〈x, y〉 ∈ R.

Also, the inverse of R, written R−1 is

R−1 = 〈y, x〉 | 〈x, y〉 ∈ R.

Page 63: Computation Engineering Applied Automata Theory and Logic

3.10. FUNCTIONS AS RELATIONS 55

As an example, the inverse of the ‘less than’ relation, ‘<,’ is the greater-than relation, namely ‘>.’ Similarly, the inverse of ‘>’ is ‘<.’ Please notethat the notion of inverse is defined only for binary relations - and not forternary relations, for instance. Also, inverse is different from complement.The complement of < is ≥ and the complement of ‘>’ is ‘≤,’ where thecomplementations are being done with respect to the universe Nat×Nat.

For a binary relation R, let elements(R) = domain(R) ∪ codomain(R).The restriction of R on a subset X ⊆ elements(R), written

R |X= 〈x, y〉 | 〈x, y〉 ⊆ R ∧ x, y ∈ X.

Restriction can be used to specialize a relation to a “narrower” domain. Forinstance, consider the binary relation < defined over Real. The restriction< |Nat restricts the relation to natural numbers.

Example: Putting these ideas together, the symmetric difference of ‘<’ and‘>’ is the ‘6=’ (not equal-to) relation.

Exercise 3.30 What is the symmetric difference of ‘≤’ and ‘≥’? How aboutthe symmetric difference of ‘<’ and ‘≤’?

3.10 Functions as Relations

Mathematicians seek conceptual economy. In the context of functions andrelations, it is possible to express all functions as relations; hence, mathe-maticians often view functions as special cases of relations. Let us see howthey do this.

For k > 0, a k-ary relation R ⊆ T 1D × T 2

D × . . .× T kD is said to be single-

valued if for any 〈x1, . . . , xk−1〉 ∈ T 1D × T 2

D × . . .× T k−1D , there is at-most one

xk such that 〈x1, . . . , xk−1, xk〉 ∈ R. Any single-valued relation R can beviewed as a k− 1-ary function with domain T 1

D × T 2D × . . .× T k−1

D and rangeT kD. We call single-valued relations also functional relations. As an example,

the ternary relation

〈x, y, z〉 | x, y, z ∈ Nat ∧ (x+ y = z)

is a functional relation. However, the ternary relation between defined earlieris not a functional relation.

Page 64: Computation Engineering Applied Automata Theory and Logic

56 CHAPTER 3. MATHEMATICAL PRELIMINARIES

How do partial and total functions “show up” in the world of relations?Consider a k− 1-ary function f . If a xk exists for any input 〈x1, . . . , xk−1〉 ∈T 1D×T 2

D× . . .×T k−1D , the function is total; otherwise, the function is partial.

To summarize, given a single-valued k-ary relation R, R can be viewedas a function fR such that the “inputs” of this function are the first k − 1components of the relation and the output is the last component. Also, givena k-ary function f , the k+1-ary single-valued relation corresponding to it isdenoted Rf .

Exercise 3.31 Consider the binary relation relprime over Nat ×Nat suchthat relprime(x, y) exactly when x and y are relatively prime (the greatestcommon divisor of x and y is 1). Is relprime a functional relation? Whatis its inverse? What is its complement?

Exercise 3.32 Consider the 3-ary relation “unequal3,” which consists oftriples 〈a, b, c〉 such that a 6= b, b 6= c, and a 6= c. Is this relation a functionalrelation? Provide reasons.

Exercise 3.33 How many functions with signature boolk → bool exist, as afunction of k? Here boolk is bool×bool× . . .×bool (k times). Think carefullyabout all possible distinct functions that can have this signature. Each k-aryBoolean function can be presented using a k+1-column truth table with eachrow corresponding to one input and its corresponding output.

Exercise 3.34 Repeat Exercise 3.33 for partial functions with signature boolk

→ bool. View each partial function as a table with the output field being eithera Boolean, or the special symbol ⊥ standing for undefined.

3.10.1 More λ Syntax

There are two different ways of expressing two-ary functions in the Lambdacalculus. One is to assume that two-ary functions take a pair of argumentsand return a result. The other is to assume that two-ary functions are one-ary functions that take an argument and return a result, where the result isanother one-ary function9. To illustrate these ideas, let us define function

9The latter style is known as the Curried notation, in honor of Haskell B. Curry. It wasalso a notation proposed by Schoenfinkel, and it is rumored that the term “Schoenfinkeling”did not catch on.

Page 65: Computation Engineering Applied Automata Theory and Logic

3.10. FUNCTIONS AS RELATIONS 57

RMS which stands for root mean squared in both these styles, calling themrms1 and rms2 respectively:

rms1 : λ〈x, y〉.√

x2 + y2

rms2 : λx.λy.√

x2 + y2

Now, rms1〈2, 4〉 would yield√20. On the other hand, we apply rms2

to its arguments in succession. First, rms2(2) yields λy.√

22 + y2, i.e.,

λy.√

4 + y2, and this function when applied to 4 yields√20. Usually, we use

parantheses instead of angle brackets, as in programming languages; thus,we write rms1(2, 4) and λ(x, y).

x2 + y2.The above notations can help us write characteristic predicates quite

conveniently. The characteristic predicate

(λ(z, y).(odd(z) ∧ (4 ≤ z ≤ 7) ∧ ¬y))

denotes (or, ‘defines’) the relation = 〈5, false〉, 〈7, false〉. This is differentfrom the characteristic predicate (λ(x, z, y).(odd(z) ∧ (4 ≤ z ≤ 7) ∧ ¬y))which, for x of type Bool, represents the relation R

′ ⊆ Bool ×Nat×Bool

= 〈false, 5, false〉, 〈true, 5, false〉, 〈false, 7, false〉, 〈true, 7, false〉.

Variable x is not used (it is a “don’t care”) in this formula.

Exercise 3.35 Describe the unary relation defined by the characteristic pred-icate

λx.(x > 0) ∧ (x < 10).

Write out the relation as a set natural numbers satisfying the characteristicpredicate.

Exercise 3.36 Describe the unary relation defined by the characteristic for-mula

(x > 0) ∧ (x < 10).

Write out the relation as above.

Exercise 3.37 Describe the ternary relation defined by the characteristicformula

λ(x, y, z).(x ⊆ y) ∧ (y ⊆ z) ∧ x, y, z ⊆ 1, 2.Write out the relation as a set of triples.

Page 66: Computation Engineering Applied Automata Theory and Logic

58 CHAPTER 3. MATHEMATICAL PRELIMINARIES

Exercise 3.38 Describe the ternary relation defined by the characteristicformula

λ(x, y, z).(y ⊆ z) ∧ x, y, z ⊆ 1, 2.Write out the relation as above. Here, x is a don’t-care.

Page 67: Computation Engineering Applied Automata Theory and Logic

Chapter 4

Cardinalities andDiagonalization

In this chapter, we discuss the important idea of measuring sizes of infinitesets. In addition to helping to reinforce many mathematical concepts, weobtain a better appreciation of the work of pioneers, notably George Cantor,who originated many of the fundamental ideas in this area. We will alsouse many of the ideas found in this chapter to argue the existence of non-computable functions and certain languages called non Turing-recognizable—languages for which the membership test (testing whether an arbitrary stringis a member of the language)—cannot be performed by any machine.

4.1 Cardinality basics

The cardinality of a set is its size. The cardinality of a finite set is measuredusing natural numbers; for example, the size of 1, 4 is 2. How do we “mea-sure” the size of infinite sets? The answer is that we use “funny numbers,”called cardinal numbers. The smallest cardinal number is ℵ0, the next largercardinal number is ℵ1, and so on. If one infinite set has size ℵ0 while a secondhas size ℵ1, we will say that the second is larger than the first, even thoughboth sets are infinite. Briefly for now, ℵ0 is the number of elements of Nat,while ℵ1 is the number of elements of Real. All these ideas will be made clearin this section.

To understand that there could be “smaller” infinities and “bigger” infini-ties, think of two infinitely sized dogs Fifi and Howard. While Fifi is infinitely

59

Page 68: Computation Engineering Applied Automata Theory and Logic

60 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

sized, every finite patch of her skin has a finite amount of hair. This meansthat if one tries to push apart the hair on Fifi’s back, they will eventuallyfind two adjacent hairs between which there is no other hair. Howard is notonly huge - every finite patch of his skin has an infinite amount of hair! Thismeans that if one tries to push apart the hair on Howard’s back, they willnever find two hairs that are truly adjacent. In other words, there will be ahair between every pair of hairs! This can happen if Fifi has ℵ0 amount ofhair on her entire body while Howard has ℵ1 amount of hair on his body1.

We begin with the question of how one “counts” the number of elementsin an infinite set. For example, are there the same “number” of naturalnumbers as there are real numbers? Since we cannot count infinite sets, letus adopt a method that our ancestors sometimes used when they could notcount certain finite sets2. Our ancestors used to conduct trade successfullythrough the barter system without actually counting the number of objects;say, a cabbage for an elephant, and so on3. The real idea behind barter is toestablish a bijection or correspondence between two sets of elements, withoutactually counting them. The same technique works quite well when we haveto count the contents of infinite sets; in fact, that is the only technique thatworks! But what does ‘counting’, ‘countable’, etc., mean?

4.1.1 Countable sets

A set S is said to be countable if there is a 1-1 total mapping from it tonatural numbers. (This mapping need not be onto.) Clearly, finite sets arecountable. Consider the infinite set Odd, the set of odd numbers. Sincethere is a total 1-1 mapping λx.(x− 1)/2 from Odd numbers to Nat, Odd iscountable. The set Real is not countable, as we shall show in this chapter.

4.1.2 Cardinal numbers

We now discuss the use of cardinal numbers more precisely. The cardinalityof Nat is defined to be ℵ0, written |Nat| = ℵ0. Two sets A and B have thesame cardinality if there is a bijection from A to B. Function λx.(x − 1)/2actually serves as a bijection from Odd numbers to Nat. To sum up,

1Hope this wouldn’t be viewed as splitting hairs. . .2It is good that Romans didn’t discover the concept of Avogadro’s number - how could

they have carved it out on stone tablets?3Cabbages with magical powers, perhaps.

Page 69: Computation Engineering Applied Automata Theory and Logic

4.1. CARDINALITY BASICS 61

• Odd is countable,

• |Odd| = |Even| = |Nat| = ℵ0.• But, note that Odd ⊂ Nat and Even ⊂ Nat

• Therefore, it is entirely possible that for two sets A and B, A ⊂ B, andyet |A| = |B|.

The above example demonstrates that one cannot determine the cardinalityof sets purely based on subset relationships. One correct (and handy) methodfor using subset relationships to determine the cardinality of sets is usingcardinality traps.

4.1.3 Cardinality “trap”

To motivate the notion of cardinality trap, consider the question “how manypoints are there in the map of mainland USA?” Let us treat this map as aregion of Real ×Real. The theorem which we call cardinality trap says:

If, for three sets A, B, and C, we have |A| = |C|, and A ⊂ B ⊂ C,then |A| = |B| = |C|.

Specifically, cardinality trap allows one to “trap” the cardinality of a set Bto be between those of two sets A and C. Exercise 4.10 asks you to provethat cardinality trap is a simple corollary of the famous Schroder-Bernsteintheorem. For the question at hand,

• Any given map of USA (set B of points) can be properly inscribedwithin a (larger) square (set C of points).

• Within the given map of USA, one can properly inscribe a (smaller)square (set A of points).

• All squares in Real×Real have the same number of points, ℵ1 (this isa result we shall prove later (|A| = |C|).

• Therefore, |A| = |B| = |C|, or the map of USA, however drawn, hasthe same number of points as in a square, namely ℵ1.

Now, we present one of George Cantor’s central results, which allows us toprove that two sets have different cardinalities. Known as the diagonaliza-tion method, it is basically a specific application of the principle of proof bycontradiction.

Page 70: Computation Engineering Applied Automata Theory and Logic

62 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

4.2 The Diagonalization method

Let us return to our original question: is there a bijection from Nat to Real?The answer is no and we proceed to show how. We follow the powerfulapproach developed by Cantor, called diagonalization. Diagonalization is aparticular application of the principle of proof by contradiction or reductio adabsurdum in which the solution-space is portrayed as a square matrix, andthe contradiction is observed along the diagonal of this matrix. We now walkyou through the proof, providing section headings to the specific steps to beperformed along the way.

Most textbooks prove this result using numbers represented in Decimal,which is much easier than what we are going to present in this section -namely, prove it in Binary. We leave the proof in Decimal as an exercise foryou. In addition to being a ‘fresh,’ as well as illuminating proof, a proof inBinary also allows us to easily relate cardinality of Reals to that of languagesover some alphabet. Here, then, are the steps in this proof.

4.2.1 Simplify the set in question

We first simplify our problem as follows. Note that (λx.1/(1 + x)) is abijection from [0,∞] ⊂ Real to [0, 1] ⊂ Real. Given this, it suffices to showthat there is no bijection from Nat to [0, 1] ⊂ Real, since bijections are closedunder composition. We do this because the interval [0, 1] is “easier to workwith.” We can use Binary fractions to capture each number in this range,and this will make our proof convenient to present.

4.2.2 Avoid dual representations for numbers

The next difficulty we face is that certain numbers have two fractional rep-resentations. As a simple example, if the manufacturer of Ivory soap claimsthat their soap is 99.99% pure, it is not the same as saying it is 99.999% pure4.However, if they claim it is 99.99% pure (meaning an infinite number of

9s following the fractional point), then it is equivalent to saying it is 100%pure. Thus, in the decimal system, infinitely repeating 9s can be representedwithout infinitely repeating 9s. As another example, 5.1239 = 5.124. Thesame ‘dual representations’ exist in the Binary system also. For example, in

4It may still float.

Page 71: Computation Engineering Applied Automata Theory and Logic

4.2. THE DIAGONALIZATION METHOD 63

the Binary system, the fraction 0.0100 (meaning, 0.010 followed by an infi-nite number of 0s) represents 0.25 in decimal. However, the fraction 0.0101(0.010 followed by an infinite number of 1s) represents 0.0110 in binary, or0.375 in decimal. Since we would like to avoid dual representations, we willavoid dealing with number 1.0 (which has the dual representation of 0.1).Thus, we will perform our proof by showing that there is no bijection fromNat to [0, 1) ⊂ Real. This would be an even stronger result.

Let us represent each real number in the set [0, 1) ⊂ Real in binary. Forexample, 0.5 would be 0.100 . . ., 0.375 would be 0.01100 . . .. We shall continueto adhere to our convention that we shall never use any bit representationinvolving 1. Fortunately, every number in [0, 1) can be represented withoutever using 1. (This, again, is the reason for leaving out 1.0, as we don’t wishto represent it as 0.1, and we don’t want to represent it as 1.0 either.)

4.2.3 Claiming a bijection, and refuting it

For the simplicity of exposition, we first present a proof that is “nearly right,”and much simpler than the actual proof. In the next section, we repair thisproof, giving us the actual proof. Suppose there is a bijection f that putsNat and [0, 1) in correspondence C1 as follows:

0→ .b00b01b02b03 . . .

1→ .b10b11b12b13 . . .

. . .

n→ .bn0bn1bn2bn3 . . .

. . .

where each bij is 0 or 1. Now, consider the real numberD = 0.¬b00 ¬b11 ¬b22 ¬b33 . . .. This real number is not in the above listing,because it differs from the i-th number in bit-position bii for every i. Sincethis number D is not represented, f cannot be a bijection as claimed. Hencesuch an f does not exist.

4.2.4 ‘Fixing’ the proof a little bit

Actually the above proof needs a small “fix”: what if the complement of thediagonal happens to involve an 1? The danger then is that we cannot claimthat a number equal to the complemented diagonal does not appear in our

Page 72: Computation Engineering Applied Automata Theory and Logic

64 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

listing. It might then end-up existing in our listing of Reals in a “non 1form.”

We overcome this problem through a simple correction5. This correctionensures that the complemented diagonal will never contain an 1. In fact, wearrange things so that the complemented diagonal will contain zeros infinitelyoften. This is achieved by placing a 1 in the uncomplemented diagonal everyso often; we choose to do so for all even positions, by listing the Real number.12n+10 . . . (2n + 1 1s followed by 0) at position 2n, for all n: Consider thefollowing correspondence, for example:

0→ .10

1→ .c00c01c02c03 . . .

2→ .1110

3→ .c10c11c12c13 . . .

4→ .111110

5→ .c20c21c22c23 . . .

6→ .11111110

. . .

2n→ .12n+10 . . .

2n+ 1→ .cn0cn1cn2cn3 . . .

. . .

Call this correspondence C2. We obtain C2 as follows. We know that thenumbers .10, .1110, .111110, etc, exist in the original correspondence C1.C2 is obtained from C1 by first permuting it so that the above elements aremoved to the even positions within C2 (they may exist arbitrarily scatteredor grouped within C1). We then go through C1, strike out the above-listedelements, and list its remaining elements in the odd positions within C2. Werepresent C2 using rows of .cij, as above.

We can now finish our argument as follows. The complemented diagonaldoesn’t contain an 1, because it contains 0 occurring in it infinitely often.Now, this complemented diagonal cannot exist anywhere in our .cij listing.Thus, the complemented diagonal is certainly a real number missed by theoriginal correspondence C1 (and hence, also missed by C2). Thus, we arriveat a contradiction that we have a correspondence. Hence, we cannot assign

5Exercises 4.6 ask you to propose another correction.

Page 73: Computation Engineering Applied Automata Theory and Logic

4.2. THE DIAGONALIZATION METHOD 65

the same cardinal number to the set [0, 1) ⊆ Real. It is therefore of highercardinality.

The conclusion we draw from the above proof is that Real and Nat havedifferent cardinalities. Are there any cardinalities “in between” that of Realand Nat? Loosely speaking, “is there a ℵ0.5?!.” The hypothesis that states“no there isn’t a cardinality between ℵ0 and ℵ1,” or in other words, “thereisn’t a ℵ0.5,” is known as the Continuum Hypothesis. While assumed to betrue, it has been an open problem for the last 120 years, and in fact is thefirst of Hilbert’s 23 challenges to Computer Science. For further details,please check [?].

The following exercises pertain to cardinality. Some of the exercises askyou complete a few cardinality comparisons sketched out.

Exercise 4.1 What is the cardinality of the set of all possible C programs?

Exercise 4.2 What is the cardinality of the set of all a.out files?

Exercise 4.3 What is the cardinality of the set of all fundamental particlesin the universe?

Exercise 4.4 Show that |Real| = |Real ×Real|. Hint: First map the num-bers into [0, 1]. Thereafter, interleave the bits of pairs of reals to get a uniquemapping to a single real. For example, given the pair

〈.b00b01b02b03 . . . , .c00c01c02c03 . . .〉

, we can uniquely obtain the single real number .b00c00b01c01b02c02 . . ..

Exercise 4.5 Show that there are more points on the map of the USA (viewedas a set of points ⊂ |Real × Real|) than hair on an infinitely sized dog suchas Fifi (even though Fifi is infinitely sized, there isn’t a hair between everypair of her hairs – unlike with Howard). Hint: Use the idea of cardinalitytrap.

Exercise 4.6 Propose another trick to deal with Reals of a “non 1 form.”

We shall use cardinality arguments when comparing the set of all functionsand the set of all computable functions.

Page 74: Computation Engineering Applied Automata Theory and Logic

66 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

4.2.5 Cardinality of 2Nat and Nat → Bool

In this section, we argue that the sets 2Nat, the powerset of Nat, andNat → Bool, the set of functions from Nat to Bool have the same car-dinality as Real. Notice that each set within 2Nat can be represented by aninfinitely long characteristic sequence. For instance, the sequence 10010100represents the set 0, 3, 5; the sequence 101010 . . . represents the set Even;the sequence 010101 . . . represents the set Odd; and so on. Notice that thevery same characteristic sequences represent functions from Nat to Boolalso. For instance, the sequence 10010100 represents the function that maps0, 3, and 5 to true, and the rest of Nat to false, the sequence 101010 . . .represents the function λx.even(x), and the sequence 010101 . . . representsthe function λx.odd(x). Thus, the above two sets have the same cardinalityas the set of all infinitely long bit-sequences. How many such sequences arethere? By putting a “0.” before each such sequence, it appears that we candefine the Reals in the range [0, 1]. However, we face the difficulty caused byinfinite 1s, i.e., we will end-up having 1 occurring within an infinite numberof infinite sequences. Therefore, we cannot directly used the arguments inSection 4.2.2, which relied on such numbers being absent from the listingunder consideration.

We now present the Schroder-Bernstein theorem that allows us to handlethis, and other “hard-to-count” sets very cleanly. We present the theoremand its applications in the next section.

4.3 The Schroder-Bernstein theorem

Theorem 4.1 (Schroder-Bernstein Theorem): For any two sets A and B,if there is a 1-1, total, and into map f going from A to B, and another 1-1, total, and into map g going from B to A, then these sets have the samecardinality.

Section 4.3.3 discusses a proof of this theorem.

4.3.1 Application: Cardinality of all C programs

As our first application of the Schroder-Bernstein Theorem, let us arrive atthe cardinality of the set of all C programs, CP . We show that this is ℵ0by finding 1-1, total, and into maps from Nat to CP and vice versa. The

Page 75: Computation Engineering Applied Automata Theory and Logic

4.3. THE SCHRODER-BERNSTEIN THEOREM 67

real beauty of this theorem is that we can find completely arbitrary suchmaps. For instance, we consider the class of C programs beginning withmain(). This is, believe it or not, a legal C program! The next longer such“wierd but legal” C program is main();. The next ones are main();;,main();;;, main();;;;, and so on! Now,

• A function f : Nat→ CP that is 1-1, total, and into is the following:

− Map 0 into the legal C program, main()

− Map 1 into another legal C program main();

− Map 2 into another legal C program main();;

− . . ., map i into the C program main();i—i.e., one that containsi occurrences of ;.

• A function g : CP → Nat that is 1-1, total, and into is the following:view each C program as a string of bits, and obtain the value of thisbit-stream viewed as an unsigned binary number.

By virtue of the existence of the above functions f and g, from the Schroder-Bernstein Theorem, it follows that |CP | = |Nat|.

4.3.2 Application: Functions in Nat → Bool

We have already shown that such functions can be viewed as infinite bit-sequences, IBS. As already pointed out, we cannot interpret such sequencesstraight-forwardly as Real numbers in [0, 1) because of the presence of 1that gives rise to multiple representations. We use the following alternativeapproach:

• We map every member of IBS that does not contain an occurrence of1 into the range [0, 1) by putting a “0.” before it, and interpreting itas a Real number.

• We map every member of IBS that contains an occurrence of 1 intothe range [1, 2] by putting a “1.” before it, interpreting it as a Realnumber in [1, 2], and converting it into an equivalent form without 1.For example, 0.010101 is mapped to 1.010110.

• This is a 1-1, total, and into map from IBS to [0, 2]. Composing thismap with the scale-factor λx.(x/2), we obtain the desired function fthat goes from IBS into [0, 1].

Page 76: Computation Engineering Applied Automata Theory and Logic

68 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

• Now we obtain function g that is 1-1, total, and into going from [0, 1]to IBS, as follows:

• map every number except 1 in this range to the correspondingnumber in IBS (with a “0.” before it) that does not contain 1.

• map 1 to 0.1.

This mapping hits every member of IBS except those contain 1 (theonly exception being for 1). This is 1-1, total, and into.

From the Schroder-Bernstein Theorem, it follows that |IBS| = |Real|.

Exercise 4.7 What is the cardinality of the set of all Binary search routinesone can write in C to search an array of size 4?

Exercise 4.8 Compare the cardinality of the powerset of Odd and the pow-erset of the powerset of Even.

Exercise 4.9 What is the cardinality of the set of all curves in 2D space?

Exercise 4.10 Prove the theorem of cardinality trap presented on Page 61.

Illustration 68.1 Using the Schroder-Bernstein Theorem, define a bijectionbetween Nat× Int and Int.Solution: Using the SB theorem, we just need to find a 1-1 total into mapsgoing from Nat× Int to Int and one going from Int to Nat× Int. Here isthe first map:

λ〈x, y〉.sign(y)× (2x × 3|y|).

That this map is total is obvious because × is defined everywhere. Whyis this 1-1? That’s because of the unique prime decomposition of any number(note that we are not ignoring the sign). It is into because we can’t generatenumbers that are a multiple of 5, 7, etc.

The reverse map is much easier: just pair the int with some arbitraryNat:

λx.〈0, x〉.How about finding a bijection directly? It can be done as follows (but

it will become apparent how much harder this is, compared to using theSchroder-Bernstein Theorem):

Page 77: Computation Engineering Applied Automata Theory and Logic

4.3. THE SCHRODER-BERNSTEIN THEOREM 69

List all 〈Nat, Int〉 pairs systematically, then list all Ints system-atically against it.

Listing all of the former proceeds systematically as follows:

• All that “add-up to 0 ignoring signs,” with signs later attached inevery possible way: < 0, 0 > is the only pair that adds up to 0. Addi-tionally, −0 is 0.

• All that “add-up to 1 ignoring signs,” with signs later attached inevery possible way. < 0, 1 > and < 1, 0 > add up to 1. List them asfollows.

< 0, 1 >, < 0,−1 >, < 1, 0 >

• All that “add-up to 2 ignoring signs,” with signs later attached inevery possible way. < 0, 2 >,< 1, 1 >, and < 2, 0 > add up to 2. Listthem as follows:

< 0, 2 >,< 0,−2 >,< 1, 1 >,E < 1,−1 >,< 2, 0 >.

The full bijection looks like the following:

<0,0> -> 0 <0,3> -> 5 <0,4> -> -8

<0,-3> -> -5 <0,-4> -> 9

<0,1> -> 1 <1,2> -> 6 <1,3> -> -9

<0,-1> -> -1 <1,-2> -> -6 <1,-3> -> 10

<1,0> -> 2 <2,1> -> 7 <2,2> -> -10

<2,-1> -> -7 <2,-2> -> 11

<0,2> -> -2 <3,0> -> 8 <3,1> -> -11

<0,-2> -> 3 <3,-1> -> 12

<1,1> -> -3 <4,0> -> -12

<1,-1> -> 4

<2,0> -> -4

4.3.3 Proof of the Schroder-Bernstein Theorem

We offer a proof of this theorem due to Hurd [?]. Page 73 provides analternative proof similar to that in [?].

Our goal is to show that if there are two sets P and Q with injectionsf : P → Q and g : Q → P , there exists a bijection h : P → Q as inFigure 4.1.

Imagine P and Q being mirrors facing each other. Rays emanating fromP fall within Q, and vice versa. It stands to reason that these sets then have

Page 78: Computation Engineering Applied Automata Theory and Logic

70 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

P Q

fg

Figure 4.1: Proof of the Schroder-Bernstein Theorem

the same cardinality. Our proof will exploit this analogy. We will first proveLemma 4.1 and then finish our proof.

Lemma 4.1 For two sets A and B, if B ⊂ A and there exists an injectionf : A→ B, then there exists a bijection j : A→ B.

Proof: Consider A and B as in Figure 4.2. The ring-shaped region A−B canbe regarded as f 0(A−B). The diagram also shows f(A−B), f(f(A−B)),etc. (We will call f i(A−B) for i ≥ 1 as “thought bubbles” in what follows.)Since f is an injection from A to B, f(A − B) is embedded within B, andlikewise are the other f i(A − B) regions. Let X = ∪i≥0 f i(A − B), i.e., theunion of the ring and all the thought bubbles embedded within B. Also,observe that f(X) are the thought bubbles alone. So A can be written asX ∪ (B − f(X)). We will exploit this fact in creating a bijection from A toB, as follows:

• Suppose we propose the mapping j : A→ B as λa. if a ∈ X then f(a) else a.In other words, everything within the ring and the thought-bubblesmaps to the thought-bubbles (f(X)), and everything in the remainingregion (which is the shaded region B − f(X)) maps to itself.

• Suppose we show that f is a bijection from X to f(X) (Claim 1 below),then j will be a bijection from A to B (j is defined partly by the

Page 79: Computation Engineering Applied Automata Theory and Logic

4.3. THE SCHRODER-BERNSTEIN THEOREM 71

A − BA

B

f ( f ( A − B ) )

f ( A − B )

Figure 4.2: Proof Approach

identity map and partly by f , both of which are injective; furthermore,j’s domain covers A and range covers B).

Now, going back to the original problem, the desired bijection from P toQ is found as follows:

• g(Q) is a subset of A. Furthermore, the composition of f and g, g f ,is an injection from A to g(B). Hence there is a bijection j from Ato g(B). Now, since g is injective, g−1 exists. Therefore, the desiredbijection from A to B is inv(g) j.

Proof of Claim 1: f is a bijection from X to f(X) because f is surjective,and f is injective from A to B and hence it must be so when restricted todomain X.

Illustration of the proof

Let us choose P = O (O standing for odd, the set of positive odd numbers)and Q = E (standing for even, the set of positive even numbers). Clearlyλx.x− 1 is a bijection that can be found manually. Let’s see which bijectionis found by the method in the proof (which, of course, works automaticallyfor any situation).

According to the Schroder-Bernstein theorem, if there is an injection fromO into E and vice-versa, there exists a bijection between O and E. Two suchinjections are easy to find, for example f = λx.2x and g = λx.(2x+ 1). Theproof gives a way to construct another bijection automatically from these.

Page 80: Computation Engineering Applied Automata Theory and Logic

72 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

• The set A of the lemma is O, the Odd numbers.

• Applying f to O, we get f(O) = 2, 6, 10, 14, . . ..

• Applying g to E, we get g(E) = 1, 5, 9, 13, . . .. Call this set O1.

• Now, g f = λx.(4x + 1). Applying this to O, we get g f(O) =5, 13, 21, 29, . . .. Call this set O2. Notice that O2 ⊂ O1 ⊂ O.

• Now, as far as the lemma goes, the set “A” is O and “B” is O1. Thereis a bijection (namely g f) from O to O2. Thus, the same function isan injection from O to O1.

• Now, we can build function j as suggested in the lemma as follows.

− The set A− B is O − O1 = 3, 7, 11, 15, 19, . . .. This is the “outerring.”

− The “f” of the lemma is g f = λx.(4x + 1). Applying this toO − O1, we get 13, 29, 45, 61, 77, . . ., which is the first “thoughtbubble.”

− Similarly, f 2(O −O1) = 53, 117, 181, 145, . . ..

− X is the union of all the above sets.

• Now, the j function is λa.if a ∈ (O1−∪i≥1f(O−O1)) then a else f(a)

• Equivalently, j is λa.if a ∈ (O −X) then a else f(a)

• Thus the j function is given by the map1 → 1, 3 → 13, 5 → 5, 7 → 29, 9 → 9, 11 → 45, . . . This is because1, 5, 9, . . . is what’s missed by X.

• Finally, the bijection from O to E is given by g−1j, which is illustratedby the following table where the first map is the application of j andthe second the application of g−1.

1 -> 1 -> 0

3 -> 13 -> 6

5 -> 5 -> 2

7 -> 29 -> 14

9 -> 9 -> 4

11 -> 45 -> 22

...

Page 81: Computation Engineering Applied Automata Theory and Logic

4.3. THE SCHRODER-BERNSTEIN THEOREM 73

Alternate proof

The classical proof of the Schroder-Bernstein theorem goes as follows (thereader may draw a diagram to better understand the proof). Consider theinjections f : P → Q and g : Q→ P . Let P0 = P−g(Q) and Q0 = Q−f(P ).Let Qi = f(Pi−1) and Pi = g(Qi−1). Now, f : Pi−1 → Qi and g : Qi−1 → Piare bijections.

Define Peven = ∪even nPn, and likewise define Podd, Qodd, and Qeven. DefineP∞ = P−(Peven∪Podd), and similarly for Q. Now define the desired bijectionh : P → Q to be f on Peven, g

−1 on Podd and either on P∞.

Exercise 4.11 Illustrate this proof of the Schroder-Bernstein theorem on theabove example involving Odd and Even.

Cardinal Numbers

The topic of cardinalities is quite detailed; we have barely scratched thesurface. One of the more important take-away messages is that sets such asNatural numbers are denumerable or countable while sets such as Reals arenot denumerable or are uncountable. Thus, we cannot list real numbers as“the first real,” “the second real,” etc., because they are not denumerable.How about the powerset of Reals then? Are they of the same cardinality asReals? We conclude with a theorem that shows that there are more than afinite set of cardinalities (we omit delving into sharper details).

Theorem 4.2 For a set S and its powerset 2S, we have |S| ≺ |2S|, where ≺says “of lower cardinality.”

The notion of cardinal numbers basically stems from this theorem. Thecardinal numbers form a sequence ℵ0, ℵ1, ℵ2, . . ., beginning with the cardi-nality of the set of natural numbers ℵ0 and going up in cardinality each timea powerset operation is invoked on the earlier set (ℵ1 for Reals, ℵ2 for thepowerset of Reals, etc.).

Exercise 4.12 Prove Theorem 4.2 by assuming there exists a total bijectionf from S to 2S, and arguing whether there can exist an element of S whichmaps to the following set:

D = x | x ∈ S ∧ x /∈ f(x).

Page 82: Computation Engineering Applied Automata Theory and Logic

74 CHAPTER 4. CARDINALITIES AND DIAGONALIZATION

If such an element d exists, first ask whether d ∈ D, and then ask if d /∈ D.Derive a contradiction.

The above would be a generalization of the diagonalization proof. Thestyle of the proof is precisely what will be used to prove the undecidabilityof the Halting problem.

Page 83: Computation Engineering Applied Automata Theory and Logic

Chapter 5

Binary Relations

While binary relations are abstract mathematical ideas, they are “workhorses”when it comes to ordering the elements of a set, or grouping subsets of a setas being equivalent. In this chapter we examine these issues in great detail,providing sufficient examples so that the purpose of the underlying abstractmathematical ideas becomes clear. Our definitions in this chapter follow thatof [?] and [?] closely.

5.1 Binary Relation basics

A binary relation R on S is a subset of S × S. Binary relations are fun-damental to studying properties of ordered sets. In this section, we shallstudy several binary relations with real-world applications, so that the basicpurpose of defining these types of relations becomes clear. Below, we havetried to logically group relations into various sections.

5.1.1 Types of Binary Relations

In all these definitions, we assume that the binary relation R in question ison S, i.e., a subset of S × S.

Reflexive, and Related Notions

R is reflexive, if for all x ∈ S, 〈x, x〉 ∈ R. Informally, “every element is relatedto itself.” For S = 1, 2, 3, R1 = 〈x, x〉 | x ∈ S is reflexive, and so is R2 =S×S. Another reflexive relation isR3 = 〈1, 1〉, 〈2, 2〉, 〈3, 3〉, 〈1, 2〉, 〈2, 1〉, 〈2, 3〉, 〈3, 2〉.

75

Page 84: Computation Engineering Applied Automata Theory and Logic

76 CHAPTER 5. BINARY RELATIONS

A relation R is irreflexive if there are no reflexive elements; i.e., for no x ∈ Sis it the case that 〈x, x〉 ∈ R.

Note that irreflexive is not the negation of reflexive, because the logicalnegation of the definition of reflexive would be “there exists x ∈ S such that〈x, x〉 /∈ R, which is not what irreflexive is.

Symmetric, and Related Notions

R is symmetric if, for all x, y ∈ S, 〈x, y〉 ∈ R ⇒ 〈y, x〉 ∈ R. Here, x and yneed not be distinct.

Examples: R1, R2, and R3 are symmetric relations. Also note that φ is asymmetric relation.R is asymmetric if, there exist no two distinct x, y ∈ S such that 〈x, y〉 ∈R ∧ 〈y, x〉 ∈ R. In other words, there is no instance that displays symmetry.Example: “elder brother” is an asymmetric relation, and so is < over Nat.

Again note that asymmetric is not the same as the negation of symmetric(the negation of the definition of symmetric would be that there exists distinctx, y such that 〈x, y〉 ∈ R, but 〈y, x〉 /∈ R.R is antisymmetric if, for all x, y ∈ S, 〈x, y〉 ∈ R ∧ 〈y, x〉 ∈ R ⇒ x = y(they are the same element).

Antisymmetry is a powerful notion that, unfortunately, is too strong formany purposes. Consider the elements of 2S, the powerset of S as an example.If, for any two elements x and y in S, we have x ⊆ y and y ⊆ x, then we canconclude that x = y. Thus, the set containment relation ⊆ is antisymmetric.Thus, antisymmetry is appropriate for comparing two sets in the “less thanor equals” sense.

Consider, on the other hand, two basketball players, A and B. Supposethe coach of their team defines the relation ¹BB as follows: A ¹BB B ifand only if B has more abilities or has the same abilities as A. Now, if wehave two players x and y such that x ¹BB y and y ¹BB x, we can concludethat they have identical abilities - they don’t end-up becoming the very sameperson, however! Thus, ¹BB must not be antisymmetric. Thus, dependingon what we are comparing, antisymmetry may or may not be appropriate.

Transitive

R is transitive if, for all x, y, z ∈ S, 〈x, y〉 ∈ R ∧ 〈y, z〉 ∈ R ⇒ 〈x, z〉 ∈ R.Relations R1 and R2 above are transitive; however R3 is not, since it is lacking

Page 85: Computation Engineering Applied Automata Theory and Logic

5.1. BINARY RELATION BASICS 77

the pair 〈1, 3〉. Another non-transitive relation is 6= over Nat, because froma 6= b and b 6= c, we cannot always conclude that a 6= c.

Exercise 5.1 Find a, b, c for which transitivity holds, and those for whichtransitivity does not hold.

5.1.2 Preorder (Reflexive plus Transitive)

IfR is reflexive and transitive, then it is known as a preorder. Continuing withthe example of basketball players, Let the ¹BB relation for three membersA, B, and C of the team be

〈A,A〉, 〈A,B〉, 〈B,A〉, 〈B,B〉, 〈A,C〉, 〈B,C〉, 〈C,C〉.

This relation is a preorder because it is reflexive and transitive. It helpscompare three players A, B, and C, treating A and B to be equivalent inabilities, and C to be superior in abilities than both.

Exercise 5.2 Consider electrical resistors. Let the set of resistors be definedas follows. If x is a Real number, then the term res(x) is a resistor. (Thinkof terms as trees.) If r1 and r2 are in Resistor, then series(r1, r2) is a resis-tor. (Again, series(r1, r2) is a term.) Some of the elements in set Resistorare

• res(1), res(2), res(3), series(res(1), res(2)),

• series(res(1), series(res(2), res(3))), and

• series(series(res(2), res(3)), res(1)).

In any1circuit, we must be able to substitute res(3) by series(res(1), res(2)),as series connected resistors add-up the resistivity. However, we cannot re-gard res(3) and series(res(1), res(2)) as identical because they have distinctrepresentations in the set. To compare two resistors “properly”, we definethe relation Resistor leq:

Resistor leq = 〈x, y〉 | sumR(x) ≤ sumR(y)

where sumR is defined as follows:

sumR(res(i)) = i

1We are ignoring aspects such as size, weight, tolerance, etc.

Page 86: Computation Engineering Applied Automata Theory and Logic

78 CHAPTER 5. BINARY RELATIONS

sumR(series(x, y)) = sumR(x) + sumR(y)

Given the above, show that Resistor leq is reflexive and transitive, and hencea preorder. Also show that Resistor leq is not antisymmetric.

In Section 5.3, we present a more elaborate example of a preorder.

5.1.3 Partial Order (Preorder plus Antisymmetric)

If R is reflexive, antisymmetric, and transitive, then it is known as a partialorder. As shown in Section 5.1.1 under the heading of antisymmetry, thesubset or equals relation ⊆ is a partial order.

5.1.4 Total Order, and Related Notions

A total order is a special case of a partial order. R is a total order if for allx, y ∈ S, either 〈x, y〉 ∈ R or 〈y, x〉 ∈ R. Here, x and y need not be distinct(this is consistent with the fact that total orders are reflexive).

The ≤ relation on Nat is a total order. Note that ‘<’ is not a totalorder, because it is not reflexive2. However, ‘<’ is transitive. Curiously, ‘<’is antisymmetric.

Exercise 5.3 Show that the ‘less than’ relation, ‘<’, defined over Nat, isantisymmetric.

A relation R is said to be total if for all x ∈ S, there exists y ∈ S such that〈x, y〉 ∈ R. Thus, a “total” relation is one in which every element x is relatedto at least one other element y. If we consider y to be the image (mapping)of x under R, this definition is akin to the definition of a total function.

Note again that R being a total order is not the same as R being a partialorder and R being a total relation. For example, the following relation R overset S = a, b, c, d

R = 〈a, a〉, 〈b, b〉, 〈c, c〉, 〈d, d〉, 〈a, b〉, 〈c, d〉is a partial order. R is also a total relation. However, R is not a total

order, because there is no relationship between b and c (neither 〈b, c〉 nor〈c, b〉 is in R).

2Some authors are known to abuse these definitions, and consider < to be a total order.It is better called a strict total order or irreflexive total order, then.

Page 87: Computation Engineering Applied Automata Theory and Logic

5.2. EQUIVALENCE (PREORDER PLUS SYMMETRY) 79

5.2 Equivalence (Preorder plus Symmetry)

An equivalence relation is reflexive, symmetric, and transitive. Consider the¹BB relation for three basketball players A, B, and C. Now, consider a“specialization” of this relation obtained by leaving out certain edges:

≡BB= 〈A,A〉, 〈A,B〉, 〈B,A〉, 〈B,B〉, 〈C,C〉.

This relation is an equivalence relation, as can be easily verified.

Note that ≡BB=¹BB ∩ ¹−1BB. In other words, this equivalence relationis obtained by taking the preorder ¹BB and intersecting it with its inverse.The following section demonstrates that the fact that ¹BB ∩ ¹−1BB is anequivalence relation is not an accident.

5.2.1 Intersecting a Preorder and its inverse

Theorem 5.1 The intersection of any preorder R and its inverse R−1 is anequivalence relation.

Proof: First we show that R ∩ R−1 is reflexive. Let R be a preorder overset S. Therefore, R is reflexive, i.e., it contains 〈x, x〉 pairs for every x ∈ S.From the definition of R−1, it also contains these pairs. Hence the intersectioncontains these pairs. Therefore R ∩R−1 is reflexive.

Next we show that R∩R−1 is symmetric. That is, to show that for everyx, y ∈ S, 〈x, y〉 ∈ R ∩ R−1 implies 〈y, x〉 ∈ R ∩ R−1. If the premis, i.e.,〈x, y〉 ∈ R ∩R−1 is false, the assertion is vacuously true. Consider when thepremis is true for a certain 〈x, y〉. These x and y must be such that 〈x, y〉 ∈ Rand 〈x, y〉 ∈ R−1. The former implies that 〈y, x〉 ∈ R−1. The latter impliesthat 〈y, x〉 ∈ R. Hence, 〈y, x〉 ∈ R ∩R−1. Hence R ∩R−1 is symmetric.

Next we prove that R ∩ R−1 is transitive. Since R is a preorder, itis transitive. We now argue that the inverse of any transitive relation istransitive. From the definition of transitivity, for every x, y, z ∈ S, from thepremises 〈x, y〉 ∈ R and 〈y, z〉 ∈ R, the conclusion 〈x, z〉 ∈ R follows. Fromthese premises, we have that 〈y, x〉 ∈ R−1 and 〈z, y〉 ∈ R−1 respectively.From the above conclusion 〈x, z〉 ∈ R, we have that 〈z, x〉 ∈ R−1. Thus, R−1

is transitive and so is the conjunction of R and R−1.

Page 88: Computation Engineering Applied Automata Theory and Logic

80 CHAPTER 5. BINARY RELATIONS

5.2.2 Identity Relation

Given a set S, the identity relation R over S is 〈x, x〉 | x ∈ S. Anidentity relation is one extreme (special case) of an equivalence relation.This relation is commonly denoted by the equality symbol, =, and relatesequals with equals.

Exercise 5.4 Prove that the intersection of a partial order R over set S andits inverse R−1 is the identity relation over S.

Please note the contrast with Theorem 5.1.

5.2.3 Universal Relation

The universal relation R over S is S×S, and represents the other extreme ofrelating everything to everything else. This is one of the most uninterestingBinary relations3.

5.2.4 Equivalence class

An equivalence relation R over S partitions elements(R) = domain(R) ∪codomain(R) into equivalence classes. Intuitively, the equivalence classes Ei

are those subsets of elements(R) such that every pair of elements in Ei arerelated by R, and Eis are the maximal such subsets. More formally, givenan equivalence relation R, there are two cases:

1. R is a universal relation. In this case, there is a single equivalence classE1 associated with R, which is elements(R) itself.

2. R is not a universal equivalence relation. In this case, an equivalenceclass Ei is a maximal proper subset of elements(R) such that the re-striction of R on Ei, R |Ei

, is universal (meaning that every pair ofelements inside each of the Eis are related by R).

Putting it all together, the set of all equivalence classes of an equivalencerelation R is written “elements(R)/R.” It can be read “the elements of Rpartitioned according to R.” In general, we will write S/ ≡ meaning “set Spartitioned according to the equivalence relation ≡.”In Section 5.3.1, we will demonstrate Theorem 5.1 on the Power relationthat relates machine types.

3This is sort of what would happen if I were to give everyone in this class an ‘A’ grade.

Page 89: Computation Engineering Applied Automata Theory and Logic

5.3. THE POWER RELATION BETWEEN MACHINES 81

5.2.5 Reflexive and Transitive closure

The reflexive closure of R, denoted by R0, is

R0 = R ∪ 〈x, x〉 | x ∈ S.

This results in a relation that is reflexive.The transitive closure of R, denoted by R+, is

R+ = R ∪ 〈x, z〉 | ∃y ∈ S : 〈x, y〉 ∈ R ∧ 〈y, z〉 ∈ R+.

The use of + highlights the fact that transitive closure relates items that are“one or more steps away.”The reflexive and transitive closure of a relation R, denoted by by R∗, is

R∗ = R0 ∪R+.

The use of ∗ highlights the fact that reflexive and transitive closure relatesitems that are “zero or more steps away.”Example: Consider a directed graph G with nodes a, b, c, d, e, f . Suppose itis necessary to define the reachability relation among the nodes of G. Often-times, it is much easier to instead define the one-step reachability relation

Reach = 〈a, b〉, 〈b, c〉, 〈c, d〉, 〈e, f〉

and let the users4perform the reflexive and transitive closure of Reach. Do-ing so results in ReachRTclosed, that has all the missing reflexive and transitivepairs of nodes in it:

ReachRTclosed = 〈a, b〉, 〈b, c〉, 〈c, d〉, 〈e, f〉, 〈a, a〉, 〈b, b〉, 〈c, c〉, 〈d, d〉,〈e, e〉, 〈f, f〉, 〈a, c〉, 〈a, d〉, 〈b, d〉

5.3 The Power relation between machines

An example that nicely demonstrates the versatility of preorders is the onethat defines the “power” of computing machines. Let

MT = dfa, nfa, dpda, npda, lba, dtm, ndtm4“Why sweat? Let the end-users do all the work.”

Page 90: Computation Engineering Applied Automata Theory and Logic

82 CHAPTER 5. BINARY RELATIONS

ndtm dtm

lba

npda

dpda

nfa

dfa

Figure 5.1: The binary relation Power

ndtm dtm

lbanpdadpda

nfa dfa

Figure 5.2: Power shown without the transitive edges

Page 91: Computation Engineering Applied Automata Theory and Logic

5.3. THE POWER RELATION BETWEEN MACHINES 83

represent the set of machine types studied in this book. These acronymsstand for deterministic finite automata, non-deterministic finite automata,deterministic push-down automata, non-deterministic push-down automata,linear bounded automata, deterministic Turing machines, and non-deterministicTuring machines, respectively. A binary relation , or Power, that situatesvarious machine types into a dominance relation is shown in Figure 5.1. Eachordered pair in the relation shows up5as an arrow (→). We draw an arrowfrom machine-type m1 to m2 if for every task that a machine of type m1 canperform, we can find a machine of type m2 to do the same task. Spelt outas a set, the relation Power is

Power =〈dfa, dfa〉, 〈nfa, nfa〉,〈dpda, dpda〉, 〈npda, npda〉,〈lba, lba〉,〈dtm, dtm〉, 〈ndtm, ndtm〉,

〈dfa, nfa〉, 〈nfa, dfa〉, 〈dtm, ndtm〉,〈ndtm, dtm〉, 〈dfa, dpda〉, 〈nfa, dpda〉,〈dfa, lba〉, 〈nfa, lba〉,〈dpda, npda〉, 〈npda, dtm〉, 〈npda, ndtm〉,〈dpda, lba〉, 〈npda, lba〉,〈lba, dtm〉, 〈lba, ndtm〉,

〈dfa, npda〉, 〈nfa, npda〉,〈dpda, dtm〉, 〈dpda, ndtm〉,

〈dfa, dtm〉, 〈dfa, ndtm〉,〈nfa, dtm〉, 〈nfa, ndtm〉

We will now study this dominance relation step by step.Any machine is as powerful as itself; hence, Power is reflexive, as shown

by the ‘self-loop’s. Power is transitive relation because for everym1,m2,m3 ∈MT , if 〈m1,m2〉 ∈ Power and 〈m2,m3〉 ∈ Power, then certainly 〈m1,m3〉 ∈Power. (We do not show the transitive edges in the drawing.) Power isnot antisymmetric because even though dfa and nfa dominate each other,

5To avoid clutter, we will hereafter show diagrams of this kind without the transitiveedges, as in Figure 5.2.

Page 92: Computation Engineering Applied Automata Theory and Logic

84 CHAPTER 5. BINARY RELATIONS

they have distinct existence in the set of machine types MT . Power is apreorder, and in our minds captures exactly how the space of machines mustbe subdivided. It is not a partial order.

5.3.1 The Equivalence Relation over Machine Types

nfa

dfa

npda dpda lba ndtm

dtm

Figure 5.3: The equivalence relation Power ∩ Power−1

Applying Theorem 5.1 to Power, we obtain the equivalence relation ≡MT

over machine-types:

Power ∩ Power−1 =〈dfa, dfa〉, 〈nfa, nfa〉,〈dpda, dpda〉, 〈npda, npda〉,〈dtm, dtm〉, 〈ndtm, ndtm〉,〈lba, lba〉,

〈dfa, nfa〉, 〈nfa, dfa〉, 〈dtm, ndtm〉, 〈ndtm, dtm〉

Figure 5.3 illustrates ≡MT . We can see that ≡MT subdividesMT into fiveequivalence classes, dfa, nfa, dpda, npda, lba, and dtm, ndtm.

Exercise 5.5 Define the following relation ≺ over intervals of Reals as fol-lows. Let [a, b] for a, b ∈ Real be an interval. For intervals [a, b] and [c, d],[a, b] ≺ [c, d] iff a ≤ c ∧ d ≤ b. Now, is ≺ a preorder? Is it a partial order?Show that ≺ ∩ ≺−1 is an identity relation.

Exercise 5.6 Continuing with the above question, let’s change ≺ as follows.For intervals [a, b] and [c, d], define

[a, b] ≺ [c, d] ⇔ ∃x : (a ≤ x ≤ b) ∧ (c ≤ x ≤ d).

Page 93: Computation Engineering Applied Automata Theory and Logic

5.3. THE POWER RELATION BETWEEN MACHINES 85

Now, is ≺ a preorder or a partial order? Is ≺ ∩ ≺−1 an identity equivalencerelation or simply an equivalence relation?

Exercise 5.7 Find out which are true and which are false assertions.

1. The intersection of two preorders is a preorder

2. The intersection of a preorder order and its inverse can never be theidentity relation

3. The universal relation is a preorder

4. The empty relation is a preorder

Exercise 5.8 Choose all correct answers: The intersection of a partial orderand its inverse is

1. The universal relation

2. The identity relation

3. An equivalence relation

4. A preorder

Exercise 5.9 Choose all correct answers: The intersection of a preorder andits inverse is

1. a reflexive relation

2. a congruence relation, modulo any given function f

3. a transitive relation

4. an antisymmetric relation

Exercise 5.10 Find a relation that is symmetric, asymmetric, antisymmet-ric, reflexive, irreflexive, transitive, intransitive, an equivalence relation, apreorder, and a partial order (all at the same time).

elements(R)/R = Ei | Ei ⊆ elements(R)∧ Ei × Ei ⊆ R∧¬∃Y : Ei ⊂ Y ⊆ elements(R) ∧ Y × Y ⊆ R

.

Page 94: Computation Engineering Applied Automata Theory and Logic

86 CHAPTER 5. BINARY RELATIONS

There are four equivalence classes over Power∩Power−1, they being dfa, nfa,dpda, npda, and dtm, ndtm, as can be seen from Figure 5.3 as fourmaximal universal relations. Given an equivalence relation R, the equiva-lence class of an element x ∈ elements(R) is denoted by [x].

Exercise 5.11 List some of the equivalence classes of the equivalence rela-tion ≺ ∩ ≺−1 of Exercise 5.6.

Exercise 5.12 Compare the equivalence classes defined by ≺ ∩ ≺−1 as de-fined by Exercise 5.6 and Exercise 5.5. It is the case that each equivalenceclass of one of these examples contains many equivalence classes of the otherexample. Explain this statement. Specifically, consider the interval [x, x] forsome Real number x. Consider its equivalence class [[x, x]]. In which exampledoes this equivalence class contain more members besides [x, x]?

5.4 Lattice of all binary relations over S

A lattice (Q,¹) is a partially ordered set such that for any two elementsx, y ∈ Q, the greatest lower bound glb(x, y) and the least upper boundlub(x, y) exist in Q, and are unique. An element x

′is lower than x if x′ ¹ x.

An element x′is lower bound of x and y if x

′is lower than x and y. glb(x, y)

is then a lower bound that is below no other lower bound. Similarly, we candefine lub(x, y) to be an upper bound that is above no other upper bound.As an example, the powerset of a set S, 2S, forms a lattice under the partialorder ⊆. In this lattice, the glb among all elements of S is φ, and the lubamong all elements is S itself.

As another illustration, the set of all equivalence relations over S forms alattice under the normal set inclusion operator. The glb of this lattice is theuniversal relation S × S, and the lub of this lattice is the identity relation〈x, x〉 | x ∈ S.

5.5 Equality, Equivalence, and Congruence

By now, we have seen the notion of the identity relation, which is what the= symbol really corresponds to. Our use of the term “equality” will, withoutfurther qualifications, refer to =. Figure 5.4 illustrates identity. Our use ofthe word equivalence, denoted by ≡, has already been explained.

Page 95: Computation Engineering Applied Automata Theory and Logic

5.5. EQUALITY, EQUIVALENCE, AND CONGRUENCE 87

nfa dfa npda dpda lba ndtm dtm

Figure 5.4: The identity relation over MT

Exercise 5.13 Define the following family of equivalence relations over Nat:Equivalence relation Ei, for 2 ≤ i ≤ 4, contains contains all pairs 〈x, y〉such that (x = y) mod i. For example, for i = 2, one equivalence class idOdd and another equivalence class is Even. Sketch the equivalence classesgenerated by i = 2, i = 3, and i = 4. Show the inclusion relationships betweenthese equivalence classes. Also show the inclusion relationships between theseequivalence classes and the universal and the identity relations over Nat.

5.5.1 Congruence relation

A congruence relation captures the notion of substitutability. For example,not only are 2 and (3 − 1) equivalent, but in most contexts, one can use 2instead of (3−1). As discussed in Section 5.1.2, a resistor may be substitutedwith another having the same Ohmic value. Let us take a generic example.Consider something “big”, say B, and let x be a “small part of it.” Insymbols, we denote B as B[x], or “B that contains x.” Let x ≡ y be trueunder some equivalence relation ≡. If ≡ is a congruence relation (or simply“congruence”), then B[x] ≡ B[y]. As an example of Big, think of a Radioreceiver, with x being a 10Ω resistor inside the receiver, and y being two 5Ωresistors in series. Most Radio receivers will continue to behave the sameif one were to pull out a 10Ω resistor and replace it by two 5Ω resistors inseries. This is really the idea behind congruence.

A context (or “hole”) inside a “Big” expression is elegantly capturedby a lambda expression. Thus, λz.z + 1 gives the context “. + 1.” Now,2 and 200 − 198 are congruent modulo operator λz.z + 1 because (λz.z +1)2 = (λz.z + 1)(200 − 198). Such congruence can be lost in systems whereaddition is implemented in a resource-bounded fashion, causing overflows orloss of precision. Thus, while arithmetic substitutivity is a congruence, inreal hardware implementations, this substitutivity may be lost!

Formally, an equivalence relation R modulo operator

op : ×n elements(R)→ elements(R)

for some n ≥ 0 is called a congruence relation, denoted R/≡, if

Page 96: Computation Engineering Applied Automata Theory and Logic

88 CHAPTER 5. BINARY RELATIONS

op(〈x1, . . . , xn〉) = op(〈y1, . . . , yn〉), for all i ∈ n, and

∀i : yi ∈ [xi] (here, yi in the equivalence class of xi).

Exercise 5.14 Electrical engineers can realize a resistor of a certain Ohmicvalue by connecting resistors in series, parallel, or a combination. Two re-sistors of Ohmic value R1 and R2 in series are equivalent to a single resistorof Ohmic value R1+R2 while two such in parallel are equivalent to a resistorof R1R2

R1+R2Ohms. This equivalence is a congruence relation modulo the series

connection and parallel connection operators, as far as resistivity goes.Some attributes of circuits are not preserved by this substitution. For

example, if each resistor in a series connection has tolerance 5% (meaningthe actual Ohmic value may be +

−5%), then two 10Ω resistors, each with

tolerance +−5% that are connected in parallel do not have the same tolerance

as a single 5Ω resistor (of tolerance +−5%).

• Derive a formula for the tolerance of a parallel connection.

• Is the planarity of the circuit layout preserved by this substitition?

• Think of three other attributes that are not preserved.

This exercise drives home the point that not everything of interest may bepreserved in a substitution under a congruence.

Illustration 88.1 (Binary relations:) Let a relation ≺ over intervals ofReals be defined as follows. Let [a, b] for a, b ∈ Real be an interval. Forintervals [a, b] and [c, d], [a, b] ≺ [c, d] iff a ≤ c ∨ d ≤ b. Find out the exactnature of ≺: i.e., is it reflexive, symmetric, antisymmetric, transitive, etc?Solution: We will show the following. First of all, ≺ is reflexive. It is not sym-metric. If it were symmetric, then we must have ((a ≤ c)∨ (d ≤ b))⇒ ((c ≤a)∨(b ≤ d)). This would mean that ((c > a)∧(b > d))⇒ ((a > c)∧(d > b)).But this is not so: consider (a, b) = (0, 10) and (c, d) = (2, 9).

It is not transitive. If it were transitive, it would mean that ((a ≤ c)∨(d ≤b) ∧ (c ≤ e) ∨ (f ≤ d)) ⇒ ((a ≤ e) ∨ (f ≤ b)). But this is not so: consider(a, b) = (2, 10), (c, d) = (3, 15) and (e, f) = (1, 11).

Illustration 88.2 Equivalence over Boolean FormulasLet V = x, y, z be a set of Boolean variables. Let F be the set of all

Boolean formulas formed over V using ∧,∨,¬. Let ⇒ stand for the implica-tion symbol. Define the relation R to be the set of all formula pairs 〈f1, f2〉such that f1 ⇒ f2 is true, where f1 and f2 come from F . It is easy to see

Page 97: Computation Engineering Applied Automata Theory and Logic

5.5. EQUALITY, EQUIVALENCE, AND CONGRUENCE 89

that R is a pre-order. Also, R ∩ R−1 is an equivalence relation where eachequivalence class contains formula pairs that are logically equivalent. Sincethere are only 3 Boolean variables used, there 22

3

equivalence classes. Eachequivalence class is a set with cardinality ℵ0 (e.g., f , ¬¬f , ¬¬¬¬f , etc., arein the equivalence class of formula f).

Also, the set F with the operator ⇒ forms a lattice, with f1∨ f2 being theleast upper-bound and f1 ∧ f2 being the greatest lower-bound of two formulasf1 and f2.

Page 98: Computation Engineering Applied Automata Theory and Logic

90 CHAPTER 5. BINARY RELATIONS

Page 99: Computation Engineering Applied Automata Theory and Logic

Chapter 6

Mathematical Logic, Induction,Proofs

In this chapter, we present a sampling of ideas from mathematical logic. Weconsider various operators such as if and if and only if that are crucial to theproper understanding of formal statements made while discussing automatatheory. We then describe methods to write down formal definitions, andformal proofs, including inductive definitions, and proofs by contradiction.

6.1 Proof Methods

A proof is a sequence of steps justifying why something is true. Moreformally, a proof of a statement sn in formal logic is a sequence s0, s1, . . . , snsuch that each si is either an axiom or follows from some of the earlier

sj (j < i) through a rule of inference. In Chapter 16, we shall introducethe formal logical machinery necessary to define what a step can be, andwhat true means. In this section, we list a few styles of proof and their usesthroughout this book. We begin with a review of some of the basic operators.

6.1.1 The implication operation

Implication, or ⇒, stands for “if-then.” It used to assert the truth of itsconclusion conditional on the truth of its premis. For example,

If (x > 2 ∧ even(x)) then composite(x)

91

Page 100: Computation Engineering Applied Automata Theory and Logic

92 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

is a true assertion. Written using ⇒, the above statement reads

(x > 2 ∧ even(x))⇒ composite(x).

This assertion is true because every even number is a composite number, andfor odd numbers, the above assertion is vacuously true (its premis is false).What about the statement

(x > 2 ∧ x < 2)⇒ (x = x+ 1)?

The premis that asserts that x is both > 2 and < 2 is false. In this case also,the assertion is vacuously true. Note that we are not drawing the conclusionx = x+1 - the moment we find that the premis is false, we leave the conclusionalone. Another sentence of this form is “if the moon is made of blue cheesethen horses can fly” in that it is a true assertion1. The assertion

(x > 2 ∧ even(x))⇒ prime(x)

is, on the other hand, neither true nor false (it is both falsifiable and satisfi-able). Note that a⇒ b is equivalent in meaning to ¬a ∨ b.

Exercise 6.1 Indicate which of the following implicational formulae are true,and which are false:

1. (1 = 2)⇒ (2 = 3)

2. gcd(x, y) = z ⇒ gcd(x + y, y) = z. Here, gcd stands for the greatestcommon divisor of its arguments.

3. (k > 1) ∧ hasclique(G, k) ⇒ hasclique(G, k−1). Here, hasclique(G, k)means that graph G has a clique of size k.

4. (k > 1) ∧ hasclique(G, k) ⇒ hasclique(G, k + 1).

5. ((a⇒ (b⇒ c))⇒ (a⇒ b))⇒ (a⇒ c)

1Notice that we are not concluding that horses can fly.

Page 101: Computation Engineering Applied Automata Theory and Logic

6.1. PROOF METHODS 93

An exercise in reading iff

We now turn to the iff operator. Basically, a iff b, also written a ⇔ b ora = b, means a ⇒ b and b ⇒ a. In other words, a has the same truth valueas b; if a is true, so is b; if a is false, so is b.

Definitions involving iff are sometimes tricky to read. Consider the fol-lowing definition that talks about ultimately periodic sets [?]:

A set S ⊆ N is said to be ultimately periodic (UP) if there existsa number n ≥ 0 and another number (called ‘period’) p > 0 suchthat for all m ≥ n, m ∈ S iff m+ p ∈ S.

See if you understand the above definition by doing the following exercise.

Exercise 6.2

1. Justify that φ is UP.

2. Justify that 0, 1, 27 is UP.

3. Justify that 0, 1, 5, 23, 24, 26, 39, 41, 54, 56, 69, 71, 84, 86, 99, 101, . . . isUP.

The fact that the set in Question 3 is UP is clear, because n = 26 and p = 15works. Think how you get the other two answers.

6.1.2 ‘If,’ or ‘Definitional Equality’

When one presents mathematical ideas, one often adopts a certain systematicstyle consisting of first introducing a bunch of definitions, followed by thestatement and proof of various theorems. While introducing definitions, youmay have noticed that people use various kinds of equality or arrow symbols,

some of the typically used symbols being.=, or

4=, or sometimes even⇐. We

will prefer.= over all these various forms.

This is known as definitional equality. Definitional equalities are used tointroduce new definitions into the domain of discourse. Note that such adefinition, by itself, causes no contradictions. For example, let there be adefinition

foo(x).= odd(x) ∧ prime(x)

Page 102: Computation Engineering Applied Automata Theory and Logic

94 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

in some domain of discourse, where foo has never before been introduced,while odd and prime have already been defined. However, if one contin-ues on defining functions ‘willy nilly,’ and later introduces the definitionfoo(x)

.= even(x), one would have introduced a contradiction that asserts

that even(x) = odd(x) ∧ prime(x)! In many frameworks, a second defini-tion of something that is already defined is flagged as an error. Thus, someprotection (through checking) is available while using definitional equalities.

Considering all this, a real conundrum seems to arise when one uses a def-initional equality symbol to introduce a new symbol which also appears onthe right-hand side of the definition! How can we be introducing something“afresh” when we are also using it on the right-hand side, as if it were alreadyexisting? This is, however, how recursive definitions are! Do such recursivedefinitions always ‘make sense?’ Can they cause contradictions of the kindalluded to, above? How do we understand recursive definitions that, on onehand, are so very convenient, but on the other hand prone to circularity?Chapter 5 examines these questions in greater detail, and helps set the stagefor using recursive definitions with more assurance. If you are planning toskip that chapter, try to at least study the following recursive definitionsto see which ones define a function (any function at all), which ones de-fine a function uniquely, and try to understand carefully why uniqueness isachieved.

Exercise 6.3 Study the following recursive definitions as suggested above.Assume that x ranges over Int. The operator |x| takes the absolute value ofx:

1. f(x).= f(x) + 1

2. f(x).= f(x+ 1)

3. f(x).= f(x)

4. f(x).= if (x = 0) then 1 else f(x+ 1)

5. f(x).= if (x = 0) then 1 else f(x− 1)

6. f(x).= if (x = 0) then 1 else f(|x| − 1)

7. f(x).= if (x = 0) then 1 else x+ f(|x| − 1)

Page 103: Computation Engineering Applied Automata Theory and Logic

6.1. PROOF METHODS 95

The as Notation

Sometimes, when we define a function f , we might be interested in not onlythe entire argument fed to f but also the sub-structure of the arguments. Inthis case, the use of the ‘as’ notation comes in handy.

Example:

f(x as 〈y, z〉, w) = if w > 0

then f(x,w − 2)

else z.

In this function, if w > 0 then we recurse, passing x “as is” in the firstargument. If w = 0, we return the second component of x, which is z.The as notation allows both x and its components to be referred to on theright-hand side.

6.1.3 Proof by Contradiction

Proof by contradiction, or reductio ad absurdum is a reasoning principle oneuses frequently in day-to-day life, for example in playing games, in detectingcontradictions in arguments, etc. This principle is captured by the contra-positive rule

(A⇒ B)⇔ (¬B ⇒ ¬A),

as can be easily seen by converting A ⇒ B to ¬A ∨ B and also similarlyconverting (¬B ⇒ ¬A)2. If we assume something (“A”) and derive acontradiction, we have essentially proved A ⇒ false, which, by the contra-positive, allows us to conclude true⇒ ¬A, or ¬A.

We have already seen how proof by diagonalization uses proof by con-tradiction. We shall use proof by contradiction also in the context of thePumping Lemma in Chapter 10, and in the context of mapping reducibilityarguments in Chapter 14.

Exercise 6.4 Consider the game of Mastermind played using three colors(to simplify the exposition) y, g, b standing for yellow, green, and blue.

2The above theorem can also be written (A⇒ B) ≡ (¬B ⇒ ¬A) as the equality symbol≡ takes the meaning of ⇔ for Booleans.

Page 104: Computation Engineering Applied Automata Theory and Logic

96 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

CODE y g b b Move number

-------------------------

rrr g g b b 3

www b b y y 2

rw y y g g 1

The game is played between two players A and B. A selects a secret codehidden from view of the opponent B. In the example below, the code is y g b

b. Player B must try and guess the code, improving his/her guesses based onscore assigned by A. In our example, B’s moves are shown below, numberedas shown. B’s first move is y y g g which fetches a r (for red) and w (forwhite). A red means that one of the pegs is in the right place and of the rightcolor, while a white means that there is a peg of the right color, but in thewrong place. In our example first move, one y peg fetches a red, and one ofthe g pegs fetches a white. The next two moves of B are as shown. With thegame poised at this point, show that B can, by using the principle of proofby contradiction, finish the game in two more moves. (Hint: B views move2 with interest. This move reveals that if there are two y in the code, theremust only be one b. Taking this assumption to move 1 leads to an immediatecontradiction. Continue to argue through the details.)

6.1.4 Quantification operators ∀ and ∃We now present some facts about the ∀ and ∃ operators. These opera-tors allow you to perform iteration over a certain range, similar to what Σ(summation) and Π (product) do in traditional mathematics. The universalquantification operator ∀ is used to make an assertion about all the objects inthe domain of discourse. (In mathematical logic, these domains are assumedto be non-empty.) Thus,

∀x : Nat : P (x)

is equivalent to an infinite conjunction

P (0) ∧ P (1) ∧ P (2) . . .

or equivalently ∧x∈Nat : P (x). Note that we are saying “x : Nat.” This issimply saying “x belongs to type Nat.” It is entirely equivalent to x ∈ Nat—we show both uses here, as both these usages may be found in the literature,and therefore it pays to be familiar with both usages.

Page 105: Computation Engineering Applied Automata Theory and Logic

6.1. PROOF METHODS 97

Here is an example of how ∀ works for a finite range:

∀x ∈ 0, 1, 2 : P (x)

The above formula is equivalent to P (0) ∧ P (1) ∧ P (2).The above definitions allow us to establish numerous identities, for instance:

(∀ x : Nat : P (x)) ∧ (∀ x : Nat : Q(x)) ≡ (∀ x : Nat : (P (x) ∧Q(x))),

as the explicit ∧ gets absorbed into the iterated conjunction denoted by ∀.Similarly,

∃x : Nat : P (x)

is equivalent to an infinite disjunction

P (0) ∨ P (1) ∨ P (2) . . .

or equivalently ∨x∈Nat : P (x).In [?], Lamport introduces a notational style for writing long mathemat-

ical formulae in a readable manner. His insight is that by using suitableindentations, and by employing the operators ∧ and ∨ in a manner similarto bullets, one can avoid many parantheses that would otherwise be needed.The above formula would, for instance, be written as

∨ P (0)

∨ P (1)

∨ P (2)

∨ . . .

We will employ this style in the following illustration.

Illustration 97.1 Fully expand-out the ∀ and ∃ below, and simplify the re-sulting Boolean formula. and and or are equivalent to ∧ and ∨, except theyare written in the prefix syntax (not infix). Your final answer should be T orF (true or false), with justifications.

(∀x ∈ Bool : ∀y ∈ Bool : (or(x, y)⇒ ∃z ∈ Bool : and(y, z)))

Solution: The given formula is

Page 106: Computation Engineering Applied Automata Theory and Logic

98 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

∀x ∈ Bool : ∀y ∈ Bool :or(x, y) ⇒ ∃z ∈ Bool : and(y, z)

Expanding x, we get∧ ∀y : or(0, y)⇒ ∃z : and(y, z)∧ ∀y : or(1, y)⇒ ∃z : and(y, z)

Now expand y,∧ ∧ or(0, 0)⇒ ∃z : and(0, z)∧ or(0, 1)⇒ ∃z : and(1, z)

∧ ∧ or(1, 0)⇒ ∃z : and(0, z)∧ or(1, 1)⇒ ∃z : and(1, z)

Now, expand z,∧ ∧ or(0, 0)⇒ ∨ and(0, 0)

∨ and(0, 1)∧ or(0, 1)⇒ ∨ and(1, 0)

∨ and(1, 1)∧ ∧ or(1, 0)⇒ ∨ and(0, 0)

∨ and(0, 1)∧ or(1, 1)⇒ ∨and (1, 0)

∨and (1, 1)This simplifies to 0, or false.

In first-order logic, quantification is allowed over the domain of individuals(numbers, strings, etc.) while in the second-order logic, quantification overfunctions and relations is allowed. Further facts about quantification will beintroduced as necessary.

6.1.5 Generalized DeMorgan’s Law relating ∀ and ∃DeMorgan’s law relates ∧ and ∨. For two Boolean variables a and b, theDeMorgan’s laws are:

¬(a ∧ b) = (¬a ∨ ¬b), , and ¬(a ∨ b) = (¬a ∧ ¬b).

Extending the above to infinite conjunctions (∀) and infinite disjunctions(∨), we have the following generalize DeMorgan’s laws, or otherwise known

Page 107: Computation Engineering Applied Automata Theory and Logic

6.1. PROOF METHODS 99

as the duality between ∀ and ∃:

∀ x : Nat : P (x) ≡ ¬∃ x : Nat : ¬P (x)

and

∃ x : Nat : P (x) ≡ ¬∀ x : Nat : ¬P (x).

Exercise 6.5 How does one prove the statement “There exist horses with awhite tail?” (For this, and the following ‘horse proofs,’ conserve your horsepower - simply offer one-sentence proofs of the form “Go find a horse satisfy-ing criterion x, then look for criterion y in them; hence, proved/disproved.”)

Exercise 6.6 How does one disprove the statement “There exist horses witha white tail?”

Exercise 6.7 How does one prove the statement “For all horses that haveblack ears there are some that have a white tail?”

Exercise 6.8 How does one disprove the statement “For all horses that haveblack ears there are some that have a white tail?”

Exercise 6.9 Propose a quantified statement in first-order logic that ex-presses the fact that set S ⊆ Nat is infinite. Construct a statement thatgoes like “for all elements of S, . . ..”

Exercise 6.10 Propose a quantified statement in first-order logic that ex-presses the fact that set S ⊆ Nat is finite. Construct a satement that goeslike “there exists. . ..”

Exercise 6.11 Eliminate the negation operator entirely from the followingformula:

¬(∀x : ∃y : ∀z : (p(x, y)⇒ ¬q(y, z))).

6.1.6 Inductive Definitions of Sets and Functions

The term inductive definition finds widespread use throughout computer sci-ence, and tends to mean a whole bunch of (related) things. We introducetwo specific usages of this term now.

Page 108: Computation Engineering Applied Automata Theory and Logic

100 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

Inductively Defined Sets and Functions

A set that is inductively defined is constructed in a principled way, by firstintroducing the basis elements and then “pumping” new elements out ofexisting elements through constructors. For example, the set of all lists overN , denoted list[N ], is defined as follows:

nil ∈ list[N ]

if l ∈ list[N ] and n ∈ N , then cons(n, l) ∈ list[N ]

The intent is to say that list[N ] is the smallest set closed under the above tworules. In a general setting, the process of inductively defining a set S consistsof first including the basis elements B = bi | i ∈ m into S. Thereafter,for every element x in the set S, one of a (usually finite set of) constructorsC = ci | i ∈ n are applied to obtain elements of the form ci(x). The setS being defined is the least set that includes the basis-case elements and isclosed under constructor application. Said more formally,

Rule 1: For every i ∈ m, bi ∈ S

Rule 2: If x ∈ S, and ci is a constructor for i ∈ n, then ci(x) ∈ S

Rule 3: S is the least such set.

There are actually two other (and somewhat non-obvious) ways to expressthe intent of Rule 3. They are explained below, as they tend to occur inother books as well as papers.

1. S is the intersection of all such sets.

2. Express S directly as follows:

S = x | (x ∈ B) ∨ (∃ci ∈ C : x = ci(y) ∧ y ∈ S)

The constructors of an inductively defined set S have the status of prim-itive functions that operate over the set. In most cases, a whole list of otherinductively (or, equivalently, recursively) defined functions are also definedover an inductively defined set. One can then establish properties of thesefunctions by structural induction. For instance, if we define the length of alist, and the append function on lists, inductively as follows

Page 109: Computation Engineering Applied Automata Theory and Logic

6.1. PROOF METHODS 101

length(nil) = 0

length(cons(n, L)) = 1 + length(L)

append(nil, L) = L

append(cons(x, L1), L2) = cons(x, append(L1, L2))

One can then easily prove, via structural induction, properties such as

length(append(L1, L2)) = length(L1) + length(L2)

. We shall use proof by structural induction when we deal with data struc-tures such as trees and lists.

Exercise 6.12 Inductively define the set of all Binary trees with nodes la-beled by elements from Nat. Employ the constructor Node(n) to introduce atree of height zero containing just a node n ∈ Nat. Employ the constructorTree(n, T1, T2) to introduce a tree rooted at node n with left sub-tree and rightsub-tree being T1 and T2, respectively.

Exercise 6.13 Inductively define a function to add-up the values situated atevery node of a Binary tree such as in Exercise 6.12.

Free inductive definitions: The inductive definition of a set S is called afree inductive definition if, for every object x in S, either x ∈ B, xor x = ci(y)for exactly one constructor ci and exactly one element y ∈ S. (We use xorto highlight the fact that only one of these conditions must hold.) Suchsets are called freely generated from a set of basis elements B and a set ofconstructors C.

To understand the notion of freely generated sets better (and to see whyit matters), suppose we have a set S such that for some element y in it, y ∈ Band y = ci(z). Or suppose we have a set S such that for some element yin it, y = ci(z1) and also y = cj(z2) for z1 6= z2 or ci 6= cj. Then S is notfreely generated. For example, in list[N ] if some element y = nil as well asy = cons(x1, cons(x2, cons(x3, nil))) for some arbitrary x1, x2, and x3. Whatcan go wrong?

Exercise 6.14 Explain what can go wrong. (Hint:) Consider length(y).Can we get two possible answers when length is applied to the same y?

Page 110: Computation Engineering Applied Automata Theory and Logic

102 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

With freely generated sets, we can be assured that functions that are induc-tively defined over them exist, and are unique. With non-freely generatedsets, such functions may not have a unique definition.

6.2 Induction Principles

We now provide a brief tour through some of the induction principles thatwe shall use repeatedly. We begin with a recap of the two familiar principlesof induction over N , namely arithmetic induction and complete induction.We shall demonstrate that these principles are equivalent.

6.2.1 Induction over natural numbers

Generally the induction principle over natural numbers is stated in two forms,namely as arithmetic induction and as complete induction. We now statethese forms and prove them equivalent.

Arithmetic: The principle of arithmetic induction states that for all P , ifwe can show P (0) and for all x we can show that P (x−1) implies P (x), thenfor all x, P (x). In symbols,

∀P : ∧ P (0)∧ ∀x > 0 : P (x− 1)⇒ P (x)⇒ (∀x : P (x))

Complete: The principle of complete induction states that for all P , if wecan show that for all y, for all x < y P (x) implies P (y), then for all x, P (x).In symbols,

∀P : ∀y : (∀x < y : P (x))⇒ P (y)⇒ (∀x : P (x))

Proof of equivalence: To prove that these induction principles are equiva-lent, we proceed as follows. First pick an arbitrary predicate P for which theproof is being done. Then, these induction principles are of the form A⇒ Band C ⇒ B (A standing for arithmetic and C for complete). Therefore, wemerely need to prove A ⇔ C, i.e., that the antecedents of these inductiontheorems are equivalent.

Page 111: Computation Engineering Applied Automata Theory and Logic

6.2. INDUCTION PRINCIPLES 103

1. We need to prove A⇔ C, where

A = P (0) ∧ ∀x > 0 : P (x− 1)⇒ P (x)

and

C = ∀y : (∀x < y : P (x))⇒ P (y).

2. (A⇔ C) ≡ (A⇒ C) ∧ (C ⇒ A)

3. Let’s prove (C ⇒ A). I’ll leave (A⇒ C) as an exercise for you (Exer-cise 6.15.)

4. Here there are two approaches possible: one is to prove (C ⇒ A) itself,and the other is to prove the contrapositive form (¬A⇒ ¬C). We nowdetail the proof using the contrapositive form (the other form is quitesimilar).

5. ¬A = ¬P (0) ∨ ¬(∀x > 0 : P (x− 1)⇒ P (x)).

6. Let’s assume ¬P (0).

7. Now, ¬C = ∃y : (∀x < y : P (x)) ∧ ¬P (y).

8. Consider y = 0. For this y, there is no x < y. Thus, the (∀x < y : P (x))part is vacuously true.

9. Thus, by the “infinite disjunction” property of ∃,

∃y : (∀x < y : P (x)) ∧ ¬P (y)

reduces to

¬P (0) ∨ . . .which obviously follows from the assumption ¬P (0).

10. This finishes one of the cases of ¬A. Let us move on to the other case,by now assuming

¬(∀x > 0 : P (x− 1)⇒ P (x)).

This means (∃x > 0 : P (x− 1) ∧ ¬P (x)).

Page 112: Computation Engineering Applied Automata Theory and Logic

104 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

11. (I know how to “pull” a magic assumption here, but won’t do it, as Iwant to illustrate this proof getting stuck and having to come back todiscover the magic.)

12. Since (∃x > 0 : P (x− 1)∧¬P (x)), assume a witness for x. Call it x0.

13. Thus, we have P (x0 − 1) ∧ ¬P (x0) as our assumption.

14. We can instantiate the y in C to x0, to get (∀x < x0 : P (x))⇒ P (x0).

15. Here, we are stuck. We cannot instantiate the x in the above formula,as it lies under an “implicit negation.” (Anything belonging to the an-tecedent of an implication is under an implicit negation.) Said anotherway, if ∀, an infinite conjunction, implies something, we cannot saythat one of the conjuncts implies the same thing.)

16. Revisit Step 12. In there, assert that the witness x0 is the first such xsuch that P (x− 1) ∧ ¬P (x).

17. Now, consider (∀x < x0 : P (x)) ⇒ P (x0) again. Clearly, because ofthe way we selected x0, for all x < x0 we do have P (x) holding true.So we are left with P (x0). Since this is false, we have ¬C again.

18. Thus, for both cases resulting from ¬A we have ¬C, or in other words,¬A⇒ ¬C, or C ⇒ A.

Exercise 6.15 Prove the remaining case of A⇒ C.

6.2.2 Noetherian Induction

Both arithmetic and complete induction are special cases of the noetherianinduction rule stated in terms of well-founded partial orders. A well-foundedpartial order ¹ is one that has no infinite descending chains of the form. . . ¹ a−3 ¹ a−2 ¹ a−1 ¹ a0. The total order (N,≤) is a special case ofa well-founded partial order. Other well-founded partial orders include thelexicographic ordering of words, subset ordering, etc. In a general setting,well-founded partial orders can have multiple minimal elements (giving rise

Page 113: Computation Engineering Applied Automata Theory and Logic

6.2. INDUCTION PRINCIPLES 105

to multiple basis cases). For example, consider the ordering between closedintervals over natural numbers

¹= 〈[a, b], [A,B]〉 | a ≥ A ∧ b ≤ B.

We have [2, 2] ¹ [1, 3], but neither [2, 2] ¹ [3, 3] nor [3, 3] ¹ [2, 2]. In thiscase, we have an infinite number of minimal elements.The principle of noetherian induction: Suppose (W,¹) is a well-foundedpartial order. Suppose property P holds for all minimal elements inW . Also,for all non-minimal elements x, if for all y ¹ x P (y) implies P (x). Then∀x.P (x). In symbols,

∀P : ∧i∈min(W ) P (i)∧ ∀x /∈ min(W ) : (∀y ¹ x : P (y))⇒ P (x)⇒ (∀x : P (x))

Exercise 6.16 Provide an example of a well-founded partial order with morethan one minimal element.

Exercise 6.17 Suppose we have to prove a predicate P over all intervals,as defined in Exercise 5.5. (For definiteness, let P state that the averagevalue of an interval is obtained by adding the extreme values of the intervaland dividing by two.) Show how the principle of Noetherian induction can beemployed for this task.

6.2.3 Structural

The principle of structural induction can easily be seen to be a special caseof the principle of Noetherian induction by selecting ¹ to keep track of thenumber of “layers” of application of constructors. Specifically, min(W ) =B = bi | i ∈ m, and

preceq = 〈x, y〉 | x ∈ B ∨ x −→ y

where x −→ y means there exists a sequence of constructors that can beapplied to x to obtain y.

Exercise 6.18 Develop a formula for the number of leaf nodes in a k-arytree. Prove the formula by structural induction.

Page 114: Computation Engineering Applied Automata Theory and Logic

106 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

6.3 Putting it All Together: The Pigeon-Hole

Principle

The colloquial version of the pigeon-hole principle states that if there aren pigeons and n − 1 holes, then surely there is a hole with more than onepigeon. Stated in general, we can say that there is no total injection from afinite set S into any of its proper subsets. This is not true of infinite sets, asthe exercise below shows.

Exercise 6.19 Show that there exist infinite sets S such that there is a totalinjection from S into one of its proper subsets.

We can prove the pigeon-hole principle using induction, as well as proofby contradiction.

Exercise 6.20 Finish the above proof.

Exercise 6.21 Consider the set of characteristic formulae, CF (x, y), overtwo variables x and y. This set can be inductively defined by the followingrules:

1. true ∈ CF (x, y) and false ∈ CF (x, y)

2. If f1 ∈ CF (x, y) and f2 ∈ CF (x, y), then

(a) f1 ∧ f2 ∈ CF (x, y)

(b) f1 ∨ f2 ∈ CF (x, y)

(c) ¬f1 ∈ CF (x, y)

(d) (f1) ∈ CF (x, y)

3. CF (x, y) is the intersection of all such sets.

While we allow the use of paranthesis ( and ), they may be left out with theconvention that ¬ has the highest precedence, followed by ∧ and finally ∨.

Examples of formulae in CF (x, y) are (x∨y), x∨y, false, (true), (x∧¬x),(x ∧ y ∨ x ∧ y ∨ ¬x), etc. Clearly many of these characteristic formulas areequivalent, in the sense that they denote the same relation.

Page 115: Computation Engineering Applied Automata Theory and Logic

6.4. HOW RELIABLE ARE PROOFS? 107

1. Show that (CF,⇒) forms a preorder. ⇒ is the implication operator.〈x, y〉 ∈⇒ precisely when the formula x ⇒ y is valid (or ‘is a tautol-ogy,’ or ‘is true for all x, y’).

2. Show that ≡= (⇒ ∩ ⇒−1)x is an equivalence relation. Recall that⇒−1

can be written as ⇐.

3. How many equivalence classes does the equivalence relation (CF,≡)have? Show by going through the definition of equivalence classes givenearlier (specifically, elements(R)/R).

4. Argue that two formulas f1 ≡ f2 denote the same relation, and hence[f1] = [f2].

5. Arrange the relations denoted by the equivalence classes of CF (x, y)into a lattice, clearly pointing out the glb and lub.

6.4 How reliable are proofs?

This book is about “automata” in the broad sense of the word introducedin Chapter 1. You will be learning automata-theoretic concepts as well aswriting proofs. How does one write proofs? How does one ensure that proofsare correct? How does one present proofs in such a way that the high-levelpoints are clear, and others can believe in your proof? There are no easyanswers to these questions; nevertheless, these are such important questionsthat a discussion is in order.

6.4.1 Beginner’s mistakes

Experience has shown that most proofs written by beginners are fatallyflawed. For instance, it is easy to simply forget to prove one of the directionsof an iff proof. Other common mistakes include using unproven facts, inusing totally circular definitions, etc. While there is no point fretting overone’s past mistakes, good ways to avoid fundamental mistakes are to proof-read one’s proof presentations, write it out neatly, preferably in a bullettedfashion, and presenting one’s proofs to others. Other ways to avoid thesemistakes include a careful observation of how “common situations” are han-dled, and remember them by heart. For instance, while negating A ⇒ B,

Page 116: Computation Engineering Applied Automata Theory and Logic

108 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

since A is already “under a negation” (because A ⇒ B ≡ ¬A ∨ B), it doesnot get negated in the final result, which is A ∧ ¬B. We shall try to catalogsuch common situations to help avoid whole classes of mistakes.

6.4.2 Mistakes due to inattention to detail

After passing the initial hurdles, the second category of “mistakes” are sim-ply due to inattention to detail, or due to unfamiliarity with the particulardomain. Here are some examples of common mistakes (the details are unim-portant now; only the pattern of the mistakes need to be noted; the chapter‘Conclusions’ presents a summary of many of the abuses of theory):

1. Misusing the unidirectional Pumping Lemma: We shall study severalPumping lemmas of the form A ⇒ B as well as A ⇔ B. An exampleof a Pumping lemma of the form A ⇒ B is Regular(L) ⇒ C whereC is a big complicated formula. The recommended usage is to assumefor some language L that Reg(L) holds, and to find that ¬C (i.e.,a contradiction), thus proving ¬Reg(L). Many students abuse thisargument to try and prove Reg(L).

2. Forgetting to do full case-analysis: Even after grasping the PumpingLemma, many students forget to do full case analysis to derive con-tradictions in all cases. Similar mistakes are often repeated in doingproofs by reducibility, where also contradictions must be obtained inall possible cases.

3. Forgotten cases of definitions: Here, my favorite is relating to NP-completeness proofs, where the full definition of NPC is not kept inmind. Many students forget to show that the given problem is in theclass NP, thus leaving open the question of whether the problem is evendecidable (i.e., it is not enough to establish polynomial reducibility).

4. Abuse of hard theorems: Theorems such as the Rice’s theorem aresimply not well understood, and hence abused. What appears to be a“magic theorem” or “panacea” really isn’t.

Here, there is no quick answer except to gain experience, as well as be formalabout definitions.

Page 117: Computation Engineering Applied Automata Theory and Logic

6.4. HOW RELIABLE ARE PROOFS? 109

6.4.3 Experts mistakes and the ‘social process’

Experts do make mistakes! Lamport [?] observes that many of the proofspublished in mathematics are simply wrong. In fact, everyone knows that,and the attitude at this level is not to “denigrate” or “get even” with theperson, but to help fix the proofs. Therefore, the best advice one can passon is

“How do I present my proofs so that it helps others easily spotmy mistakes and correct them?”

The intent here is not to rely on the social process [?] exclusively to debugone’s proofs.

Proofs do have value even when flawed. Proofs are, after all, programs, al-beit in a mathematical language, and hence prone to ‘programming errors[?].’While mathematical notation helps you avoid nasty errors due to pointersoverflowing their ranges, etc, a poorly written formal description is no betterthan a program written an insidiously low level and hence fraught with manydangers.

6.4.4 Whether to trust machine-checked proofs

Mechanical theorem proving has been developed to be able to machine-checkproofs. Other tools such as model-checkers and finite-state reasoning toolshave also been developed to verify correctness. In a well-developed mechan-ical reasoning system, all the critical reasoning steps are carried out inside alogic kernel that is very compact and hence easy to verify through repeateduse as well as through the social process. In Chapters 16 and 17, we shall geta taste of some of these tools. While these tools are no panacea, they haveoften helped find very subtle flaws that, for all practical purposes, cannot bedetected through casual inspection or repeated simulation using ad hoc testcases. Continued development of these tools and techniques is indeed a signof maturation of our area. For additional details, please refer to [?, ?, ?, ?, ?].

Page 118: Computation Engineering Applied Automata Theory and Logic

110 CHAPTER 6. MATHEMATICAL LOGIC, INDUCTION, PROOFS

Page 119: Computation Engineering Applied Automata Theory and Logic

Chapter 7

Dealing with Recursion

Recursion is a topic central to computer science. Lambda calculus offers us avery elegant (and fundamental) way to model and study recursion. In a bookon computability and automata, such a study also serves another purpose:to concretely demonstrate that Lambda calculus provides a universal mecha-nism to model computations, similar to the role played by Turing machines.While this chapter can be skimmed, or even skipped, we have taken sufficientpains to make this chapter as “friendly” and intuitive as possible, permit-ting this chapter to be covered without spending too much time. Coveringthis chapter will permit fixed-point theory to be used as a conceptual “glue”in covering much important material, including while studying context-freegrammars, state-space reachability methods, etc.

7.1 Recursive definitions

Let’s get back to function ‘Fred,’ but make it recursive, thus apparentlymaking it hard to “de-Fred:”

function Fred x = if (x=0) then 0 else x + Fred(x-1)

It is quite tempting to arrive at the following de-Freded form:

function(x) = if (x=0) then 0 else x + self(x-1)

However, it would be really nice if we can avoid using such ad hoc conventions,and stay within the confines of the Lambda notation as introduced earlier.We shall soon demonstrate how to achieve this; it is however instructive toexamine one more recursive definition, now involving a function called Bob:

111

Page 120: Computation Engineering Applied Automata Theory and Logic

112 CHAPTER 7. DEALING WITH RECURSION

function Bob(x) = Bob(x+1)

One would take very little time to conclude that this recursive definitionis “plain nonsense,” from the point of view of recursive programming. Forexample, a function call Bob(3) would never terminate. However, unlesswe can pin down exactly why the above definition is “nonsense,” we willhave a very ad hoc and non-automatable method for detecting nonsensicalrecursive definitions - relying on human visual inspection alone. Certainlywe do not expect humans to be poring over millions of lines of code manuallydetecting nonsensical recursion. Here, then, is how we will proceed to “de-Fred” recursive definitions (i.e., create irredundant names for them):

• We will come up with a ‘clever’ Lambda expression, called Y, that willallow recursion to be expressed purely within the framework of Lambdacalculus. No arbitrary strings such as “Fred” will be stuck inside thedefinition.

• Then we will be able to understand recursion in terms of solving anequation involving a function variable F .

• We will then demonstrate that recursive definitions ‘make sense’ whenwe can either demonstrate that such equations have a unique solution,or if there are multiple solutions, we can select using a sound criterionwhich of these solutions ‘makes sense’ as far as computers go.

7.1.1 Recursion viewed as solving for a function

Let us write our Fred function as an equation:

Fred = lambda x. if (x=0) then 0 else x + Fred(x-1)

The above equation can be rewritten as

Fred = (lambda Fred’ . lambda x . if(x=0) then 0 else x + Fred’(x-1)) Fred

The fact that this equation is equivalent to the previous one can be easilyverified: we can apply the Beta rule, plugging in Fred in place of Fred’ toget back the original form.

We now can apply the Alpha rule, and change Fred’ to y, to obtain thefollowing:

Fred = (lambda y . lambda x . if(x=0) then 0 else x + y(x-1)) Fred

Page 121: Computation Engineering Applied Automata Theory and Logic

7.1. RECURSIVE DEFINITIONS 113

Well, we are almost done eliminating the redundant name “Fred”. Whatwe have achieved is that we have expressed Fred using an equation of theform

Fred = H Fred

where H is the Lambda expression (lambda y . lambda x . if(x=0)

then 0 else y(x-1)). Note that H contains no trace of Fred.

7.1.2 Fixed-point equations

We now make a crucial observation about the nature of the equation Fred =

H Fred. Recall that juxtaposing H and Fred is, in Lambda calculus, equiva-lent to applying H to Fred. If we wish, we can rewrite the above equation asFred = H(Fred) to express it in the more familiar syntax of “f(x)”—for fapplied to x. Note that there is something peculiar going on; when we feedFred to H, it spits out Fred. The application of H seems to be “stuck” atFred.

In mathematics, an equation of the form x = f(x) is called a fixed-pointequation. Think of a fixed-point as a fixed point, i.e., an “immovable point.”It appears that things are “stuck” at x; even if we apply f to x, the result isstuck at x.

Can we find such “stuck function applications?” Surely!

• Take a calculator, and clear its display to get 0 on the display. Thenhit the cos (cosine) key to compute cos(0) = 1.

• Hit cos again, to get 0.9998477

• Hit cos again, to get 0.99984774

• Hit cos again, to get 0.99984774 - we are now “stuck” at the fixed-pointof cos. (The number of steps taken to achieve the fixed-point will, ofcourse, depend on the precision of your calculator.)

• Now (assuming you have the factorial function on your calculator),compute 0, 0!, 0!!, etc., then 2, 2!, 2!!, etc., and finally 3, 3!, 3!!, etc.How many fixed-points did you discover for factorial?

Exercise 7.1 Perform such fixed-point experiments using a calculator (orpaper and pencil) for four other functions, finding a fixed-point in each case.In each case, answer whether the fixed-point is unique (are there multiplefixed-points for any of the functions you choose?). Choose at least one func-tion that has multiple fixed-points.

Page 122: Computation Engineering Applied Automata Theory and Logic

114 CHAPTER 7. DEALING WITH RECURSION

Here is another way to get a “fixed-point out of a xerox xerox machine.”More specifically, we can get the fixed-point of the image transformation

function of a xerox machine. Go xerox your face; then xerox the xerox, xeroxthe xerox of the xerox, etc. In most xerox machines, the image stabilizes, byturning all gray regions to black or (sometimes) white. Such a stable imageis then one fixed-point of the image transformation function of the xeroxmachine.

Coming back to our example, we want a fixed-point, namely a func-tion Fred, that solves the fixed-point equation Fred = H(Fred). Ideally, wewould like this fixed-point to be unique, but if that’s not possible, we wouldlike some way to pick out the fixed-point that corresponds to what computerswould compute, should they handle the recursive program.

The beauty of Lambda calculus is that it does not fundamentally dis-tinguish between functions and “values”, and so the principles of obtainingfixed-points remain the same, independent of whether the fixed-points inquestion are functions, numbers, or images. For the example at hand, ifFred is a fixed-point of H, we must

• either ensure that there is only one fixed-point, thus giving a uniquesolution to Fred, or

• in case multiple fixed-points exist, find a canonical way of picking thedesired fixed-point (solution).

We shall resolve these issues in what follows.

7.1.3 The Y operator

There exists a class of (almost “magical”) functions, the most popular onebeing function Y below, that can find the unique fixed-points of functionssuch as H!

Y = (lambda x. (lambda h. x(h h)) (lambda h. x(h h)))

In other words, we claim that (Y f) is the fixed-point of any arbitraryfunction f. Things seem almost too good to be true: if we desire the fixed-point of any function f expressed in Lambda calculus, simply “send Y afterit.” In other words, Y (f) would be the fixed-point. For the above to be true,the following fixed-point equation must hold:

(Y f) = f(Y f).

Page 123: Computation Engineering Applied Automata Theory and Logic

7.1. RECURSIVE DEFINITIONS 115

Here is how the proof goes:

Y f = (lambda x. (lambda h. x(h h)) (lambda h. x(h h))) f

= (lambda h. f(h h)) (lambda h. f(h h)) <-- look --|

|

|

= f( (lambda h. f(h h)) (lambda h. f(h h)) ) -------

= f( Y f )

In the above derivation steps, in the penultimate step, we have f applied to abig long Lambda form that was obtained in the second step as an expansionof Y f. Thus, in the last step, we can obtain the simplification indicated.Okay, now, finally, we have successfully “de-Freded” our original recursivedefinition for Fred. We can write Fred as

Fred = Y (lambda y . lambda x . if(x=0) then 0 else x + y(x-1))

where the right-hand side contains no trace of Fred. The right-hand sideis now an irredundant name defining what was originally cast as an explicitrecursion.

7.1.4 Illustration of reduction

While we have shown how to turn the equation Fred = H Fred into Fred =(Y H), does the new irredundant name really capture the reduction semanticsof functional evaluation? Let us demonstrate that indeed it does, by applyingFred to an argument, say 5:

Fred 5 = (Y H) 5

= H (Y H) 5

= (lambda y . lambda x . if(x=0) then 0 else x + y(x-1)) (Y H) 5

= (lambda x . if(x=0) then 0 else x + (Y H)(x-1)) 5

= (if(5=0) then 0 else 5 + (Y H)(5-1))

= 5 + (Y H) 4

= ...

= 5 + 4 + (Y H) 3

Page 124: Computation Engineering Applied Automata Theory and Logic

116 CHAPTER 7. DEALING WITH RECURSION

= ...

= 5 + 4 + 3 + 2 + 1 + 0

= 15

From the above, one can observe that the main characteristic of Y is that ithas the ability to “self replicate” a Lambda expression. Notice how a copy of(Y H) is “stashed away” “just in case” there would be another recursive-call.Self-replication, unfortunately, is also the basis on which many maliciousprograms such as computer viruses operate. In advanced computability the-ory, the deep connections between “self replication” and computability arecaptured in the so called recursion theorem. The interested reader is encour-aged to read-up on this topic, including Ken Thompson’s widely cited article“Reflections on trusting trust,” to be fully informed of the true potentialsas well as societal consequences of computers - on one hand, computers aremankind’s most impressive invention to date; on the other hand, they areprone to abuse stemming either from innocent oversight or malicious intent- in both cases demanding the eternal vigil of the computing community toguard against, detect outbreaks, and restore normal operations if bad thingsdo happen.

Exercise 7.2 What is one fixed point of Y itself? (Hint: Straightforwardfrom definition of Y as Y f = f(Y f).)

Exercise 7.3 When it comes to computing, we “take many things for granted.”Read Ken Thompson’s “Reflections on trusting trust” fromhttp://www.acm.org/classics/sep95/. (If you don’t follow this article en-tirely, do your best to at least follow the high-level arguments. Later we shallrevisit this topic under recursion theorem.) Try compiling and running thefollowing two programs. Then modify them (even slightly) to obtain differ-ent programs which are still self-replicating. Compile and run the modifiedprograms to verify that they indeed do self-replicate.

main()char *c="main()char *c=%c%s%c;printf(c,34,c,34);";printf(c,34,c,34);

#define T(a) main()printf(a,#a);

T("#define T(a) main()printf(a,#a);\nT(%s)")

Page 125: Computation Engineering Applied Automata Theory and Logic

7.2. RECURSIVE DEFINITIONS AS SOLUTIONS OF EQUATIONS 117

7.2 Recursive definitions as solutions of equa-

tions

The reduction behavior using Y indeed tracks the normal function evaluationmethod.

Fred 5 = (lambda x . if(x=0) then 0 else x + Fred(x-1)) 5

= if(5=0) then 0 else 5 + Fred(5-1)

= 5 + (Fred 4)

= (lambda x . if(x=0) then 0 else x + Fred(x-1)) 4

= 5 + (if(4=0) then 0 else 4 + Fred(4-1))

= 5 + 4 + (Fred 3)

= ...

= 5 + 4 + 3 + 2 + 1 + 0

= 15

Thus, what really is the advantage of a fixed-point formulation? To betterunderstand this, let us study the connection between recursion and solvingfor functions more deeply.

The fact that we have equated Fred to a Lambda term (lambda x. if

(x=0) then 0 else x + Fred(x-1)) containing Fred suggests that Fred isa solution for f in an equation of the form

f = (λx.if (x = 0) then 0 else x+ f(x− 1))

How many solutions exists for f? In other words, how many differentfunctions can be substitute in place of f and satisfy the equation? Also, ifthere are multiple possible solutions, then which of these solutions did (Y H)

correspond to? Might the function f0 below, which is undefined over itsentire domain N , for instance, be a solution?

f0 = (λx. ⊥)

Here, ⊥ stands for “undefined” or “bottom” valueSubstituting f0 for f , the right-hand simplifies to the function

(λx.if (x = 0) then 0 else x+ ⊥),

or(λx.if (x = 0) then 0 else ⊥).

This function is different from f0 in that it is defined for one input, namely0. Hence the above equation is not satisfied. Calling this function f1, let us

Page 126: Computation Engineering Applied Automata Theory and Logic

118 CHAPTER 7. DEALING WITH RECURSION

see whether it would serve as a solution. Substituting f1 on the right-handside, we get

(λx.if (x = 0) then 0 else x+ f1(x− 1))

Simplifying f1(x− 1), we get

(λx.if (x = 0) then 0 else ⊥)(x− 1)

= if ((x− 1) = 0) then 0 else ⊥

= if (x = 1) then 0 else ⊥

The right-hand side now becomes

(λx.if (x = 0) then 0 else x+ (if (x = 1) then 0 else ⊥))

(λx.if (x = 0) then 0 else if (x = 1) then x+ 0 else x+ ⊥)

(λx.if (x = 0) then 0 else if (x = 1) then 1 else ⊥)

which (calling it f2) is yet another function that defined for one more inputvalue. In summary, substituting f0 for f , one gets f1, and substituting f1,one gets f2. Continuing this way, each fi turns into a function fi+1 that isdefined for one more input value. Thus, while none of these functions satisfiesthe equation, in the limit of these functions is a total function that satisfiesthe equation, and hence is a fixed point. (Compared with earlier examples,such as the cos function, which seemed to “stabilize” to a fixed-point in afew steps on a finite-precision calculator, in case of the fi series, we achievethe fixed-point only in the limit.) This limit element happens to be the leastfixed-point (in a sense precisely defined in the next section), and is written

µx.(if (x = 0) then 0 else x+ f(x− 1)).

Calling this least fixed-point function be called h. It is easy to see that h isthe following function:

h(n) = Σni=0 i.

Thus, it is reassuring to see that the least fixed-point is indeed the func-tion that computes the same “answers” as function Fred would compute ifcompiled and run on a machine. It turns out that in recursive program-ming, the “desired solution” is always the least fixed-point, while in other

Page 127: Computation Engineering Applied Automata Theory and Logic

7.2. RECURSIVE DEFINITIONS AS SOLUTIONS OF EQUATIONS 119

contexts (e.g., in reachability analysis of finite-state machines demonstratedin Chapter 9), that need not be true.

The ‘solution’ point of view for recursion also explains recursive defini-tions of the form

function Bob(x) = Bob(x+1).

The only solution for function Bob is the everywhere undefined functionλx. ⊥. To see this more vividly, one can try to de-Bob the recursion toget

Bob = Y (lambda y . lambda x . y(x+1))

Suppose H = (lambda y . lambda x . y(x+1)). Now, supplying a value,such as 5 to Bob, and continuing as with function Fred, one obtains the fol-lowing reduction sequence:

Bob 5 = (Y H) 5

= H (Y H) 5

= (lambda y . lambda x . y(x+1)) (Y H) 5

= (lambda x . (Y H)(x+1)) 5

= (Y H)(5+1)

= (Y H) 6

= ...

= (Y H) 7

= ...

= (non-convergent)

In other words, Bob turns out to be the totally undefined function, or “bottomfunction.”

7.2.1 The least fixed-point

In the above example, we obtained a fixed-point h which we asserted to be the“least” in a sense that will be now made clear. In general, given a recursiveprogram, if there are multiple fixed-points,

Page 128: Computation Engineering Applied Automata Theory and Logic

120 CHAPTER 7. DEALING WITH RECURSION

• The desired meaning of recursive programs corresponds to the leastfixed-point, and

• The fixed-point finding combinator Y is guaranteed to compute the leastfixed-point [?].

To better understand these assertions, consider the following four defini-tions of functions of type Z × Z → Z, where Z is the integers [?]:

f1 = λ(x, y) . if x = y then y + 1 else x+ 1

f2 = λ(x, y) . if x ≥ y then x+ 1 else y − 1

f3 = λ(x, y) . if x ≥ y and x− y is even then x+ 1 else ⊥

Now consider the recursive definition:

F (x, y) = if x = y then y + 1 else F (x, F (x− 1, y + 1))

We can substitute f1, f2, or f3 in place of F and get a true equation!

Exercise 7.4 Verify this assertion.

However, of these functions, f3 is the least defined function in the sense that

• whenever f3(x) is defined, fi(x) is defined for i = 1, 2, but not viceversa, and

• there is no other function (say f4) that is less defined than f3 and alsoserves as a solution to F .

To visualize concepts such as “less defined,” imagine as if we were plottingthe graphs of these functions. Now, when a function is undefined for an xvalue, we introduce a “gap” in the graph. In this sense, the graph of theleast fixed-point function is the “gappiest” of all – it is a solution, and is themost undefined of all solutions – it has the most number of “gaps.” Thesenotions can be precisely captured in terms of least upper bounds of pointedcomplete partial orders [?, ?]. In our example, it can be shown that f3 is theleast fixed-point for the recursion.

Page 129: Computation Engineering Applied Automata Theory and Logic

7.3. FIXED-POINTS IN AUTOMATA THEORY 121

7.3 Fixed-points in automata theory

We will have many occasions to appeal to the least fixed-point idea in thisbook. Here are some examples:

• Cross-coupled combinational gates attain stable states defined by fixed-points. For instance, two inverters in a loop become a flip-flop thatstores a 0 or a 1. These are the two solutions of the recursive circuit-node equation x = not(not(x))1.

• Three inverters in a loop form a ring oscillator. The reason, to a firstapproximation (see [?] for detailed answers), is that they are all trying,in vain, to solve the recursive node equation x = not(not(not(x))).

• Context-free grammar productions are recursive definitions of languages.The language defined by such recursive definitions can be determinedthrough a least fixed-point iteration similar to what we did by startingwith ⊥; for context free grammars, the empty language φ serves as the“seed” from which to begin such iterations.

• In the section pertaining to Binary Decision Diagrams, we will formu-late a process of computing the set of reachable states as a fixed-pointcomputation.

• In the section on non-deterministic automata, we will write recursivedefinitions for Eclosure and justify that such recursive definitions arewell-defined (i.e., they are not nonsensical similar to the recursion forfunction Bob, for which no solution exists).

It is also worth noting that while least fixed-points are most often of interest,in many domains such as temporal logic, the greatest fixed-points are also ofinterest.

The functional (higher order function) H, where

H = (λy.λx.if (x = 0) then 0 else x+ y(x− 1))

1Of course, this is a simplified explanation, as a golden wedding ring can also be thoughtof as a recursive node equation x = x = not(not(x)); the difference is of course that thereis no “amplification” that occurs around the wedding ring.

Page 130: Computation Engineering Applied Automata Theory and Logic

122 CHAPTER 7. DEALING WITH RECURSION

has an interesting property: it works as a “bottom refiner!” In other words,when fed the “everywhere undefined function” f0 (called the “bottom func-tion”), it returns the next better approximation of the least fixed-point hin the form of f1. When fed f1, it returns the next better approximationf2, and so on. It is strange that fixed-point theory is capable of calculatingthe meaning of recursive programs starting from the most uninteresting offunctions, namely the ‘bottom’ or ⊥ function. Later when we study state-space reachability analysis techniques, context-free grammars, etc., we willappeal to this “bottom-refining” property. We will be identifying a suitable“bottom” object in each such domain.

Page 131: Computation Engineering Applied Automata Theory and Logic

Chapter 8

Strings and Languages

Now we embark on studying strings of symbols over a given alphabet, andlanguages, which are sets of strings. An alphabet is a finite collection ofsymbols. Usually, an alphabet is denoted by Σ. Alphabets are alwaysnon-empty. In most cases, alphabets are finite, although that is strictlynot necessary.

We said that an alphabet Σ is a finite collection of symbols. What cansymbols be? They can be letters, such as a, b, c, bits 0, 1, musical notes,Morse-code characters, a collection of smoke-signal puffs, hieroglyphics, kicksyou might administer to an errant vending machine, or just about anythingwe wish to regard as an indivisible unit of communication or activity. Wedo not worry about the inner structure of a symbol, even if it has one. Weemphasize this point early on, lest you be totally surprised when we startregarding such objects as regular expressions (expressions that denote lan-guages - they are discussed in Chapter 8) as symbols.

Strings are zero or more symbols in juxtaposition. Each symbol from analphabet can also be regarded as a string of length one. Languages are setsof strings. Each string or sequence can model sequences of key-strokes youmight type on a computer, a piano, a telegraph machine, etc. We call eachsuch sequence or string a computation, a run of a machine, etc. A vendingmachine has a computation consisting of coin plinks, candy ejects, and kicks.A formal machine such as a Turing machine has computations depending onwhat program is loaded into it, and what input is submitted on its tape.Mostly, we consider finite strings, although in Chapter 17, we will considerinfinite strings. Infinite strings are useful to model certain properties ofreactive systems - systems, such as operating systems that are supposed to

123

Page 132: Computation Engineering Applied Automata Theory and Logic

124 CHAPTER 8. STRINGS AND LANGUAGES

run forever (at least at a conceptual mathematical level).Strings and languages are fundamental to computer science theory. By

measuring runtimes or storage requirements of programs in terms of thelengths of their input strings, we are able to study time and space com-plexity. By studying patterns that sets of strings possess, we are able totaxonomize degrees of problem solving abilities (computational “power”). Infact, life seems to be full of strings (pun intended1.) We now proceed tosystematically examine this central role played by strings and languages incomputer science theory.

8.1 (Finite) Strings

The set of positions in a finite string is bounded (or, equivalently, they arebounded arrays of symbols). A string may also be empty, meaning it haszero symbols in it. In programming languages, we denote empty strings by"". In automaton books, symbols such as ε and λ are often used. We shalluse ε to denote empty strings. However, whenever the symbol ε confusesyou, think of it as "" and see if it helps. Examples of non-empty stringsfrom programming languages are "abcd", "aaa", etc. In automata theory,we shall model these strings using abcd and aaa.

To avoid some confusion early on, we stress upon a few basic conven-tions. In a context where we are talking about strings, ‘123’ is a string oflength three, consisting of the symbols 1, 2, and 3, and not a single integer.Thus, when asked to design a DFA that has alphabet 0, . . . , 9 and acceptsall numeral sequences divisible without remainder by 3, the DFA reads thesymbols one by one, and checks for the familiar condition for divisibility by3 (the digits add-up to a number divisible by 3). Thus, when given the input126, it reads three symbols—not the entire number 126 in one fell swoop.

8.2 Languages

A language is a set of strings. Like strings, languages are a central notion incomputer science theory. Questions pertaining to undecidability as well ascomplexity can be studied in terms of languages. The smallest language is

1DNA sequences are strings over the symbol-set A, C, T , and G – see “Cartoon Guideto Genetics,” by Larry Gonick and Mark Wheelis.

Page 133: Computation Engineering Applied Automata Theory and Logic

8.2. LANGUAGES 125

φ, since |φ| = 0. The next larger language has one string in it. Of coursethere are many such languages containing exactly one string. One example isthe language ε, which contains one string that happens to be empty. Giventhe alphabet Σ = 0, 1, a few languages of size 1 are 1, 0, 0101, afew languages of size 2 are 0, 00, 0, 0101, etc. Many of the languages westudy will be infinite in size.

8.2.1 How many languages are there?

Are there countably many languages, or uncountably many languages? Theanswer is that there are uncountably many languages over any alphabet.In other words, there are as many languages over Σ = 0, 1 as there areReal numbers. This is because each language L can be represented by aninfinite characteristic sequence which tells which (among the infinite stringsover Σ = 0, 1) are present in L and which are absent. Since each suchcharacteristic sequence can be read as a real number in the range [0, 1],the set of all such characteristic sequences is uncountable, as explained inSection 4.2.5.

Exercise 8.1 How many languages over an alphabet of size 1 exist?

Exercise 8.2 Using the Schroder-Bernstein Theorem, argue that there areas many languages over alphabet 0 as there are over alphabet 0, 1.

Exercise 8.3 Name the main difference between this proof of the uncount-ability of languages versus the uncountability of the set of Reals.

Why does it matter that there are uncountably many languages? To an-swer this question, let us briefly consider how we shall model computations.We shall study computations in terms of languages accepted by Turing ma-chines. Each Turing machine M is a very simple computer program writtenfor a very simple model of a computer. M is started with an input stringx on its tape, and “allowed to run.” Each such run has three outcomes:M halts in the accept state, it halts in the reject state, or it loops (muchlike a computer program can go into an infinite loop). The set of all stringsx that cause M to halt in the accept state constitute the language of M .The language of a Turing machine is, in a sense, the essence or meaning ofa Turing machine. Now, each Turing machine M itself can be representedby a single finite string over Σ = 0, 1. (This is akin to viewing an entire

Page 134: Computation Engineering Applied Automata Theory and Logic

126 CHAPTER 8. STRINGS AND LANGUAGES

a.out file obtained by compiling a C program as a single string.) Since theset of all finite strings is countable, there can only be countably many Turingmachines, and hence countably many Turing machine languages. Thus thereare languages that are the language of no Turing machine at all. In a sense,these languages (called non Turing recognizable) carry “patterns” that are“beyond the reach of” (or “defy analysis by”) any man-made computationaldevice. Their existence is confirmed by the above cardinality arguments.

Exercise 8.4 Choose among the sets given below, those with cardinality ℵ2:

1. The set of all languages over alphabet Σ = 0, 1

2. The powerset of the set of all languages over alphabet Σ = 0, 1

3. The set of all languages over alphabet Σ = 0

4. The powerset of the set of all languages over alphabet Σ = 0

Length, Character at index, and Substring of a String

First, let us define some notation with respect to strings. Given string s, itslength is denoted by length(s). We have, for every string s, length(s) ≥ 0.For each position i ∈ length(s) (i.e., 0 ≤ i ≤ (length(s) − 1)) of a string s,s[i] denotes the character at position i. If i and j are positions within a strings, and j ≥ i, substr(s, i, j) is the substring of s beginning at i and endingat j, both inclusive. If j < i, substr(s, i, j) = ε. Thus, substr(apple, 0, 0) =a, substr(apple, 1, 1) = substr(apple, 2, 2) = p, substr(apple, 0, 2) = app,substr(apple, 2, 4) = ple, and substr(apple, 4, 3) = ε. For every string s,s = substr(s, 0, length(s)− 1).

Lexicographic orders for Strings

There are two systematic methods available for listing the contents of setsof words. The first is known as the lexicographic order, or “order as ina dictionary.” In this order, given two strings x and y, we compare thecharacters comprising x and y position by position. We do this for eachposition i ∈ min(length(x), length(y)). (Here, min finds the minimum oftwo numbers). Thus, if we compare apple and pig, we compare for eachposition i ∈ 3, as pig is the shorter of the two, and has length 3. While

Page 135: Computation Engineering Applied Automata Theory and Logic

8.2. LANGUAGES 127

comparing characters at a position, we compare with respect to the ASCIIcode of the character at that position2.

Given all this, for strings x and y such that x 6= y, x said to be strictlybefore y in lexicographic order, written x <lex y, under one of two circum-stances:

1. If there exists a position j ∈ min(length(x), length(y)) such that x[j] <y[j], and for all i ∈ j (meaning, for positions 0 . . . (j − 1)), x[i] = y[i].Thus, aaabb <lex aaaca, aaron <lex abate, apple <lex pig, and pig <lex

putter.

2. For all positions j ∈ min(length(x), length(y)), we have x[j] = y[j],and length(x) < length(y).

Definition: Define string identity, x =lex y to hold for two identical stringsx and y. Also define ≤lex to be <lex ∪ =lex, and >lex to be the complementof ≤lex.Definition: Given two strings x and y, x is before y in lexicographic orderiff x ≤lex y.Let us now go through some examples of lexicographic order:

• ε <lex aa follows from Condition 2 above. This is because there are nopositions within a length of 0, and so the only condition to be satisfiedis length(x) < length(y), which is true for x 6= y and x = ε. Sinceε <lex aa, we also have ε ≤lex aa.

• a <lex aa <lex aaa <lex aaaa . . .. Thus, these are also in the ≤lex

relation.

Exercise 8.5 Prove that ≤lex is a total order, while <lex is not.

Numeric orders for Strings

The idea of numeric order is of importance while systematically enumeratingsets. To motivate this idea, consider a lexicographic ordering of all stringsover alphabet a, b. Such a listing will go as follows: ε, a, aaa, aaaa, . . .. Inother words, not a single string containing a b will get listed. To avoid thisproblem, we can define the notion of a numeric order as follows. For twostrings x and y,

2The ASCII code totally orders all the characters available on modern keyboards. Referto the web or a book on hardware design to know what this total order is.

Page 136: Computation Engineering Applied Automata Theory and Logic

128 CHAPTER 8. STRINGS AND LANGUAGES

1. If length(x) = length(y), then x <numeric y exactly when x <lex y.

2. Otherwise, if length(x) < length(y), then x <numeric y.

3. Otherwise (length(x) > length(y)) x >numeric y.

As before, we define <numeric to be strictly before in numeric order, and

≤numeric=<numeric ∪ =lex

to before in numeric order, or simply “in numeric order.”Thus, the numeric order of listing the strings over alphabet a, b will yieldthe sequence

ε, a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb, aaaa, . . . .

Exercise 8.6 Viewing C programs as ASCII strings, obtain a good estimateof the number C programs of less than n bytes, for n ≤ 12? Obtain at leastleast five such C programs; compile them and demonstrate that they do notcause any compilation errors.

Hint: main(); is one such program, with byte-count 9. View each Cprogram as a string (so you could write "main();" for clarity, if you wish).In later discussions of strings, we will omit the string delimiters ".

Exercise 8.7 What is the set consisting of the first ten words in Englishoccurring in lexicographic order? (Follow any dictionary you may have accessto. Treat the upper and lower cases the same.)

Exercise 8.8 What is the set consisting of the first ten words in Englishoccurring in numeric order? (Follow any dictionary you may have accessto.)

Exercise 8.9 Suppose that in a strange language ‘Longspeak’ typeset in char-acters a through z, there are words of all lengths beginning with any letter.Consider a lexicographic order listing of all words in Longspeak. At what rank(ordinal position) does a word beginning with letter d appear? How about fora numeric order listing?

Considering the answers to these questions, suppose you are charged withthe task of producing a dictionary for Longspeak. Fortunately, you are toldthat you don’t have to include words of length beyond 30. Would you pro-duce your dictionary in lexicographic order or numeric order? Explain yourreasoning.

Page 137: Computation Engineering Applied Automata Theory and Logic

8.2. LANGUAGES 129

8.2.2 Operations on languages

Given languages L,L1, L2, new languages may be defined using various oper-ations. First, consider the set operations of union, intersection, set difference,and symmetric difference. These familiar set operations help define new lan-guages as defined below.

8.2.3 Languages defined through set operations

Union: L1 ∪ L2 = x | x ∈ L1 ∨ x ∈ L2Intersection: L1 ∩ L2 = x | x ∈ L1 ∧ x ∈ L2Set difference: L1 \ L2 = x | x ∈ L1 ∧ x /∈ L2Symmetric difference: (A \B) ∪ (B \ A)

Exercise 8.10 Define languages L1 and L2 over alphabet Σ = 0, 1 asfollows:

L1 = x | x is a string over Σ of odd lengthand

L2 = L1 ∩ x | x is a string of 0′s.What is the symmetric difference of languages L1 and L2?

8.2.4 Concatenation and Exponentiation

Concatenation performs the juxtaposition of strings from two languages. Ex-ponentiation is n-ary concatenation. These ideas are basically quite simple.Suppose we have a finite-state machine M1 that accepts strings containingeither an odd number of as or an even number of bs. Suppose we have anotherfinite-state machine M2 that accepts strings containing either an even num-ber of as or an odd number of bs. We can now build a new machine M whoseruns are of the form xy such that x is accepted by M1 and y by M2. We canalso build a new machine M ∗

1 whose runs are of the form xx . . . x, where x isrepeated some k ≥ 0 times, and x is a run of M1. The set of runs of thesenew machines can be captured using concatenation and exponentiation.We now formally define the concatenation and exponentiation operations onlanguages:Concatenation: L1 L2 = xy | x ∈ L1 ∧ y ∈ L2.

Page 138: Computation Engineering Applied Automata Theory and Logic

130 CHAPTER 8. STRINGS AND LANGUAGES

Exponentiation: First of all, for any language L, define L0 = ε. Thatis, the zero-th exponent of any language L is ε. Note that this means thateven for the empty language φ, φ∗ = ε. (The reason for this conventionwill be clear momentarily.)

Exponentiation may now be defined in one of two equivalent ways:

Definition-1 of exponentiation: For all n ≥ 1,

Ln = x1 . . . xn | xi ∈ L

Definition-2 of exponentiation:

For n > 0, Ln = xy | x ∈ L ∧ y ∈ Ln−1.

In the second definition, note that we should define the basis case for therecursion, which is L0. We must put into L0 anything that serves as theidentity element for string concatenation, which is ε. Hence, we define L0 =ε for any language, L.Consider a simple example:

a, aba ca, da = aca, ada, abaca, abada,while

ca, da3 = ca, da ca, da ca, dawhich is the set

cacaca, cacada, cadaca, cadada, dacaca, dacada, dadaca, dadada.Consider another example where L1 (which models the runs of M1) is the set

L1 = x | x is a sequence of odd number of a′s or even number of b′sand L2 (which models the runs of M2) is the set

L2 = x | x is a sequence of even number of a′s or odd number of b′s.Then, L1 L2 is a language in which each string consists of

• an odd number of as,• an odd number of bs,• an odd number of as followed by an odd number of bs, or• an even number of bs followed by an even number of as.

In addition, Lk1 is a language in which each string can be obtained as a k-fold

repetition of a string from L1.

Page 139: Computation Engineering Applied Automata Theory and Logic

8.2. LANGUAGES 131

8.2.5 Kleene Star, ‘∗’Star performs the union of repeated exponentiations:

L∗ = x | ∃k ∈ N : x ∈ Lk.This definition can also be written as

L∗ = ∪k≥0 Lk.or even as

L∗ = ∪k∈Nat Lk.

Notice that φ∗ = ε according to these definitions. This is because for anyk, φk is defined to be ε. In turn, this is so because we must have a basiselement in the recursive definition of concatenation. In our case, ε is theidentity for concatenation.Examples:

• a, b, c∗ is all possible strings over a, b, and c, including the emptystring ε.

• a, bc∗ is all possible strings formed by concatenating a and bc in someorder some number of times. In other words, all the strings in a, bck,for all k, are present in this set.

In a programming sense, star is iteration that is unable to keep track ofthe iteration count. The ability to “forget” the number of iterations is crucialto obtaining simpler languages, and ultimately decidability.

Exercise 8.11 Among the following languages, choose the ones for whichLL = L.

• L = φ• L = ε• L = x | x is an odd length string over0, 1• L = x | x is an even length string over0, 1• L = x | #0(x) = #1(x); here, #0 determines the number of 0s ina given string, and likewise for #1.• L = x | #0(x) 6= #1(x).

Exercise 8.12 Repeat the above exercise to meet the condition L∗ = L.

Exercise 8.13 Repeat the above exercise to meet the condition L∗ = LL =L.

Page 140: Computation Engineering Applied Automata Theory and Logic

132 CHAPTER 8. STRINGS AND LANGUAGES

8.2.6 Complementation

Using the just defined notion of star, we can now specify a universe Σ∗ ofstrings, and define complementation with respect to that universe:

L = x | x ∈ Σ∗ \ L.

Exercise 8.14 Complement each of the languages defined above.

8.2.7 Reversal

The notion of reversing a string is pretty straightforward - we simply putthe first character last, the second character penultimate, and so on. Thedefinition of reverse(s) for s being a string in some alphabet, is as follows:

reverse(ε) = ε

reverse(s as ax) = reverse(x) a

We use the as construct (as used in the programming language Standard ML)to elaborate a quantity. ‘s as ax’ means ‘view string s as a concatenation ofsymbol a and string x. The term ‘reverse(x) a’ says reverse x and appenda to its end.

For language L, reverse(L) is then reverse(x) | x ∈ L.

8.2.8 Homomorphism

A homomorphism is a function that maps strings to strings that respectsstring concatenation. In other words, if h : Σ∗ → Γ∗ is a homomorphismthat takes strings from some alphabet Σ to strings in an alphabet Γ (notnecessarily distinct), then it must satisfy two conditions:

• h(ε) = ε

• For string s = xy in Σ∗, h(xy) = h(x)h(y). In other words, any strings can be, prior to being mapped, broken into two pieces x and y in anyarbitrary manner and h can be applied to the individual pieces.

To obtain a better appreciation for the feature of a homomorphism that“it respects string concatenation,” let us consider something that is not ahomomorphism—say g. Let Σ = a, b, c, d and Γ = 0, 1, 2.• g(abcd) = 0

Page 141: Computation Engineering Applied Automata Theory and Logic

8.2. LANGUAGES 133

• g(ab) = 1

• g(cd) = 1

• g(s) = 2, for all other strings.

g is not a homomorphism because g(abcd) = 0 while g(ab)g(cd) is 11.So how do we ensure that something is a homomorphism? The most com-monly used approach to ensure that something is a homomorphism is tospecify a mapping that goes from symbols to strings, and then to “lift” it upto map from strings to strings. Let h

′be the symbols to string map, with

signatureh′

: Σ ∪ ε → Γ∗

and define h using h′. Here is an example. Let h

′be as follows:

• h′(ε) = ε

• h′(a) = 0

• h′(b) = 1

• h′(c) = h

′(d) = 2

Formally, h(s) is defined in terms of h′as follows:

• If length(s) > 1, then let x 6= ε and y 6= ε be such that s = xy. Then,h(s) = h(x)h(y).

• If length(s) = 1, then h(s) = h′(s).

Now, by definition, h respects string concatentation, and all works out well!Homomorphisms can be defined over languages also in a straightforwardmanner. Given L,

h(L) = y | h(x) = y for some x in L.

8.2.9 Inverse Homomorphism

Given a homomorphism h, an inverse homomorphism h−1 maps a string yto the maximal set of strings that y could have come from; formally:

h−1(y) = x | h(x) = y

Example: Consider h to be

h′(ε) = ε

Page 142: Computation Engineering Applied Automata Theory and Logic

134 CHAPTER 8. STRINGS AND LANGUAGES

h′(a) = 0

h′(b) = 1

h′(c) = h

′(d) = 2

Then, h−1(012) = abc, abd because 2 could have come either from c orfrom d. Inverse homomorphisms can be defined over languages also in astraightforward manner. Given L,

h−1(L) = ∪y∈L h−1(y).

In other words, for each y ∈ L, take h−1(y), and then take the union of allthese sets.

Further motivations

In parsing a programming language such as C, the parser appeals to a tok-enizer that first recognizes the structure of tokens (sometimes called lexemes)such as integers, floating-point numbers, variable declarations, keywords, andsuch. The tokenizer pattern-matches according to regular expressions, whilethe parser analyzes the structure of the token-stream using a context-freegrammar (the idea of a grammar will later be elaborated). What wouldhappen if one were to attempt to write a grammar at the character level?We can show that such a grammar can be obtained by substituting thecharacter-stream corresponding to each token in place of each token in amodular fashion. Moreover, thanks to the modular nature of the substitu-tion, the resulting grammar would also be context-free. Thus, main featureof a homomorphism is that of modular substitution.

Exercise 8.15 Consider the languages

• L1 = x | x is an odd length string over0, 1• L2 = x | x is an even length string over0, 1

1. Argue that the following map is a homomorphism (call it h)

h(ε) = ε

h(0) = 0

h(1) = 00

2. Determine h(L1) ∩ h(L2).

Page 143: Computation Engineering Applied Automata Theory and Logic

8.2. LANGUAGES 135

8.2.10 Prefix-closure

A language L is said to be prefix-closed if for every string x ∈ L, every prefixof x is also in L. If we are interested in every run of a machine, then itslanguage will be prefix-closed. Prefix closure is an operation that ‘closes’ aset by putting in it all the prefixes of the strings in the set. Thus, given theset of strings a, aab, abb, its prefix closure is ε, a, aa, ab, aab, abb.

Exercise 8.16 In Problem 8.11, take each pair of languages in turn, applythe prefix-closure operation to this pair of languages, and see if the resultsbecome the same language. Point out which pairs become the same languageafter prefix-closure.

Page 144: Computation Engineering Applied Automata Theory and Logic

136 CHAPTER 8. STRINGS AND LANGUAGES

Page 145: Computation Engineering Applied Automata Theory and Logic

Chapter 9

Machines, Languages, DFA

This chapter begins our study of machine models. We will first briefly exam-ine the general characteristics of various abstract machines that can serve aslanguage recognizers for the languages depicted in Figure 5.1 of Section 5.3.We will refer to this diagram as the “Power” diagram. We will then studythe Deterministic Finite-state Automaton (DFA) model in the rest of thechapter.

9.1 Machines

All the machines discussed in the Power diagram, including DFAs discussedin this chapter, are primarily string processors. They are started in theirinitial state and are fed an input string. The inputs of interest are finitelylong (“finite”) strings. The main question of interest is whether, at the endof the input, the machine is in an accepting state or not. The Turing machinehas, in addition to a set of accepting states, a set of rejecting states also.

There are several axes along which machines can be distinguished. Someof these distinctions are now pointed out. We will remind the reader of thesedetails when specific machine types are discussed. The purpose of the currentdiscussion is to portray the overall nature of the variations that exist amongmachines.

All machine types in the Power diagram that are of lower power thanthe LBA (namely the DFA, NFA, DPDA, and NPDA) must read their inputstrings completely before it is deemed that they accept the input string.Furthermore, they must read their input string in the process of a single left-

137

Page 146: Computation Engineering Applied Automata Theory and Logic

138 CHAPTER 9. MACHINES, LANGUAGES, DFA

to-right scan (meaning, they cannot go back and re-read a previously readsymbol). In each step of computation, these machines are allowed to readat-most one input symbol (they may read none, as well).

The remaining machine types in the Power diagram, namely the LBA,DTM, and the NDTM, are not obligated to read the whole input on theirinput “tape”. In other words, it is possible for these machines to accept aninput without reading all of it. Furthermore, they are allowed to re-read apreviously read input. This re-reading may happen in any order (i.e., notconfined to the order in which inputs are laid out on the tape).

All the machines in the Power diagram possess a finite-state control flowgraph describing their behavior. The NFA, NPDA, and NDTM allow non-determinism in their control-flow graph (non-determinism is described in thenext section). While the DFA and NFA are totally described in terms of thisfinite-state control, the remaining machine types carry an additional datastructure in the form of a stack or one or more tapes.. The DPDA andNPDA have access to a single unbounded stack from which they pop a singlesymbol at every step. Additionally, in each step, zero or more symbols maybe pushed back on to the stack.

The LBA, the DTM, as well as the NDTM have, in addition to a controlflow graph, a single read/write tape. There is the notion of a current positionon this input tape, usually specified by the position of a read/write “head.”Also, there is the notion of presenting the input string over a contiguoussequence of cells on the input tape. The LBA is allowed to write a tape cellonly if this cell belongs to the contiguous sequence of cells over which theinitial tape input was presented. A Turing machine has no such restriction.

Note that we avoid depicting the Buchi automaton in the Power diagramof Chapter 3, as this machine type is not comparable with these other ma-chine types. For example, one can simulate an LBA on a Turing machine,or a DFA on a push-down automaton; no such simulation of the Buchi au-tomata on any of the machines in the Power diagram is possible. In a sense,the presentation of Buchi automata is a way of making the reader aware ofthe existence of these machines, and the practical uses that these machineshave in modeling time in an abstract manner, and broaden the reader’s per-spective early on. Details on Buchi automata appear in Chapter ??.

With this general introduction, we proceed to examine the various ma-chine types beginning with the DFA. While describing each machine type, weshall try to re-explain the above issues, providing reasons for various choices.

Page 147: Computation Engineering Applied Automata Theory and Logic

9.1. MACHINES 139

I

0F1

0

1

Figure 9.1: A DFA that recognizes strings over 0, 1 ending with 1

9.1.1 The DFA

Informally, a deterministic finite-state automaton is specified through a tran-sition graph which also specifies the manner in which it reads its input. For-mally, a deterministic finite-state automaton D is described by five quantitiespresented as a tuple, (Q,Σ, δ, q0, F ), where:

• Q, a finite nonempty set of states;

• Σ, a finite nonempty alphabet;

• δ : Q× Σ→ Q, a total transition function;

• q0 ∈ Q, an initial state; and

• F ⊆ Q, a finite, possibly empty set of final states.

For example, in Figure 9.1, Q = I, F, Σ = 0, 1, q0 = I, F = F, andδ = 〈I, 0, I〉, 〈I, 1, F 〉, 〈F, 0, I〉, 〈F, 1, F 〉. Here are conventions pertainingto drawing DFAs as well as naming their states:

• The initial state will be called I. In drawings, it will be the state towhich an arrow without a source node points to.

• If the initial state is also a final state, it will be called IF. It will thenbe a double-circled node.

• All final state names (other than the state named as IF, as explainedabove) will begin with letter F, and will be double-circled nodes.

• All other state names begin with letters other than I and F, and are bestchosen with mnemonic significance. They are drawn as single circles.

• Generally one would expect a DFA to decode every move out of everystate. If, for expediency, we leave out some of these moves, we rec-ommend that the following comment accompany such diagrams: “weassume that all moves not shown lead the DFA to the black-hole state.”We recommend that the state name BH (for black-hole) be used for thatstate from which no escape is possible1.

Page 148: Computation Engineering Applied Automata Theory and Logic

140 CHAPTER 9. MACHINES, LANGUAGES, DFA

/* Encoding of a DFA that accepts the language (0+1)*1 */

main()

bool state=0;

char ch;

while(1)

ch = getch();

switch (state)

case I: switch-off green light;

switch (ch)

case 0 : break;

case 1 : state=F;

case F: switch-on green light;

switch (ch)

case 0 : state=I;

case 1 : break;

Figure 9.2: Pseudocode for the DFA of Figure 9.1

I

0

F1

0,1

Figure 9.3: Multiple symbols labeling a transition in lieu of multiple transi-tions

I

0

F1

X0,1

0

1

Y 0,1

Figure 9.4: A DFA with unreachable (redundant) states

Page 149: Computation Engineering Applied Automata Theory and Logic

9.1. MACHINES 141

IF

FA

0

FB

1

0

A11

1

B00

0

1

1

0

Figure 9.5: Another example DFA

• One could have a DFA with multiple symbols labeling a single transi-tion, in lieu of separate transitions that bear these symbols, as shownin Figure 9.3.

• One could have a DFA with truly unreachable states, as shown in Fig-ure 9.4. These states (states X and Y in this example) may be removedwithout any loss.

One can see the conventions explained above more clearly in Figure 9.5.The DFA in Figure 9.1 is really encoding programs similar to the one in

Figure 9.2. In general, such “DFA programs” are always programs with thewhile(1), to keep accepting symbols, always have a switch to decode thecurrent symbol, either decide to update the state or keep the current stateafter decoding, and when any final state (including the IF state) is entered,turn on a green light. The green light stays on so long as the DFA stays in thefinal state, and turned off when it exits it (we assume that these DFA spendzero time making the transitions). A DFA-program is not allowed to use anyother variable than the single variable called state. It cannot produce anyother output than shine the green light. It appears to be programming at itssimplest - yet, such humble DFAs are enormously powerful and versatile!

δ is commonly presented also as a table, with rows being states, columnsbeing inputs, with the final state(s) starred. For our example, we have

input

1This is similar to gravitationally hyper-dense stars from which nothing - includinglight - escapes. We did vote down the alternate name RM (for ‘Roach Motel’) proposedby some students.

Page 150: Computation Engineering Applied Automata Theory and Logic

142 CHAPTER 9. MACHINES, LANGUAGES, DFA

-----

state| 0 1

------------

I | I F

*F | I F

Any string after which the green-light of a DFA is on is said to be acceptedby the DFA, and any string after which the green-light is off is said to berejected by the DFA. Thus, DFAs are nothing but string classifiers. The setof strings accepted by a DFA is said to be its language. A DFA is said torecognize its language (not accept its language - the term ‘accept’ is for asingle string).

Formally speaking, a DFA is said to accept an input x ∈ Σ∗ if x takes itfrom q0 to one of its final states; i.e., δ(q0, x) ∈ F , where

For a ∈ Σ and x ∈ Σ∗, δ(q, ax) = δ(δ(q, a), x)

δ(q, ε) = q

The set of all strings accepted by a DFA is its language. A DFA is saidto recognize this language. An alternate definition of the language of aDFA is based on the notion of instantaneous descriptions (IDs). An ID is a“snapshot” of DFA machine in operation. For a DFA, this snapshot containsthe current state and the unconsumed input string. Knowing these, it ispossible to predict the entire future course of a DFA.

Formally speaking, we define the type of the ID of a DFA to be TID DFA =Q× Σ∗. In other words, we take the view that the type of the ID, TID DFA,includes all possible 〈state, string〉 pairs, i.e., Q × Σ∗. We now define tworelations, ` and `∗, the latter being the reflexive and transitive closure ofthe former. ` is a one-step move function that captures how a DFA movesfrom one ID to another.

Suppose id1 is one snapshot, (q1, σ1), and id2 is another snapshot, (q2, σ2)such that σ1 = aσ2. Now, if δ(q1, a) = q2, we know that the DFA cango from snapshot (q1, σ1) to (q2, σ2). In other words, define the relation` ⊆ TID DFA × TID DFA. as follows:

` (id1 as (q1, σ1), id2 as (q2, σ2)) = ∃a ∈ Σ : σ1 = aσ2 ∧ δ(q1, a) = q2

In the infix syntax, (q1, σ1) ` (q2, σ2) iff ∃a ∈ Σ : σ1 = aσ2 ∧ δ(q1, a) = q2.As said before, `∗ to be the reflexive transitive closure of `. In other words,`∗ is the smallest relation ⊆ TID DFA × TID DFA closed under the followingrules:

Page 151: Computation Engineering Applied Automata Theory and Logic

9.1. MACHINES 143

• 〈x, x〉 ∈ `∗ for x ∈ TID DFA

In other words, x `∗ x for x ∈ TID DFA

• 〈x, y〉 ∈ ` ∧ 〈y, z〉 ∈ `∗ ⇒ 〈x, z〉 ∈ `∗

In other words, x ` y ∧ y `∗ z ⇒ x `∗ z

Now, for DFA d, its language

L(d) = x | ∃q ∈ F : (q0, x) `∗ (q, ε).The above definition says that the language of a DFA consists of all stringsx such that `∗ can take the ID (q0, x) to the ID (q, ε), where q ∈ F .Example: Consider the DFA in Figure 9.3. What is in the ` relation withrespect to snapshots that involve I and F? Since this is an infinite set, weshow a few members below (we use 〈 and 〉 as well as ( and ) to delimittuples, to enhance readability):

〈(I, 1), (F, ε)〉, 〈(I, 111), (F, 11)〉, . . . 〈(I, 1010100110), (F, 010100110)〉, . . ..Now we show a few members of `∗:

〈(I, 1111), (F, ε)〉, . . . 〈(I, 1010100110), (F, 0110)〉,〈(I, 1010100110), (F, 010)〉, 〈(I, 1010100110), (F, 01)〉,〈(I, 1010100110), (F, ε)〉, . . ..

Note that `∗ can consume multiple symbols.Some definitions, tips, and conventions:

• A language L is said to be regular if there is a DFA D such thatL = L(D).

• Often helpful to think of a DFA as an “online processor” – even aftera string s is accepted, the user continues to type in its extension sw.Whenever the DFA reaches one of its accept states, its “green lightgoes on.”

• While it is helpful to leave out those transitions of a DFA that lead tothe black hole - BH - state, please remember to reinstate the black-holestate and all the transitions going to this state before you perform DFAoperations, such as complementation2.

2Otherwise you will find yourselves in a ‘DH’ - deep hole.

Page 152: Computation Engineering Applied Automata Theory and Logic

144 CHAPTER 9. MACHINES, LANGUAGES, DFA

Exercise 9.1 What is the language of the DFA in Figure 9.1 if:

• the only accept state is the starting state?

• there are no accepting states (no double-circled states)?

• we reverse every arc, rename state I as state F, and rename state F asstate I, making it the initial state?

Exercise 9.2 What is the language recognized by the DFA in Figure 9.5?List ten strings in its language and ten not in its language in lexicographicorder.

Exercise 9.3 Draw a DFA that is different from (non-isomorphic to) theDFA shown in Figure 9.5, with the same language, however.

Exercise 9.4 What happens to the language of the DFA in Figure 9.5 if A1jumps to B0 upon receiving a ‘1’, and B0 jumps to A1 upon receiving a ‘0’?

Exercise 9.5 Draw a DFA having language equal to the union of the lan-guages of the DFAs in Figure 9.1 and Figure 9.5.

Exercise 9.6 Draw a DFA having language equal to the symmetric differ-ence of the languages of the DFAs in Figure 9.1 and Figure 9.5.

Exercise 9.7 Apply the following homomorphism to the language of theDFA of Figure 9.1, and draw a DFA for the resulting language. Repeat forthe DFA of Figure 9.5.

h(ε) = ε

h(0) = 11

h(1) = 00

Exercise 9.8 Obtain a DFA for the prefix-closure of the language of theDFA of Figure 9.1. Repeat for the DFA of Figure 9.5.

Exercise 9.9 Draw a DFA having language equal to the concatenation ofthe languages of the DFAs in Figure 9.1 and Figure 9.5.

Exercise 9.10 Draw a DFA having language equal to the star of the languageof the DFA in Figure 9.1. Repeat this exercise for the DFA of Figure 9.5.

Page 153: Computation Engineering Applied Automata Theory and Logic

9.1. MACHINES 145

Exercise 9.11 Draw a DFA having language equal to the reverse of the lan-guage of the DFA in Figure 9.1. Repeat this exercise for the DFA of Fig-ure 9.5.

Exercise 9.12 Draw a DFA having language equal to the complement ofthe language of the DFA in Figure 9.1. Repeat this exercise for the DFA ofFigure 9.5.

Exercise 9.13 What happens if, in the DFA in Figure 9.1, every transitioncarries label ‘1’? Think of what the corresponding DFA program will do.

Exercise 9.14 Give a DFA accepting the set of strings over Σ = 0, 1 suchthat each block of five consecutive symbols contains at least two 0’s.

Exercise 9.15 Develop a DFA that recognizes the following language

L = x | x ∈ 0, 1∗ and (||x|| mod 5) = 0

Here ||x|| stands for the magnitude of x viewed in unsigned binary. Example:|| 0101 ||= 5, with the leftmost 0 coming first. Hint: Notice that the bitsof x are being presented MSB-first. Act on each new bit and recalculate theremainder and remember it in the DFA states. Think of what happens toa number, say N , when it gets shifted left and a least significant bit b getsinserted. Basically N becomes 2×N + b.

Exercise 9.16 With respect to the language L of Question 9.15, design aDFA for rev(L), the set of strings which are reverses of strings in L. (Thisis tantamount to processing the bit-stream LSB-first, as opposed to L whichprocesses it MSB-first.) Example: || 0101 ||= 5, with the rightmost 1 comingfirst. Hint: N becomes b × 2m + N where m is the position into which thenew MSB b walks in. N mod 5 becomes (b × 2m + N) mod 5. We needa recurrence that keeps track of 2m for various m. Thus, each DFA stateremembers a pair:(“powers of 2 mod 5”, “the mod of the number mod 5”).A few steps are:(1,0) - 1 -> (2,1) - 1 -> (4,3) - 1 -> (3,2).

In general, the evolution of the state goes as follows.

State N mod 5 −→(b× (Weight mod 5) +N mod 5) mod 5.

Page 154: Computation Engineering Applied Automata Theory and Logic

146 CHAPTER 9. MACHINES, LANGUAGES, DFA

Now, Weight mod 5 is maintained as follows:

2m mod 5 −→ (2× (2m mod 5)) mod 5.

A few of the state transitions are given below. Complete the rest.

(1,0) - 0 -> (2,0)

(1,0) - 1 -> (2,1)

(2,0) - 0 -> (4,0)

(2,0) - 1 -> (4,2)

(2,1) - 0 -> (4,1)

(2,1) - 1 -> (4,3)

...

Exercise 9.17 Repeat both parts above for k instead of 5 and for arbitrarynumber base b (with Σ suitably adjusted) instead of Binary.

9.1.2 The “power” of DFAs

Despite their simple appearance, DFAs are incredibly powerful and versatile!For instance, the above exercises demonstrate that using DFAs, one canperform division either going MSB-first (the ‘high-school’ method) or evenLSB-first, so long as one only has to keep track of the remainder. Themost touted applications of DFAs come from scanner generation and patternrecognition. In addition to providing examples from these domains, we shallsoon demonstrate the power of DFAs to efficiently encode Boolean reasoning.

Most questions about DFAs are algorithmically decidable. In particular,the following questions about DFAs are decidable:

• Does a given DFA accept a given input?

• Does a given DFA have an empty language?

• Is the language of a given DFA all possible strings over Σ?

• Are two given DFAs equivalent?

DFAs have many desirable properties, some of which are the following:

• Given any DFA d, one can algorithmically obtain another DFA d′whose

language is the reverse of the language of d.

• Given any DFA d, one can algorithmically obtain another DFA d′whose

language is the complement of the language of d.

Page 155: Computation Engineering Applied Automata Theory and Logic

9.1. MACHINES 147

9.1.3 Limitations of DFAs

DFAs have two main limitations. First, despite being adequately expressive,they may require “too many” states. Second, for many frequently occur-ring patterns, DFAs are simply inadequate. It is best to see these problemsthrough two concrete problems.

Exercise 9.18 Design a DFA for the following language (7 can be n in gen-eral):

x0z | x ∈ 0, 1∗ and z ∈ 0, 17.Prove that the required DFA has exponentially many states (exponential inn).

We shall soon see that using the concept of nondeterminism, we can obtainexponentially more succinct finite-state representations, giving rise to thenext machine type we shall study, namely non-deterministic finite automata(NFA).

To see the second limitation of DFAs more vividly, consider the followingexercise.

Exercise 9.19 Either design a DFA for the following language

L = (n )n | n ≥ 0,

or prove that no such DFA can exist!

In this case, we can easily prove that there can exist no DFA. The proof goesas follows:

• Suppose there exists an M -state DFA for this language. Consider thestring w = (M )M .

• w causes M + 1 states to be visited even within (M

• Since only M of these can be distinct states, one state repeats in the(M traversal.

• Therefore there exists also a shorter, non state-repeating path leadingto the final state, thus causing that DFA to accept also (k)M for k < M– a contradiction with the fact that this is the DFA for the abovelanguage.

Page 156: Computation Engineering Applied Automata Theory and Logic

148 CHAPTER 9. MACHINES, LANGUAGES, DFA

It is obvious that to recognize a language such as L above, all we need to do isto add a single stack to the DFA D. Doing so, we obtain a machine known asdeterministic push-down automaton (DPDA). In general, by adding differentkinds of data structures to the finite-state control, one can handle languageswhose strings have more complicated structure. We shall now present a fewexamples that illustrate this point.

9.1.4 Machine-types that accept various languages

Consider the language

L1 = aibjck | i, j, k ≥ 0 ∧ if i = 1 then j = k.

In all the strings in this language, the characters a, b, and c appear in thatorder, with bs and cs being equal in number if the number of as is 1. Thislanguage can obviously be handled using a DPDA, using its stack. In fact,

reverse(L1)

can also be handled using a DPDA, as the finite-state control can rememberwhether the number of bs and cs were equal by the time the a (or as) appear.

Exercise 9.20 Write clear pseudo-code for a stack-based algorithm that aDPDA can employ in recognizing reverse(L1).

Now consider

L2 = aibjckdm | i, j, k,m ≥ 0 ∧ if i = 1 then j = k else k = m.

This language can still be processed using only one stack, as the matchingbetween bs and cs or cs and ds, whichever is to occur, can be decided by firstseeing the as. No such luck awaits

reverse(L2)

which has to have two decisions, whether the number of bs and cs match, orwhether the number of cs and ds match, in hand by the time the a (or as)arrive. Clearly, these two decisions cannot be generated using a single stack,thus showing that reverse(L2) cannot be processed by a DPDA. The samecan be concluded about

L3 = ww | w ∈ 0, 1∗

Page 157: Computation Engineering Applied Automata Theory and Logic

9.1. MACHINES 149

also, as the second ws head must be matched against the first ws head which,unfortunately, is at the bottom of the stack. It turns out (as we shall demon-strate later) that

L4 = overline(L3)

indeed can be processed using a single-stack push-down automaton.One obvious solution to handling L3 as well as reverse(L2) would be to

employ two stacks instead of one. This unfortunately gives more power thannecessary (the machine becomes as powerful as a Turing machine). Anothermachine-type, the linear bounded automaton (LBA), can be used. An LBAhas finite control as well as a tape such that it can read/write only the regionof the tape in which the input initially appeared. In addition, an LBA hasa finite tape alphabet that may (and typically does) contain Σ, the inputalphabet, plus (typically) many additional symbols. Exploiting the abilityto repeatedly scan the input, an LBA can decide membership in all thelanguages listed above. By the same token, an LBA can decide membershipin the language

anbncn | n ≥ 0as well as

anbn2 | n ≥ 0.We later present languages that cannot be decided using LBAs. To handlethe full generality of languages, we remove the restriction on LBAs that theycan write only where the input is presented...thus also obtaining a Turing ma-chine. We shall now return to the topic of non-deterministic finite automata,saving the study of the remaining machine types for subsequent chapters.

Drawing DFAs neatly

We close this chapter off with some pragmatic tips for DFA drawing. Almostall drawings in this book are created using the dot package which is freelydownloadable as part of the graphviz tools. The latest version at the timeof writing this book is graphviz-1.7.7. After downloading and installingthis package, you will see an executable file dot. You may then presentyour drawings in a file with extension .dot, and process the drawing into apostscript file for inclusion into your documents, including Latex documents,where you may further process the drawing. For example, figures 9.6 and 9.7provide all the commands needed to generate Figure 9.1.

Page 158: Computation Engineering Applied Automata Theory and Logic

150 CHAPTER 9. MACHINES, LANGUAGES, DFA

digraph G

/* Defaults */

fontsize = 12;

ratio = compress;

rankdir=LR;

/* Bounding box */

size = "4,4";

/* Node definitions */

I [shape=circle, peripheries=1];

F [shape=circle, peripheries=2];

"" [shape=plaintext];

/* Orientation */

orientation = landscape;

/* The graph itself */

"" -> I;

I -> I [label="0"];

I -> F [label="1"];

F -> F [label="1"];

F -> I [label="0"];

/* Unix command: dot -Tps exdfa.dot >! exdfa.ps */

Figure 9.6: Drawing DFAs using dot

% Include everything till %--- in a latex document, and run latex

\beginfigure

\hfill \psfigfile=exdfa.ps,height=4.5cm,width=2cm,angle=-90\hfill

\captionWhatever caption you desire

\labelfig:label-for-cross-referencing

\endfigure

%---

Figure 9.7: Processing .ps files in Latex

Page 159: Computation Engineering Applied Automata Theory and Logic

Chapter 10

DFA in Hardware Design

DFA in hardware design: compare classical DFA with hardware-orientedmachines.

151

Page 160: Computation Engineering Applied Automata Theory and Logic

152 CHAPTER 10. DFA IN HARDWARE DESIGN

Page 161: Computation Engineering Applied Automata Theory and Logic

Chapter 11

NFA and Regular Expressions

In the last chapter, we have seen many examples of the versatility of regularlanguages. We have also seen several examples of regular languages for whichthe DFAs were both simple and intuitive. In Chapter 9, we will further elabo-rate on the power of DFAs by showing that they can be used to encode as wellas reason about statements from certain decidable branches of mathematicallogic. In Section 9.1.3, we also discussed some of the limitations of DFAs.Their main limitation is, of course, that they cannot serve as recognizers fornon-regular languages such as L = (n )n | n ≥ 0, and these languages arevery important in computer science. In fact, they are part of the syntax ofmost computer programming languages. In later chapters, we will developrigorous proof-techniques for proving that certain languages are not regular.We will then study machines that are strictly more powerful than DFAs.

In this chapter, we continue the study of regular languages, and in thatcontext, the main limitation of DFAs is that they can be unnatural or ex-ponentially large or both. To overcome these limitations, in this chapter wewill introduce a new machine type called non-deterministic finite automata,or NFA. NFAs subsume DFAs; in fact, every DFA is an NFA (more preciselysaid, every DFA can, with a small adjustment of mathematical definitions,be regarded as an NFA). We also wish to point out that non-determinism isone of the central ideas in theoretical computer science, going well beyondits role as a ‘description size compressor.’ In this book, we will be repeatedlyrevisiting the concept of non-determinism in the context of various machinetypes.

NFA are specified much like DFA are: through pictures of directed graphs(for human consumption), or through a description of the δ function, say

153

Page 162: Computation Engineering Applied Automata Theory and Logic

154 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

through a tabular function description (for machine consumption). However,often a textual syntax is preferred over these styles. This is preciselywhat regular expressions are. In other words, regular expressions allow usto write down mathematical expressions that denote regular languages, andeach such regular expression has a very straightforward interpretation as anNFA. All regular languages over an alphabet Σ can be specified just usingthe primitive regular expressions φ, ε, and a ∈ Σ, and the regular-expressionbuilding operators ∪, and ∗.Thus, to sum up, the key results we are aiming to establish by the end ofChapter 12:

• Regular languages are those recognized by a deterministic finite au-tomaton.

• For every DFA, there is an equivalent NFA; for every NFA there is anequivalent RE; and for every RE, there is an equivalent DFA.

• For any regular language, the minimal DFA—the DFA with the small-est number of states that serves as a recognizer for the language—isunique.

• Regular languages are closed under many operations, including:

− Union, Intersection, Complementation, Star

− Homomorphism, Inverse homomorphism

− Reversal, Prefix Closure,

In this chapter, we will introduce NFAs and REs through several examples.We explain how NFA process strings by moving from sets of states to othersets of states either by reading a symbol from an alphabet or without readingany symbols (through ε moves). We explain how to determine the languageof an NFA using the notion of Eclosure.

We strive to provide an experimental flavor to this chapter by employingthe grail tool suite for illustrations. In Section 11.2.6, we illustrate througha case study how to use the grail tool suite to design and formally debugregular expressions. Virtually all the diagrams in this book are generatedusing grail, and a graph drawing package called dot. We also try to relatethe material to the real world by demonstrating how a simple lexical analyzercan be built using the flex tool.

Page 163: Computation Engineering Applied Automata Theory and Logic

155

What is Nondeterminism?

Non-determinism has many uses in computer science. It can help designersdescribe the systems they are about to build even at stages of the designwhere they have not determined all the details. In some cases, while thesedetails may be known to designers, they may still choose not to include themto avoid inundating their audience through excess information1.

To understand non-determinism in a real-world context, consider the actof summoning an elevator car in a busy building that has multiple elevators.After one presses the call button, it is not entirely predictable what will hap-pen. One may, if lucky, get a car headed in the same direction as they intendto travel. If unlucky, one would get a car going in the opposite direction, ora car that is full, etc. If one had perfect knowledge about the entire buildingand its occupants, they could predict the outcome with certainty. However,most people2do not want to keep track of all such information, instead pre-ferring to live according to a non-deterministic protocol that goes as follows:“if I am fated not to get an empty car, I should try again.” In short,by employing non-determinism, one can write system descriptions at a highlevel, without worrying about pinning down details early. It is well-knownthat pinning down details early (“premature optimization”) is at the root ofall that is evil in software design.

A curious side-effect of employing non-determinism is that descriptionswritten in a non-deterministic style often end-up overapproximating the sys-tem. Here, ‘overapproximation’ means that we are ignoring special cases,and pretending as if the system had more behaviors. (It is akin to packag-ing a delicate, but odd-shaped, electronic gadget by inserting it between apair of moulded styrofoam carrier, thus smoothening the overall appearence.)Adding behaviors often helps “round” things up, hence simplifying the wholesystem.

To better understand the practical ramifications of overapproximation,consider a building where all northside elevators are designed not to be inter-rupted during their upward journey during early mornings3. In other words,each elevator control algorithm has an if-then-else in it that tests whether

1In the modern society that suffers from ‘info pollution,’ non-determinism can be thebreath of oxygen that saves us from asphyxiation.

2Except perhaps “control freaks.”3Perhaps to ensure the speedy progression of the janitorial staff and their accouterments

to top floors.

Page 164: Computation Engineering Applied Automata Theory and Logic

156 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

it is running inside a northside elevator, and checks what time of the dayit is, and prevents interruption if the tests confirm ‘north’ and ‘morning.’Now, if one were to hire a formal verification specialist to mathematicallyverify that all the elevator control algorithms are working correctly, here ishow they could employ non-determinism to simplify their activities. Theycould simply replace the if-then-else with a non-deterministic jump to bothcases. Such a modified control algorithm has more behaviors than the orig-inal, in which every elevator could non-deterministically decide whether toignore the user’s interrupt or to heed to it. Now, if the verification special-ist is handed the property to verify: “If I ring for a car, it will eventuallycome to my floor,” that property would pass on the non-deterministicallyoverapproximated system. The specialist would end up having verified thisproperty more easily, by ignoring the if-then-else. On the other hand, if theyare handed another property, namely: “if the car is below my floor, is headedto a floor above my floor, and I press the ‘up’ button, it will stop for me,”the verification will fail for the overapproximated model because every eleva-tor can exercise the ‘ignore interrupt’ option. The verification specialist willrealize that this is a false negative—a false alarm—and then add the missingdetail, which is the if-then-else statement. Thus, during system design andverification, one can add just enough information to prove each properties ofinterest. The alternative approach of revealing all internal information4bothtaxes the mind and adds to verification time. Computer science’s essentialmission is complexity management, and non-determinism plays an essentialrole in this regard.

In this book, we now return to the use of non-determinism for describingregular languages. We now begin discussing the topic “what else does non-determinism affect?” We provide the answers under separate headings.

How non-deterministic affects automaton operations

The presence of non-determinism affects the ease with which automaton op-erations (such as union, concatenation, and star) can be carried out. In thecontext of finite automata, while the operations of union, concatenation, andstar become easier with the use of non-determinism, the operations of com-plementation, intersection, and equivalence become harder to perform. Wewill see the details when we introduce these algorithms.

4Popular with many legal departments, who do so in fine print.

Page 165: Computation Engineering Applied Automata Theory and Logic

11.1. REGULAR EXPRESSIONS 157

How non-deterministic affects the power of machines

Non-determinism also fundamentally affects the power of various machinetypes, as follows:

• Non-deterministic push-down automata are strictly more powerful thandeterministic push-down automata.

• The equivalence between deterministic PDAs is algorithmically decid-able. This is actually a recent result. It was obtained 30 years after itwas first conjectured to be true.

• The equivalence between non-deterministic and deterministic PDAs(and hence between two non-deterministic PDAs) is undecidable.

• It is still an open problem whether non-deterministic and deterministiclinear bounded automata are equivalent in power.

• The power of Turing machines does not change (with respect to de-cidability properties) through the use of non-determinism. However,with respect to complexity, certain problems (for instance NP-completeproblems discussed later) take exponential time on deterministic ma-chines, but can solved in polynomial time on non-deterministic Turingmachines. Also, Turing machine equivalence is undecidable.

With this general introduction, we look at regular expressions and NFA ingreater detail. We begin with regular expressions because we believe thatthey will be already familiar to users of computers - if not known by thatname.

11.1 Regular Expressions

The idea of using Regular Expressions to denote whole classes of strings isquite widespread. In many card games, the Joker card is called the “wild-card” in that it can be used in lieu of any other card. In most OperatingSystems, the wildcard is ∗ that matches all file names—as in rm *.* or del*.*. Such expressions are practical examples of regular expressions in day-to-day use. Formally, we define regular expressions as follows. We use theinductive definition style in which a basis case and a list of inductive rules areprovided, and define regular expressions to be the least set satisfying theserules:

Page 166: Computation Engineering Applied Automata Theory and Logic

158 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

• φ is an RE denoting the empty language φ.

Most computer-assisted tools have trouble accepting φ directly,and so their syntax for φ varies. It is written as in grail.Check with each tool to see how it encodes φ. A command man

regexp or man egrep issued on most Unix systems usually revealshow Unix encodes REs.

• ε is an RE denoting the language ε

Again, most computer-assisted tools encode ε variously—as ""

by grail, for example. In our drawings we sometimes use e inlieu of ε. Unix has several flavors of ε—those occurring withinwords, at the beginning, at the end, etc.

• a ∈ Σ is an RE denoting the language a.

Most tools directly encode this regular expression in ASCII - forexample a.

• For REs r, r1 and r2, r1r2, r1 + r2, r∗, and (r) are regularexpressions. These regular expressions denote the following languages,with the indicated encodings in grail (we write L(x) to mean ‘languageof’):

RE r1r2 denotes L(r1) L(r2) encoded in grail as r1 r2

RE r1 + r2 denotes L(r1) ∪ L(r2) encoded in grail as r1+r2

RE r∗ denotes (L(r))∗ encoded in grail as r*

RE (r) denotes L(r) encoded in grail as (r)

The above regular operators form a basis set in the sense that we do not needany other operators such as complementation, reversal, intersection RE, etc.,to build regular languages. All regular languages can be specified using onlythe above regular expression syntax. In practice, however, there are manylanguages that are very difficult to specify without the use of these additionaloperators, especially complementation (also known as negation)5. Thereason for this difficulty is that the regular expression syntax, which onlyhas ∗, , and +, allows us to build up the language from simpler languages,while often it would be much more natural to build down a language, say, by

5Recall that for two languages L1 and L2, the complement of their union, (L1 ∪ L2), isequivalent to the intersection of their complements, L1 ∩ L2, by DeMorgan’s law. Hence,if we have complementation and union, we can obtain the effect of intersection.

Page 167: Computation Engineering Applied Automata Theory and Logic

11.1. REGULAR EXPRESSIONS 159

specifying two different (but larger) languages and intersecting them. If youattempt Exercises 11.8 and 11.6, you will confront the extreme difficulty ofspecifying two example languages directly in the RE syntax of the basis set- i.e., without the use of negation.

Illustration 159.1 In previous chapters we have seen how to write out lan-guages using set-theoretic syntax. In particular, we can express language

Labck = xabc | x ∈ 0, 1k ∧ a, b, c ∈ 0, 1 ∧ (a = 0 ∨ b = 1)

for various k as follows:

• k = 0 : ε 00, 01, 11 0, 1• k = 1 : 0, 1 00, 01, 11 0, 1• k = 2 : 0, 12 00, 01, 11 0, 1• . . .

We can encode these languages using regular expressions as follows:

• k = 0 : ε (00 + 01 + 11) (0 + 1)

• k = 1 : (0 + 1) (00 + 01 + 11) (0 + 1)

• k = 2 : (0 + 1)(0 + 1) (00 + 01 + 11) (0 + 1)

• . . .

In the Grail syntax, these regular expressions can be encoded as:

• k = 0 : ""(00+01+11)(0+1)

• k = 1 : (0+1)(00+01+11)(0+1)

• k = 2 : (0+1)(0+1)(00+01+11)(0+1)

• . . .

Illustration 159.2 Consider the language

Lk = x0z | x ∈ 0, 1∗ and z ∈ 0, 1k.

For various k, we can express this language using regular expressions (ingrail syntax) as follows:

• k = 0: (0+1)*0

Page 168: Computation Engineering Applied Automata Theory and Logic

160 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

1 1

5

0

7

0

3

1

0

1

0

6

0

2

1

4

0

1

1

0

0

1

1

0

1 1

9

0

0

1

0

5

1

11

0

2

1

8

0

1

0

3

1

0

4

1

13

0 1

10

0

1

0

6 0

14

1

1

12

0

7

0

1

0

15

10

1

1

0

1

0

30 1

0

0

1

0

2

1

3

0

4

1

5

0

6

1

7

0

8

1

9

0

10

1

11

0

12

1

13

0

14

1

15

0

16

1

17

0

18

1

19

0

20

1

21

0

22

1

23

0

31

1

24

0

25

1

26

0

27

1

28

0

29

1

0

1

0

1

0

1 0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

1

0

Figure 11.1: Minimal DFAs for Lk for k = 2, 3, 4, with 2k+1 states

I

0,1

A0

B0,1

F0,1

Figure 11.2: An NFA for (0 + 1)∗ 0 (0 + 1)k, for k = 2

Page 169: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 161

• k = 1: (0+1)*0(0+1)

• k = 2: (0+1)*0(0+1)(0+1)

• k = 3: (0+1)*0(0+1)(0+1)(0+1)

• . . .

We see that the size of the regular expression grows linearly with k—a prop-erty also shared by non-deterministic automata for this language, as we shallillustrate in Figure 11.2. In contrast, as shown in Figure 11.1, minimal DFAsfor this language will grow exponentially with k. In particular, the minimalDFA for each k has 2k+1 states.

11.2 Non-deterministic Finite Automata

We now introduce non-deterministic finite automata formally. Let Σε standfor (Σ ∪ ε). A non-deterministic finite-state automaton N is a structure(Q,Σ, δ, q0, F ), where:

• Q, a finite nonempty set of states;

• Σ, a finite nonempty alphabet;

• δ : Q× Σε → 2Q, a total transition function. (Note, however, that therange includes φ; thus, if for some state q, δ(q, x) = φ, the move fromstate q on input x is essentially to a black-hole6.)

• q0 ∈ Q, an initial state; and

• F ⊆ Q, a finite, possibly empty set of final states.

The use of non-determinism allows much more natural-looking as well as com-pact descriptions of many automata: contrast Figure 11.2 with the equivalentminimal DFAs in Figure 11.1. Notice that these minimal DFAs are unique;in other words, it is impossible to avoid the exponential growth based on k.We now discuss various aspects of non-deterministic automata step by step,and present a comprehensive practical example in Section 11.2.5.

6Unlike in recent theories on real black-holes by Stephen Hawking, nothing escapes anautomaton black-hole.

Page 170: Computation Engineering Applied Automata Theory and Logic

162 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

11.2.1 Non-deterministic behavior without ε

We provide two examples illustrating NFA. The first, provided in Figure 11.2,demonstrates one type of non-determinism where, from a single state, an NFAcan advance to multiple next states upon reading an input symbol—in thiscase, from state I upon reading 0 to states I and A. The various componentsof this NFA are as follows:

• Q =I,A,B,F

• Σ =0,1

• q0 =I

• F =F• δ : I, A,B, F × 0, 1, ε → 2I,A,B,F is given by the following table:

Input 0 1 e

---------------------

States |

I | I,A I

|

A | B B

|

B | F F

|

F |

Notice that this NFA moves to the empty set of states, φ (written ) wher-ever, in the diagram, there is no explicit move shown. To trace out thebehavior of this NFA on an input string such as 10101, we place a tokenin state I and subject it to moves according to its δ function. The use ofinstantaneous descriptions (ID) as employed in Section 9.1.1 suggests itselfas a way to keep track of ‘where the NFA is in its diagram’ and ‘what theunconsumed input string is,’ except we know that an NFA can be in a set ofstates after a move. What type of IDs must we employ to keep track of allthis? Of the many choices possible, we employ the following type:

TID NFA = 2Q × Σ∗.

This ID keeps a set of states and the unconsumed string. We now use the `notation to indicate ‘one step’ of forward progress of the NFA, where a stepmay be made through a member of Σ or through ε.

Page 171: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 163

` relation for an NFAWe define ` ⊆ TID NFA × TID NFA as follows:

(Q1, σ1) ` (Q2, σ2).= ∃a ∈ Σε : σ1 = aσ2 ∧ Q2 = ∪q1∈Q1

δ(q1, a).

In other words, ` causes a move from an ID (Q1, σ1) to another ID (Q2, σ2)by moving every state q1 ∈ Q1 via the δ function upon a ∈ Σε, and takinga union of the resulting sets of states. Notice that we allow the choice of εfor a, meaning that σ1 can be the same as σ2, and the NFA can simply movethrough ε. An example that illustrates the use of ` is as follows:

• Initial ID of the NFA is (I, 10101)• (I, 10101) ` (I, 0101); after reading 1, the NFA is still in the set

of states I, as given by the δ function of the NFA.

• (I, 0101) ` (I, A, 101); after reading 0, the NFA is simultaneouslyin two states, I, A, as given by the δ function.

• (I, A, 101) ` (I, B, 01); after reading 1, the token in state I goesback to state I itself, while the token in state A advances to state B.Thus, the NFA is now simultaneously in two states, I, B. We obtainI, B by taking the union of δ(I, 1) and δ(A, 1). Continuing in thismanner, we go through the IDs shown below.

• (I, B, 01) ` (I, A, F, 1); after reading 0, state I goes to the set ofstates I, A whereas state B goes to state F . Now, the DFA has seena substring that it accepts, namely 1010. If 1010 were the entire string,one token would have reached F , and consequently the NFA wouldhave accepted. However, since the entire string is 10101, we continueas below.

• (I, A, F, 1) ` (I, B, ε); after reading the final 1, F “falls out ofthe diagram” (goes to the empty set of states, as shown by δ(F, 1)),while the other states I and A move to I and B. Since neither of theseare final states, the NFA does not accept 10101.

11.2.2 Non-deterministic behavior with ε

We now illustrate an NFA’s ability to advance through ε moves with the aidof Figure 11.3, which is a slight modification of Figure 11.2. In Figure 11.3,the NFA can move from state F to state I via ε. In short, this machine

Page 172: Computation Engineering Applied Automata Theory and Logic

164 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

I

0,1A0 B

0,1

F

0,1e

Figure 11.3: An NFA for ((0 + 1)∗ 0 (0 + 1)k)+, for k = 2

accepts one or more repetitions of a string in the language of Figure 11.2.Let us see how. In terms of instantaneous descriptions, the sequence of IDchanges when running with input 10101010 are as follows: As before,

• (I, 10101010) ` (I, 0101010)• (I, 0101010) ` (I, A, 101010)• (I, A, 101010) ` (I, B, 01010)• (I, B, 01010) ` (I, A, F, 1010).• One way to proceed now is to treat the ID (I, A, F, 1010) as

(I, A, F, ε1010) and allow the ε transition to fire. This sends I andA to the empty set of states, while sending F to I:

• (I, A, F, ε1010) ` (I, 1010).• We now can perform (I, 1010) ` (I, 010).• Let us accelerate our presentation now by using `∗.• (I, 010) `∗ (I, A, F, ε). The string is accepted since one of the

tokens has reached F .

Another way in which the NFA could proceed from (I, A, F, 1010) isby moving states I, A, and F on 1 (without inserting the ε):

• (I, A, F, 1010) ` (I, B, 010), in which move I proceeds to I, Aproceeds to B, and F proceeds to φ.

• (I, B, 010) ` (I, A, F, 10).• (I, A, F, 10) ` (I, B, 0).• (I, B, 0) ` (I, A, F, ε), also resulting in the string being accepted.

Exercise 11.1 It seems as if adding the ε transition from F to I, we didn’tchange the language of the NFA in Figure 11.3. Argue that this is true.

Page 173: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 165

IF

e FA0

FB1A1

1 B0

e

0

1

e

e

0

e

1

0

Figure 11.4: An Example NFA

To sum up, using `, we have to be prepared to read a string such as 0100as x0x1x0x0x, where each x is any number of εs in sequence. Section 11.2.3introduces a better alternative for determining the behavior of NFAs usingthe concept of Eclosure or ε-closure. Eclosure, in effect, considers all7ε-laden interpretations of strings in “one fell swoop.”

The language of an NFA

The language of an NFA consists of all those sequences of symbols that can beencountered while tracing a path from the start state to some final state. Weeliminate all occurrences of ε from such sequences, unless the entire sequenceconsists of εs, in which case, we turn the sequence into a single8ε. Formally,given an NFA N = (Q,Σ, δ, q0, F ) with the ` relation defined as above, itslanguage

L(n) = w | (q0, w) `∗ (Q, ε) ∧ Q ∩ F 6= φ.In other words, if the ID (q0, w) can evolve through zero or more ` stepsto an ID (Q, ε) where Q contains a final state, then w is in the language ofthe NFA.

11.2.3 ε-closure

Eclosure(q) obtains, starting from a state q, the set of states reached by anNFA traversing zero or more ε labeled transitions. The best way to see whichstates are included in Eclosure(q), for any q, is to imagine this way:

7While discussing non-deterministic push-down automata (NPDA) in later chapters,we will see that a construct similar to Eclosure cannot be that easily defined for NPDA.

8Please note that ε is not part of the alphabet of the NFA.

Page 174: Computation Engineering Applied Automata Theory and Logic

166 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

Apply a high voltage to state q; imagine that every ε edge isa diode that conducts in the direction of the arrow; now seewhich are the states that would be fatal to touch due to thehigh-voltage9; all those are in Eclosure(q).

Example: Let us obtain the Eclosure of various states in Figure 11.4.

• The Eclosure of state IF is IF itself. The high-voltage spreads fromIF to itself.

• What is Eclosure of FA? Applying high-voltage to this state, itspreads to state FA, toB0, toA1, and finally FB. Thus, Eclosure(FA) =FA,B0, A1, FB.

Unfortunately, an intuitive definition in terms of voltages isn’t rigorous enough!Thus, we set-up an alternate definition of Eclosure(q) as follows.

• First we need a way to compute “which all states can be reached froma given state?” To define that, we do the following (illustrated withrespect to an arbitrary relation →⊆ Q×Q.

Given →⊆ Q×Q, define q → to be10the image of q under →:

q →= x | 〈q, x〉 ∈ →.

• Now, extend this idea to work only for ε labeled transitions. For that,

define the relationε→:

ε→ = 〈q1, q2〉 | q1, q2 ∈ Q ∧ q2 ∈ δ(q1, ε)• Next, define the reflexive and transitive closure of

ε→ in the usual way.

Call itε→∗.

Given all this, we define

Eclosure(q) = qε→∗.

The reflexive part above is very important to ensure that q gets includedwithin Eclosure(q).Example: Redoing our example with respect to Figure 11.4,

Eclosure(FA) = FAε→∗.

9An ideal non-leaky diode that does not break down (er., I don’t mean ‘boo hoo hoo!’)10Think of q → as a postfix operator, → applied to q.

Page 175: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 167

• This “grabs” state FA itself (the reflexive part ofε→∗ does this).

• Next, B0 enters this set, as it is one step away. In fact, B0 is in FAε→1.

• Next, A1 enters this set, as it is two steps away. In fact, A1 is in FAε→2.

• Finally, FB enters this set, as it is three steps away. In fact, FB is in

FAε→3.

• No more states enter Eclosure(FA).

Exercise 11.2 For the NFA in Figure 11.4, step through its IDs startingwith input 101101. Also, answer whether this string is accepted or not.

Exercise 11.3 This exercise may be attempted if you read Chapter 7, “Deal-ing with Recursion.” An alternate definition of Eclosure is via recursion:

For q ∈ Q, Eclosure(q) = q ∪ y | y ∈ δ(q, ε)∨ ∃z ∈ δ(q, ε) ∧ y ∈ Eclosure(z)

Notice that the use of recursion here is “well defined.” In particular, wecan compute the least fixed-point of the Eclosure function. We start withEclosure(q) returning the empty set φ for every state. Plug this definition ofEclosure on the right-hand side and “pump” the recursion one step, obtain-ing the Eclosure function for the left-hand side for every state. This adds qitself to Eclosure, and also adds those states that are directly reachable from qvia ε edges. Next plug in this version of Eclosure on the right-hand side andpump up again, till no more states are added to any Eclosure. This processcomes to a halt since we have a finite number of states. Argue that this leastfixed-point based definition of Eclosure is equivalent to the transitive-closurebased definition.

As said earlier, Eclosure helps define the behavior of an NFA as well asits language more directly. It also helps us define the NFA to DFA conversionalgorithm (Chapter 12) very directly.

11.2.4 Language of an NFA

Having defined Eclosure, we can now formally define the language of anNFA. For a string x ∈ Σ∗, define δ(q0, x) of an NFA to be the set of statesreached starting from Eclosure(q0) and traversing all the symbols in x, taking

Page 176: Computation Engineering Applied Automata Theory and Logic

168 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

an Eclosure after every step. We are, in effect, taking the image of q0under string x, except that we are allowing an arbitrary number of εs to bearbitrarily inserted into x. Also, we will overload Eclosure to work over setsof states in the obvious manner, as follows:

Eclosure(S) = x | ∃s ∈ S : x ∈ Eclosure(s).

(This definition can also be written as

Eclosure(S) = ∪s∈S Eclosure(s).)

Likewise, we overload δ to work over sets of states:

δ(S, a) = x | ∃s ∈ S : x ∈ δ(s, a).

Now we define δ(q, x), the ‘string transfer function’ for state q and stringx inductively as follows:

δ(q, ε) = Eclosure(q)

For a ∈ Σ and x ∈ Σ∗,

δ(q, ax) = y | ∃s ∈ Eclosure(δ(Eclosure(q), a)) : y ∈ δ(s, x)

In other words, for every symbol a in ax, we Eclose state q, “run” a fromeach one of these states, and Eclose the resulting states. From each state sthat results, we recursively run x.

Finally, the language of an NFA N is

L(N) = w | δ(q0, w) ∩ F 6= ∅.

In other words, after running the NFA from state q0 with input w, we seewhether any ‘token’ has reached a final state.A good way to intuitively understand the above definitions pertaining toNFAs is through the following ‘token game:’

• Place a token in state q0. Spread one copy of the token to each statein Eclosure(q0).

• For each symbol a from Σ that is entered, advance each token to its setof a successors.

• Eclose the tokens and continue.

Page 177: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 169

• If and when one of the tokens reaches some final state, the string seenso far is accepted.

Illustration 168.1 The NFA in Figure 11.4 has string 0001 in its language.First, Eclosure(IF ) = IF. After the first 0, the NFA is in state FA,whose Eclosure is FA,B0, A1, FB. After the second 0, A1 goes to FA,and the Eclosure results in FA,B0, A1, FB. The same happens after thethird 0. After the 1, FA and A1 go to A1, B0 goes to FB, and the token inFB goes to φ, resulting in A1, FB, which is also its own Eclosure. Thismatches the definition

δ(q, ax) = y | ∃s ∈ Eclosure(δ(Eclosure(q), a)) : y ∈ δ(s, x)

because Eclosure(δ(Eclosure(IF ), 0)) is FA,B0, A1, FB, and recursivelywe process input 001 from these states to reach A1, FB, which has a finalstate in it.

Exercise 11.4 What is the language of the NFA of Figure 11.4? List allstrings of length six or less in the language of this NFA. For each string,provide all possible reasons for that string to be in the language.

Exercise 11.5 Give an NFA accepting the set of strings over Σ = 0, 1such that every block of five consecutive symbols contains at least two 0’s.

Exercise 11.6 Attempt to write a regular expression directly for the lan-guage over Σ = 0, 1 in which every block of five consecutive symbols con-tains at least two 0’s. Spend some time directly encoding the said conditionusing regular expressions, and verify your results using grail. If you fail,you may try again later by drawing a DFA and converting that DFA to anRE.

Exercise 11.7 Design an NFA to recognize the language of strings over0, 1 beginning with a 0, ending with a 1, and having an occurrence of 0101somewhere in every string.

Exercise 11.8 Attempt to directly write an RE for the language over Σ =0, 1 in which no string has a 0101 occurring in it. Spend some time directlyencoding the ‘no 0101’ condition using regular expressions, and verify yourresults using grail. If you fail, you may try again later by drawing a DFAand converting that DFA to an RE.

Page 178: Computation Engineering Applied Automata Theory and Logic

170 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

-------------- --------------

Legal Illegal

-------------- --------------

[201]221-1221 [201]2211221

201-221-1221 201-2211221

2012211221 201221-1221

221-1221 2211-221

2211221

-------------- --------------

Figure 11.5: Legal and illegal phone numbers in our example

Exercise 11.9 Draw an NFA to recognize the star of the language of theNFA in Figure 11.4.

Exercise 11.10 Design an NFA to recognize the reverse of the language ofthe NFA in Figure 11.4.

Exercise 11.11 Design an NFA to capture the syntax of floating-point num-bers over alphabet 0, 1,+,−, E, . (for the sake of simplicity). A floating-point number begins with an optional sign character (one of + or −), fol-lowed by a mantissa, the letter E, an optional sign, and an exponent. Whilethe exponent is an integer, the mantissa is a decimal fraction of the formwhole.frac where whole or frac (but not both) may be empty.

11.2.5 A detailed example: Telephone Numbers

We now point out that in some cases we prefer NFAs (or Regular Expressions)not because they are smaller, but simply because they are much easier tospecify, and the results would be much more convincing than if we were todraw a DFA directly, or simply write a natural language description of thelanguage in question. Our example also concretely illustrates the ability of εtransitions to make descriptions clearer.

Illustration 170.1 Suppose we want to develop a lexical analyzer11for tele-phone numbers. We want to be maximally flexible, and allow multiplesyntaxes for phone numbers for users’ convenience. First, let us assume

11Also known as a “scanner” - a tool that digests input at the character level andconverts them into meaningful entities called tokens.

Page 179: Computation Engineering Applied Automata Theory and Logic

11.2.NON-D

ETERMIN

ISTIC

FIN

ITE

AUTOMATA

171

200

0

e

1

e

3

e

5

e

61

e

62

e

128

e

129

e

131

e

133

e

20

0

22

1

24

2

2

0

4

1

6

2

85

0

87

1

89

2

63

[

150

0

152

1

154

2

130

0

132

1

134

2

26

0

28

1

30

2 012 012

8

0

10

1

12

2

14

0

16

1

18

2 0 12 0 12

0 1 20 1 2

0 1 20 1 20 1 2

32

0

34

1

36

20 120 12

38

0

40

1

42

20 1 20 1 2

44

0

46

1

48

2 01 2 01 2

50

0

52

1

54

20 1 20 1 2

56

0

58

1

60

2 012 012

91

0

93

1

95

2 0 120 12

65

0

67

1

69

2

71

0

73

1

75

2 01 201 2

77

0

79

1

81

20 120 12

83

] ]]

0 12

97

0

99

1

101

2 0 120 12

103

- --

105

0

107

1

109

2

111

0

113

1

115

2 01 201 2

117

0

119

1

121

20 1 20 1 2

123

0

125

1

127

20 12 0 12

156

0

158

1

160

20 12 0 12

136

0

138

1

140

2

142

0

144

1

146

2 012 012

01 2 01 2

148

---

01 2

162

0

164

1

166

2 0 120 12

168

- --

170

0

172

1

174

2

176

0

178

1

180

2 01 201 2

182

0

184

1

186

20 1 20 1 2

188

0

190

1

192

20 12 0 12

Figu

re11.6:

AnNFA

formally

specify

ingallow

edtelep

hon

enumbers

(""+(0+1+2)(0+1+2)(0+1+2))(0+1+2)(0+1+2)(0+1+2)(0+1+2)(0+1+2)(0+1+2)(0+1+2)+

(""+[(0+1+2)(0+1+2)(0+1+2)])(0+1+2)(0+1+2)(0+1+2)-(0+1+2)(0+1+2)(0+1+2)(0+1+2)+

(""+(0+1+2)(0+1+2)(0+1+2)-)(0+1+2)(0+1+2)(0+1+2)-(0+1+2)(0+1+2)(0+1+2)(0+1+2)

Figu

re11.7:

AnRE

specify

ingtheNFA

ofFigu

re11.6

Page 180: Computation Engineering Applied Automata Theory and Logic

172 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

0

4

01

2

1

[

5

01

26

01

2

8

01

2

2 14

20

1

17

01

2

3

19

01

2

20

01

2

7

01

2

24

-

23

01

2

9

01

2

110

12

10

01

2

]

12

01

2

15

01

2

13

01

2

16

01

2

01

2

- 22

01

2

18

01

2

21

01

2-

01

2

Figure 11.8: A minimal DFA for the NFA of Figure 11.6

a world where we have not invented the terminology of non-deterministicautomata or regular expressions. We will then be forced to describe the setof legal telephone numbers in natural language prose (in our case, English).Here is how an initial attempt might look like:

A telephone number consists of three parts: an area-code (3 digits),

a middle part (3 digits) and the extension (4 digits), written in that

order. We consider only digits in the range 0 to 2, to keep things

simple. All three parts may be written in juxtaposition, or may be

separated by exactly one dash, ‘-’. If the area code is separated from

the middle part by a dash, the middle part must also be separated

from the extension by a dash. The area-code may also be surrounded

by brackets12, ‘[’ and ‘].’ In this case, the middle part must be

separated from the extension by a dash. The area-code is optional; if

not present, neither the brackets surrounding the area code nor the

dash separating the area code from the middle part must be employed.

Example telephone numbers that are legal and illegal according to theseconventions are listed in Figure 11.5. (Please verify that these indeed followthe rules stated in English.)

12We use brackets [ and ] in lieu of the more traditional parantheses ‘(’ and ‘)’ becausethe grail tool we use assigns special significance to parantheses - and we wanted to avoidwork-arounds to keep things simple.

Page 181: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 173

/*----------- Definitions space -----------*/

D [0-2] /* Digit */

TD DDD /* Three Digits */

FD DTD /* Four Digits */

BAC "["TD"]" /* Bracketed Area Code */

DAC TD"-" /* Dashed Area Code */

BACOpt BAC|"" /* Optional Bracketed Area Code */

DACOpt DAC|"" /* Optional Dashed Area Code */

ACOpt TD|"" /* Optional Area Code */

Tele ACOptTDFD|BACOptTD"-"FD|DACOptTD"-"FD

/* (""+(0+1+2)(0+1+2)(0+1+2)) (0+1+2)(0+1+2)(0+1+2)(0+1+2)(0+1+2)(0+1+2)(0+1+2)+

(""+[(0+1+2)(0+1+2)(0+1+2)])(0+1+2)(0+1+2)(0+1+2)-(0+1+2)(0+1+2)(0+1+2)(0+1+2)+

(""+(0+1+2)(0+1+2)(0+1+2)-)(0+1+2)(0+1+2)(0+1+2)-(0+1+2)(0+1+2)(0+1+2)(0+1+2) */

/*----------- Rules space -----------*/

%%

Tele

printf("A number:%s\n",yytext);

[\t\n]+ /* eat up whitespace */

. printf("Unrecognized character:%s\n",yytext);

/*----------- User code space -----------*/

%%

int yywrap();

main()

yylex();

int yywrap()

return 1;

/*----------- End -----------*/

> flex telephone.l

> cc lex.yy.c

> a.out

> 0120120120

A number:0120120120

> 000-0000

A number:000-0000

> [000]111-2222

A number:[000]111-2222

> 000-000-0000

A number:000-000-0000

> 0001111

A number:0001111

> [122] 111-1111

Unrecognized character:[

Unrecognized character:1

Unrecognized character:2

Unrecognized character:2

Unrecognized character:]

Unrecognized character:

A number:111-1111

Figure 11.9: Skeleton of an encoding of the telephone number syntax in flex

Page 182: Computation Engineering Applied Automata Theory and Logic

174 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

Unfortunately, as is well known, natural language descriptions are oftenprone to misinterpretation. Let us therefore provide a formal specificationof legal and illegal telephone numbers. We do this by writing a regularexpression as shown in Figure 11.7. This RE can be turned into an NFAas shown in Figure 11.6. This NFA exhibits three classes of strings in theform of “three lobes” of state clusters. The correspondence between the REand the NFA must be quite apparent. The minimal DFA for this NFA isgiven in Figure 11.8. We can then use a modern lexical analyzer generator(flex in our case) that generates a recognizer for this language, as sketchedin Figure 11.9. The commands to generate the lexical analyzer and a fewinteractions with the generated analyzer (including an error scenario) are alsoincluded. The concept behind scanner programs was illustrated in Figure 9.2;modern scanners are, however, far more efficient than the simple-mindedprogram used in Figure 9.2.

Writing regular expressions is a highly error-prone activity. The next sectionillustrates how to formally verify regular expressions through putative queries(“challenge queries”). The main idea we stress on is that often the comple-ment of the language we are interested in is far easier to characterize—atleast to a large approximation. We can then intersect the complement of thelanguage with the language of interest to see if the intersection is empty.

11.2.6 Tool-assisted Study of NFAs, DFAs, and REs

In this section, we illustrate the use of the Grail tools to help understandthe material so far. Basically, grail and the support scripts provide us thefollowing commands that can be invoked from Unix:

• fa2grail.perl, a Perl script

• grail2ps.perl, a Perl script

• dot, a graphics package

• ghostview or gv, a postscript viewer

• One of several grail-specific commands. We include some of thesecommands below (for details, kindly see the grail user manual):

– retofm for converting regular expressions into grail-specific inter-nal “fm” format,

Page 183: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 175

– fmdeterm for determinizing fm-formatted NFAs,

– fmmin for minimizing DFAs,

– fmcment for complementing DFAs,

– fmcross for obtaining the intersection of two DFAs

A quick tool check

In order to check that all the tools are available in one’s search-path, type

> echo ’(a+b)*’ | retofm | fmdeterm | fmmin | perl grail2ps.perl - | gv -

Here > stands for the Unix prompt (Grail currently runs under Unix). If thiscommand works, it would have the following effect. The regular expression(ab)*+ would be turned into an NFA, this NFA determinized and minimized,and finally picked up by the script grail2ps.perl which, with the help ofthe dot tool, produces postscript and draws the DFA for you! Note how wequote the command using ‘ and ’ to prevent Unix from assigning a specialinterpretation to *. Also note the use of the Unix pipe | and the character- that denotes “standard input.”

Tool-assisted RE debugging

While regular expressions are often easy to write, often they can prove tobe error-prone to write. We highly recommend that one always apply toolssimilar to grail for debugging proposed REs. In this section, we present asystematic method of debugging REs using grail.

Proposed RE

Someone proposes the following RE to express the language L, where “L =the set of strings of 0’s and 1’s with at most one pair of consecutive 1’s.”

(0+ + ε) (10+)∗ (11 + 1) (0+1)∗ (0+ + ε)

We assume that the reader did not find anything wrong about this RE, butnevertheless wanted to make sure. Let’s formally debug by applying putativequeries to study the expected properties of such an RE. We know that thelanguage of this machine must not have any 11(0+1)*11 or any 111 in it.

Page 184: Computation Engineering Applied Automata Theory and Logic

176 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

One can often identify and specify whole classes of such “bad strings” muchmore reliably than one can do with the RE of interest. Capitalizing thesefact, we proceed to debug as follows.

1. First, obtain a minimal DFA corresponding to the given RE:

> echo ’(00*+"")(100*)*(11+1)(00*1)*(00*+"")’ | retofm

| fmdeterm | fmmin >! a1.txt

> cat a1.txt | perl grail2ps.perl - >! a1.ps

10

4

1

3

0

0

11 0

2

01

0

2. Second, get “the machine of all bad strings” built (make sure you un-derstand that this machine contains all the bad strings):

> echo ’(0+1)*11(0+1)*11(0+1)*+(0+1)*111(0+1)*’ | retofm

| fmdeterm | fmmin >! a2.txt

> cat a2.txt | perl grail2ps.perl - >! a2.ps

10

3

10

2

1

4

0

0

11 0

10

Page 185: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 177

3. Now we take a look at the complement of the machine of bad strings:

> echo ’(0+1)*11(0+1)*11(0+1)*+(0+1)*111(0+1)*’ | retofm

| fmdeterm | fmcment | fmmin >! a2c.txt

> cat a2c.txt | perl grail2ps.perl - >! a2c.ps

20

3

10

0

1

1

01

0

We find that the complement of the “bad machine” is not the same as“our machine.” This is clearly indicative of something being wrong withour original RE (assuming that the complement machine was correct).

4. Let’s at least make sure that our machine doesn’t contain any badstrings.

> fmcross a1.txt a2.txt | perl grail2ps.perl - >! cross.ps

> gv cross.ps

6 0

19

1

0

2

0 1

0

5

7

0

0

15

1

10

22

01

0

0

20

0

1 0

4

1

11

21

0

1 0

14

1

16

0

1

12

1

0

17

0

1

3 0

1

8 0

1

13

23

0

1

0

1

18

0

1

10

9

1010 10

24

10

Here we see the unminimized DFA. Apparently none of its states arereachable. We now proceed to minimize and view this machine:

> fmcross a1.txt a2.txt | fmmin | perl grail2ps.perl - >! crossmin.ps

> gv crossmin.ps -- this gives an empty automaton - NOTHING printed by gv!!

This experiment indicates that our machine does not include any badstrings. In other words, it is leaving out some of the good strings. Thenext quest is to find out which these are.

Page 186: Computation Engineering Applied Automata Theory and Logic

178 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

5. So what is in the complement of the bad machine that’s not in ourmachine (i.e. what are we leaving out)?

> cat a1.txt | fmcment | fmmin >! a1c.txt

> fmcross a2c.txt a1c.txt | fmmin >! a1ca2ccrossmin.txt

> cat a1ca2ccrossmin.txt | perl grail2ps.perl - >! a1ca2ccrossmin.ps

0 0

Clearly we left out the all zeros case! Let’s fix it and view it.

> echo ’(00*+"")(100*)*(11+1+"")(00*1)*(00*+"")’ | retofm

| fmdeterm | fmmin >! a1fixed.txt

> cat a1fixed.txt | perl grail2ps.perl - >! a1fixed.ps

20

3

10

0

1

1

01

0

Thus, we have detected the error in our RE and also repaired it.

Inputting DFAs into the Grail pipeline

We have created a script to intuitively encode DFAs in an ASCII syntax andinput into grail. We illustrate this through an example. Let file expt1

contain the following:

I - 0 -> A

I - 1 -> Dead

A - 0 -> Dead

A - 1 -> Fbfi

Fbfi - 0 -> Ffia

Page 187: Computation Engineering Applied Automata Theory and Logic

11.2. NON-DETERMINISTIC FINITE AUTOMATA 179

1

2

0

3

1

04

1

01

1

5

00

1Figure 11.10: DFA obtained via grail

Fbfi - 1 -> Dead

Ffia - 1 -> Fbfi

Ffia - 0 -> A

Dead - 0,1 -> Dead

Now, if we type

> fa2grail.perl expt1 | fmdeterm | perl grail2ps.perl - | gv -

we can obtain the DFA in Figure 11.10. We will introduce further featuresof the grail pipeline in subsequent chapters.

Page 188: Computation Engineering Applied Automata Theory and Logic

180 CHAPTER 11. NFA AND REGULAR EXPRESSIONS

Page 189: Computation Engineering Applied Automata Theory and Logic

Chapter 12

Operations on RegularMachinery

In this chapter, we introduce algorithms that operate on ‘regular machinery,’meaning various representations of regular sets.

• We present algorithms to convert between NFA, DFA, and regular ex-pressions, thus establishing the equivalence of their expressive power.

• We present algorithms for performing the following operations on ma-chines, and discuss which machine type (NFA or DFA) each operationwould be natural to perform on:

− Union, Intersection, Complementation, Star;

− Homomorphism, Inverse homomorphism;

− Reversal, and Prefix Closure.

The existence of these algorithms establishes the closure of regular lan-guages under these operations.

• We present an algorithm to minimize DFAs, arguing that the results areunique (upto isomorphism). We then briefly discuss a similar operationon NFAs.

• Finally, we discuss the notion of ultimate periodicity. Ultimate period-icity not only helps understand regular languages better, but also setsthe stage to prove that certain languages are not regular. For instance,it helps us argue very directly that sets such as ai2 | i ≥ 0 are notregular.

181

Page 190: Computation Engineering Applied Automata Theory and Logic

182 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

12.1 NFA to DFA conversion

NFA DFA

=== ===

Input 0 1 e Input 0 1

--------------------- ------------------

States | States |

I | I,A I I | I,A I

| |

A | B B I,A | I,A,B I,B

| |

B | F F I,B | I,A,F I,F

| |

F | I I,A,B | I,A,B,F I,B,F

|

I,A,B,F | I,A,B,F I,B,F

|

I,B,F | I,A,F I,F

|

I,A,F | I,A,B I,B

|

I,F | I,A I

|

Figure 12.1: NFA to DFA conversion illustrated on the NFA of Figure 11.3

Given an NFA N = (Q,Σ, δ, q0, F ), a DFA that is language-equivalent toN is given by

D = (2Q,Σ, δD, Eclosure(q0), f | f ∈ 2Q ∧ f ∩ F 6= φ).

This DFA D is designed to mimic the acceptance behavior of the NFA N asfollows:

• N can have its tokens in (potentially) any combination of its states.Therefore, the states of D are chosen to be 2Q, the powerset of thestates of N .

• When we place a token in the initial state of N , it will spread (ε-close)to all states reachable through ε moves. To model this, D’s startingstate is chosen to be Eclosure(q0).

Page 191: Computation Engineering Applied Automata Theory and Logic

12.1. NFA TO DFA CONVERSION 183

• N accepts a string when one of its tokens reaches a state in F . Tomodel this, the final states of D are chosen to be those subsets of 2Q

that include a state of F .

• The last (and crucial) detail is δD:

δD(S, a) = y | ∃s ∈ S : y ∈ Eclosure(δ(Eclosure(s), a)).

In other words, δD takes a state S of D, first Ecloses all the statesit contains, runs δ of the NFA from each resulting state, and finallyEcloses and unions the results.

Another way to define δD is through the δ (string transition) function of theNFA, which, in effect, does the same thing as the above definition:

δD(S, a) = y | ∃s ∈ S : y ∈ δ(s, a).

Exercise 12.1 Suppose an alternate definition of δD is offered:

δD(S, a) = y | ∃s ∈ S : y ∈ Eclosure(δ(s, a)).

Does it change the behavior of the resulting DFA? Justify your answer. Whydo we perform Eclosure before and after δ in case of the NFA, on Page ???

It is straightforward to see that this construction is correct: by construc-tion, whenever N accepts a string w, D also accepts w, and vice versa. SinceNFAs can be converted to DFAs, the language of an NFA is also regular.Also, D can potentially have 2|Q| states.

Illustration 183.1 Let us convert the NFA of Figure 11.3 to its equivalentDFA. The results are shown in Figure 12.1. We succinctly demonstrate theconversion by listing their state transition tables side-by-side as below. Weintroduce only those states of 2Q that are reachable. A systematically way toproceed is now illustrated with respect to an example. Suppose the DFA is instate I,A,B. Suppose the input be 0. Then we walk through the column0 in the NFA table, union what we see against I, with what we see againstA and what we see against B. These sets are, respectively, I,A, B, andF. The union of these sets is I,A,B,F. Now, we perform the Eclosurestep, which applies only to state F which has an ε move to state I. But sinceI is already in I,A,B,F, the final result is I,A,B,F. We proceed to dothe same for every other state of the DFA.

Page 192: Computation Engineering Applied Automata Theory and Logic

184 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

Exercise 12.2 Convert the NFA in Example 11.4 into an equivalent DFA,showing all the steps.

Exercise 12.3 Convert the NFA of Figure 11.2 to a DFA for k = 2 andk = 5. Repeat for the modified NFA in Figure 11.3.

Exercise 12.4 The token-game of an NFA can be succinctly stated as fol-lows: an NFA accepts a string x if there exists a path labeled by z from thestart state to some final state, and x is z projected onto the alphabet Σ. Con-sider a variant of an NFA called all-paths NFA. In an all-paths NFA, a stringx is accepted if and only if all such z paths that are in the machine actuallylead to some final state. Formally define the all-paths NFA as a five-tuplemathematical structure, and prove that its language is regular, by convertingit to an equivalent DFA.

12.2 Operations on Machines

We now take operations one at a time and illustrate it on regular machinery.Under each operation, we comment on the relative difficulty of performing theoperation directly on a certain machine type. More importantly, we point outthose algorithms that are incorrect to carry over to the ‘wrong’ machine type.The operations will be illustrated on DFA D1 and D2, NFA N1 and N2, andREs R1 and R2, as the case may be. For the sake of notational uniformity, weemploy alphabet names Σ1 and Σ2, where in fact Σ1 = Σ2 = Σ. Specifically,we use

D1 = (Q1,Σ1, δ1, q10, F1), and D2 = (Q2,Σ2, δ2, q

20, F2), for binary oper-

ations, and D1 alone for unary operations.

Similarly, we will employ NFA

N1 = (Q1,Σ1, δ1, q10, F1) and N2 = (Q2,Σ2, δ2, q

20, F2)

and regular expressions R1 and R2. The results will be presented in terms of

D = (Q,Σ, δ, q0, F ), for DFA, N = (Q,Σ, δ, q0, F ), for NFA, and R, forregular expressions.

Page 193: Computation Engineering Applied Automata Theory and Logic

12.2. OPERATIONS ON MACHINES 185

In some cases, the operation changes the type of the machine, and if this hap-pens we will explicitly point it out. For example, concatenation of two DFAcan be directly accomplished if we treat the DFA as NFA and concatenatethem; the result will then be an NFA). We show the results for REs alongwith NFAs. Through minor notational abuse, we also will call the resultingmachines D1 for the complement of DFA D1, D1 ∪D2 for the union of twoDFAs, etc.

12.2.1 Union

With DFA, D models the marching of D1 and D2 in lock-step, and seeingwhen one machine or the other accepts. In particular,

D = D1 ∪D2 = (Q1 ×Q2,Σ, δ, 〈q10, q20〉, (Q1 × F2 ∪ F1 ×Q2)), where

δ(〈x, y〉, a) = 〈δ1(x, a), δ2(y, a)〉.

As an example, the result of applying the union algorithm on the DFA shownin Figures 12.2(a) and 12.2(b) is given in Figure 12.2(c). Notice that theresult is a DFA, and that its state names are derived from the names of thestates of the individual DFA by glueing the state names with an underscore‘_’ - a convenient way to convert pairs of states to state names.

Now we illustrate how to performing union on two NFAs. We introducea new start state I0 not present in Q1 or Q2, from which ε transitions areintroduced into both the start states q10 and q20. The result of applying thisalgorithm on Figures 12.3(a) and 12.3(b) is given in Figure 12.3(c). ThisNFA is equivalent to the DFA given in Figure 12.2(c).

In symbols,

N = N1 ∪N2 = (Q1 ∪Q2 ∪ I0,Σ, δ, I0, F1 ∪ F2), whereδ = δ1 ∪ δ2 ∪ 〈I0, ε, q10, q20〉.

Exercise 12.5 What are the languages of the machines in Figures 12.2 andFigures 12.3?

As can be seen, with NFA and RE, union is a linear-time operation,while with a DFA, it is an O(N 2) operation. For regular expressions, unionis achieved using the RE builder ‘+.’

Page 194: Computation Engineering Applied Automata Theory and Logic

186 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

(a)

I F1

BH10

A1

0

1 00,1

(b)

IF

B1

BH201

00,1

(c)

I_IF

F_B

1

BH1_BH2

0

A_IF1

1

0,1

(d)

I_IF

F_B1

BH1_BH2

0

A_IF1

1

0,1

(e)

I

F

1

BH10

A

1

0

10

0,1

Figure 12.2: DFA(a) ∪ DFA(b) = DFA(c); DFA(a) ∩ DFA(b) = DFA(d); DFA(a)

= DFA(e)

Page 195: Computation Engineering Applied Automata Theory and Logic

12.2. OPERATIONS ON MACHINES 187

(a)

A1 A21 A3

1

1

(b)

B1 B21

B31

1

(c)

I

A1 A21 A3

1

1e

B1

e

B21

B31

1

(d)

A1

A21

B1e

A3

1

1e

B21

B31

1

Figure 12.3: NFA(a) ∪ NFA(b) = NFA(c); NFA(a) NFA(b) = NFA(d)

12.2.2 Intersection

With DFA, intersection works exactly like union, except that F = F1 × F2,as shown in Figure 12.2(d) which has no final states. Performing intersec-tion directly on NFA and RE is not straightforward (unless an extended REnotation containing intersection is employed). Therefore before performingintersection, it is customary to convert the NFA or RE (as the case may be)to a DFA. This can incur exponential cost.

Page 196: Computation Engineering Applied Automata Theory and Logic

188 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

12.2.3 Complementation

This operation is most easily performed on DFA, resulting in

D = D1 = (Q1,Σ1, δ1, q10, Q1 \ F1)

as illustrated in Figure 12.2(e). Again, with NFA and RE, the operation ismost commonly done after conversion to a DFA. In fact, with NFA, doingcomplementation directly by exchanging final and non-final states is incor-rect.

Exercise 12.6 Call the DFA in Figure 9.5 D. Obtain the complement DFAD by the complementation algorithm. Then obtain a DFA corresponding tothe union of D and D. Repeat for the intersection of D and D. Check thatyou are indeed obtaining the right answers.

Exercise 12.7 Give an example of an NFA on which performing comple-mentation as with DFAs (exchanging final and non-final states) is correct,and another example where it is incorrect.

12.2.4 Concatenation

Concatenation on DFAs involves treating them as NFAs and applying theNFA concatenation algorithm. With NFAs, concatenation involves introduc-ing a ε transition from all the final states of the first NFA to the start stateof the second NFA, and regard the final states of the second NFA as the finalstates of the resulting NFA; see Figure 12.3(d). In symbols,

N = N1 N2 = (Q1 ∪Q2,Σ, δ, q10, F2), where

δ = δ1 ∪ δ2 ∪ 〈x, ε, q20〉 | x ∈ F1.With RE, R = R1 R2 or simply R = R1R2 (recall that we can omit andsimply use juxtaposition).

12.2.5 Star

With DFA, star is best performed by treating the DFA as an NFA. WithNFA, we introduce a new start state I0, regard this state also as a final state(thus introducing ε into the language of N ∗), and introduce a transition on εfrom I0 to q0. Finally, to introduce iteration, we introduce a transition fromevery state in F to q0. In symbols,

Page 197: Computation Engineering Applied Automata Theory and Logic

12.2. OPERATIONS ON MACHINES 189

IF I

A1e

B1e

eA2

1 A31

e

1e

B21

e

B31

1

Figure 12.4: The result of Starring Figure 12.3(c)

NewI

I

A1

e

A3

e

IF

e

B2

e

e

A2

1

1

1

e

e

e

e

B1e

1

B3

1

1

Figure 12.5: The result of Reversing the NFA of Figure 12.4

N = N ∗1 = (Q1 ∪ I0,Σ1, δ, I0, F1 ∪ I0), where

δ = δ1 ∪ 〈I0, ε, q10〉 ∪ 〈x, ε, q10〉 | x ∈ F1.

Figure 12.4 illustrates this construction.

Exercise 12.8 Describe an alternate construction for Star.

With REs, naturally we have R = R∗1.

12.2.6 Reversal

With DFA, reversal is best performed by treating the DFA as an NFA. WithREs, reversal is best performed after converting to an NFA. With NFA, to doreversal, we introduce a new state I0, make that the initial state, introduce

Page 198: Computation Engineering Applied Automata Theory and Logic

190 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

a transition on ε from I0 to the set of states F1. We also reverse every edgein the NFA diagram, and regard q10 as the set of final states. In symbols,

N = rev(N1) = (Q1 ∪ I0,Σ1, δ, I0, q10), whereδ = 〈I0, ε, F1〉 ∪

〈r, a, q | q ∈ Q1 ∧ r ∈ δ(q, a)〉 | r ∈ Q1 ∧ a ∈ Σ1 ∪ ε.Figure 12.5 illustrates this construction. Notice that in this example, wehave a jump from state NewI to state IF because IF was both a final stateand start state in the original machine.

The reason why δ looks so complicated is because we try to reverse thetransitions that, for an NFA, are specified via its delta function as movesfrom a state to a set of next states. We must also preserve the ‘state to setof states’ nature of the mapping for the δ of the reversed machine. In the δfor the reversed machine, we introduce a move from every state r ∈ Q1 forevery a ∈ Σ1 ∪ ε to all those states q such that those q states had at leastone ‘forward’ transition through a to r.

Exercise 12.9 Instantiate the NFA in Figure 11.3 for n = 2 and n = 3,calling these machines N2 and N3, respectively. Perform union and concate-nation. With respect to both these results, list eight strings in lexicographicorder.

Exercise 12.10 For the N3 machine in Exercise 12.9, perform the star op-eration. How does the result differ from the machine in Figure 11.3 fork = 3?

Exercise 12.11 Reverse the NFAs in figures Figure 11.2 and Figure 11.3 forn = 3. Convert each resulting NFA to a DFA, and compare their languages.

12.2.7 Homomorphism

In Chapter 6, we introduced the notion of a homomorphism as a modularmapping with signature h : Σ∗ → Γ∗. In general, a homomorphism can be afunction that, in place of each symbol in Σ1, substitutes a regular languageover Γ1. Suppose for x ∈ Σ1, we must substitute language Lx. In the contextof an NFA, such a homomorphism can be easily implemented as follows:

• Build a separate NFA Nx corresponding to each instance of x in thegiven NFA N1.

Page 199: Computation Engineering Applied Automata Theory and Logic

12.3. MORE CONVERSIONS 191

• Wherever a move on x appears in N1 from state s1 to state s2, replace itwith a jump from s1 to the starting state of Nx via ε, and a jump fromevery final state of Nx via ε to s2. Make every state of Nx non-final.

12.2.8 Inverse Homomorphism

We illustrate this construction through an example. Consider h to be

h(ε) = ε

h(a) = 0

h(b) = 1

h(c) = h(d) = 2

Then, h−1(012) = abc, abd because 2 could have come either from c or fromd. Therefore, given an NFA over alphabet 0, 1, 2, we can label each moveon a symbol (such as 2) with a move that is labeled with all the symbols thatmap to 2 (in our case, c and d).

12.2.9 Prefix Closure

This operation is straightforward to perform either on an NFA or on a DFA.We simply turn every state along the way from the start state towards oneof the final states into a final state.

12.3 More Conversions

In this section, we define additional inter-conversions between REs, NFA,and DFA, thus establishing the equivalence of their expressive power.

12.3.1 RE to NFA

We first specify how to convert the basic regular expressions φ, ε, and a ∈ Σinto an NFA. For all other REs, we can recursively convert their constituentbasic REs into NFA and then apply the corresponding NFA-building opera-tor. The conversion of basic REs goes as follows:

Page 200: Computation Engineering Applied Automata Theory and Logic

192 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

• φ is an RE denoting φ The corresponding NFA is

N = (qφ,Σ, φ, qφ, φ)

where qφ is the only state of the NFA. The transition function is φ, i.e.,has no moves.

• ε is an RE denoting ε The corresponding NFA is

N = (qε,Σ, φ, qε, qε)

where qε is the only state of the NFA that also happens to be a finalstate.

• a ∈ Σ is an RE denoting a. The corresponding NFA is

N = (qa0 , qaF,Σ, δa, qa0 , qaF)

where δ is 〈qa0 , a, qaF〉.

• The NFA for r1 r2, r1 + r2, and r∗1 are obtained by first obtaining theNFA for r1 and r2, and applying, respectively, the algorithms for NFAconcatenation, union, and star.

12.3.2 NFA to RE

Given NFA N = (Q,Σ, δ, q0, F ), we can convert it to a regular expression bysuccessively eliminating its states (this is often called the GNFA approach).

• Preprocess N by adding one new initial state B and one new final stateE. We will, in the following steps, eliminate every state of N , leavingonly B and E behind. The transition connecting B and E will belabeled with the desired RE upon termination of our algorithm.

• Make all states F non-final, and introduce a transition from every statein F to E via ε.

• Introduce an ε transition from B to q0.

• Repeatedly eliminate a state from Q. In all the steps below, wheneverany pair of states p and q has two transitions going from p to q labeledwith regular expressions R1 and R2, replace them by a single transitionlabeled R1 +R2.

Page 201: Computation Engineering Applied Automata Theory and Logic

12.3. MORE CONVERSIONS 193

IF

e

FA

0

A11

B0e

e 1 FB

e

A2

01

e

e

0

e

1e

e

Figure 12.6: An Example NFA to be converted to a regular expression

B IFe

E

e

e

FA

0

e

A11 B0

e

e 1

FB

e

A20

1

e

e

e

0

e

e

1

e

Figure 12.7: Result of the preprocessing step

• Suppose

– state p has a transition into s labeled with RE Rps,

– state s has a transition to itself labeled Rss, and

– state s has a transition out of it to state q labeled Rsq.

Then, we can eliminate the ps and sq transitions, and introduce a directtransition from p to q labeled Rps(Rss)

∗Rsq. We keep repeating thesesteps till state s is disconnected from the rest of the graph, at whichpoint, it can be eliminated.

We now illustrate the NFA to RE conversion algorithm on the exam-ple NFA given in Figure 12.6. The result of the preprocessing step is in

Page 202: Computation Engineering Applied Automata Theory and Logic

194 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

B IFe

Eee

FA

0

e

FB(11*)

B0

e

A2

01

ee

(1*)

0

e

(1+e)(1*)

(1+e)(1*)

e

Figure 12.8: Result of eliminating state A1

B IFe

E

e

eFA0

e

FB

(1+e)1*+11*

A20

1

e

e1*+(1+e)1*

0

(1+e)1*

Figure 12.9: Result of Eliminating A2 and B0

Figure 12.7. This machine is represented as a generalized NFA or GNFA,meaning that we label the transitions using regular expressions.

Notice that state B0 reaches state A1 via (1e)+. No other immediatesimplifications exist. Now, let us choose to eliminate state A1. Introduce thefollowing paths:

FA to FB labeled by 11*

FB to FA labeled by 1*

B0 to FA labeled by (1+e)1*

B0 to FB labeled by (1+e)1*

The result appears in Figure 12.8.Now, let us choose to eliminate states A2 and B0. Introduce the following

paths:

Page 203: Computation Engineering Applied Automata Theory and Logic

12.3. MORE CONVERSIONS 195

B

FB

(1+e)1*+(1*+(1+e)1*)((1+e)1*)*((1+e)1*+11*)

E

e+(1*+(1+e)1*)((1+e)1*)*

IF (01*0+e)e

0((1+e)1*)*((1+e)1*+11*)

(e+0((1+e)1*)*)

e

Figure 12.10: Result of Eliminating FA

FB to IF labeled by (01*0+e)

FB to FA labeled by (1*+(1+e)1*)

FA to FB labeled by ((1+e)1*+11*)

Self-loop FA to FA labeled by (1+e)1*

Self-loop FB to FB labeled by (1+e)1*

The results appear in Figure 12.9.Now, let us choose to eliminate state FA. Introduce the following paths:

IF to E labeled by (e+0((1+e)1*)*).

Self-loop FB to FB labeled by ((1+e)1*+(1*+(1+e)1*)((1+e)1*)*((1+e)1*+11*))

IF to FB labeled by 0((1+e)1*)*((1+e)1*+11*).

FB to E labeled by (e+(1*+(1+e)1*)((1+e)1*)*).

The results appear in Figure 12.10.We leave the final result (obtainable by eliminating FB and IF) as an

exercise to the reader.

Exercise 12.12 Express the above conversion algorithm recursively. As-sume that you are given a preprocessed NFA. Check whether this NFA is“done” by seeing that it has a direct path from B to E, and if so, outputthe RE that labels this path. Else, express the choice of a state s at randomusing ∃ or choose. Then, eliminate s, updating the REs of all the states thatare directly reachable from s or directly reach s. Recurse on the resultingautomaton.

Exercise 12.13 Convert the DFA of Figure 11.10 into a regular expressionusing the conversion procedure explained above. Now convert the RE youobtain to an NFA. Determinize this NFA, and compare the resulting DFA tothe one you started from.

Page 204: Computation Engineering Applied Automata Theory and Logic

196 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

12.3.3 Minimizing DFA

The most important result with regard to DFA minimization is the Myhill-Nerode Theorem.

Theorem 12.1 The result of minimizing DFAs is unique, upto isomorphism.

The theorem says that given two DFAs over the same alphabet that arelanguage-equivalent, they will result in identical DFAs when minimized, uptothe renaming of states. One very dramatic illustration of the Myhill-Nerodetheorem will be in Chapter 13 where it will be shown that Binary DecisionDiagrams (BDDs)—an efficient data structure for Boolean functions—areminimized DFAs for certain finite languages of binary strings. These finitestrings in fact encode the truth assignment for Boolean formulae accordingto certain conventions that will be explained in Chapter 13. Because of thisuniqueness, equality testing between two Boolean functions can be reducedto pointer-equality in a representation of BDDs using hash-tables. We nowdiscuss the minimization algorithm itself.

The basic idea behind DFA state minimization is to consider all pairs ofstates systematically, by constructing a table. For each pair of states, weconsider all strings of length zero and up, and see if they can distinguish anypair of states. Initially, we distinguish all pairs of states 〈p, q〉 such that p isa final state and q is non-final. We enter a x in the table to record that thesestates are ε-distinguishable. In essence, at the beginning of the algorithmwe are treating all final states as belonging to one equivalence class, and allnon-final states as belonging to another.

Thereafter, in the ith iteration of table filling, we see if any of the statepairs 〈p, q〉 that are not yet distinguished have a move on some a ∈ Σ suchthat they go to states 〈p′ , q′〉 that are distinguishable (at the end of the i−1stiteration); if so distinguish 〈p, q〉. The algorithm stops when two iterationsk and k + 1 result in the same table.Illustration: Consider the transition table in Figure 12.11 where the finalstates are 1, 2, and 5, and Σ = a, b.

1. The initial blank table that permits all pairs of states to be comparedis in Figure 12.12.

2. All 0-length string distinguishable states are all pairs of states thatconsist of exactly one accept state (see below). All subsequent stepsidentifying i-distinguishable states for all i are also in Figure 12.13.

Page 205: Computation Engineering Applied Automata Theory and Logic

12.3. MORE CONVERSIONS 197

3

4a

5

a1 b

2a

6a

b

ab

ab

ba

Figure 12.11: Example for DFA Minimization

2 .

3 . .

4 . . .

5 . . . .

6 . . . . .

1 2 3 4 5

Figure 12.12: Initial table for DFA Minimization

2 x 2 x 2 x 2 x

3 x . 3 x . 3 x . 3 x .

4 . x x 4 . x x 4 x x x 4 x x x

5 . x x . 5 . x x . 5 x x x . 5 x x x .

6 x . . x x 6 x x x x x 6 x x x x x 6 x x x x x

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

0-dist 1-dist 2-dist 3-dist? No change!

So, done.

Figure 12.13: DFA Minimization

Page 206: Computation Engineering Applied Automata Theory and Logic

198 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

4a

b

1 2

ab

3

ab

ab

Figure 12.14: The final minimized DFA

3. Let us understand how one x was added. In Figure 12.13, we put an x

(distinguish between) states 2 and 6. Why is this so? This is because2 has a move upon input a to state 4, while state 6 moves upon a tostate 6 itself. From the 0-dist table, we know that states 4 and 6 aredistinguishable, one being a final and the other being a non-final state.

4. At the 3-dist step, there was no change from the previous table. Atthis point, state pairs 3,2 and 5,4 still have a ‘.’ connecting them(they could not be distinguished). Therefore we merge these states,resulting in the minimized DFA of Figure 12.14.

Exercise 12.14 Apply the DFA minimization algorithm to the DFA of Fig-ure 11.10.

Exercise 12.15 Perform the Grail command sequence

> echo ’(00*+"")(100*)*(11+1+"")(00*1)*(00*+"")’ | retofm

| fmdeterm >! a1fixed-unmin.txt

Then, hand-minimize the result (meaning, construct the DFA from the regularexpression by inspection and then hand-minimize it), comparing it with theminimized version a1fixed.txt discussed on Page 178.

Exercise 12.16 Express the DFA minimization algorithm neatly using pseudo-code. Analyze its time complexity.

Page 207: Computation Engineering Applied Automata Theory and Logic

12.4. ULTIMATE PERIODICITY AND DFAS 199

12.4 Ultimate Periodicity and DFAs

Ultimate periodicity is a property that captures a central property of regularsets.

Theorem 12.2 If L is a regular language over an alphabet Σ, then the setLen = length(w) | w ∈ L is ultimately periodic.

Note: The converse is not true. For example, the language 0n1n | n ≥ 0has strings whose lengths are ultimately periodic, and yet this language isnot regular.

A good way to see that Theorem 12.2 is true is as follows. Given anyDFA D over some alphabet Σ, consider the NFA N obtained from D by firstreplacing every transition labeled by a symbol in Σ \ a by a. In otherwords, we are applying a homomorphism that replaces every symbol of theDFA by a. This is a length-preserving homomorphism. Thus, the resultingmachine is bound to be an NFA, and the lengths of strings in its languagewill be the same as those of the strings in the language of the original DFA.Now, if we convert this NFA to a DFA, we will get a machine that startsout in a start state and proceeds for some number (≥ 0) of steps before it“coils” into itself. It cannot have any other shape than the ‘coil’ (why?).This coiled DFA shows that the length of strings in the language of any DFAis ultimately periodic with the period defined by the size of the coil. (For thedefinition of ultimate periodicity, see Exercise ??.)

We now take an extended example to illustrate these ideas. Consider theDFA built over Σ = a, b, c, d, f using the following command pipeline:

echo ’(ad)*+((abc)((acf)*+(da)*)d)’ | retofm | fmdeterm

| fmmin | grail2ps.perl - | gv -

Page 208: Computation Engineering Applied Automata Theory and Logic

200 CHAPTER 12. OPERATIONS ON REGULAR MACHINERY

8 5

a

3

d

7b

2

ad

9

c

d

6

a

1

c

4

a

0

d

f

The DFA generated by converting every symbol to a and determinizingthe result is as follows.

echo ’(aa)*+((aaa)((aaa)*+(aa)*)a)’ | retofm | fmdeterm

| fmmin | grail2ps.perl - | gv -

0 1

a

7

a

6

a

5

a

3

a

4

a

2a

a

The length of strings in the language of this DFA is ultimately periodic,with the values of the constants n = 2 and p = 6 as per the definition of UPappearing in Section 6.1.1.

Notice that a language over a singleton alphabet is regular if and only ifthe lengths of strings in this language is ultimately periodic.

Exercise 12.17 Name a language over a singleton alphabet such that thelength of strings in this language is not ultimately periodic.

Exercise 12.18 Apply the homomorphism 1 → 0, 0 → 0, and ε → ε tothe DFA of Figure 11.10. Convert the resulting NFA into a DFA. Show thatthe length of strings in this DFA is ultimately periodic by finding n and pparameters.

Page 209: Computation Engineering Applied Automata Theory and Logic

Chapter 13

The Automaton/LogicConnection, SymbolicTechniques

Most believe that computer science is a very ‘young’ topic. In a sense, thatis true - there was the theory of relativity, vacuum tubes, radio, and tupper-ware well before there were computers. However, from another perspective,computer science is at least 150 years old! Charles Babbage1started build-ing his Analytic Engine in 1834 which remained unfinished till his death in1871. His less programmable Difference Engine No. 2 was designed between1847 and 1849, and built to his specifications in 1991 by a team at London’sScience Museum. As for the ‘theory’ or ‘science’ behind computer science,George Boole published his book on Boolean Algebra 2in 1853.

Throughout the entire 150 years (or so) history of computer science, onecan see an attempt on part of researchers to understand reasoning as well ascomputation in a unified setting. This direction of thinking is best capturedby Hilbert in one of his famous speeches made in the early 1900s in whichhe challenged the mathematical community with 23 open problems. Manyof these problems are still open, and some were solved only decades afterHilbert’s speech. One of the conjectures of Hilbert was that the entire bodyof mathematics could perhaps be “formalized.” What this meant is basicallythat mathematicians had no more creative work to carry out; if they wanted

1Apparently, Babbage is also credited with the invention of the ‘Cow-catcher’ that yousee in front of locomotive engines!

2Laws of thought. (You might add: to prevent loss of thought through loose thought.)

201

Page 210: Computation Engineering Applied Automata Theory and Logic

202CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

to discover a new result in mathematics, all they had to do was to programa computer to systematically crank out all possible proofs, and check to seewhether the theorem whose proof they are interested in appears in one ofthese proofs!

In 1931, Prof. Kurt Godel dropped his ‘bomb-shell3.’ He formally statedand proved the result “Such a device as Hilbert proposed is impossible!”By this time, Turing, Church, and others demonstrated the true limits ofcomputing through concrete computational devices such as Turing machinesand the Lambda calculus. The rest “is history!”

13.1 The Automaton-Logic Connection

The upshot of all these developments from the point of view of mathemati-cal logic as well as mechanized reasoning is that scientists now have a firmunderstanding of how computation and logic/reasoning are inexorably linkedtogether. The work in the mid 60’s, notably that of J.R. Buchi, furtheredthese connections, by relating branches of mathematics known as presburgerarithmetic, and branches of logic known as WS1S4with deterministic finiteautomata. Work in the late 70’s, notably by Pnueli, resulted in the adoptionof temporal logic as a formal logic to reason about concurrency. TemporalLogic was popularized by Manna and Pnueli through several textbooks andpapers. Work in the 80’s, notably by Emerson, Clarke, Kurshan, Sistla,Sifakis, Vardi, and Wolper established deep connections between temporallogic and automata on infinite words (in particular Buchi automata). Workin the late 80’s, notably by Bryant, brought back yet another thread of con-nection between logic and automata by the proposal of using Binary DecisionDiagrams, essentially minimized deterministic finite automata for the finitelanguage of satisfying instances of a Boolean formula, as a data structurefor Boolean functions. The symbolic model-checking algorithm proposed byMcMillan in the late 80’s hastened the adoption of BDDs in verification, thusproviding means to tackle the correctness problem in computer science. Also,spanning several decades, several scientists, including McCarthy, Wos, Con-stable, Boyer, Moore, Gordon, and Rushby, led efforts on the development ofmechanical theorem-proving tools that provide another means to tackle thecorrectness problem in computer science.

3Some mathematicians view the result as their salvation.4WS1S stands for the weak monadic second order logic of one successor.

Page 211: Computation Engineering Applied Automata Theory and Logic

13.2. BINARY DECISION DIAGRAMS 203

13.1.1 DFA can ‘scan’ and also ‘do logic’

In terms of practical applications, the most touted application domain forthe theory of finite automata is in string processing – pattern matching, rec-ognizing tokens in input streams, scanner construction, etc. However, thetheory of finite automata is much more fundamental to computing. Mostin-depth studies about computing in areas such as concurrency theory, tracetheory, process algebras, Petri nets, and temporal logics rest on the studenthaving a solid foundation on classical automata such as we have studied sofar. This chapter introduces some of the less touted, but nevertheless equallyimportant, ramifications of the theory of finite automata in computing. Itshows how the theory of DFA helps arrive at an important method for repre-senting Boolean functions known as Binary Decision Diagrams. The efficientrepresentation as well as manipulation of Boolean functions is central to au-tomated reasoning in several areas of computing including computer-aideddesign and formal verification. In Chapter ??, we demonstrate how exploit-ing the “full power” of DFAs, one can represent logics with more power thanpropositional logic. In Chapter ??, we demonstrate how automata on infinitewords can help reason about finite as well as infinite computations generatedby finite-state devices. In this context, we briefly sketch the connections be-tween automata on infinite words as well as Temporal Logics. We now turnto Binary Decision Diagrams, the subject of this chapter.Note: We use ∨ and + interchangeably, depending on what looks morereadable in a given context; they both mean the same (the or function).

13.2 Binary Decision Diagrams

Binary Decision Diagrams (BDD) are Bit-serial DFA for Satisfying Instancesof Boolean Formulae. To better understand this characterization, considerthe language

Labcd,d∨(a∧b∧c) = abcd | d ∨ (a ∧ b ∧ c).A regular expression describing this language by spelling out all the satisfyinginstances of d∨(a∧b∧c) is the finite regular language denoted by the followingregular expression (as is conventional, ∧ has higher precedence than ∨):(1110+1111+0001+0011+0101+0111+1001+1011+1101)

By putting this regular expression in a file called a.b.c+d, we can use thefollowing grail command sequence to obtain a minimal DFA for it, as shownin Figure 13.1(a).

Page 212: Computation Engineering Applied Automata Theory and Logic

204CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

(a)

1

6

1

7

0

4

1

5

0 1 0

3

1

2

0 0 1

0

1 0 1

(b)

7

1

0

6

1

5

1

4

0 1

3

1

2

0 1

0

1 0 1

Figure 13.1: Minimal DFAs for d ∨ (a ∧ b ∧ c) for (a) variable ordering abcd,and (b) dabc. The edges show transitions for inputs arriving according tothis order.

cat a.b.c+d | retofm | fmdeterm | fmmin | perl grail2ps.perl -

> a.b.c+d.ps

Now consider the language that merely changes the bit-serial order in whichthe variables are examined from abcd to dabc:

Ldabc,d∨(a∧b∧c) = dabc | d ∨ (a ∧ b ∧ c).

Using the regular expression(0111+1111+1000+1001+1010+1011+1001+1011+1101) as before, we ob-

tain the minimal DFA shown in Figure 13.1(b). The two minimal DFAs seemto be of the same size. Should we expect this in general? The minimal DFAsin Figure 13.1, and Figure 13.2 to follow, are suboptimal as far as their role indecoding the Binary decision goes, as they contain redundant decodings. For

Page 213: Computation Engineering Applied Automata Theory and Logic

13.2. BINARY DECISION DIAGRAMS 205

(a)

5

4

0

1

1

9

0 1

7

0

8

1

6

0 1

2

0

3

1

0

0 1

(b)

0

1

0

2

1

3

0

4

1

5

0

6

1

7

0

8

1

9

0

10

1

11

0

12

1

13

0

14

1

15

0

16

0

21

0

17

01 1 1 1

18

0

19

01 1

20

0 1

Figure 13.2: Minimal DFAs where the variable ordering matters

instance, in Figures 13.1(a), after abc = 111 has been seen, there is no needto decode d; however, this diagram redundantly considers 0 and 1 both goingto the accepting state 0. In Figures 13.1(b), we can make node 6 point di-rectly to node 0. Eliminating such redundant decodings, Figures 13.1(a) and(b) will, essentially, become BDDs; the only difference with a BDD at thatpoint would be that BDDs explicitly include a 0 node to which all falsifyingassignments lead to.

Let us now experiment with the following two languages where we shalldiscuss these issues even more, and actually present the drawing of a BDD.

Linterleaved = abcdef | a = b ∧ c = d ∧ e = fhas a regular expression of satisfying assignments

(000000+001100+000011+110000+001111+110011+111100+111111)

andLnoninterleaved = acebdf | a = b ∧ c = d ∧ e = f

Page 214: Computation Engineering Applied Automata Theory and Logic

206CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

01

14: f

27: d

28: b

10: f

29: d

30: b

31: e

32: d

33: b

34: d

35: b

36: e

37: c

38: b 39: b

40: e

41: b 42: b

43: e

44: c

exp4: a

Figure 13.3: BDD for a = b ∧ c = d ∧ e = f for variable order acebdf

has

(000000+010010+001001+100100+011011+101101+110110+111111).

These regular expressions when converted to minimized DFAs yields Fig-ures 13.2(a) and (b), where the size difference due to the variable orderingsis very apparent. The BDD for Figures 13.2(b) created using the BED toolappears in Figure 13.3. The commands used to create this BDD were:

bed> var a c e b d f % declares six variables

bed> let exp4 = (a=b) and (c=d) and (e=f) % defines the desired expression

bed> upall exp4 % builds the BDD

bed> view exp4 % displays the BDD

By comparing Figure 13.3 against Figure 13.2(b), one can see how, in general,BDDs eliminate redundant decodings.

BDDs are an efficient data structure for representing Boolean functions.In many applications, they are ‘robust,’ i.e., their sizes remain modest as the

Page 215: Computation Engineering Applied Automata Theory and Logic

13.2. BINARY DECISION DIAGRAMS 207

computation advances. For these applications, the task at hand is very oftenthe computation of all reachable states. This task cannot be accomplishedin practice by explicitly enumerating the states, as there could be well over2100 states. However, BDDs can often very easily represent such large statespaces by capitalizing on an implicit representation of states as described inSection 13.3. BDDs can deliver this ‘magic’ only if a “good” variable orderingis chosen. One also has to be aware of the following realities when it comesto using BDDs:

• The problem of determining an optimal variable ordering is NP-complete[?]; this means that the best known algorithms for this task today runin exponential worst-case time (see Chapter ?? for details).

• In many problems, as the computation proceeds and new BDDs arebuilt, variable orderings must be recomputed through dynamic variablere-ordering algorithms, which are never ideal and add to the overhead.

• For certain functions (e.g., the middle bits of the result of multiply-ing two N -bit numbers), the BDD is provably exponentially sized, nomatter which variable ordering is chosen.

Even so, BDDs find extensive application in representing as well as manip-ulating state transition systems realized in hardware and software. We nowproceed to discuss how BDDs can be used to represent state transition rela-tions and also how to perform reachability analysis.

Exercise 13.1 Similar to Figure 13.3, draw a BDD for all 16 Boolean func-tions over variables x and y. (Some of these functions are λ(x, y).true,λ(x, y).false, λ(x, y).x, λ(x, y).y, etc. Down this list, you have more “famil-iar” functions such as λ(x, y).nand(x, y), and so on. Express these functionswithout the “lambda” part in BED syntax, and generate the BDDs usingBED.)

Exercise 13.2 Consider the examples given in Figure 13.2. Construct simi-lar examples for the addition operation. More specifically, consider the binaryaddition of two unsigned two-bit numbers a1a0 and b1b0 resulting in answerc2c1c0. Generate a BDD for the carry output bit, c2. Choose a variable order-ing that minimizes the size of the resulting BDD and experimentally confirmusing BED.

Page 216: Computation Engineering Applied Automata Theory and Logic

208CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

Exercise 13.3 Repeat Exercise 13.2 to find out the variable ordering thatmaximizes the BDD size.

13.3 Basic operations on BDDs

BDDs are capable of efficiently representing transition relations of finite-state machines. In some cases, transition relations of finite-state machinesthat have of the order of 2100 states have been represented using BDDs. Thisis, of course, achieved by implicitly representing the state space; an explicitrepresentation (e.g., using pointer based data structures) of a state-space ofthis magnitude is practically impossible. Given a transition relation, onecan perform forward or backward reachability analysis. ‘Forward reachabilityanalysis’ is the term used to describe the act of computing reachable statesby computing the forward image of the current set of states (starting fromthe initial states). Backward reachability analysis computes the pre-image ofthe current set of states. One typically starts from the current set of statesviolating the desired property, and attempts to find a path back to the initialstate. If such a path exists, it indicates the possibility of a computation thatviolates the desired property.

Each step in reachability analysis takes the current set of states rep-resented by a BDD and computes the next set of states, also representedby a BDD. It essentially performs a breadth-first traversal, generating eachbreadth-first frontier in one step from the currently reached set of states.The commonly used formulation of traversal is in terms of computing theleast fixed-point of the state transition relation. When the least fixed-pointis reached, one can query it to determine the overall properties of the system.One can also check whether desired system invariants hold in an incrementalfashion (without waiting for the fixed-point to be attained) by testing theinvariant after each new breadth-first frontier has been generated.

We will now take up these three topics in turn, first illustrating how weare going to represent state transition systems.

13.3.1 Representing state transition systems

We represent the entire state of the system using a single state vector, andcapture the transition system by specifying a relation between the current andnext states (state vectors), and also specifying a unary relation (predicate)capturing the initial states. If inputs and outputs are to be modeled, they

Page 217: Computation Engineering Applied Automata Theory and Logic

13.3. BASIC OPERATIONS ON BDDS 209

are made part of the state vector. Depending on the problem being modeled,we may not care to highlight which parts of the state vector are inputs andwhich are outputs. In some cases, the entire state of the system will becaptured by the states of inputs and outputs. Here is an extremely simpleexample of a state transition system modeled along these lines. We presentthe state transition system using the notation curstate ---> nextstate.

Initial state = 0

0 ---> 1

1 ---> 1

1 ---> 0

Initially the state is 0. Whenever the state is 0, it can become 1. When itis 1, it can either stay 1 or become 0. These requirements can be capturedusing a single Boolean variable b to represent the current state, and anotherBoolean variable b

′to represent the next state5, and specifying the initial

state predicate and state transition relation as follows:

• Initial state predicate: The initial state predicate for our example isλb.¬b, since the initial state is 0. Often, instead of using the lambdasyntax, initial state predicates are introduced by explicitly introducinga named initial state predicate I and defining it by an equation suchas I(b) = ¬b. For brevity6, we shall often say “input state representedby ¬b.”

• State transition relation: The state transition relation for our exampleis λ(b, b

′).¬bb′ + bb

′+ b¬b′ , where each product term represents one of

the transitions. The values of b and b′for which this relation is satisfied

represent the present and next states in our example. In other words,

– a move where b is false now and true in the next state is representedby ¬bb′ ; this corresponds to the move 0 ---> 1.

– a move where b is true in the present and next states is representedby bb

′; this corresponds to the move 1 ---> 1.

– finally, a move where b is true in the present state and false in thenext state is represented by b¬b′ ; this corresponds to the move 1

---> 0.

5The ‘primed variable’ notation was first used by Alan Turing in one of the very firstprogram proofs published by him in [?].

6Syntactic sugar causes cancer of the semi-colon – Perlis

Page 218: Computation Engineering Applied Automata Theory and Logic

210CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

This expression can be simplified to λ(b, b′).(b+b

′). The above relation

can also written in terms of a transition relation T defined as T (b, b′) =

b + b′. We shall hereafter say “transition relation b + b

′.” Notice that

this transition relation is false for b = 0 and b′= 0, meaning there is

no move from state 0 to itself (all other moves are present).

An Aside: Modeling inputs and outputs

If it is of interest to represent the “inputs” and “outputs” of the state tran-sition system, it can be easily arranged by making them part of the statevector. For instance, under the transport delay model, the behavior of a sin-gle inverter can be modeled using a pair of bits representing the input andoutput. In the transport delay model, every input change, however short, isfaithfully copied to the output, but after a small delay. Using state transi-tion systems, we can model the essence of this behavior (but not its absolutetimings) as shown below:

Initial state = 01 % ‘‘input’’, ‘‘output’’.

01 ---> 01 % initially, the input of the inverter is 0, and the

% output is 1; the input is raised; the inverter is

01 ---> 11 % in a transient state; the output transitions and

11 ---> 10 % the inverter attains steady state. The inverter

10 ---> 10 % remains in its steady state another input

10 ---> 00 % transition that puts the inverter back in a

00 ---> 01 % transient state steady state at last!

To sum up, in the transport delay model, the duration of an input “pulse” (a0->1->0 or a 1->0->1 change) does not matter; it gets reflected faithfully onthe output. There is another delay model called the inertial delay model inwhich short pulses may not make it to the output. We can represent inertialdelays using state transitions by employing non-determinism

Initial state = 01 % ‘‘input’’, ‘‘output’’

01 ---> 01 % quiescent

01 ---> 11 % input rises

11 ---> 10 % output changes; an autonomous change; pulse lasted long enough

11 ---> 01 % input changes back to 0 before output could change; pulse killed

10 ---> 10 % quiescent

10 ---> 00 % transient

00 ---> 01 % output responds

00 ---> 10 % input changes back too quickly; pulse killed

Exercise 13.4 Represent the behavior of a Nand gate under the inertial de-lay model as a state transition system.

Page 219: Computation Engineering Applied Automata Theory and Logic

13.3. BASIC OPERATIONS ON BDDS 211

Exercise 13.5 Draw a BDD for the transition relation of a two-bit binarycounter with output bits a1a0 for initial state 00, counting in the usual 0, 1, 2, 3order. Repeat for a two-bit Gray-code counter that counts 00, 01, 11, 10, andback to 00.

13.3.2 Forward reachability

Suppose we want to determine the set of reachable states in the previoussimple transition system, starting from initial state ¬b, i.e., the b = 0 state.We can proceed as follows:

• Compute the set of states in the initial set of states.

• Compute the set of states rechable from the initial states in n steps,for n = 1, 2, . . ..

In other words, we can introduce a predicate P such that a state x is in Piff it is reachable from the initial state I through a finite number of steps, asdictated by the transition relation T . The above recursive recipe is encodedas

P (s) = (I(s) ∨ ∃x.(P (x) ∧ T (x, s))).Rewriting, we have

P = λs.(I(s) ∨ ∃x.(P (x) ∧ T (x, s)))).

Rewriting again, we have

P = (λG.(λs.(I(s) ∨ ∃x.(G(x) ∧ T (x, s)))))P.

In other words, P is a fixed-point of

λG.(λs.(I(s) ∨ ∃x.(G(x) ∧ T (x, s)))).

Let us call this Lambda expression H:

H = λG.(λs.(I(s) ∨ ∃x.(G(x) ∧ T (x, s)))).

In general, H can have many fixed-points. Of these, the least fixed-pointrepresents exactly the reachable set of states.

It is instructive to see whether other fixed-points (solutions to P in thiscase) might exist. Suppose we modify the transition relation we are dealingwith to the following.

Page 220: Computation Engineering Applied Automata Theory and Logic

212CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

Initial state = 0

0 ---> 0

1 ---> 1

In other words, we include one unreachable strongly connected component(SCC) at state 1. In this case, if we begin iterating with P0 = λx.true,we will deliver a solution for P that includes this unreachable SCC. Now,imagine having more such unreachable SCCs as Exercise 13.6 requests.

Exercise 13.6 Construct a general-enough example such that H has threedistinct fixed-points: (i) the least fixed-point representing exactly the set ofstates reachable from the starting state, (ii) the greatest fixed-point whichrepresents the largest set of states that are “stable” under the application ofthe state transition relation, and (iii) an “intermediate” fixed-point whichincludes some (but not all) of the unreachable states that are stable under theapplication of the transition relation.

Fixed-point iteration to compute the least fixed-point

As shown in Section 7.1, the least fixed-point can be obtained by “bottomrefinement” using the functional obtained from the recursive definition. Inthe same manner, we will determine P by computing its approximants that,in the limit, become P . Let us denote the approximants P0, P1, P2, . . .. Wehave P0 = λx.false, the “everywhere false” predicate. The next approxima-tion to P is obtained by feeding P0 to the “bottom refiner” (as illustrated inSection 7.1):

P1 = λG.(λs.(I(s) ∨ ∃x.(G(x) ∧ T (x, s))))P0

which becomes λs.I(s). This approximant says that P is a predicate trueof s whenever I(s). While this is certainly not true (P must represent thereachable state set and not the initial state alone), it is certainly a betteranswer than what P0 denotes, which is that there are no states in the reach-able state set! We now illustrate all the steps of this computation, takingour example transition system for illustration. We use the abbreviation ofnot showing the lambda abstracted variables in each step.

• I = λb.¬b

• T = λ(b, b′). (b+ b

′)

Page 221: Computation Engineering Applied Automata Theory and Logic

13.3. BASIC OPERATIONS ON BDDS 213

• P0 = λs.false

• P1 = λG.(λs.(I(s) ∨ ∃x.(G(x) ∧ T (x, s))))P0.

This simplifies to P1 = I, as we shall argue in full detail momentarily.The meaning of this conclusion is interesting to point out, however; itis that we’ve “just reached” the initial state, starting from P0 whichhas the status “we’ve reached nowhere yet!” (as it is an empty set).

• Let’s see the derivation of P1 in detail. Expanding T and P0, we haveP1 = λG.(λs.(I(s) ∨ ∃x.(G(x) ∧ (x+ s) ))) (λx.false).

• The above simplifies to ¬b. By this token, we are expecting P2 to beall states that are zero or one step away from the start state. Let’s seewhether we obtain this result.

• P2 = λG.(λs.(I(s) ∨ ∃x.(G(x) ∧ T (x, s))))P1.

= λs.(¬s ∨ ∃x.(¬x ∧ (x+ s))).

= λs.1.

• This shows that the set of states reached by all the breadth-first fron-tiers (combined) that are zero and one step away from the start stateincludes every state. Another iteration would not change things; a leastfixed-point has been reached.

As can be seen, this fixed-point “grows” to the desired definition of thereachable state-space. In the BED tool, this is computed by the reach

command.

Why Stabilization at a Fixed-point is Guaranteed

In every finite-state system modeled using a finite-state Boolean transitionsystem, a least fixed-point is always reached in a finite number of steps. Letus try to argue this fact first using only a simple observation. The obser-vation is that all the Boolean expressions generated during the course offixed-point computation are over the same set of variables. Since we haveonly 22

N

Boolean functions over N Boolean variables, eventually two of theapproximants in the fixed-point computation process will have to be the

Page 222: Computation Engineering Applied Automata Theory and Logic

214CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

same. However, this argument does not preclude the possibility of “eva-sive” or “oscillatory” fixed-points where there is a sequence of fixed-pointsPi, Pi+1, . . . , Pj such that i 6= j and Pj = Pi. In this case, it is possible to cy-cle through Pi, . . . , Pj without ever stabilizing on a fixed-point. Fortunately,each approximant Pi+1 is “more defined” than the previous approximant Pi,and since there is only a finite number of monotonically ascending approx-imants, an oscillatory behavior is precluded. See Andersson’s paper [?] foradditional examples of forward reachability.

The following BED commands compute the reachable set of states usingforward reachability in our example. In BED, the primed variables must bedeclared immediately after the unprimed counterparts. The reach commanddoes the fixed-point iteration.

var b bp % Declare b and b’

let I = !b % Declare init state

let t1 = !b and bp % 0 --> 1

upall t1 % Build BDD for it

view t1 % View it

let t2 = b and bp % 1 --> 1

let t3 = b and !bp % 1 --> 0

let T = t1 or t2 or t3 % All three edges

upall T % Build and view

view T % the BDD

% ’reach’ computes

let RS = reach(I,T) % the least-fixed pt

upall RS % Build BDD for

view RS % reachable set

Exercise 13.7 Consider a three-bit shift-register based counter with the in-dicated next-state relation for its three bits:

next(a) = b ; next(b) = c ; next(c) = not(and(a,b,c))

c b a

1. Represent the next-state relation of the Johnson counter7using a singleROBDD. Choose a variable ordering that minimizes the size of your

Page 223: Computation Engineering Applied Automata Theory and Logic

13.3. BASIC OPERATIONS ON BDDS 215

ROBDD and justify your choice.

2. Compute the set of all reachable states using forward reachability anal-ysis, using the reach command, starting at state 000.

3. Justify the correctness of the answer you obtain. The answer you ob-tain must be a Boolean formula over a, b, c. Show that this formula issatisfied exactly for those states reachable by the system.

13.3.3 Playing Tic-Tac-Toe using BDDs

This section provides a comprehensive example illustrating how BDDs canbe used to model state transition systems of various kinds. Once so mod-eled, we can explore all moves of the transition system in parallel, usingBoolean/symbolic methods. We assume the reader knows the game of Tic-Tac-Toe. After illustrating reachability analysis on this simple game, we shallproceed to illustrate how problems such as “show all the draw configurations”can be easily modeled and answered through forward reachability analysis.

We model two players, A and B. The state of the game board is modeledusing a pair of variables ai,j, bi,j (we omit the pairing symbols 〈〉 for brevity)for each square i, j where i ∈ 3 and j ∈ 3. We assume that player A markssquare i, j with an o, by setting ai,j and resetting bi,j, while player B markssquare i, j with an x, by resetting ai,j and setting bi,j . We use variable turn tomodel whose turn it is to play (with turn = 0 meaning A’s turn). The statetransition relation for each square will be specified using the four variablesai,j, ai,j , bi,j , and bi,jp. We model the conditions for a row or column remainingthe same, using predicates samerowi and samecoli. We define nine possiblemoves for A and the same for B. For example, M00 model’s A’s move intocell 0, 0: 8

let M00 = !a00 and !b00 and a00p and !b00p and !turn and turnp

and samerow1 and samerow2 and samecol1 and samecol2;

Similarly, we employ N00 to model B’s move into cell 0, 0, and so on for theremaining cells.

Almost the entire script of an encoding in the BED tool now follows. Thereader may enter this script into the BED tool and experimentally understandBDDs. The transition relation is now defined as a disjunction of theMi,j and

7Named after Emeritus Prof. Bob Johnson, University of Utah.

Page 224: Computation Engineering Applied Automata Theory and Logic

216CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

Ni,j moves. We now capture the constraint atmostone that says that at-mostone player can play into any square. We then enumerate the game-board forall possible wins and draws. We compute the reachable set of states and firstof all query it to make sure that only the correct states are generated. Thenwe compute the set of states defining draw configurations. The relevant BEDdefinitions are as follows.

var turn turnp

a00 a00p b00 b00p

a01 a01p b01 b01p

a02 a02p b02 b02p

a10 a10p b10 b10p

a11 a11p b11 b11p

a12 a12p b12 b12p

a20 a20p b20 b20p

a21 a21p b21 b21p

a22 a22p b22 b22p ;

/* Initial state of the game board and turn. Nobody has moved, and

it is A’s turn.

*/

let init=!a00 and !b00 and !a01 and !b01 and

!a02 and !b02 and !a10 and !b10 and

!a11 and !b11 and !a12 and !b12 and

!a20 and !b20 and !a21 and !b21 and

!a22 and !b22 and !turn ;

/* This defn says that row0 does not change in going to the next state ... */

let samerow0 = (a00=a00p) and (b00=b00p) and (a01=a01p) and

(b01=b01p) and (a02=a02p) and (b02=b02p);

/* Likewise, define samerow1, samerow2, samecol0, samecol1, samecol2 */

/* Define A making an entry into cell 00 */

let M00 = !a00 and !b00 and a00p and !b00p and !turn and turnp

and samerow1 and samerow2 and samecol1 and samecol2;

/* Likewise define M01, M02, M10, M11, M12, M20, M21, M22, for player a

and N00, N01, N02, N10, N11, N12, N20, N21, N22, for player b

*/

/* The Trel of the system now is */

let T = M00 or M01 or M02 or

M10 or M11 or M12 or

M20 or M21 or M22 or

N00 or N01 or N02 or

N10 or N11 or N12 or

Page 225: Computation Engineering Applied Automata Theory and Logic

13.3. BASIC OPERATIONS ON BDDS 217

N20 or N21 or N22 ;

/* Define that each cell has one a or one b move, not both */

let atmostone =

!(a00 and b00) and !(a01 and b01) and !(a02 and b02)

and

!(a10 and b10) and !(a11 and b11) and !(a12 and b12)

and

!(a20 and b20) and !(a21 and b21) and !(a22 and b22) ;

/* One possible win for a */

let wina1 = atmostone and

(a00 and !b00) and (a01 and !b01) and (a02 and !b02) ;

/* Likewise define wina2, wina3, wina4, wina5, wina6, wina7, wina8

winb1, winb2, winb3, winb4, winb5, winb6, winb7, winb8 */

/* A game in which the players have filled every square */

let allmoved =

(a00 = !b00) and (a01 = !b01) and (a02 = !b02)

and

(a10 = !b10) and (a11 = !b11) and (a12 = !b12)

and

(a20 = !b20) and (a21 = !b21) and (a22 = !b22) ;

/* When a draw occurs */

let draw = allmoved and

!wina1 and !wina2 and !wina3 and !wina4 and

!wina5 and !wina6 and !wina7 and !wina8 and

!winb1 and !winb2 and !winb3 and !winb4 and

!winb5 and !winb6 and !winb7 and !winb8 ;

/* Build all BDDs so far via a series of ’upall’ commands... */

/* Compute the reachable set */

let RS = reach (init, T);

upall RS;

/* Find all draw possibilities in one fell swoop */

let result = RS and draw;

upall result;

/* Is it satisfiable? (I.e. is a draw possible?) */

anysat result;

/* Apply additional debugging queries */

Exercise 13.8 Encode the Man-Wolf-Goat-Cabbage problem using BDDs.

Page 226: Computation Engineering Applied Automata Theory and Logic

218CHAPTER 13. THE AUTOMATON/LOGIC CONNECTION, SYMBOLIC TECHNIQUES

In this problem, a man has to carry a wolf, goat, and cabbage aross a river.The man has to navigate the boat in each direction. He may carry no morethan one animal/object on the boat (besides him) at a time. He must notleave the wolf and goat unattended on either bank, nor must he leave the goatand cabbage unattended on either bank. The number of moves taken is to beminimal. Use appropriate Boolean variables to model the problem.

Page 227: Computation Engineering Applied Automata Theory and Logic

Chapter 14

The ‘Pumping’ Lemma

Theorem 12.2 reiterates why regular languages are so called – their stringsare “regular” in length. This fact can be taken advantage of in reasoningabout languages. The most common usage comes in the form of incompletePumping Lemmas or one-way Pumping Lemmas that help prove that certainlanguages are not regular. There are also complete Pumping Lemmas that canhelp prove that certain languages are regular. We now discuss one incompletePumping lemma in depth and show many usages of the same. This will befollowed by a brief discussion of one complete Pumping lemma.

14.1 Incomplete Pumping Lemmas for Reg-

ular Languages

Our Incomplete Pumping Lemma is as follows. If a language L is regular,then there exists a number n (typically equal in magnitude to the number ofstates of the minimal DFA D of L) such that for any string w ∈ L exceedingn in length, w will have a loop somewhere in it. This is because in making nmoves, this DFA will have to visit n+ 1 states1which cannot all be distinct,causing the DFA to revisit a state. Thus, w has a portion called x beforethe loop begins, a portion y that forms the loop, and a portion z that formsthe tail of w leading up to one of the accepting states s ∈ F of D. It is thenclear that the portion y can be repeated any number k ≥ 0 of times in going

1If the n moves are compared to n webs on the foot of a duck, then the duck must haven+ 1 digits!

219

Page 228: Computation Engineering Applied Automata Theory and Logic

220 CHAPTER 14. THE ‘PUMPING’ LEMMA

to s, thus ensuring that strings of the form xykz are in L. In symbols,

Regular(L)⇒∃n ∈ N :∀w ∈ L : |w| ≥ n

⇒∃x, y, z ∈ Σ∗ :∧ w = xyz∧ y 6= ε∧ |xy| ≤ n∧ ∀k ≥ 0 : xykz ∈ L

To use the Incomplete Pumping Lemma in proving a language Lsuspect non-regular, we proceed as follows. First, claim Regular(Lsuspect), and using theIncomplete Pumping Lemma, obtain as a consequence C, as in

∃n ∈ N :∀w ∈ Lsuspect : |w| ≥ n

⇒∃x, y, z ∈ Σ∗ :∧ w = xyz∧ y 6= ε∧ |xy| ≤ n∧ ∀k ≥ 0 : xykz ∈ Lsuspect

Now, we try to show that formula C false, i.e., ¬C is true. If we succeedin doing so, by the contrapositive law, we can conclude ¬Regular(Lsuspect),i.e., that Lsuspect is indeed non-regular. One should, however, bear in mindthe following important fact:

If, instead of showing that C is false, one ends up showing C, i.e,that C is a tautology, then we cannot draw any conclusion aboutLsuspect. It could either be regular or non-regular! (Note thatwith respect to a given language, C is either false or true—it is afully quantified formula, or a sentence.)

Let us now proceed with the assumption that we have chosen a non-regular language and in addition it is possible to show ¬C. What doesshowing ¬C involve? We first express ¬C by negating the above formula, toobtain:

Page 229: Computation Engineering Applied Automata Theory and Logic

14.1. INCOMPLETE PUMPING LEMMAS FOR REGULAR LANGUAGES221

∀n ∈ N :∃w ∈ Lsuspect : |w| ≥ n

∧∀x, y, z ∈ Σ∗ :∨ w 6= xyz∨ y = ε∨ |xy| > n∨ ∃k ≥ 0 : xykz /∈ Lsuspect

The quantification ∀x, y, z allows us to satisfy the disjunction following it inmany trivial ways:

• pick x, y, z such that w 6= xyz,

• pick y = ε, or

• pick x, y such that |xy| > n.

Luckily, in order to show ∀x, y, z, one must also consider the remaining cases,which are as follows:

• split w in all possible ways x, y, z such that w = xyz and y 6= ε,

• pick x, y such that |xy| ≤ n (look for a loop within the first n moves inw), and

• find a k ≥ 0 such that xykz /∈ Lsuspect.

We shall now illustrate these steps as well as related methods on severalexamples.

Exercise 14.1 Example: Consider L = 0m10m1 | m ≥ 0. To show L isnot regular,

1. Choose w = 0n10n1

2. Choose y 6= ε

3. Choose x, y, such that |xy| ≤ n – ah ha! y must contain a 0

4. Does there exist a k ≥ 0 such that xykz /∈ L?

5. Sure! For k = 0. Hence, L is not regular.

Page 230: Computation Engineering Applied Automata Theory and Logic

222 CHAPTER 14. THE ‘PUMPING’ LEMMA

Exercise 14.2 Example: Consider L = 10m10m | m ≥ 0. To show L isnot regular,

1. Choose w = 10n10n

2. Choose y 6= ε

3. Choose x, y, such that |xy| ≤ m

4. We have three choices for y:

(a) y = 1

(b) y = 10l for l < n

(c) y = 0l for 0 < l < n

5. Does there exist a k ≥ 0 such that xykz /∈ L for all these choices?

6. Sure!

(a) For y = 1, choose k = 0 (other choices work too)

(b) y = 10l for l < n, choose k = 0 (other choices work too)

(c) y = 0l for 0 < l < n also, choose k = 0 (other choices work too)

(d) In all these cases, the assertion ∃ k ≥ 0 such that xykz /∈ L issatisfied.

(e) Thus we get full contradiction, and hence L is not regular.

Illustration 222.1 Quantifier alternation The Pumping Lemma resem-bles the following example English assertion: “A Zoo Z is interesting if forallGiraffes in Z whose right-rear leg is more than n feet in length, there existsa reticulation patch on its skin of exactly

√n feet circumference, such that

within this reticulation patch, forall hair h, the color of h is brown.”

To show that Z is uninteresting, we have to find one Giraffe of height ≥ nsuch that for all patches, either the patch is not sqrtn feet in circumference,or (it is, and) there exists a non-brown hair in it.

Page 231: Computation Engineering Applied Automata Theory and Logic

14.2. AN ADVERSARIAL ARGUMENT 223

14.1.1 A Stronger Incomplete Pumping Lemma

There is a stronger version of the Pumping Lemma which allows strings“in the middle” to be pumped. We now state this Pumping Lemma semi-formally, and illustrate its power on a simple example.

Regular(L)⇒∃n ∈ N :∀w ∈ L : |w| ≥ n

⇒∀x, y, z ∈ Σ∗ :∧ w = xyz∧ |y| ≥ n∃u, v, w ∈ Σ∗ :∧ v 6= ε∧ ∀k ≥ 0 : xuvkwz ∈ L

As can be seen, the pumping can occur “in the middle.”

Illustration 223.1 Consider the language Lif defined on Page 14.3. Byapplying the ordinary Pumping Lemma, we cannot derive a contradictionstarting from string abncn because the possibilities include x = ε, y = a, andz = bncn, and by pumping y, we do not go outside the language. With thestronger Pumping Lemma, though, we can pick x, y, and z suitably, with|y| ≥ n. Thus, letting x to be a, we can situate u, v, w in the bncn region,obtaining a violation in all cases.

14.2 An adversarial argument

The Pumping Lemma provides a concrete setting to understand adversarialarguments. Consider proving, directly using the Pumping Lemma, that thelanguage

L = 0i1j | i 6= jis not regular. Here is how the proof goes as an adversarial argument. Sup-pose an adversary (Y) claims that this is a regular language. You (U) wantto prove it is not. Here is how you can argue and win:

1. U: “OK if L is regular, you have a DFA D with you right?”

Page 232: Computation Engineering Applied Automata Theory and Logic

224 CHAPTER 14. THE ‘PUMPING’ LEMMA

2. Y: “Yes.”

3. U: “How many states in it?”

4. Y: “n”

5. U: “OK, describe to me the sequence of states that D goes thru uponseeing the first n symbols of the string 0n1(n+n!).” Here, n is chosento be the number of states in D. Since n 6= (n + n!), this string mustsurely be in L. (The choice of (n + n!) as the exponent of 1s is ratherpurposeful, as we shall see momentarily.)

6. Y (first lies and utters): “It visits s0, s1, . . ., all of which are differentfrom one another.”

7. U: (Red faced): “Lie! If there are n states in your DFA, then seeingn symbols, the DFA must have traversed a loop, and hence you musthave listed two states that are the same. Don’t you know that thisfollows from the pigeon-hole principle?”

8. Y: (Blue faced): “OK, you are right, it is “s0, s1, . . . , si, si+1, si+2, . . . , si, sj . . ..”

9. U: “Ah ha, I’m gonna call the pieces of the above march as follows”

(a) the piece that leads upto the loop, x = s0, s1, . . . , si;

(b) the piece that traverses the loop, y = si, si+1, si+2, . . . , si; and

(c) the piece that exits the loop and visits the final state, z = si, sj . . ..

“You may pick any such x, y, z and I’m going to confound you.”

10. Y: “How?”

11. U: “Watch me!” (Private thoughts of U now follow...)

(a) Since I have no idea what |y| is, I must ensure that by pumpingy, no matter what its length, I should be able to create a stringof 0s equal in length to (n+ n!).

(b) So, by pumping, if I can create an overall string 0(n+n!)1(n+n!), Iwould have created the desired contradiction.

Page 233: Computation Engineering Applied Automata Theory and Logic

14.3. CLOSURE PROPERTIES AMELIORATE PUMPING 225

(c) The initial distribution of 0s along the path xyz is as follows:

i. x has |x| 0s,ii. y has |y| 0s, andiii. z has (n− |y| − |x|) 0s.

(d) Thus, by pumping y k-times, for integral k, we must be able toattain n+ n! = |x|+ k × |y|+ (n− |y| − |x|).

(e) Simplifying, we should be able to satisfy n! = (k − 1)|y|. Since|y| ≤ n, such a k exists!

12. U: “See the above argument. I can now pump up the y of your stringk times where k = n!/|y| + 1. Then you get a string 0n+n!1n+n! thatis not in L. This path also exists in your DFA. So your DFA cannot bedesigned exactly for L— it accepts illegal strings also. Admit defeat!”

13. Y: Tries for an hour furiously picking all possible x, y, z and goes backto step 8. For each such choice, U defeats Y in the same fashion. FinallyY admits defeat.

14. U: “Thank you. Next victim please.”

14.3 Closure properties ameliorate pumping

The use of closure properties can simplify the application of the PumpingLemma. However, extreme caution is to be exercised to avoid unsound argu-ments. We now provide a few illustrations and exercises. First, let us reworkProblem 14.2 as follows:

1. The reverse of L = 10m10m | m ≥ 0 is L′= 0m10m1 | m ≥ 0

2. That was proved to be not regular in Problem 1

3. Since reverse preservers regularity, the original language isn’t regular.

Exercise 14.3 Consider the example language of Section 14.2 again:

L = 0i1j | i 6= j

Using closure properties, show that this set is not regular.

Page 234: Computation Engineering Applied Automata Theory and Logic

226 CHAPTER 14. THE ‘PUMPING’ LEMMA

Exercise 14.4 L0n1n = 0n1n | n ≥ 0 is easily shown to be non-regular.Now, show

Leq = x | x ∈ 0, 1∗ and #0(x) = #1xis not regular. (Hint: intersection with 0* 1*.)

Exercise 14.5 What’s wrong with this argument?

1. Suppose M ∩ L(0∗ 1∗) = N

2. Suppose we can show (thru Pumping Lemma) that M is not regular

3. Conclude that N is not regular.

4. This does not work!

5. Example:

(a) Leq is non-regular

(b) But Leq ∩ φ is regular!

The correct way to use closure properties is as follows:

1. Suppose M ∩ L(0∗ 1∗) = N

2. Suppose we can show (thru Pumping Lemma) that N is not regular

3. Then we can conclude that M is not regular.

Exercise 14.6 Among the assertions below, identify those that are true, andjustify them. For those that are false, provide a counter-example.

1. The union of two non-regular sets is always non-regular.

2. The intersection of two non-regular sets is always non-regular.

3. A regular set can have a non-regular subset.

4. A regular set can have a non-regular superset.

5. Every regular set has a regular subset.

6. The star of a non-regular set can never be regular.

Page 235: Computation Engineering Applied Automata Theory and Logic

14.3. CLOSURE PROPERTIES AMELIORATE PUMPING 227

7. The prefix-closure of a non-regular set can never be regular.

8. The reverse of a non-regular set can never be regular.

9. The union of a regular and a non-regular set can be regular.

10. The union of a regular and a non-regular set can be non-regular.

11. The concatenation of a regular and a non-regular set can sometimes beregular.

12. There is a finite non-regular set.

13. It is possible to apply a homomorphism to turn a non-regular set intoa regular set.

Here is another abuse of the Incomplete Pumping Lemma (from [?]). Con-sider the language

Lif = aibjck | i ≥ 0, j, k > 0, and if i = 1 then j = k.While this language is not regular, we can still show that the C that resultsfrom the Incomplete Pumping Lemma is a tautology (“can pump k withoutcausing any violations”).

Exercise 14.7 Show that using the initial Incomplete Pumping Lemma, wecan pump, i.e., prove the Pumping Lemma condition to be true. Now, applya closure property and use the incomplete Pumping Lemma to show that Lif

is non-regular.

Exercise 14.8 Show using the stronger Pumping Lemma that Lif is non-regular.

Exercise 14.9 Prove the following languages to be non-regular:

1. Lsq = 0i2 | i ≥ 02. L() = x | x ∈ (, )∗ and x is well − paranthesized3. The set of palindromes over 0, 1∗ is not regular.

The definition of well-parenthesized is as follows:

1. The number of ( and ) in x is the same

2. In any prefix of x, the number of ( is ≥ the number of )

Page 236: Computation Engineering Applied Automata Theory and Logic

228 CHAPTER 14. THE ‘PUMPING’ LEMMA

14.4 Complete Pumping Lemmas

There are many complete Pumping Lemmas of the form Regular(L) = ⇔conditions, i.e., a language is regular if and only if certain conditions hold.We present two popular versions, one due to Jaffe [?] and the other dueto Stanat and Weiss [?]. Possible uses of these complete pumping lemmasinclude showing that certain languages are regular.

14.4.1 Jaffe’s Complete Pumping Lemma

For a language L over a finite alphabet Σ, Jaffe’s Pumping Lemma is thefollowing:

Regular(L)⇔∃k ∈ N :∀y ∈ Σ∗ : |y| = k

⇒∃u, v, w ∈ Σ∗ :∧ y = uvw∧ v 6= ε∧ ∀z ∈ Σ∗ :

∀i ∈ N : (yz ∈ L ⇔ uviwz ∈ L)

Notice that for a “long string” y = uvw with a pumpable middle portionv, it is expressed that we can follow the original string y with an arbitrary zand stay within L if and only if we can pump the middle and still follow itwith that same z and stay within L. In [?], a proof of this Pumping Lemmais provided.

14.4.2 Stanat and Weiss’s Complete Pumping Lemma

For a language L over a finite alphabet Σ, Stanat and Weiss’s PumpingLemma is the following:

Regular(L)⇔∃p ∈ N :∀x ∈ Σ∗ : |x| ≥ p

⇒∃u, v, w ∈ Σ∗ :

Page 237: Computation Engineering Applied Automata Theory and Logic

14.4. COMPLETE PUMPING LEMMAS 229

∧ x = uvw∧ v 6= ε∧ ∀r, t ∈ Σ∗ :

∀i ∈ N : (rut ∈ L ⇔ ruvit ∈ L)

Notice that this Pumping Lemma does not require the pumpable string tobe part of the prefix: an arbitary string r can lead-off, and an arbitrary tailt can follow. In [?], a proof of this Pumping Lemma using Jaffe’s PumpingLemma is provided.

Page 238: Computation Engineering Applied Automata Theory and Logic

230 CHAPTER 14. THE ‘PUMPING’ LEMMA

Page 239: Computation Engineering Applied Automata Theory and Logic

Chapter 15

Context-free Languages

A context-free language (CFL) is a language accepted by a push-down au-tomaton (PDA). Alternatively, a context-free language is one that has acontext-free grammar (CFG) describing it. This chapter is mainly aboutcontext-free grammars, although a brief introduction to push-down automatais provided. The next chapter will treat push-down automata in greater de-tail, and also describe algorithms to convert PDAs to CFGs and vice versa.

A CFG is a structure (N,Σ, P, S) where N is a set of symbols known asnon-terminals, Σ is a set of symbols known as terminals, S ∈ N is calledthe start symbol, and P is a finite set of production rules. Each productionrule is of the form

L→ R1 R2 . . . Rn

where L ∈ N is a non-terminal and each Ri belongs to N ∪Σε. We will nowpresent several context-free grammars through examples, and then proceedto examine their properties as well as classification schemes.

Consider the CFG G1 = (S, 0, P, S) where P is the set of rules shownbelow:

S -> 0

A CFG is a machinery to produce strings according to production rules. Westart with the start symbol, find a rule whose left-hand side matches the startsymbol, and derive the right-hand side. We then repeat the process if theright-hand side contains a non-terminal. Using grammar G1, we can produceonly one string starting from S, namely 0, and so the derivation stops. Nowconsider a grammar G2 obtained from G1 by adding one extra productionrule:

231

Page 240: Computation Engineering Applied Automata Theory and Logic

232 CHAPTER 15. CONTEXT-FREE LANGUAGES

S -> 0

S -> 1 S

Using G2, an infinite number of strings can be derived as follows:

1. Start with S, calling it a sentential form

2. Take the current sentential form and for one of the non-terminals Npresent in it, find a production rule of the form N → R1 . . . Rm, andreplace N with R1 . . . Rm. In our example, S -> 0 matches S, resultingin sentential form 0. Since there are no non-terminals left, this senten-tial form is called a sentence. Each such sentence is a member of thelanguage of the CFG - in symbols, 0 ∈ L(G2). The step of going fromone sentential form to the next is called a derivation step. A sequenceof such steps is a derivation sequence.

3. Another derivation sequence using G2 is

S ⇒ 1S ⇒ 11S ⇒ 110.

To sum up, given a CFG G with start symbol S, S is a sentential form.If S1S2 . . . Si . . . Sm is a sentential form and there is a rule in the productionset of the form Si → R1 R2 . . . Rn, then S1S2 . . . R1 R2 . . . Rn . . . Sm is asentential form. We write

S ⇒ . . .⇒ S1S2 . . . Si . . . Sm ⇒ S1S2 . . . R1 R2 . . . Rn . . . Sm ⇒ . . . .

As usual, we use ⇒∗ to denote a multi-step derivation.Given a CFG G and one of the sentences in its language, w, a parse tree

for w with respect to G is a tree with frontier w, and each interior nodecorresponding to one derivation step. The parse-tree for string 110 withrespect to CFG G2 appears in Figure 15.1(a).

15.1 The language of a CFG

The language of a CFG G, L(G), is the set of all sentences that can bederived starting from S. In symbols, for a CFG G,

L(G) = w | S ⇒∗ w ∧ w ∈ Σ∗.

Page 241: Computation Engineering Applied Automata Theory and Logic

15.1. THE LANGUAGE OF A CFG 233

S

1 S

1 S

0

(a)

S

S S

1 S S

1 0

(b)

S

S S

S S 0

1 1

(c)

E

E + T

T T * F

F F 4

5 6

(d)

Figure 15.1: (a) The parse-tree for string 110 with respect to CFG G2; (b)and (c) are parse-trees for 110 with respect to G4; (d) is a parse-tree for4 + 5 ∗ 6 with respect to G8.

According to this definition, for a CFG G3, with the only production rulebeing S → S, L(G3) = φ. The same is true of a CFG all of whose productionscontain a non-terminal, since, then, we can never get rid of all non-terminalsfrom any sentential form.

A derivation sequence in which the leftmost non-terminal is selectedfor replacement in each derivation step is known as a leftmost derivation.Specific derivation sequences such as the leftmost and rightmost derivationsequences are important in compiler construction. We will refer to leftmostand rightmost derivation sequences as a means to pin down the exact deriva-tion sequence of interest in a specific discussion. This, in turn, decides theshape of the parse tree. To make this clear, consider a CFG G4 with threeproductions

• S → SS | 1 | 0

The above notation is a compact way of writing three distinct elementaryproductions S → SS, S → 1, and S → 0. A string 110 can now be derivedin two ways:

• S ⇒ SS ⇒ 1S ⇒ 1SS ⇒ 11S ⇒ 110 (Figure 15.1(b)), or

• S ⇒ SS ⇒ S0⇒ SS0⇒ S10⇒ 110 (Figure 15.1(c)).

Now consider grammar G5 with production rules

S → aSbS | bSaS | ε

Page 242: Computation Engineering Applied Automata Theory and Logic

234 CHAPTER 15. CONTEXT-FREE LANGUAGES

The terminals are a, b. What CFL does this CFG describe? It is easy tosee that in each replacement step, an S is replaced with either ε or a stringcontaining an a and a b; thus, all strings that can be generated from G5 havethe same number of as and bs. Can all strings that contain equal as and bsbe generated using G5? We visit this (much deeper) question in the nextsection. If you experimentally try to check this conjecture out, you will findthat no matter what string of as and bs you try, you can find a derivationfor it using G5 so long as the string has equal number of as and bs.

Exercise 15.1 Draw the parse-tree for string

a a a b b a b b b b b b a a b a b a a a

with respect to grammar G5, thus showing that this string can be derivedaccording to the grammar.

15.2 Consistency, completeness, and redun-

dancy

Consider the grammar G6

S → aSbS | bSaS | SS | ε.

As with grammar G5, all strings generated by G6 also have equal numberof as and bs. If we identify this property as consistency, then we findthat grammars G5 and G6 satisfy consistency. What about completeness?In other words, will all such strings be derived? Does it appear that the

production S → SS is essential to achieve completeness? It turns out that itis not - we can prove that G5 is complete, thus showing that the productionS → SS of G6 is redundant.

How do we, in general, prove grammars to be complete? The generalproblem is undecidable1, as we shall show in Chapter ??. However, forparticular grammars and particular completeness criteria, we can establishcompleteness, as we demonstrate below.

1The undecidability theorem that we shall show later is that for an arbitrary grammarG, it is not possible to establish whether L(G) is equal to Σ∗. Now, Σ∗ can be capturedusing a CFG. Thus, it is possible to ask of any given grammar G whether it is completewith respect to generating the language Σ∗. This is undecidable.

Page 243: Computation Engineering Applied Automata Theory and Logic

15.2. CONSISTENCY, COMPLETENESS, AND REDUNDANCY 235

a S b S

---

--- --- ---

--- --- ---

--- ---

|...........................---

a a a a b b a b b b

0 1 2 3 4 3 2 3 2 1 0

Figure 15.2: A string that does not cause zero-crossings

a S b S

---

--- --- ---

--- --- ---

|....................---.................................---

--- ---

--- --- --- ---

--- --- --- ---

---

a a a b b a b b b b b b a a b a b a a a

0 1 2 3 2 1 2 1 0 -1 -2 -3 -4 -3 -2 -3 -2 -3 -2 -1 0

Figure 15.3: A string that causes zero-crossings

Page 244: Computation Engineering Applied Automata Theory and Logic

236 CHAPTER 15. CONTEXT-FREE LANGUAGES

The proof of completeness typically proceeds by induction. We have todecide between arithmetic or complete induction; in this case, it turns outthat complete induction works better. Using complete induction, we writethe inductive hypothesis as follows:

Let all strings less than n in length and have an equal number ofas and bs be generatable from G5.

Consider now a string of length n+2 – the next longer string that has equalnumber of as and bs. We can draw a graph of #as (go up one step for eacha) versus #b (go down one step for each b), as in Figure 15.2 or Figure 15.3.This plot of the running difference between #a and #b is either fully abovethe x-axis, fully below, or has zero-crossings. In other words, it can havemany “hills” and “valleys.” Let us perform a case analysis:

1. The graph has no zero-crossings. There are further cases:

(a) it begins with an a and ends with a b, as in Figure 15.2.

(b) it begins with a b and ends with an a (this case is symmetric andhence will not be explicitly argued).

2. It has zero-crossings, as in Figure 15.3. Again, we consider only onecase, namely the one where the first zero-crossing from the left occursafter the curve has grown in the positive direction (i.e., after more asoccur initially than bs).

Let us consider Case 1a. By induction hypothesis, the shorter string in themiddle can be generated via S. Now, the entire string can be generated asshown in Figure 15.2 using production S -> aSbS, with a matching the first a,the first S matching ‘the shorter string in the middle,’ the b matching the lastb in the string, and the second S going to epsilon. Case ?? may be similarlyargued. If there is a zero-crossing, then we attack the induction as illustratedin Figure 15.3, where we split the string into the portion before its last zero-crossing and the portion after its last zero-crossing. These two portions can,by induction, be generated from G5, with the first portion generated as aSband the second portion generated as an S, as in Figure 15.3.

Illustration 236.1 Consider

Lambnck = ambnck | m,n, k ≥ 0 and ((m = n) or (n = k))

Page 245: Computation Engineering Applied Automata Theory and Logic

15.2. CONSISTENCY, COMPLETENESS, AND REDUNDANCY 237

Develop a context-free grammar for this language. Prove the grammar forconsistency and completeness.The grammar is given below. We achieve “equal number of a’s and b’s” bygrowing “inside out” as captured by the rule M -> a M b. We achieve zero ormore cs by the rule C -> c C or epsilon. Most CFGs get designed throughthe use of such “idioms.”

S -> M C | A N

M -> a M b | epsilon

N -> b N c | epsilon

C -> c C | epsilon

A -> a A | epsilon

Consistency: No string generated by S must violate the rules of being inlanguage Lambnck. Thus, if M generates matched a’s and b’s, and C generatesonly c’s, we are OK. The other case of A and N is very similar.

Notice that from the production of M, we can see that it generates matcheda’s and b’s in the epsilon case. Assume by induction hypothesis that in the Moccurring on the right-hand side of the rule M -> a M b follows consistency.Then the M of the left-hand side of this rule has an extra a in front and anextra b in the back. Hence, it preserves consistency.Completeness: We need to show that any string of the form anbnck orakbncn can be generated by this grammar. We will consider all strings of thekind anbnck and develop a proof for them. The proof for the case of akbncn

is quite similar and not presented.We resort to arithmetic induction for this problem. Assume, by induction

hypothesis that the particular 2n+k-long string anbnck was derived as follows:

• S ⇒ M C

• M ⇒∗ anbn through derivation-sequence S1, and

• C ⇒∗ ck through derivation sequence S2.

• S ⇒ M C ⇒∗ anbn C ⇒∗ anbn ck. Notice that in this derivation se-quence, the first ⇒∗ derivation sequence is what we call S1 and thesecond ⇒∗ derivation sequence is what we call S2.

Now, consider the next legal longer string. It can be either an+1bn+1ck oranbnck+1. Consider the goal of deriving an+1bn+1ck. This can be achieved asfollows:

Page 246: Computation Engineering Applied Automata Theory and Logic

238 CHAPTER 15. CONTEXT-FREE LANGUAGES

• S ⇒ M C ⇒ a M b C

• Now, invoking the S1 sequence, we get ⇒∗ a anbn b C

• Now, invoking the S2 sequence, we get ⇒∗ a anbn b ck, Thus, we canderive an+1bn+1ck.

Now, anbnck+1 can be derived as follows:

• S ⇒ M C ⇒ M c C

• Now, invoking the S1 derivation sequence, we get ⇒∗ anbn c C

• Finally, invoking the S2 derivation sequence, we get ⇒∗ anbn ck+1.

Thus, we can derive any string that is longer than anbnck, and so by inductionwe can derive all legal strings.

15.2.1 More consistency proofs

In case of grammars G5 and G6, writing a consistency proof was rather easy:we simply observed that all productions introduce equal as and bs in eachderivation step. Sometimes, such “obvious” proofs are not possible. Considerthe following grammar G9:

S -> ( W S | epsilon

W -> ( W W | )

It turns out that grammar G9 generates the language of the set of all well-paranthesized strings, even though three of the four productions appear tointroduce unbalanced parantheses. Let us first formally define what it meansto be well-paranthesized, and then show that G9 satisfies this criterion.

Well-paranthesized strings

A string x is well-paranthesized if:

1. The number of ( and ) in x is the same

2. In any prefix of x, the number of ( is ≥ the number of )

With this definition in place, we now show that G9 generates only consistentstrings. We provide a proof outline:

1. Conjecture about S: same number of ( and ). Let us establish this viainduction.

Page 247: Computation Engineering Applied Automata Theory and Logic

15.2. CONSISTENCY, COMPLETENESS, AND REDUNDANCY 239

(a) Epsilon satisfies this.

(b) How about ( W S ?

(c) OK, we need to “conquer” W.

i. Conjecture about W: it generates strings that have one more) than (.

ii. This is true for both arms of W.

iii. Hence the conjecture about W is true.

(d) Hence the conjecture about S is true.

(e) Need to verify one more step: In any prefix, number of ( morethan the number of )?

(f) Need conjecture about W: In any prefix of a string generated by W,number of ) at most one more than the number of (. Induct onthe W production and prove it. Then S is indeed what we want.

Exercise 15.2 Prove by induction that the following grammar generatesonly strings with an odd number of 1’s. Clearly argue the basis case(s), theinductive case(s), and what you prove regarding T and regarding S.

S → S T 0 | 0 1T → 1 1 T | ε

Exercise 15.3 Write the consistency proof pertaining to G9 in full detail.Then write a proof for the completeness of the above grammar (that it gen-erates all well-paranthesized strings).

15.2.2 Fixed-points again!

Fixed-points offer a “one-stop shopping” for concepts. In other words, Theidea is that if one concept would serve to explain multiple things, the fun-damental importance of this concept escalates. Specifically in our case, thelanguage generated a CFG was explained through the notion of a derivationsequence. Can this language also be obtained through fixed-points?

Consider re-casting the grammar G5 as a language equation (call thelanguage S5 to go with grammar G5):

L(S5) = a L(S5) b L(S5) ∪ b L(S5) a L(S5) ∪ ε.

Page 248: Computation Engineering Applied Automata Theory and Logic

240 CHAPTER 15. CONTEXT-FREE LANGUAGES

What solutions to this language equation exist? In other words, findlanguages to plug in place of L(S5) such that the right-hand side language(obtained through the union of three languages, the first two of which areobtained through the indicated language concatenations) becomes equal tothe left-hand side language. We can solve such language equations using thefixed-point theory introduced in Chapter 7. In particular, one can obtainthe least fixed-point by iterating from “bottom” which, in our context, is theempty language ϕ. The least fixed-point is also the language computed bytaking the derivation sequence perspective.

To illustrate this connection more vividly, consider a language equationobtained from CFG G6:

L(S6) = a L(S6) b L(S6) ∪ b L(S6) a L(S6) ∪ b L(S6) L(S6) ∪ ε.

It is easy to verify that this equation admits two solutions, one of which is thedesired language of equal a’s and b’s, and the other language is a completelyuninteresting one (see the exercise below.) The language equation for L(S5)does not have multiple solutions. It is interesting to note that the reasonwhy L(S6) admits multiple solutions is because of the redundant rule in it.To sum up:

• Language equations can have multiple fixed-points.

• The least fixed-point of a language equation obtained from a CFGcorresponds to the language generated by the derivation method also.

These observations help sharpen our intuitions about least fixed-points. Thenotion of least fixed-points is central to programming because programs alsoexecute through “derivation steps” similar to those employed by CFGs. Leastfixed-points start with “nothing” and help attain the least (most irredundant)solution. Other fixed-points capture arbitrary information that is usually notof interest. However, we must note that in Computational Tree Logic (CTL)invented by Emerson [?], and discussed in [?], both the least fixed-point andthe greatest fixed-point play significant and meaningful roles.

Exercise 15.4 Which other solution to the language equation of L(S6) ex-ists?

Page 249: Computation Engineering Applied Automata Theory and Logic

15.3. AMBIGUOUS GRAMMARS 241

15.3 Ambiguous Grammars

We already saw, in Figures 15.1(b) and 15.1(c), that a given string 1110 canhave two distinct parse-trees. In a compiler, the existence of two distinctparse trees means that the compiler is going to produce two different codes- or is ascribe two different meanings - to the same sentence. This is highlyundesirable in most settings. For example, if we have an arithmetic expres-sion 4 + 5 ∗ 6, we want it parsed as 4 + (5 ∗ 6) and not as (4 + 5) ∗ 6. If wewrite the expression grammar as G7

E → E + E | E ∗ E | numberthen both these parses (and their corresponding parse-trees) would be legal,in effect providing an ambiguous interpretation to expressions such as 4+5∗6.It is necessary to disambiguate the grammar, for example by rewriting thesimple expression grammar above to grammar G8, which is:

E → E + T | TT → T ∗ F | FF → number | (E)

In the rewritten grammar, for any expression containing + and ∗, the parsetrees will situate ∗ deeper in the tree (closer to the frontier) than +, thus ineffect forcing the evaluation of ∗ first, as illustrated in Figure 15.1(d).

15.3.1 If-then-else Ambiguity

An important practical example of ambiguity arises in the context of gram-mars pertaining to if statements, as illustrated below:

STMT -> if EXPR then STMT

| if EXPR then STMT else STMT

| OTHER

OTHER -> p

EXPR -> q

The reason for ambuity is that the else clause can match either of thethen clauses. Compiler-writers avoid the above if/then/else ambiguity bymodifying the above grammar in such a way that the else matches with theclosest unmatched then. One example of such a rewritten grammar is thefollowing:

Page 250: Computation Engineering Applied Automata Theory and Logic

242 CHAPTER 15. CONTEXT-FREE LANGUAGES

STMT -> MATCHED | UNMATCHED

MATCHED -> if EXPR then MATCHED else MATCHED | OTHER

UNMATCHED -> if EXPR then STMT

| if EXPR then MATCHED else UNMATCHED

OTHER -> p

EXPR -> q

This forces the else to go with the closest previous unmatched then.

15.3.2 Undecidability of Ambiguity

In general, it is impossible to algorithmically decide whether a given CFG isambiguous. In practice, this means that a user has to be very astute and spotambiguity. To make things worse, there are inherently ambiguous languages– languages for which every CFG is ambiguous. If the language that one isdealing with is inherently ambiguous, it is not possible to eliminate ambiguityin all cases, such as we did by rewriting grammar G7 to grammar G8. Noticethat the terminology is not inherently ambiguous grammar but inherentlyambiguous language - what we are saying is every grammar is ambiguous forcertain CFLs.

An example of an inherently ambiguous language is

0i1j2k | i, j, k ≥ 0 ∧ i = j ∨ j = k.

The intuition is that every grammar for this language must have productionsgeared towards matching 0’s against 1’s and 1’s against 2’s. In this case,given a string of the form 0k1k2k, either of these options can be exercised. Aformal proof may be found in advanced books in this area.

15.4 A Taxonomy of Formal Languages and

Machines

We now summarize the formal machines as well as languages studied in thisbook in the table given in Figure 15.4. For each machine, we describe the

Page 251: Computation Engineering Applied Automata Theory and Logic

15.4. A TAXONOMY OF FORMAL LANGUAGES AND MACHINES243

Machines Languages Nature of grammar

=================================================================================

DFA/NFA Regular Left-linear productions only

OR

Right-linear productions only

DPDA Deterministic Each left-hand side has one non-terminal

CFL (the productions are deterministic)

NPDA (or ‘‘PDA’’) CFL Each left-hand side has one non-terminal

LBA Context Left-hand sides may have length > 1

Sensitive but |LHS| <= |RHS| - ignoring epsilon

Languages productions

DTM/NDTM Recursively General grammars (|LHS| >= |RHS| allowed)

Enumerable

=================================================================================

Figure 15.4: The Chomsky Hierarchy and Allied Notions

nature of their languages, and indicate the nature of the grammar used todescribe the languages. It is interesting that simply by varying the natureof production rules, we can obtain all members of the so called ChomskyHierarchy of Formal Languages. This single table, in a sense, summarizessome of the main achievements of over 50 years of research in computability,machines, automata, and grammars.

CFGs and CFLs are fundamental to computer science because they helpdescribe the structure underlying programming languages. The basic “sig-nature” of a CFL is “nested brackets.” Nesting occurs in expressions andin statically scoped structures, for example. In contrast, the basic signa-ture of regular languages is “iteration (looping) according to some ultimateperiodicity.”

Illustration 243.1 Let us argue that the programming language C is notregular. Let there be a DFA for C with n states. Now consider the C program

CNOP = main()nn

Clearly, the DFA for C will loop in the part described by main()n, and bypumping this region wherever the loop might fall, we will obtain a malformedC program. Some of the pumps could, for instance result in the C programmaiaiain(). . ., and such!

Page 252: Computation Engineering Applied Automata Theory and Logic

244 CHAPTER 15. CONTEXT-FREE LANGUAGES

Using a CFG, we can describe this sub-language of C using productionrules, as follows:

L_C_nop -> main Paren Braces

Paren -> ()

Braces -> epsilon | Braces

15.4.1 Non-closure of CFLs under complementation

It may come as a surprise that most programming languages are not context-free! For instance, in C, we can declare function prototypes that can introducean arbitrary number of arguments. Later when the function is defined, thesame arguments must appear in the sam.e order. The structure in suchdefine/use structures with a positional significance can be captured by thelanguage

Lww = ww | w ∈ 0, 1∗.As we shall argue shortly, this language is not context-free. It is a context-sensitive language which can be accepted by a linear bounded automaton(LBA). Basically, an LBA has a tape, and can sweep across the tape asmany times as it wants, writing “marking bits” to compare across arbitraryreaches of the region of the tape where the original input was presented. Thismechanism can easily spot a w and a later w appearing on the tape. The useof symbol tables in compilers essentially gives it the power of LBAs, makingcompilers able to handle C prototype definitions.

It is very interesting that the complement of Lww, Lww, is indeed a CFL!It is generated by the following grammar Gwwbar.

S -> AB | BA | A | B

A -> CAC | 0

B -> CBC | 1

C -> 0 | 1

This means that CFLs are not closed under complementation.

Illustration 244.1 For each language below, write R if the language is reg-ular, DCFL if the language is deterministic context-free (can be recognizedby a DPDA), CFL if it can be recognized by a PDA but not a DPDA, IAif the language is CFL but is inherently ambiguous, and N if not a CFL.Also provide a 1-sentence justification for your answer. Note: In some cases,the language is described using the set construction while in other cases, thelanguage is described via a grammar (“L(G)”).

Page 253: Computation Engineering Applied Automata Theory and Logic

15.4. A TAXONOMY OF FORMAL LANGUAGES AND MACHINES245

1. x | x is a prefix of w for w ∈ 0, 1∗

Solution: This is R, because the language is nothing but 0, 1∗.

2. L(G) where G is the CFG S → 0 S 0 | 1 S 1 | ε.

Solution: CFL, because nondeterminism is required in order to guessthe midpoint.

3. anbmcndm | m,n ≥ 0

Solution: N , because comparison using a single stack is not possible. Ifwe push an followed by bm, it no longer is possible to compare cn againstan, as the top of the stack contains bm. Removing bm “temporarily”and restoring it later isn’t possible, as it is impossible to store away bm

in the finite-state control.

4. anbn | n ≥ 0

Solution: DCFL, since we can deterministically switch to matching bs.

5. aibjck | i, j, k ≥ 0 and i = j or j = k

Solution: IA, because for i = j = k, we can have two distinct parses,one comparing as and bs, and the other comparing bs and cs. (The ca-pability for these two comparisons must exist in any grammar, becauseof the “or” condition.)

Illustration 245.1 Indicate which of the following statements pertaining toclosure properties is true and which is false. For every true assertion below,provide a one-sentence supportive answer. For every false assertion below,provide a counterexample.

1. The union of a CFL and a regular language is a CFL.

Solution: True, since regular languages are also CFLs. Write the top-level production of the new CFL as S -> A | B where A generates thegiven CFL and B generates the given regular language.

2. The intersection of any two CFLs is always a CFL.

Solution:

Page 254: Computation Engineering Applied Automata Theory and Logic

246 CHAPTER 15. CONTEXT-FREE LANGUAGES

False. Consider ambmcn | m,n ≥ 0 ∩ ambncn | m,n ≥ 0. Thisis anbncn | n ≥ 0 which is not a CFL.

3. The complement of a CFL is always a CFL.

Solution: False. Consider Lww = ww | w ∈ 0, 1∗ which was dis-cussed on Page 244. Try to come up with another example yourselves.

Illustration 246.1 Describe the CFL generated by the following grammarusing a regular expression. Show how you handled each of the nonterminals.S → TTT → UT | UU → 0U | 1U | ε

It is easy to see that U generates a language represented by regular ex-pression 0 + 1)∗. T generates U+. Note that for any regular expression R,(R∗)+ is R∗ ∪ R∗R∗ ∪ R∗R∗R∗ . . . which is R∗. Thus, T generates (0 + 1)∗.Now, S generates TT , or (0 + 1)∗(0 + 1)∗, which is the same as (0 + 1)∗..

Illustration 246.2 Simplify the following grammar, explaining why eachproduction or non-terminal was eliminated.

S → A B | DA → 0 A | 1 B | CB → 2 | 3 | AD → A C | B DE → 0

Solution: Grammars are simplified as follows. First, we note which non-terminals are generating - have a derivation sequence into a terminal string.(If a non-terminal is non-generating, the language denoted by it is φ.) Wecan note that in our example, B is generating. Therefore, A is generating.C is an undefined non-terminal, and so we can eliminate that. Now, we notethat S is generating. D is reachable (‘reachable’ means appears in at leastone derivation sequence starting at S) but non-generating, so we can elimi-nate that. Finally, E is not reachable from S through any derivation path,and hence we can eliminate its production. Thus, we obtain:

Page 255: Computation Engineering Applied Automata Theory and Logic

15.4. A TAXONOMY OF FORMAL LANGUAGES AND MACHINES247

S → A BA → 0 A | 1 BB → 2 | 3 | A

The general principles applied to simplify CFGs are as follows (it is as-sumed that the productions are represented in terms of elementary produc-tions):

• A non-generating non-terminal is useless, and it can be eliminated.

• A non-terminal for which there is no rule defined (does not appear onthe left-hand side of any rule) is non-generating in a trivial sense.

• The property of being non-generating is ‘infectious’ in the followingsense: if non-terminal N1 is non-generating, and if N1 appears in everyderivation sequence of another non-terminal N2, then N2 is also non-generating.

• A non-terminal that does not appear in any derivation sequence start-ing from S is unreachable.

• Any CFG production rule that contains either a non-generating or anunreachable non-terminal can be eliminated.

Illustration 247.1 Simplify the following grammar, clearly showing howeach simplification was achieved (name criteria such as ’generating’ and’reaching’)

S -> A B | C D

A -> 0 A | 1 B

B -> 2 | 3

D -> A C | B D E

E -> 4 E | D | 5

B is generating. Hence A is generating. S is generating. B, A, and S arereachable. Hence S, A, and B are useful. C and D are reachable; however, C isnot generating. Hence production CD is useless. Thus we are left with

S -> A B

A -> 0 A | 1 B

B -> 2 | 3

Exercise 15.5 Prove that Gwwbar is a CFG for the language Lww. Hint:The productions S -> A and S -> B generate odd-length strings. Also, S

Page 256: Computation Engineering Applied Automata Theory and Logic

248 CHAPTER 15. CONTEXT-FREE LANGUAGES

-> AB and S -> BA generate all strings that are not of the form ww. Thisis achieved by generating an even-length string pq where |p| = |q| and if p isput “on top of” q, there will be at least one spot where they both differ.

We now examine push-down automata which are machines that recognizeCFLs, and bring out some connections between PDAs and CFLs.

15.5 Push-down Automata

A push-down automaton (PDA) is a structure (Q,Σ,Γ, δ, q0, z0, F ) where Γis the stack alphabet (that usually includes the input alphabet Σ), and z0 isthe initial stack symbol, and

δ : Q× (Σ ∪ ε)× Γ→ 2Q×Γ∗

.

In each move, a PDA can optionally read an input symbol. However, in eachmove, it reads the top of the stack. (Later, we will see that this assumptioncomes in handy when we have to convert a PDA to a CFG.) Since we willalways talk about an NPDA by default, the δ function returns a set of options.Each option is a next-state to go to, and a stack string to push on the stack,with the first symbol of the string appearing on top of the stack after thepush is over. For example, if δ(q0, x, a)contains〈q1, ba〉, the move can occurwhen x can be read from the input and the stack top is a. In this case, thePDA moves over x (it cannot read x again). Also, an a is removed fromthe stack. However, as a result of the move, an a is promptly push backon the stack, and is followed by a push of b, with the machine going tostate q1. The transition function δ of a PDA may be either deterministic ornon-deterministic.

15.5.1 DPDA versus NPDA

A push-down automaton can be deterministic or non-deterministic. DPDAand NPDA are not equivalent in power; the latter are strictly more powerfulthan the former. Also, notice that unlike with a DFA, a deterministic PDAcan move on ε. Thus, the exact specification of what deterministic meansbecomes complicated. We summarize the definition from [?]. A PDA isdeterministic if and only if the following conditions are met:

1. δ(q, a,X) has at most one member for any q in Q, a in Σε, and X in Γ.

Page 257: Computation Engineering Applied Automata Theory and Logic

15.5. PUSH-DOWN AUTOMATA 249

--------------------------------------------

state input stack newstack newstate

--------------------------------------------

q0 0 z0 0 z0 q1

q0 1 z0 1 z0 q1

q1 0 0 0 0 q1

q1 0 1 0 1 q1 -- that’s all we can do (stack)

-- we can’t possibly match

q1 0 0 epsilon q1

q1 1 1 1 1 q1

q1 1 0 1 0 q1 -- that’s all we can do (stack)

-- we can’t possibly match

q1 1 1 epsilon q1

q1 epsilon z0 z0 q2

--------------------------------------------

Figure 15.5: A PDA for the language L0

2. If δ(q, a,X) is nonempty, for some a in Σ, then δ(q, ε,X) must beempty.

Exercise 15.6 Argue that a DPDA satisfying the above definition cannotbe designed for the language wwR | w ∈ Σ∗.

In this book, I will refrain from giving a technically precise definition ofDPDAs. It really becomes far more involved than we wish to emphasize inthis chapter, at least. For instance, with a DPDA, it becomes necessary toknow when the string ends, thus requiring a right end-marker a. For details,please see Kozen’s “Automata and Computability,” Page 176, SupplementaryLecture F.

Page 258: Computation Engineering Applied Automata Theory and Logic

250 CHAPTER 15. CONTEXT-FREE LANGUAGES

15.5.2 Deterministic Context-free Languages (DCFL)

A deterministic context-free language (DCFL) is one for which there is aDPDA that accepts the same language. Consider language L0:Language L0: L0 = wwR | w ∈ 0, 1∗.

With respect to the above list of languages, L0 is not a DCFL becausein any PDA, the use of nondeterminism is essential “guess” the midpoint.Figure 15.5 presents the δ function of a PDA designed to recognizes L0. Thewhole PDA will be the structure

PL0= (q0, , 0, 1, 0, 1, z0, δ, q0, z0, q0, q2).

Let us further our intuitions about PDAs by considering a few languages:

L1: aibjck | if i = 1 then j = k.

L2: aibjckdm | i, j, k,m ≥ 0 ∧ if i = 1 then j = k else k = m

L3: ww | w ∈ 0, 1∗.

L4: 0, 1∗ \ ww | w ∈ 0, 1∗.

L5: aibjck | i = j or i = k.

L6: anbncn | n ≥ 0.

Consider language L1. L1 is a DCFL as, seeing whether i = 1 or not,a deterministic algorithm can be engaged in. A DPDA can be designed forreverse(L1) also. Likewise, a DPDA can be designed for L2. However, asdiscussed in Section 9.1.4, reverse(L2) is not a DCFL, as it is impossibleto keep both decisions – whether j = k or k = m – ready by the time i isencountered. L3 is not a CFL at all. However, L4, the complement of L3, isa CFL. L5 is a CFL (but not a DCFL) – the guesses of i = j or i = k canbe made non-deterministically. Finally, L6 is not a CFL, as we cannot keeptrack of the length of three distinct strings using one stack.

15.5.3 Some factoids

Here are a few more factoids that tie together ambiguity (of grammars) anddeterminism (of PDA).

Page 259: Computation Engineering Applied Automata Theory and Logic

15.6. RIGHT- AND LEFT-LINEAR CFGS 251

• If one can obtain a DPDA for a language, then that language is notinherently ambiguous. This is because for an inherently ambiguous lan-guage, every CFG admits two parses, thus meaning that there cannotbe a DPDA for it.

• There are CFLs that are not DCFLs (have no DPDA), and yet theyhave non-ambiguous grammars. The grammar

S -> 0 S 0 | 1 S 1 | epsilon

is non-ambiguous, and yet denotes a language that is not a DCFL.In other words, this CFG generates all the strings of the form wwR,and these strings have only one parse tree. However, since the mid-point of such strings isn’t obvious during a left-to-right scan, a non-deterministic PDA is necessary to parse such strings.

Exercise 15.7 Answer whether true or false.

1. There are more regular languages (RLs) than CFLs

2. Every RL is also a CFL

3. Every CFL is also a RL

4. Every CFL has a regular sub-language (“Sublanguage” means the sameas “subset.”)

5. Every RL has a CF sub-language

The answers are false (in a cardinality sense they are the same number), true,false, true, and false.

15.6 Right- and Left-linear CFGs

A right-linear CFG is one where every production rule has exactly one non-terminal and that too it appears right-most. For example, the followinggrammar is right-linear:

S -> 0 A | 1 B | epsilon

A -> 1 C | 0

B -> 0 C | 1

C -> 1 | 0 C

Page 260: Computation Engineering Applied Automata Theory and Logic

252 CHAPTER 15. CONTEXT-FREE LANGUAGES

Recall that S -> 0 A | 1 B | epsilon is actually three different produc-tion rules S -> 0 A, S -> 1 B, and S -> epsilon, where each rule is right-linear. This grammar can easily be represented by the following NFA ob-tained almost directly from the grammar:

IS - 0 -> A

IS - 1 -> B

IS - epsilon -> F1

A - 1 -> C

A - 0 -> F2

B - 0 -> C

B - 1 -> F3

C - 0 -> C

C - 1 -> F4

A left-linear grammar is defined similar to a left-linear one. An exampleis as follows:

S -> A 0 | B 1 | epsilon

A -> C 1 | 0

B -> C 1 | 1

C -> 1 | C 0

A purely left-linear or a purely right-linear CFG denotes a regular language.However, the converse is not true; that is, if a language is regular, it does notmean that it has to be generated by a purely left-linear or purely right-linearCFG. Even non-linear CFGs are perfectly capable of sometimes generatingregular sets, as in

S -> T T | epsilon

T -> 0 T | 0

It also must be borne in mind that we cannot “mix up” left- and right-linear productions and expect to obtain a regular language. Consider theproductions

S -> 0 T | epsilon

T -> S 1

In this grammar, the productions are linear - left or right. However, sincewe use left and right linear rules, the net effect is as if we defined the grammar

S -> 0 S 1 | epsilon

which generates the non-regular context-free language 0n1n | n ≥ 0.

Page 261: Computation Engineering Applied Automata Theory and Logic

15.6. RIGHT- AND LEFT-LINEAR CFGS 253

Conversion of purely left-linear grammars to NFA

Converting a left-linear grammar to an NFA is less straightforward. Wefirst reverse the language it represents by reversing the grammar. Grammarreversal is approached as follows: given a production rule

S → R1R2 . . . Rn

we obtain a production rule for the reverse of the language represented by Sby reversing the production rule to:

Sr → RrnR

rn−1 . . . R

r1.

Applying this to the grammar at hand, we obtain

Sr -> 0 Ar | 1 Br | epsilon

Ar -> 1 Cr | 0

Br -> 1 Cr | 1

Cr -> 1 | 0 Cr

Once an NFA for this right-linear grammar is built, it can be reversed toobtain the desired NFA.

Illustration 253.1 Given the following CFG

S -> T T | U

T -> 0 T | T 0 | #

U -> 0 U 0 0 | #

• Notice that the rule for T is neither purely left-linear nor purely right-linear. Thus we are not certain that T will generate a regular language.However, in this case, it does so and denotes the regular expression(0∗#0+ + 0+#0∗ +#), which reduces to (0∗#0∗).

• The concatenation of two regular languages is regular. So that caseof S going to T T generates a regular language.

• Notice that the production of S going to T T results in strings thatcontain two #s. On the other hand, the production S -> U generatesstrings with only one #. These are therefore distinct classes of strings.Notice that if a string in the latter class has n 0s before the #, it willthen have 2n 0s after it. Thus, we can choose the string 0n # 02n andpump 0n to obtain a string that contains only one # but does not obeythe pattern 0n # 02n. This does not belong to the language of S, thusshowing that it is not regular. It is, however context-free.

Page 262: Computation Engineering Applied Automata Theory and Logic

254 CHAPTER 15. CONTEXT-FREE LANGUAGES

15.7 Developing CFGs

Developing CFGs is much like programming: there are no hard-and-fast rules.Here are a few rules of the thumb for arriving at CFGs.that are reasonablygeneral.

1. (Use common idioms): Study and remember many common patternsof CFGs and use what seems to fit in a given context. Example: Toget the effect of matched brackets, the common idiom is

S -> ( S ) | epsilon

2. Break the problem into simpler problems:Example: ambn | m 6= n, m, n ≥ 0

(a) So as and bs must still come in order

(b) Their numbers shouldn’t match-up.

i. Formulate matched-up as and bsM -> epsilon | a M b

ii. Break the match by adding either more As or more BsS -> A M | M B

A -> a | a A

B -> b | b B

15.8 A Pumping Lemma for CFLs

Consider any CFG G = (N,Σ, P, S). A Pumping Lemma for the languageof this grammar, L(G), can be derived by noting that a “very long string”w ∈ L(G) requires a very long derivation sequence to derive it from S.Since we only have a finite number of non-terminals, some non-terminal mustrepeat in this derivation sequence, and furthermore, the second occurrence ofthe non-terminal must be a result of expanding the first occurrence (it mustlie within the parse-tree generated by the first occurrence). For example,consider the CFG

Page 263: Computation Engineering Applied Automata Theory and Logic

15.8. A PUMPING LEMMA FOR CFLS 255

u v y zx

S

V_1

V_1

V_2

v yx

u z

S

V_2

x

u v y zx

S

V_2

V_1

Height |V| + 1max. branching factor = b

Figure 15.6: Depiction of a Parse Tree for the CFL Pumping Lemma

S -> ( S ) | T | e

T -> [ T ] | T T | e

Here is an example derivation:

S => ( S ) => (( T )) => (( [ T ] )) => (( [ ] ))

^ ^

| |

Occurrence-1 Occurrence-2

Occurrence-1 involves Derivation-1: T => [ T ] => [ ]

Occurrence-2 involves Derivation-2: T => e

Here, the second T arises because we took T and expanded it into [ T ] andthen to [ ] . Now, the basic idea is that we can use Derivation-1 used inthe first occurrence in place of Derivation-2, to obtain a longer string:

Page 264: Computation Engineering Applied Automata Theory and Logic

256 CHAPTER 15. CONTEXT-FREE LANGUAGES

S => ( S ) => (( T )) => (( [ T ] )) => (( [[ T ]] )) => (( [[ ]] ))

^ ^

| |

Occurrence-1 Use Derivation-1

here

In the same fashion, we can use Derivation-2 in place of Derivation-1 toobtain a shorter string, as well:

S => ( S ) => ( ( T ) ) => ( ( ) )

^

|

Use Derivation-2

here

When all this happens, we can find a repeating non-terminal that can bepumped up or down. In our present example, it is clear that we can manifest(([i ]i)) for i ≥ 0 by either applying Derivation-2 directly, or by applyingsome number of Derivation-1s followed by Derivation-2.

In order to capture the conditions mentioned so far conveniently, it is goodto resort to parse trees. Consider a CFG with |V | non-terminals, and witheach rule right-hand side containing at-most b syntactic elements (terminalsor non-terminals). Consider a b-ary tree built up to height |V |+1, as shownin Figure 15.6. The string yielded on the frontier of the tree w = uvxyz. Ifthere are two such parse trees for w, pick one that has the fewest numberof nodes. Now, if we avoid having the same non-terminal used in any pathfrom the root to a leaf, basically each path will “enjoy” a growth upto heightat-most |V | (recall that the leaves are terminals). The string w = uvxyz is,in this case, of length at-most b|V |. This implies that if we force the string tobe of length b|V |+1 (called p hereafter), a parse-tree for this string will havesome path that repeats a non-terminal. Call the higher occurrence V1 andthe lower occurrence (contained within V1) as V2. Pick the lowest two suchrepeating pair of non-terminals. Now,

• |vxy| ≤ p; if not, we would find two other non-terminals that repeatlower than V1 and V2, thus violating the fact that V1 and V2 are thelowest two such.

• |vx| ≥ 1; if not, we will in fact be having w = uxz, for which a shorterparse-tree exists (namely, the one where we directly employ V2.

• Now, by pumping, we can obtain the desired repetitions of v and y.

Page 265: Computation Engineering Applied Automata Theory and Logic

15.8. A PUMPING LEMMA FOR CFLS 257

Theorem 15.1 Given any CFG G = (N,Σ, P, S), there exists a number psuch that given a string w in L(G) such that |w| ≥ p, we can split w intow = uvxyz such that |vy| > 0, |vxy| ≤ p, and for every i ≥ 0, uvixyiz ∈L(G).

We can apply this Pumping Lemma for CFGs in the same manner as wedid for regular sets.

Exercise 15.8 Show using the Pumping lemma that the language 0n1n2n |n ≥ 0 is not context-free.

The following exercise demonstrates another “unusual” application.

Exercise 15.9 Prove that any CFG with |Σ| = 1 generates a regular set.Hint: use the Pumping Lemma for CFLs together with the ultimate period-icity result for regular sets. Carefully argue and conclude using the PL forCFLs that we are able to generate only periodic sets.

Page 266: Computation Engineering Applied Automata Theory and Logic

258 CHAPTER 15. CONTEXT-FREE LANGUAGES

Page 267: Computation Engineering Applied Automata Theory and Logic

Chapter 16

Push-down Automata andContext-free Grammars

In this chapter, we examine push-down automata at a deeper level thanexamined in the previous chapter. After formally introducing PDA in Sec-tion 16.1, we introduce two notions of acceptance - by final state and byempty stack - in Sections 16.1.2 and 16.1.3, respectively. We then present analgorithm to convert a CFG to a language-equivalent PDA in Section 16.2,and an algorithm to convert a PDA to a language-equivalent CFG in Sec-tion 16.3. In Section 16.4, we briefly discuss a normal form for context-freegrammars called the Chomsky normal form. We do not discuss other normalforms such as the Greibach normal form, which may be found in most othertextbooks. We then describe the Cocke-Kasami-Younger (CKY) parsing al-gorithm for a grammar in the Chomsky normal form. Finally, we brieflydiscuss Closure and Decidability properties in Section 16.5.

16.1 Push Down Automata

A push-down automaton (PDA) is a structure (Q,Σ,Γ, δ, q0, z0, F ) where Γis the stack alphabet (that usually includes the input alphabet Σ), z0 is theinitial stack symbol, and δ : Q × (Σ ∪ ε) × Γ → 2Q×Γ

∗. In each move, a

PDA can optionally read an input symbol, but always read the top of thestack. This convention is not universal - in [?] and in the Jflap tool, PDAsthat optionally read the top of the stack are allowed. Such variations do notfundamentally change the “power” of PDAs. We selected our convention of

259

Page 268: Computation Engineering Applied Automata Theory and Logic

260CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

always reading and popping the stack with a view to match our algorithm(following [?]) of converting PDAs to CFGs.

How do we describe the notion of a PDA accepting a string? There areactually two different notions of acceptance. According to the first, a PDAaccepts a string when, after reading the entire string the PDA is in a finalstate. According to the second, a PDA accepts a string when, after readingthe entire string the PDA has emptied its stack. We define these notions inSections 16.1.2 and 16.1.3. In both these definitions, we employ the notionsof instantaneous descriptions (ID) and step relations ` and `∗.

An instantaneous description (ID) for a PDA consists of triples of theform

(state, unconsumed input, stack state)

Formally, the type of the ID of a PDA, TID, is TID = Q×Σ∗×Γ∗. The typeof ` is ` ⊆ TID × TID. The ` relation is as follows:

(q, aσ, bγ) ` (p, σ, gγ)〉 iffa ∈ Σε ∧ b ∈ Γ ∧ g ∈ Γ∗ ∧∃(p, g) ∈ δ(q, a, b).

In other words, if δ allows a move from state q via input a ∈ Σ∪ε with thestack top being b to state p, such a step is supported by `. In this process, thestack top b is popped, and the new stack contents described by g is pushedon. The first symbol of g ends-up at the top of the stack. Sometimes, thelast symbol of g is set to b, thus helping restore b that was popped. In somecases, g is actually made equal to b, thus modeling the fact that the stack didnot suffer any changes. As usual, `∗ is the reflexive and transitive closure of`.

16.1.1 Conventions Pertaining to Describing PDAs

We prefer to draw tables of PDA moves. Please make the tables prettydetailed. Do write comments - after all, you are coding in a pretty low-leveland highly error-prone language, and so the more details you provide, thebetter it is for the readers. A diagram will also be highly desirable. Basically,the diagram resembles that of a NFA or a DFA, with the moves annotatedby insymb, ssymb→ sstr where

• insymb is an input symbol or ε,

Page 269: Computation Engineering Applied Automata Theory and Logic

16.1. PUSH DOWN AUTOMATA 261

• ssymb is a stack symbol, and

• sstr is a string of stack symbols that is pushed onto the stack when themove is executed.

Also, recall that PDAs don’t need to decode every possible case. A PDAaccepts an input if the input leads it to one of the final states. If moves cor-responding to unexpected inputs are left out, the PDA will end-up ignoringthem quite naturally. There is no need to go to black-hole states - even fora DPDA. Finally, recall the difference between NPDAs and DPDAs pointedout in Section 15.5.1.

Now we define the different notions of acceptance.

16.1.2 Acceptance by Final State

A PDA accepts a string w by final state if and only if, for some qf ∈ F ,the final set of states of the PDA, (q0, w, z0) `∗ (qf , ε, g). For any givenPDA, our default assumption will be that of acceptance by final state. Thelanguage of the PDA will be defined as follows:

w | ∃g . (q0, w, z0) `∗ (qf , ε, g) for qf ∈ F.

For a PDA P , languages defined through acceptance by final state aredenoted by L(P ).

Consider the PDA for language L0 defined in Figure 15.5, reproduced inFigure 16.1 for convenience. This figure also shows how the instantaneousdescriptions can progress with respect to string 001100 for one token (calledthe “winner”) and another token (called “loser”).

These examples show that so long as one course of forward steps via `exists leading to the final state q2, the given string is accepted. The othertokens “die out.” Such animations are best observed by using the tool JFLAPthat actually supports both these notions of acceptance (by final state andempty stack) and even a combined notion of acceptance (accept when botha final state is reached and the stack is empty). JFLAP maintains a view ofeach token as it journeys through the labrynth of a PDA transition diagram.

Conversion of P1 to P2 ensuring L(P1) = N(P2)

Given a PDA P1 that accepts by final state, we can obtain a PDA P2 thataccepts by empty stack such that N(P2) = L(P1), simply by ensuring that

Page 270: Computation Engineering Applied Automata Theory and Logic

262CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

-----------------------------------------------------------------------------

state input stack newstack newstate Comments

-----------------------------------------------------------------------------

q0 0 z0 0 z0 q1

q0 1 z0 1 z0 q1

q1 0 0 0 0 q1

q1 0 1 0 1 q1 -- That is all we can do

-- (i.e. stack).

-- We can’t possibly match.

q1 0 0 epsilon q1

q1 1 1 1 1 q1

q1 1 0 1 0 q1 -- That is all we can do

-- (i.e., stack).

-- We can’t possibly match.

q1 1 1 epsilon q1

q1 epsilon z0 z0 q2

-----------------------------------------------------------------------------

WINNER TOKEN LOSER TOKEN

(q0,001100, z_0) |- (q0,001100, z_0) |-

(q1, 01100, 0z_0) |- (q1, 01100, 0z_0) |-

(q1, 1100, 00z_0) |- (q1, 1100, z_0)

(q1, 100, 100z_0) |-

(q1, 00, 00z_0) |- OH NO! We try to start

(q1, 0, 0z_0) |- popping... but we got

(q1, , z_0) |- royally stuck here!

(q2, , z_0)

ACCEPT!

Figure 16.1: llustration of the ` relation on the PDA of Figure 15.5

Page 271: Computation Engineering Applied Automata Theory and Logic

16.1. PUSH DOWN AUTOMATA 263

Start state = q00

-----------------------------------------------------------------------------

state input stack newstack newstate Comments

-----------------------------------------------------------------------------

q00 epsilon z0 z0 z00 q0

q0 epsilon z0 z0 qS -- q0 is a final state; so jump

-- to state qS

qS epsilon any epsilon qS -- qS simply drains the stack

-- regardless of what’s on top of

-- the stack. ‘‘any’’ includes z00

q0 0 z0 0 z0 q1

q0 1 z0 1 z0 q1

q1 0 0 0 0 q1

q1 0 1 0 1 q1

q1 0 0 epsilon q1

q1 1 1 1 1 q1

q1 1 0 1 0 q1

q1 1 1 epsilon q1

q1 epsilon z0 z0 q2

q2 epsilon z0 z0 qS -- q2 is also a final state; so

-- jump to state qS

-----------------------------------------------------------------------------

Figure 16.2: The PDA of Figure 15.5 converted to one that accepts by emptystack

Page 272: Computation Engineering Applied Automata Theory and Logic

264CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

P2 has an empty stack exactly when P1 reaches a final state (for the sameinput w seen by both these PDAs). The following construction achieves theabove condition:

• To avoid the stack of P2 becoming empty “in between,” introduce anextra symbol in P2’s stack alphabet, say z00.

• Start P2 with its stack containing z00, and then z0 riding above it.

• The remaining moves of P2 are similar to that of P1. However, “final”states are insignificant for P2. Thus, whenever P1 reaches a final state,we introduce in P2, a move from it to a new stack-draining state qS.While in qS, P2 empties its stack completely.

• No state other than qS tests for the stack-top being z00. Thus, thestack is totally emptied, including of z00, only in state qS.

Figure 16.2 illustrates this construction.

16.1.3 Acceptance by Empty Stack

If the language of some PDA P2 is defined following acceptance by emptystack, its language will be denoted by N(P2). Read N(P2) as “I’m goingto define the language of this PDA P2 which accepts a string whenever itsstack becomes empty.” To further highlight that a PDA accepts by emptystack, we leave the F component of the seven-tuple presentation of PDAs,thus obtaining the six-tuple P2 = (Q,Σ,Γ, δ, q0, z0). For such PDAs, a stringw is in its language exactly when the following is true:

(q0, w, z0) `∗ (q, ε, ε).

Here, q ∈ Q, i.e., q is any state. All it matters is that the input is entirelyconsumed and an empty stack results in doing so.

Conversion of P1 to P2 ensuring N(P1) = L(P2)

Given a PDA that is defined according to the “accept by empty-stack” cri-terion, how do we convert it to a PDA that accepts by final state? A simpleobservation tells us that the stack can become empty at any control state.Thus, the trick is to start the PDA with a new bottom-of-stack symbol z00.Under normal operation of the PDA, we do not see z00 on top of the stack,as it will be occluded by the “real” top-of-stack z0. However, in any state, if

Page 273: Computation Engineering Applied Automata Theory and Logic

16.2. DIRECT CONVERSION OF CFGS TO PDAS 265

z00 shows up on top of the stack, we add a transition to a newly introducedfinal state qF . qF is the only final state in the new PDA. Thus, wheneverthe former PDA drains its stack, the new PDA ends-up in state qF .

Illustration 265.1 Develop a push-down automaton for Lambnck of Illustra-tion 236.1.

The PDA is shown in Figure 16.3. A PDA will first exercise a non-deterministic option: either I shall decide to match a’s and b’s, or do b’sagainst c’s. Recall that PDAs begin with z0 in the stack, and further wemust pop one symbol from the stack in each step. Also, in each move, wecan push zero, one, or more (a finite number) of symbols back onto the stack.

Here are some facts about this PDA (based on intuitions - no proofs):

• Non-determinism is essential. We do not know whether we are goingto match a’s and b’s or b’s and c’s. In fact, for a string “abc”, theremust be two different paths that lead to some final state - hence non-determinism exists.

• This language is inherenty ambiguous. For string “abc” it must bepossible to build two distinct parse-trees no matter which grammar isused to parse it.

16.2 Direct Conversion of CFGs to PDAs

When given the option of capturing a context-free language using a PDAor a CFG, what would one choose? In many cases, a CFG would be easierto first obtain; in that case, there exists a rather elegant direct-conversionalgorithm to convert that CFG into a PDA. This algorithm, in effect, is anon-deterministic parsing algorithm. The opposite conversion - a PDA to aCFG - is much more involved - but is nevertheless discussed in Section 16.3.

By determinizing the CFG to PDA conversion algorithm, we can obtainan exponential-time parsing algorithm for any CFG. Determinization can beachieved by arranging a backtracking search: whenever the NPDA is facedwith a non-deterministic choice, we arrange a piece of code that recursivelysearches for one of the paths to accept1. In Section 16.4, we present an

1It is interesting to observe that in so determinizing, we would have strayed off thePDA-land: we would be managing one explicit “PDA-stack” and be also using another“recursion stack” implicitly.

Page 274: Computation Engineering Applied Automata Theory and Logic

266CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

Initial state = Q0

Final states = Q0,Qc,Qd

------------------------------------------------------------------------

State Input Stack-top To-push Newstate Comments

------------------------------------------------------------------------

Q0 epsilon z0 z0 Qab Gonna match a..b

Q0 epsilon z0 z0 Qbc Gonna match b..c

Qab a z0 a z0 Qab Continue stacking a

Qab a a a a Qab Continue stacking a

Qab b a epsilon Qb Now expect b’s; match against a’s

Qb b a epsilon Qb Perhaps more b’s

Qb epsilon z0 z0 Qc The ‘‘eat c’’ state

also an accept.

Qc c z0 z0 Qc Any number of c’s OK

Qab epsilon z0 z0 Qc The ‘‘eat C’’ state

also an accept.

Qbc a z0 z0 Qbc a’s don’t bother. Qbc

will ignore them, as it

is bent on matching b’s and c’s

Qbc b z0 b z0 Qbc1 Continue stacking b; no more a’s please

Qbc b b b b Qbc1 Continue stacking b; still no more a’s plz

Qbc1 c b epsilon Qm Continue matching c’s; no more b’s please

Qm c b epsilon Qm Continue matching c’s; no more a’s or b’s

Qm epsilon z0 z0 Qd A token goes to Qd whenever

z0 shows up on top of stack

------------------------------------------------------------------------

Figure 16.3: A PDA for Lambnck of Illustration 236.1

Page 275: Computation Engineering Applied Automata Theory and Logic

16.2. DIRECT CONVERSION OF CFGS TO PDAS 267

algorithm to obtain the Chomsky Normal Form for a CFG, and in its con-text discuss an O(N 3) parsing algorithm attributed to Cocke, Kasami, andYounger (Section 16.4.1).

In the CFG to PDA conversion algorithm, the non-terminals and termi-nals of the given grammar constitute the stack alphabet of the PDA gener-ated. In addition, the stack alphabet contains z0. The conversion proceedsas follows:

• Start from state q0 with z0 on top of the stack.

• From q0, jump to state qM (for “main state”) with S, the start symbolof the grammar, on top of the stack, and z0 below it (restored in thejump).

• In state qM ,

− If the top of the stack is z0, jump to state qF , the only acceptingstate.

− If the top of the stack is the terminal x, jump back to state qMupon input x, without restoring x on top of stack. Essentially, theparsing-goal of x has been fulfilled.

− If the top of the stack is the non-terminal X, and there is a ruleX → R, where R is a string of terminals and non-terminals, jumpto state qM by poppingX and pushing R. Essentially, the parsing-goal of X is turned into zero or more parsing sub-goals.

Illustration 267.1 Let us convert the CFG in Illustration 236.1 into a PDA.The resulting PDA is given in Figure 16.4. First set-up S to be the parsinggoal. The PDA can then take a non-deterministic jump to two differentstates. One state sets up the parsing goals M and C, with M on top of thestack. The other path sets up A and N.

Suppose parsing-goal M is on top of the stack. We can then set-up theparsing goal a M b, with a on top of the stack. Discharging the parsing-goala is easy: just match a with the input. On the other hand, with parsing-goalM on top of the stack, we could also have set-up the parsing goal “epsilon”which means – we could be done! Thus, another move can simply empty M

from the stack. Then finally, when z0 shows-up on top of stack, we accept,as there are no parsing goals left.

Page 276: Computation Engineering Applied Automata Theory and Logic

268CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

Initial state = Q0

Final states = QF

------------------------------------------------------------------------

State Input Stacktop To-push Newstate Comments

------------------------------------------------------------------------

Q0 epsilon z0 S z0 Qmain Qmain is the ONLY real state of this PDA

Qmain epsilon S M C Qmain Just subgoal, ignoring input

Qmain epsilon S A N Qmain Just subgoal, ignoring input

Qmain epsilon M a M b Qmain Just subgoal, ignoring input

Qmain epsilon M epsilon Qmain Epsilon production for M

Qmain epsilon N b N c Qmain Just subgoal, ignoring input

Qmain epsilon N epsilon Qmain Epsilon production for N

Qmain epsilon C c C Qmain Just subgoal, ignoring input

Qmain epsilon C epsilon Qmain Epsilon production for C

Qmain epsilon A a A Qmain Just subgoal, ignoring input

Qmain epsilon A epsilon Qmain Epsilon production for A

Qmain a a epsilon Qmain eat an a when that’s the goal

Qmain b b epsilon Qmain eat a b when that’s the goal

Qmain c c epsilon Qmain eat a c when that’s the goal

Qmain epsilon z0 z0 QF when the parsing goals are gone,

we accept

------------------------------------------------------------------------

Figure 16.4: CFG to PDA conversion for the CFG of Illustration 236.1

Page 277: Computation Engineering Applied Automata Theory and Logic

16.3. DIRECT CONVERSION OF PDAS TO CFGS 269

delta contains Productions

---------------------- -------------------------------

S -> [p,Z0,x] for x in p,q

<p,(,Z0> <p,(Z0> [p,Z0,r_2] -> ( [p,(,r_1] [r_1,Z0,r_2]

for r_i in p,q

<p,(,(> <p,((> [p,(,r_2] -> ( [p,(,r_1] [r_1,(,r_2]

for r_i in p,q

<p,),(> <p,epsilon> [p,(,p] -> )

<p,epsilon,Z0> <q,epsilon> [p,Z0,q] -> epsilon

Figure 16.5: PDA to CFG conversion

16.3 Direct Conversion of PDAs to CFGs

We first illustrate the PDA to CFG conversion algorithm on an example.As soon as we present an example, we write the corresponding general rulein slant fonts. Further details, should you need them, may be found inthe textbook of Aho, Motwani, and Ullman, whose algorithm we adopt. Aslightly different algorithm appears in Sipser’s book.

Consider the PDA that accepts by empty stack, (p, q, (, ), (, ), Z0, δ, p, Z0)with δ given in Figure 16.5. Recall that since this is a PDA that accepts byempty-stack, we do not specify the F component in the PDA structure. Thus,the above six-tuple corresponds to (Q,Σ,Γ, δ, q0, z0). This figure also showsthe CFG productions generated following the PDA moves. The method usedto generate each production is the following. Each step is explained with asuitable section heading.

16.3.1 Name nonterminals to match stack-emptyingpossibilities

Notice that the non-terminals of this grammar have names of the form[a,b,c]. Essentially, such a name carries the following significance:

It represents the language that can be generated by starting in state a

Page 278: Computation Engineering Applied Automata Theory and Logic

270CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

of the PDA with b on top of the stack, and being able to go to state cof the PDA with the same stack contents as was present while in statea.

This is top-down recursive programming at its best: we set-up top-levelgoals, represented by non-terminals such as [a,b,c], without immediatelyworrying about how to achieve such complicated goals. As it turns out,these non-terminals achieve what they seek through subsequent recursiveinvocations to other non-terminals - letting the magic of recursion makethings work out!

General rule: For all states q1, q2 ∈ Q and all stack symbols g ∈ Γ,introduce a non-terminal [q1, g, q2]. (Most of these non-terminalswill prove to be useless later.)

16.3.2 Let start-symbol S set-up all stack-draining op-tions

All the CFG productions are obtained systematically from the PDA transi-tions. The only exception is the first production, which, for our PDA, is asfollows:

S -> [p,Z0,x] for x in p,q

In other words, two productions are introduced, they being:

S -> [p,Z0,p]

S -> [p,Z0,q]

Here is how to understand these productions. S, the start symbol of theCFG, generates a certain language. This is the entire language of our PDA.The entire language of our PDA is nothing but the set of all those stringsthat take the PDA from its start state p to some state, having gotten rid ofeverything in the stack. In our PDA, since it starts with Z0 on top of stack,that’s the only thing to be emptied from the stack. Since the PDA could beeither in p or q after emptying the stack (and since we don’t care where itends up), we introduce both these possibilities in the productions for S.

General rule: For all states q ∈ Q, introduce one productionS → [q0, z0, q].

Page 279: Computation Engineering Applied Automata Theory and Logic

16.3. DIRECT CONVERSION OF PDAS TO CFGS 271

16.3.3 Capture how each PDA transition helps drainthe stack

A PDA transition may either get rid of the top symbol on the stack or mayend-up adding several new symbols onto the stack. Thus, strictly speaking,each PDA transition is not one that necessarily helps drain the stack. How-ever, we can set-up recursive invocations to clear the extra stuff placed ontop of the stack, thus still achieving the overall goals.

To see all this clearly, consider the fact that δ contains a move, as shownbelow:

delta contains

----------------------

<p,(,Z0> <p,(Z0>

In this PDA, when in state p, upon seeing ( in the input and Z0 on top ofthe stack, the PDA will jump to state p, having momentarily gotten rid ofZ0, but promptly restoring ( as well as Z0. Then the PDA has to “furtherstruggle” and get rid of ( as well as Z0, reaching some states after these acts.It is only then that the PDA has successfully drained the Z0 from its stack.Said differently, to drain Z0 on the stack while in state p, read (, invite moresymbols onto the stack, and then recursively get rid of them as well. All thisis fine, except we don’t know rightaway where the PDA will be after gettingrid of (, and subsequently getting rid of Z0. However, this is no problem,as we can enumerate all possible states, thus obtaining as many “catch-all”rules as possible. This is precisely what the set of context-free grammar rulesgenerated for this grammar says:

[p,Z0,r_2] -> ( [p,(,r_1] [r_1,Z0,r_2] for r_i in p,q

The rule says: “if you start from state p with a view to completely drainZ0 from the stack, you will end up in some state r_2. That, in turn is athree-step process:

• Read ( and, for sure, we will be in state p;

• From state p, get rid of ( recursively, ending-up in some state r_1.

• From state r_1, get rid of Z0, thus ending-up in state the very samestate r_2!

Fortunately, this is precisely what the above production-rule says, accordingto the significance we assigned to all the non-terminals. We will have sixteenpossible rules even for this single PDA rule!! Many of these rules will proveto be useless.

Page 280: Computation Engineering Applied Automata Theory and Logic

272CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

General rule: If δ(p, a, g) contains 〈q, g1, . . . , gn, introduce onegeneric rule

[p, g, q0]→ a [q, a, q1][q1, g1, q2] . . . [qn, gn, q0]

and create one instance of the rule for each qi ∈ Q chosen in allpossible ways.

16.3.4 Final Result from Figure 16.5

We apply this algorithm to the PDA in Figure 16.5, obtaining an extremelylarge CFG.We hand-simplify, by throwing away rules as well as non-terminalsthat are never used. we further neaten the rules by assigning shorter namesto non-terminals as shown below:

Let A=[p,Z0,p], B=[p,Z0,q], C=[q,Z0,p], D=[q,Z0,q],

W=[p,(,p], X=[p,(,q], Y=[q,(,p], Z=[q,(,q].

Then we have the following rather bizzare-looking CFG:

S -> A | B

A -> ( W A | ( X C

B -> ( W B | ( X D

W -> ( W W | ( X Y

X -> ( W X | ( X Z

W -> )

B -> epsilon

How are we sure that this CFG is even close to being correct? We simplify thegrammar based on the notions of generating and reachable from the previouschapter. This process proceeds as follows:

1. Notice that C,D,Y,Z are not generating symbols (they can never gen-erate any terminal string). Hence we can eliminate production RHSusing them.

2. W and B are generating (W -> ) and B -> epsilon).

Page 281: Computation Engineering Applied Automata Theory and Logic

16.3. DIRECT CONVERSION OF PDAS TO CFGS 273

3. X is not generating. Look at X -> ( W X. While ( is generating and W isgenerating, X on the RHS isn’t generating – we are doing a “bottom-upmarking.” The same style of reasoning applies also to X -> ( X Z.

4. Even A is not generating!

Thus, in the end, we get a short (but still bizzare-looking) grammar:

S -> ( W S | epsilon

W -> ( W W | )

Fortunately, this grammar is now small-enough to apply our verificationmethods based on consistency and completeness. We sketch these proofsbelow.

Consistency

1. Same number of ( and )? Let’s induct.

(a) Epsilon has.

(b) How about ( W S ?

(c) Gotta “conquer” W.

i. Conjecture: W has one more ) than (.

ii. True for both arms of W.

iii. Hence conjecture true.

(d) Hence true of S.

(e) Need to verify one more step: In any prefix, number of ( morethan the number of )?

(f) Need conjecture about W: In any prefix of a string generated by W,number of ) at most one more than the number of (. Induct onthe W production and prove it. Then S is indeed what we want!!

Completeness

Assume, via complete induction, that all strings of length less than or equalto n, and that satisfy the above-mentioned correctness criterion:

In any prefix of a string generated by S, number of ( is greater-than or equal to the number of ) in it. Further, at the end of thestring, these counts are equal.

Page 282: Computation Engineering Applied Automata Theory and Logic

274CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

can be obtained from S. The next longer string is of length n + 2. We canconstruct a plot very similar to that given in Figure 15.32, and argue throughvarious cases. The details are left as an exercise.

Exercise 16.1 Convert the following PDA to a CFG:

delta contains

----------------------

<p,(,Z0> <q,(Z0>

<q,(,(> <q,((>

<q,),(> <q,epsilon>

<p,epsilon,Z0> <r,epsilon>

<q,epsilon,Z0> <r,epsilon>

16.4 The Chomsky Normal Form

Given a context-free grammar G, there is a standard algorithm (describedin most textbooks) to obtain a context-free grammar G

′in the Chomsky

normal form such that L(G′) = L(G) − ε. A Grammar in the Chomsky

Normal Form has two kinds of productions: A → BC, as well as A → a.If ε is required to be in the new grammar, it is explicitly added at the toplevel via a production of the form S → ε. There is another well-knownnormal form called the Greibach normal form which may be found discussedin various textbooks. In the GNF, all the production rules are of the formA→ aB1B2 . . . Bk where a is a terminal andA,B1, . . . , Bk, for k ≥ 0, are non-terminals. (With k = 0, we obtain A → a.) Obtaining grammars in thesenormal forms facilitates proofs, as well as the description of algorithms. Inthis chapter, we will skip the actual algorithms to obtain these normal forms,focussing instead on the advantages of obtaining grammars in these normalforms.

A grammar G in the Chomsky normal form has the property that anystring of length n generated by G must be derived through exactly 2n − 1derivation steps. This is because all derivations involve a binary productionA → BC or a unary production A → a. For example, given the followinggrammar in the Chomsky normal form,

S -> A B | S S B -> b A -> a

2In fact, the plot will be simpler for this grammar, as there will be no zero-crossings.There could be occasions where the plot touches the x-axis, and if it continues, it promptlytakes-off in the positive direction once again.

Page 283: Computation Engineering Applied Automata Theory and Logic

16.4. THE CHOMSKY NORMAL FORM 275

a string abab can be derived through a seven-step (2× 4− 1) derivation

S => SS => ABS => ABAB => aBAB => abAB => abaB => abab

In the next section, we discuss a parsing algorithm for CFGs assuming thatthe grammar is given in the Chomsky normal form.

16.4.1 Cocke-Kasami-Younger (CKY) Parsing Algorithm

The CKY parsing algorithm uses dynamic programming in a rather elegantmanner. Basically, given any string such as 0 0 1, and a Chomsky normalform grammar such asS → S T | 0T → S T | 1the following steps describe how we “parse the string” (check that the stringis a member of the language of the grammar):

• Consider all possible substrings of the given string of length 1, anddetermine which all non-terminals can generate them.

• Now, consider all possible substrings of the given string of length 2,and determine which all pairs of non-terminals in juxtaposition cangenerate them.

• Repeat this for strings of lengths 3, 4, . . ., till the full length of thestring has been examined.

To capture all this information, we choose a convenient tabular representationas in Figure 16.6. The given string 001 has four positions (marked 0 through3) in it, as shown by the “bars”, |. Now, build a table as shown, withpositions a through f marked in it. Position a in the table represents theportion of the string between positions 0 and 1, i.e., the first “0” in the string.Likewise, positions c and f represent 0 and 1, respectively. Let us fill thesepositions with the set of all non-terminals that can generate these strings.We know that S can generate a 0, and none else. Thus, S is the set ofnon-terminals that generate 0. Likewise T is the set of non-terminals thatgenerate a 1. Filling the table with these, we have the following table:

0

S 1

b S 2

Page 284: Computation Engineering Applied Automata Theory and Logic

276CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

Given string: 0 0 1

| 0 | 0 | 1 |

Positions in the string: 0 1 2 3

0

a 1

b c 2

d e f 3

Figure 16.6: A parsing table for CKY parsing

d e T 3

What can we say about position b in this table? It represents the regionin the string between positions 0 and 2. Which non-terminal can generatethe region of the string between positions 0 and 2? The answer depends onwhich non-terminals generate the region of the string between positions 0and 1, and which non-terminals generate the region of the string betweenpositions 1 and 2. We know these to be S and S. Then, the set ofnon-terminals that generate the substring 02 are then those non-terminalsthat yield SS. Since no non-terminals yield SS, we fill position b with .By a similar reasoning we fill position e with S, T. The table now looks asfollows:

0

S 1

S 2

d S,T T 3

Page 285: Computation Engineering Applied Automata Theory and Logic

16.5. CLOSURE AND DECIDABILITY 277

Finally, position d remains to be filled. Substring 03 can be generated in twodistinct ways:

• Concatenating substring 01 and 13, or

• Concatenating substring 02 and 23.

Substring 01 is generated by S and substring 13 by S,T. Thus, substring03 is generated by all the non-terminals that generate SS,T, i.e., thosethat generate SS,ST, i.e., S,T. No non-terminal generates substring 02,hence we don’t pursue that possibility any more. Thus, we fill position d

with S,T.The parsing succeeds because we managed to write an S in position d—the

start symbol can indeed yield the substring 03.

16.5 Closure and Decidability

In this section, we catalog the main results you should remember, plus somejustifications. Details are omitted for now.

1. Given a CFG, it is decidable whether its language is empty. Basically,if you find that S is not generating, the language of the grammar isempty! It is the bottom-up marking algorithm discussed above.

2. Given a CFG, it is not decidable whether its language is Σ∗.

3. The equivalence between two CFGs is not decidable. This follows fromthe previous result, because one of the CFGs could easily be encodingΣ∗.

4. Given a CFG, whether the CFG is ambiguous is not decidable.

5. Given a CFG, whether the CFG generates a regular language is notdecidable.

6. CFLs are closed under union, concatenation, and starring, becausethese constructs are readily available in the CFG notation.

7. CFLs are closed under reversal because we know how to “reverse aCFG.”

Page 286: Computation Engineering Applied Automata Theory and Logic

278CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

8. CFLs are not closed under complementation, and hence not also closedunder intersection.

9. CFLs are closed under intersection with a regular language. This isbecause we can perform the product-state construction between a PDAand a DFA.

10. CFLs are closed under homomorphism.

16.6 Some Important Points Visited

We know that if L is a regular language, then L is a context-free language,but not vice versa. Thus, the space of regular languages is properly containedin the space of context-free languages. We note some facts below:

• It does not follow from the above that the union of two CFLs is alwaysa non-regular CFL: it is not so, in general. Think of 0n1n | n ≥ 0and the complement of this language, both of which are context-free,and yet, their union is Σ∗ which is context-free, but also regular.

• The union of a context-sensitive language and a context-free languagecan be a regular language. Consider the languages Lww and Lww ofSection 15.4.1.

All this is made clear using a real-world analogy:

• In the real world, we classify music (compared to context-free lan-guages) to be “superior” to cacophony (compared to the regular lan-guage Σ∗) because music exhibits superior patterns than cacophony.

• By a stretch of imagination, it is possible to regard cacophony as music,but usually not vice versa.

• By the same stretch of imagination, utter silence (similar to the regularlanguage φ) can also be regarded as music.

• If we mix music and cacophony in the air (they are simultaneouslyplayed), the result is cacophony. This is similar to taking 0n1n | n ≥0 ∪ Σ∗ which yields Σ∗.

• However, if we mix music and silence in the air, the result is still music(similar to taking 0n1n | n ≥ 0 ∪ φ).

Page 287: Computation Engineering Applied Automata Theory and Logic

16.6. SOME IMPORTANT POINTS VISITED 279

• Thus, the result of taking the union of a context-free language anda regular language is either context-free or is regular, depending onwhether the regular language manages to destroy the delicate patternswe have set-up in the context-free language, or not.

It must also be clear that there are ℵ0 regular languages and the same numberof context-free languages, even though not all context-free languages areregular. This is similar to saying that not all natural numbers are primenumbers, and yet both have cardinality ℵ0.

Illustration 279.1 Consider ambmcm | m ≥ 0. This is not a CFL.Suppose it is a CFL. Let us derive a contradiction using the CFL PumpingLemma. According to this lemma, there exists a number n such that given astring w in this language such that |w| ≥ n, we can split w into w = uvwxysuch that |vx| > 0, |vwx| ≤ n, and for every i ≥ 0, uviwxiy ∈ L(G).

Select the string anbncn which is in this language. These are the cases tobe considered:

• v, w, and x fall exclusively in the region “a”• v, w, and x fall exclusively in the region “b”• v, w, and x fall exclusively in the region “c”• v and x fall in the region “a” and “b” respectively• v and x fall in the region “b” and “c” respectively

In all this case, “pumping” takes the string outside of the given language.Hence, the given language is not a CFL.

Illustration 279.2 We illustrate the CKY parsing algorithm on string aabbabwith respect to the following grammar:

S -> AB | BA | SS | AC | BD

A -> a B -> b C -> SB D -> SA

The basic idea is to subdivide the string into “regions” and apply dynamic-programming to “solve” all the shorter regions first, and use that informationto solve the “larger” regions.

The given string is aabbab

| a | a | b | b | a | b |

0 1 2 3 4 5 6

Let us build our table now. The region ‘‘01’’ is generated by the

set of non-terminals A. We just write ‘‘A’’ below. Likewise we write

the other non-terminals.

Page 288: Computation Engineering Applied Automata Theory and Logic

280CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

0

A 1

A 2

B 3

B 4

A 5

B 6

The next table is obtained as follows: There is NO non-terminal that

has a right-handside of the production as ‘‘AA’’. So we put a ‘‘phi’’ at ‘‘02’’.

Since S -> AB, and since ‘‘A’’ marks ‘‘12’’ and B marks ‘‘23’’, we put an ‘‘S’’

at ‘‘13’’, and so on. Last but not least, we write a ‘‘C’’ at ‘‘14’’ because

‘‘14’’ is understood as 12-24, or 13-34. 12-24 is ‘‘A phi’’, so forget it.

13-34 is ‘‘SB’’ and C -> SB, so write ‘‘C’’ there. Now fill the rest of the

table yourselves.

0

A 1

phi A 2

S B 3

C phi B 4

S A 5

S B 6

Page 289: Computation Engineering Applied Automata Theory and Logic

16.6. SOME IMPORTANT POINTS VISITED 281

The parsing is successful if, in position “06” you manage to write a set ofnon-terminals that contain “S”. Otherwise, the parsing fails.

Illustration 281.1 Prove that any context-free grammar over a singletonalphabet generates a regular language.

We provide a proof-sketch, leaving details to the reader. To solve thisproblem, we can actually use the Pumping Lemma for context-free languagesin an unusual way! The CFL pumping Lemma says that for a long w (longerthan some “k”), we can regard w = uvxyz such that uvixyiz ∈ L. Eachpump up via i increases the length by v+ y. However, since |vxy| ≤ k, thereare only a finite number of v + ys we can get. These are the periodicities(in the ultimate periodicity sense). If a set is described by a finite numberof periods p1, p2, . . ., it is easily described by the product of these periods.This was the argument illustrated in Section 14.2 when we tackled a regularlanguage Pumping Lemma problem, and chose 0n 1n+n! to be the initialstring. The n! we chose served as the product of all the values possible for|y|, in that problem.

For instance if a set S is such that

• it has strings of as in it, and

• S is infinite, and for a sufficiently large i,

– if ai ∈ S then ai+4 ∈ S as well as ai−4 ∈ S,

– if ai ∈ S then ai+7 ∈ S as well as ai−7 ∈ S,

then S is ultimately periodic with period 28.Thus, we conclude that any CFL over a singleton alphabet has its strings

obeying lengths that form an ultimately periodic set. This means that thelanguage is regular.

Page 290: Computation Engineering Applied Automata Theory and Logic

282CHAPTER 16. PUSH-DOWNAUTOMATAAND CONTEXT-FREE GRAMMARS

Page 291: Computation Engineering Applied Automata Theory and Logic

Chapter 17

Turing Machines

A Turing Machine (TM), M , is a machine with a finite-state control anda single infinitary read/write tape that can be repeatedly read as well aswritten. A TM is used to model how computer programs written in languagessuch as C execute. In previous chapters, we discussed how DFA can be usedto model finite-state C programs (C programs written with only a finitenumber of variables and no recursion). We also discussed how PDA can beused to model C programs with the only infinite-state component appearingin the form of a push-down stack. We also discussed that if a finite-statecontrol mechanism gains independent access to two stacks, it then becomes“all powerful” - meaning that anything computable now becomes computableon such a device. It is easy to visualize how two stacks correspond to a singleinfinite tape. The act of popping a stack and pushing the popped value intoanother stack is similar to writing something into a tape-cell and advancingalong the tape. This is also the manner in which random-access memoryworks. Thus, what we introduce as a formal computing device - a Turingmachine - is nothing but a computer stripped down to its bare essentials, andyet can compute anything that can be computed on any realistic computer.It is a device simple enough that easily understood mathematical proofs maybe written.

Formally, a TM M is a structure (Q,Σ,Γ, δ, q0, B, F ). The finite statecontrol of M ranges over the control states in Q, beginning at the initialcontrol state q0 ∈ Q. States F ⊆ Q of M are called final states, and are usedto define when a machine accepts a string. The input on which M operatesis initially presented on the input tape. It is a string over the input alphabetΣ. Once started, a TM may never halt; it may keep zig-zagging on the tape,

283

Page 292: Computation Engineering Applied Automata Theory and Logic

284 CHAPTER 17. TURING MACHINES

writing symbols all over, and running amock, much like many tricky programsdo in real-life. A TM cannot manufacture new symbols ad infinitum - so allthe symbols written by a TM on its tape do belong to a finite tape alphabet,Γ. Notice that Σ ⊆ Γ, since Γ represents what can ever appear on the tape,which therefore includes the initial input. In fact, Σ ⊂ Γ, since Γ includesthe blank symbol B that is not allowed within Σ. We assume that a TMbegins its operation scanning cell 0 of a doubly-infinite tape (meaning thatthere are tape cells numbered +x or −x for any x ∈ Nat). If the initial inputto the TM is the string ε, then the entire tape is filled with B. Otherwise,for every i ∈ length(w), w[i] is presented on tape-cell i, with all remainingtape cells containing B.

A TM may be deterministic or non-deterministic. The signature of δ fora deterministic Turing machine (DTM) is

δ : Q× Γ→ Q× Γ× L,R.

This signature captures the fact that a TM can be in a certain state q ∈ Qand be looking at something on the tape, a ∈ Γ. It can then write a

′on

the tape in lieu of a, move to a state q′, and move its head left (L), or

right (R), depending on whether δ(q, a) = 〈q′ , a′ , L〉 or δ(q, a) = 〈q′ , a′ , R〉,respectively. For an NDTM, δ(q, a) returns a set of next control states, tapesymbol replacements, and head-move directions. The signature of δ for ano-deterministic Turing machine (NDTM) is

δ : Q× Γ→ 2Q×Γ×L,R.

Think of a non-deterministic Turing machine as a C program where insteadof the standard if-then-else construct, we have an if/fi construct of thefollowing form:

if

:: condition_1 -> action_1

:: condition_2 -> action_2

:: ...

:: condition_n -> action_n

fi

The intended semantics is as follows. Each condition may be a Booleanexpression such as (x > 0). There may be more than one condition be-coming true at any given time. In that case, one of the conditions is non-deterministically chosen. The actions can be goto, assignment, while-loops,

Page 293: Computation Engineering Applied Automata Theory and Logic

17.1. ACCEPTANCE, HALTING, REJECTION 285

etc. With just this change to the C syntax, we have a class of non-deterministicC programs that are modeled using non-deterministic Turing machines.

Non-deterministic Turing machines may be regarded as “fictitious,” sincereal-world computers do not behave non-deterministically in the above man-ner1. They are, however, very powerful conceptual devices that play a fun-damental role in the study of complexity theory. We view non-deterministicchoices as a perfect guess device, and ask ourselves: what problems can weefficiently solve if we can make perfect guesses? It turns out that many prob-lems become extremely easy to solve (solvable in polynomial time) with theuse of non-determinism. However, a most surprising result that complexitytheory offers us is that even with perfect guesses, certain problems remainvery hard to solve! All this will be the subject of the next chapter. In thischapter, we will briefly discuss the computational power of NDTMs, andshow that they have the same computational power as DTMs. We now turnour attention back to DTMs.

17.1 Acceptance, Halting, Rejection

Given a Turing machine M = (Q,Σ,Γ, δ, q0, B, F ), the blank symbol B be-longs to Γ, but not Σ. The transition function δ is partial - it need not bedefined for all inputs and states. When not defined, the machine becomes“stuck” - and is said to halt. By convention, no moves are defined out of thecontrol states within F . Thus, when M reaches a state in F , it is said toaccept its input (or simply “accept”). As said earlier, a TM may loop for-ever, or get stuck in a state outside of F . When the latter outcome happens,the TM is said to reject its input (or simply “reject”). Thus, halting meansacceptance or rejection, and the third outcome is that of looping.

The notion of acceptance is defined in a rather interesting manner fora TM. If a TM finds itself in a state within F at some point during itsoperation, it is said to have accepted its input. For example, if a TM ignoresits input and moves straight from q0 to a state in F , it ends-up having alanguage Σ∗. An extreme case of this behavior is if q0 ∈ F , in which case,the TM is permanently stuck, and yet has language Σ∗. On the other hand,if a TM never accepts, its language is φ. This can be the result of the TMlooping on every input, or the TM getting stuck in a reject state for every

1Unless they have a circuit-board that is loose inside them, making erratic electricalcontact!

Page 294: Computation Engineering Applied Automata Theory and Logic

286 CHAPTER 17. TURING MACHINES

input. Finally, a TM that accepts any string within the set L1 and does notaccept (loops or rejects) strings outside L1 has L1 as its language.

In many books, a TM is defined to have a one-way infinite tape. Whensuch a TM reaches the left end of its tape and attempts to move further left,it is customary to think that the head remains in place2. We avoid sucharbitrariness and endow our TMs with a two-way infinite tape. Thus, whenour TM begins operation, its head is scanning the first character of the inputstring. The entire input string lies to the right of the head. The portion ofthe tape towards the left side of the head contains an infinite number of B(blank) symbols.

Notice that a TM with a finite tape can be simulated by placing left andright-end markers on the tape, and not defining any moves when the TMis scanning one of these end markers. If we place the end-markers a fixeddistance apart and allow the TM to perform a one-way scan of a separateinput stream, the resulting machine will be equivalent to a DFA. On theother hand, if we place the input string between the left and right-end markers(meaning that the spacing between these markers grows with the length of theinput string), then we will have a linear bounded automaton (LBA). Noticethat an LBA allows only the portion between the end-markers to be read orwritten. An LBA properly subsumes a PDA in power, and can be used torecognize languages such as Lww of Section 15.4.1. Unlike with other machinetypes, it is not known one way or the other whether non-deterministic LBAsand deterministic LBAs are equivalent in power or not.We now return to our discussion of TMs and illustrate various conceptscovered so far.

Illustration 286.1 Suppose we are given a Turing machineM = (Q,Σ,Γ, δ, q0, B, F ),where Q = q0, qa, Σ = 0, 1, Γ = 0, 1, B, F = qa, and δ is as below:

------|--------------------------------------

State | Input

------|--------------------------------------

| 0 1 B

|--------------------------------------

q0 | (q0,0,L) (q0,1,L) (q0,B,L)

------|--------------------------------------

This TM will not stop running - it will keep moving left. Even this iscalled “looping.” Its language is φ.

2One can imagine that the “belt that moves the head slips.”

Page 295: Computation Engineering Applied Automata Theory and Logic

17.1. ACCEPTANCE, HALTING, REJECTION 287

If δ is changed to the following:

------|--------------------------------------

State | Input

------|--------------------------------------

| 0 1 B

|--------------------------------------

q0 | (q_a,0,L) (q_a,1,L) (q_a,B,L)

------|--------------------------------------

We can see that the language of the TM is Σ∗. Notice that we can omit(q_a,B,L) from this δ function, and still have Σ∗ as the language of the TM.Last but not least, if we omit (q_a,1,L) as well as (q_a,B,L) from the δtable of this TM, its language is all the strings that begin with 0.

17.1.1 Instantaneous Descriptions

While capturing the ‘snapshot’ of a TM in operation, we need to recordwhat state the machine is in, the contents of the tape, and what the head isscanning. All these are elegantly captured using an instantaneous descriptioncontaining the tape contents, w, with a single state letter q placed somewherewithin. Specifically, the string lqr represents that the tape contents is lr, thefinite-state control is in state q, and the head is scanning r[0]. The initial IDis q0w, for initial input string w. We define `, the ‘step’ function that takesIDs to IDS, as follows:

l1par1 ` l1bqr1 if and only if δ(p, a) = (q, b, R). The TM changes thetape cell contents a to b and moves right one step, facing r1[0], the firstcharacter of r1.

Similarly, l1cpar1 ` l1qcbr1 if and only if δ(p, a) = (q, b, L). The TMchanges an a to a b and moves left one step to now face c, the characterthat was to the left of the tape cell prior to the move.

We define the language of a TM M using `∗:

L(M) = w | q0w `∗ lqfr, for qf ∈ F, and l, r ∈ Σ∗.

In other words, a TM that starts from the initial ID q0w and attains an IDcontaining qf (namely lqfr, for some l and r in Σ∗) ends up accepting w.

Page 296: Computation Engineering Applied Automata Theory and Logic

288 CHAPTER 17. TURING MACHINES

State a b c X Y Z B

q0 (q1,X,R) prob prob imposs (q_bcbad, imposs (qa,B,R)

-few-a -few-bc Y,R)

q1 (q1,a,R) (q2,Y,R) prob imposs (q1,Y,R) prob imposs

-few-b’s

q2 prob (q2,b,R) (q3,Z,L) imposs imposs (q2,Z,R) prob

-few-c’s

q3 (q3,a,L) (q3,b,L) (q3,c,L) (q0,X,R) (q3,Y,L) (q3,Z,L) imposs

q_bcbad imposs prob prob imposs (q_bcbad, (q_cbad, imposs

-few-a -few-c Y,R) Z,R)

q_cbad imposs imposs prob imposs imposs (q_cbad, (qa,B,R)

-few-b Z,R)

Figure 17.1: A TM for anbncn | n ≥ 0

17.1.2 An Example: A TM for anbncn

A TM to accept the language anbncn | n ≥ 0 is developed as follows(Figure 17.2 in my syntax and Figure ?? in the JFLAP syntax; in the JFLAPsyntax, we capture moves from the state in the column to the state in therow). We start in state q0 where we seek an a, changing it to a X when wesee one, at the same time entering state q1. We do not enter state q1 unlessan a has been changed to an X. In q0, if we encounter a b or a c, we have aproblem which I capture by “prob-few-a” or “prob-few-bc”. These entriesneed to be changed to something concrete that guarantees not to accept. Thesafest is to convert prob-few-a to simply (qR,b,R) where qR is a “reject”state specially introduced to indicate rejection.

Continuing along, in q0, encountering an X is impossible (why?). Like-wise, encountering a Z is impossible before we encounter a Y first, since wedecode the ε input case explicitly in the q0 state. If we encounter a Y, wedeclare that “seeing a b or a c is bad” – moving to state q bcbad to checkif this is happening. Similarly we have a state q cbad that does a check forwhether seeing a c is bad.

In state q1, similar logic applies as for state q0. We switch to state q2

Page 297: Computation Engineering Applied Automata Theory and Logic

17.1. ACCEPTANCE, HALTING, REJECTION 289

Machine type: TM

Start state: q0

Final States: q6

Transition function:

q0 q1 q2 q3 q4 q5 q6

-----------------------------------------------------------------------

q0 | - | a; X, R | - | - | Y; Y, R | - | B; B, R |

|----------------------------------------------------------------------

q1 | - | a; a, R | b; Y, R | - | - | - | - |

| - | Y; Y, R | - | - | - | - | - |

|----------------------------------------------------------------------

q2 | - | - | Z; Z, R | c; Z, L | - | - | - |

| - | - | b; b, R | - | - | - | - |

|----------------------------------------------------------------------

q3 | X; X, R | - | - | b; b, L | - | - | - |

| - | - | - | Y; Y, L | - | - | - |

| - | - | - | a; a, L | - | - | - |

| - | - | - | Z; Z, L | - | - | - |

| - | - | - | c; c, L | - | - | - |

|----------------------------------------------------------------------

q4 | - | - | - | - | Y; Y, R | Z; Z, R | - |

|----------------------------------------------------------------------

q5 | - | - | - | - | - | Z; Z, R | B; B, R |

|----------------------------------------------------------------------

q6 | - | - | - | - | - | - | - |

-----------------------------------------------------------------------

Figure 17.2: A TM for anbncn | n ≥ 0

Page 298: Computation Engineering Applied Automata Theory and Logic

290 CHAPTER 17. TURING MACHINES

only when a b has been changed to a Y.

17.2 Church’s thesis

It has been shown, beyond any shadow of doubt, that a TM is powerfulenough to implement any computational algorithm. Numerous other formaldevices have been proposed to date to capture the notion of computation andalgorithms. Some of these devices are the Lambda calculus, Thue’ systems,combinatory logic, and unrestricted grammars. All these devices have beenshown to be equivalent in computational power. This led Alonzo Church, afamous logician, mathematician, and computational theorist to proclaim thatthese devices capture what we truly can compute using machines. Naturally,Church’s thesis is just that - a strong belief - that cannot be proved in anyformal sense; it simply serves as a formal proclamation by a famous scientistof a belief that is widely shared across the computer science community.

17.3 Extensions to the basic TM

Several extensions to TMs are possible:

• A finite amount of extra storage within the finite-control can be ar-ranged.

• Multiple tracks (essentially a new view of the tape alphabet) can beprovided on the tape.

• Subroutines may be used.

• Multiple tapes may be provided.

• Nondeterminism may be allowed.

None of the above measures increase the inherent power of Turing machines.Basically, we can simulate all these varieties of TMs on a standard TM, andvice versa. We will detail these simulation arguments in the next chapter.

Page 299: Computation Engineering Applied Automata Theory and Logic

Chapter 18

Undecidability

Two deeply related questions are addressed in this chapter:

• Do algorithms exist for all problems representable and runnable1on acomputer?

• Do efficient algorithms exist for all problems representable and runnableon a computer?

Recall that an ‘algorithm’ means a method to solve a problem that haltson all inputs (does not go into an infinite loop). If an algorithm does notexist, then we would like to know whether a semi algorithm or a procedureexists. Here are some clarifying examples before we get too far along:

• There is no algorithm for showing that two CFGs are equivalent.

• There is a “semi-algorithm” to show that two CFGs are different. Asemi-algorithm is one that terminates for certain inputs, but loops forothers. Here is that semi-algorithm:

Keep generating all strings in enumeration order and parsethem using both grammars; report any difference in outcome(one grammar accepts, the other does not). If the grammarsare different, we will know that in finite time with respectto some string. If the grammars are the same, we will neverknow according to this algorithm, as the procedure keepsexamining all the ℵ0 strings in enumeration order.

1We use the rather convoluted phrase “representable and runnable” as opposed to“solvable” because ‘solvable’ in traditional literature means an algorithm exists.

291

Page 300: Computation Engineering Applied Automata Theory and Logic

292 CHAPTER 18. UNDECIDABILITY

• In the same vein, there is a semi-algorithm to tell whether a given CFGis ambiguous: if it is ambiguous, then there exists a string that causestwo distinct parse trees to be generated. However, if the grammaris unambiguous, then we will never know that fact according to thisalgorithm, as it keeps examining an infinite number of strings, findingthat all of them have a unique parse tree.

Note the emphasis on “this” in the above examples. What we want to pointout is that we chose utterly naive algorithms. Could we have designed anybetter algorithms? Well, using the model of Turing machines, we can provethat no algorithm exists at all! This is the real beauty of the theory you aregoing to get a taste for2.

18.1 The basic questions of computability and

complexity

To sum up the above discussions, the two basic questions of computabilityand complexity are “do algorithms exist?” and “do efficient algorithms ex-ist?” In a sense, these questions are related. If an algorithm does not exist,it means that for some problem instances, looping is unavoidable3. Insteadof taking a polynomial time and halting or an exponential time and halting,a program that goes into a loop takes an “infinite amount of time before ithalts.” An example of a “problem” is “the parsing problem for CFGs,” anda “problem instance” in this problem space would be a pair 〈G,w〉 where Gis a specific CFG, and w is a specific input string.

If an algorithm does exist for a problem, it may still be an exponentialtime algorithm4. Note that we use “time” as one example - we could equallywell be measuring space needs, power needs, etc, in complexity theory. Allour remaining discussions pertain only to time complexity.

2Unfortunately, we will need to devote several chapters to properly explain these ideas.3Note that looping is an extreme manifestation of time complexity!4Exponential generally means O(kn) for some constant k. In complexity theory, how-

ever, we speak a bit loosely - so, even higher complexity functions such as n! or nnn

areregarded “exponential.”

Page 301: Computation Engineering Applied Automata Theory and Logic

18.1. THE BASIC QUESTIONS OF COMPUTABILITY AND COMPLEXITY293

18.1.1 Do algorithms always exist?

It should raise no eyebrows if we were to claim that Deterministic Turingmachines (DTMs) are universal - meaning that given any computer programin a language such as C, we can design a DTM to provide the same function-ality as that program. Since a TM is a fictitious device, we can ascribe anynumeric value we like for the amount of time a DTM takes to decode a cell,change its contents, and move to the next tape cell. However, since TMs aresupposed to model real computers, we must ensure that they do take somenon-zero time to do any of these activities. In other words, a TM modelwhere the TM takes zero time to do something is clearly broken - it doesnot model anything in the real world, nor can be used as the basis to predictanything about the real world. Physics tells us that there is simply no wayto build a device that effects a state-change in zero time. Computer designcannot ignore Physics. However, given the luxury of setting the “speed” ofa DTM to anything we want, a DTM that takes an Atto Atto Atto5secondto carry out all the above steps will be faster than any real computer!

The points we are making are the following:

• There is no need to “put down” a TM saying it is an accurate but slowmodel - it can be made arbitrarily fast!

• On a specific problem instance, if a real computer would go into an infi-nite loop and never finish a computation, a DTM designed to simulatethe real computer would do so too.

• If a real computer would terminate with an answer on a problem in-stance in finite time, the DTM designed to simulate the real computerwould do so too.

To sum up, DTMs are simple devices that can be used to predict the behaviorof any real computer.

Note that so far we distinguished between “the possibility of looping”versus “halting,” or equivalently “semi-algorithms” versus “algorithms,” orequivalently “infinite time” versus “finite time.” Subdividing time into sucha coarse time-scale allows us to make some very high level (and yet funda-mental) observations about computations, without getting mired in detail. Ifwe can prove that a DTM designed for a certain problem cannot avoid goinginto an infinite loop, a similar result would have been shown to apply to all

5One Atto second is 10−18 seconds, so three Attos in a row means 10−54 seconds.

Page 302: Computation Engineering Applied Automata Theory and Logic

294 CHAPTER 18. UNDECIDABILITY

real computers and real computer programs. On the other hand, if we provethat a DTM would always halt, no matter what input, then not only do weknow that a C program exists, but we can then seek an efficient algorithm.Later, in our brief study of complexity in sections 18.2.1 and 18.2.1, we wouldbe subdividing “finite time” (the space of algorithms) into

• exponential time,

• polynomial time,

• perhaps something lower than polynomial time (which is usually notvery interesting for complexity theorists who primarily grapple withproblems hovering near exponential complexity,

• and last but not least, either polynomial or exponential time.

The last - a rather unusual complexity class - either polynomial or exponentialtime - corresponds to the so called “NP-hard” problems or “NP complete”problems (the exact details will be presented later in this chapter). Nobodyhas formally proved, one way or the other, what the true complexity is.We now return to more basic questions before we study the theory of NP-completeness.

18.1.2 Do efficient algorithms always exist?

For a very large number of problems, scientists have been able to discoverefficient, and even provably optimal algorithms. However, for many thou-sands of highly important problems, scientists have struggled, over severaldecades, to find better than exponential-time algorithms. One importantclass of problems in this area is the so called “NP-hard” problems. Theseproblems include everyday problems; for instance:

• How can a ship be most efficiently packed with cargo crates?

• How can Delta airlines schedule its planes most efficiently?

• Is a given Boolean formula satisfiable?

• Is a given graph 3-colorable? (This arises in code generation in com-pilers.)

• How do we optimally place cops at city-block intersections such that thefewest number of cops can watch over the maximal number of roads?

Page 303: Computation Engineering Applied Automata Theory and Logic

18.2. NON-DETERMINISTIC TURING MACHINES (NDTM) 295

It is striking that such a variety of problems have defied conquest usingpolynomial time algorithms for nearly four decades now.

Even though scientists have not been able to solve this open question,they have achieved a major break-through: they have come up with a strongrelationship between thousands of very specific NP-hard problems such thatfinding a polynomial algorithm for even one of these problems would immedi-ately give a polynomial algorithm for all of them! These related equivalenceclass of problems are called NP-complete problems6. In fact, the above listedproblems are not only NP-hard, but also NP-complete. Simply put, NP-complete means:

• NP-hard

• The time complexity is in NP. This simply means that there is a non-deterministic polynomial-time algorithm. In other words, there is anNDTM that, after the “guessing” phase (which is free - to be discussedbelow) checks the guess in polynomial time.

We will, in a future revision of this book, examine how problems are shownto be NP-complete.

To set the stage for the above discussions, we now introduce to you thenotion of a non-deterministic Turing machine (NDTM). This powerful con-ceptual device underlies many of our discussions pertaining to complexity -and provide a powerful framework for relating various problems. The restof this chapter is organized as follows. We first introduce NDTMs using aconcrete example. Then we devote the rest of the chapter to a gentle intro-duction to the above issues7.

18.2 Non-Deterministic Turing Machines (NDTM)

A non-deterministic Turing machine was briefly introduced in the last chapteras a machine that can have multiple choices available to it at every step.

6Refer back to Chapters 1 and 2 where we introduce these and most other ideas beingdiscussed here - we said “solving one implies solving all” to discuss the NP-completenesstechnique.

7Under normal circumstances, I would simply not have written a chapter packed withso much material. However, since I’m offered only one lecture (essentially) to cover newmaterial, this chapter is a tailor-made “wind down” chapter for the Spring 2002.

Page 304: Computation Engineering Applied Automata Theory and Logic

296 CHAPTER 18. UNDECIDABILITY

Machine type: TM

Start state: q0

Final States: q6

Transition function:

q0 q1 q2 q3 q4 q5 q6 q7 q8 q9 q10

---------------------------------------------------------------------------------------------------------------

q0 | - | 0; X, R | - | - | - | #; #, R | - | 1; X, R | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q1 | - | 1; 1, R | #; #, R | - | - | - | - | - | - | - | - |

| - | 0; 0, R | - | - | - | - | - | - | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q2 | - | - | X; X, R | 0; X, L | - | - | - | - | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q3 | - | - | - | X; X, L | #; #, L | - | - | - | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q4 | X; X, R | - | - | - | 1; 1, L | - | - | - | - | - | - |

| - | - | - | - | 0; 0, L | - | - | - | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q5 | - | - | - | - | - | X; X, R | B; B, R | - | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q6 | - | - | - | - | - | - | - | - | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q7 | - | - | - | - | - | - | - | 0; 0, R | #; #, R | - | - |

| - | - | - | - | - | - | - | 1; 1, R | - | - | - |

|--------------------------------------------------------------------------------------------------------------

q8 | - | - | - | - | - | - | - | - | X; X, R | 1; X, L | - |

|--------------------------------------------------------------------------------------------------------------

q9 | - | - | - | - | - | - | - | - | - | X; X, L | #; #, L |

|--------------------------------------------------------------------------------------------------------------

q10 | X; X, R | - | - | - | - | - | - | - | - | - | 0; 0, L |

| - | - | - | - | - | - | - | - | - | - | 1; 1, L |

---------------------------------------------------------------------------------------------------------------

Figure 18.1: A TM for w#w | w ∈ 0, 1∗

Page 305: Computation Engineering Applied Automata Theory and Logic

18.2. NON-DETERMINISTIC TURING MACHINES (NDTM) 297

Machine type: TM

Start state: q0

Final States: q5 q26

Transition function:

q0 q5 q12 q13 q14 q15 q16 q17 q18 q19 q22 q23 q25 q26 q27

--------------------------------------------------------------------------------------------------------------------------------------------------

q0 | - | B; B, R | - | - | - | - | - | - | - | - | - | - | - | - | 1; 1, S |

| - | - | - | - | - | - | - | - | - | - | - | - | - | - | 0; 0, S |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q5 | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q12 | - | - | 1; 1, L | B; B, R | - | - | - | - | - | - | - | - | - | - | - |

| - | - | 0; 0, L | - | - | - | - | - | - | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q13 | - | - | - | - | 0; P, R | 1; Q, R | X; X, R | Y; Y, R | - | - | - | - | 2; 2, R | - | - |

| - | - | - | - | - | - | - | - | - | - | - | - | 3; 3, R | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q14 | - | - | - | - | 0; 0, R | - | - | - | - | - | X; 2, R | - | - | - | - |

| - | - | - | - | 1; 1, R | - | - | - | - | - | - | - | - | - | - |

| - | - | - | - | 3; 3, R | - | - | - | - | - | - | - | - | - | - |

| - | - | - | - | 2; 2, R | - | - | - | - | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q15 | - | - | - | - | - | 1; 1, R | - | - | - | - | - | Y; 3, R | - | - | - |

| - | - | - | - | - | 0; 0, R | - | - | - | - | - | - | - | - | - |

| - | - | - | - | - | 3; 3, R | - | - | - | - | - | - | - | - | - |

| - | - | - | - | - | 2; 2, R | - | - | - | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q16 | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q17 | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q18 | - | - | - | P; P, R | - | - | - | - | 1; 1, L | - | - | - | - | - | - |

| - | - | - | - | - | - | - | - | 0; 0, L | - | - | - | - | - | - |

| - | - | - | - | - | - | - | - | 2; 2, L | - | - | - | - | - | - |

| - | - | - | - | - | - | - | - | 3; 3, L | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q19 | - | - | - | Q; Q, R | - | - | - | - | - | 1; 1, L | - | - | - | - | - |

| - | - | - | - | - | - | - | - | - | 0; 0, L | - | - | - | - | - |

| - | - | - | - | - | - | - | - | - | 2; 2, L | - | - | - | - | - |

| - | - | - | - | - | - | - | - | - | 3; 3, L | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q22 | - | - | - | - | - | - | - | - | 1; Y, L | - | - | - | - | - | - |

| - | - | - | - | - | - | - | - | 0; X, L | - | - | - | - | - | - |

| - | - | - | - | - | - | - | - | B; B, L | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q23 | - | - | - | - | - | - | - | - | - | 1; Y, L | - | - | - | - | - |

| - | - | - | - | - | - | - | - | - | B; B, L | - | - | - | - | - |

| - | - | - | - | - | - | - | - | - | 0; X, L | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q25 | - | - | - | - | - | - | - | - | - | - | - | - | 3; 3, R | B; B, R | - |

| - | - | - | - | - | - | - | - | - | - | - | - | 2; 2, R | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q26 | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |

|-------------------------------------------------------------------------------------------------------------------------------------------------

q27 | - | - | 1; Y, L | - | - | - | - | - | - | - | - | - | - | - | 1; 1, R |

| - | - | 0; X, L | - | - | - | - | - | - | - | - | - | - | - | 0; 0, R |

--------------------------------------------------------------------------------------------------------------------------------------------------

Figure 18.2: A TM for ww | w ∈ 0, 1∗

Page 306: Computation Engineering Applied Automata Theory and Logic

298 CHAPTER 18. UNDECIDABILITY

Often we refer to non-deterministic moves as “taking a guess.” We nowprovide two examples - a DTM and an NDTM - for two related languages:

Lw#w = w#w | w ∈ 0, 1∗.

Lww = ww | w ∈ 0, 1∗.The (D)TM for Lw#w basically compares across the # mark, sweeping

back and forth. It starts at state q0, branching along two identical pathsdepending on whether it sees a 0 or a 1. Each corresponding symbol is X-edout. When the TM has finally caught-up to the # mark, it checks to see thatthe portion to the right of # does not contain any surviving 0s or 1s. It thenaccepts by going to q6. Notice that all omitted decodings (for example, thereis no decoding provided in state q5 for a 0 or a 1) causes the machine to getstuck at q5, which is tantamount to rejecting the input string.

The (ND)TM for Lw#w begins its execution in state q0. Suppose the usermeant to provide our TM the string εε. They would do so by writing nothingon the tape - the tape would simply contain an infinite “sea” of blanks, B.In this case, the machine should accept - as evidenced by the move from q0

to q5. Now consider the other case - that the tape has a string of 0s and1s. The machine executes the move from q0 to q27 where the head stays thesame8(the “S” option.) What it does in state q27 is this: it takes a “guess.”You can see that in the conflicting non-deterministic moves out of state q27:on one hand, we could be happily moving over 0 and 1 (restoring them inthe process, and moving right, as evidenced by the self-loop at q27). At anymoment, however, we could exercise the option of changing the 0 to an X andmoving left, or changing a 1 to a Y and moving left, and going to state q12.This corresponds to guessing the midpoint. In state q12, the TM attains theleft-end of the tape, and then swings back to face the left-end of the tape(the q12 to q13 move). Now, from state q13, there are two symmetric pathsthe machine can take. (Please note: All TM drawings must be drawn withas much symmetry captured in the drawing as possible, as it can then makethe machines extremely easy to study and understand.) Let us discuss thesepaths briefly.

The left-hand transition is triggered by seeing a 0 as the left-most char-acter. The quest of the machine is now to discover an X somewhere along the

8We don’t need the “S” option, strictly speaking. An “S” can always be simulated byan “L” followed by an “R”.

Page 307: Computation Engineering Applied Automata Theory and Logic

18.2. NON-DETERMINISTIC TURING MACHINES (NDTM) 299

tape. Till it sees this X, the TM keeps moving right. Before it embarks onthis quest, it has work to do: it changes the triggering 0 to a P (to rememberthat that’s the spot to return to upon the next swing), and then moves tostate q14.

Notice that while the TM is at state q14, it skips over anything but an X.Now, when an X is spotted in state q14, the machine:

• Changes the X to a 2,

• Moves right one step, and

• Changes the next character as follows: if the next character is a 0, itgets turned into an X; on the other hand, if the next character is a 1,it gets turned into a Y. These X and Y act as “sign-posts” for the nextmatching step. Finally, If the next character is a B, then we have runpast the right half, and so the machine has to merely hover back andcheck that it has run past the left half also.

• The self-loop at state q18 ensures that the machine moves back all theway.

Finally, in state q18, we spot a P or a Q, the TM knows that it is time tobegin another journey.

Now let’s look at the exit condition at state q13. When finally, themachine spots a 2 or a 3 labeling the transition out of q13, it knows that ithas finished processing all the 0s and 1s that were to the left of the guessedmidpoint. This is because:

• The machine changes every 0 or 1 to the left of the guessed midpointto a P or Q, respectively.

• The machine changes every 0 or a 1 to the right of the gussed midpointto a 2 or 3, respectively.

• Thus, spotting a 2 or 3 at state q13 tells the machine “OK, now youare encroaching into the guessed right-half.”

The final act, upon reaching state q25 is to await a B - that means the guesswas right!

Two quizzical states q16 and q17 also appear in the diagram. Theseare my way of making it explicit that the TM is stymied if it sees an X

or a Y while in state q13. The reason for this is that we would then haveguessed the midpoint to be the very first symbol of a string of 0’s and 1’s.

Page 308: Computation Engineering Applied Automata Theory and Logic

300 CHAPTER 18. UNDECIDABILITY

Such a guess cannot succeed. Nothing is gained by introducing q16 andq17, except perhaps clarity - that seeing an X or a Y in q13 is going tolead to rejection. While I don’t universally recommend that you explicitlyintroduce such “stub” states, often it adds to clarity, as I claim it does inthis illustration.

18.2.1 Differences between NDTMs and DTMs

Figure 18.2 is a printout of the representation of an NDTM inside the Jflaptool. Jflap runs on ordinary (deterministic) computers. From this it must beclear that an NDTM can be simulated on a DTM. The simulation of a DTMon top of an NDTM is trivial. These observations show, at a “gut level”,that NDTMs are no more powerful than DTMs. To make this argument moreprecise, all we need to do is come up with a formal simulation algorithm tosimulate NDTMs on DTMs. (We will provide this construction in a lateredition of this chapter. Basically, the non-deterministic options are kept onthe tape, and the DTM simulates the tokens in a round-robin fashion.)

If NDTMs can be simulated on top of DTMs, then why is the notion ofNDTMs important? The answer is simple: many problems become compu-tationally easier to solve with the availability of non-determinism and “freeguesses.” In other words, the overall time cost would go down with a “guessand check” approach IF the guesses come with zero cost - essentially, there-fore, the only cost involved is in verifying a guess. We are simply making-upa method to measure costs - so indeed we can set the guessing costs to zero.We then see where this cost measure takes us. Interestingly, this “goofysounding” exercise reveals several neat insights, as we will discuss shortly. Inthe next two sections, we discuss a few problems that become easier to solvewith non-determinism, and a few that, quite stubbornly, seem to remain diffi-cult even with non-determinism. The phrase “with non-determinism” meansthe following:

There are certain problems where, with a single perfect guess, wecan answer a “yes/no” question with certainty. In those cases,we will call upon the guess generator to give us a single perfectguess - free of cost. There are, however, certain problems wherewe cannot answer a “yes/no” question with a single guess - evenwith the world’s most perfect single guess. So there we may callupon the guess generator to generate all possible guesses. The

Page 309: Computation Engineering Applied Automata Theory and Logic

18.2. NON-DETERMINISTIC TURING MACHINES (NDTM) 301

guess generator will, of course, happily oblige, supplying all theguesses free of cost. Unfortunately, the guess checker will end uppaying the price for checking all these guesses.

Problems that are easier to solve with non-determinism

Let us consider the notion of “solving with non-determinism” in more depth.Consider the problem of factoring large natural numbers. More specifically,here is the “yes/no” question:

Yes/no: the given natural number is composite.

According to the best known algorithms in existence today, we need timethat grows exponentially in the number of bits in the number to be factored.However, if the right factors can be guessed, checking that the factors arecorrect is easy: simply multiply the purported factors together and check theanswer. Thus, with a perfect guess generator that costs zero time, we canobtain the factors, and then simply check the answer. Checking costs onlypolynomial time. Then, the time complexity is defined as follows:

If a perfect guess generator gives a guess and the further checkingalone costs only polynomial time, then we say that the problemcan be solved in NP time, or “NP” or “non-deterministic poly-nomial time.”

Another example is the following. Suppose we walk into a large roomfilled with thousands of people, and answer the following question:

“Yes/no: In this room, there are k people who know each other(they form a k-clique).”

This problem takes exponential time according to today’s best-known meth-ods based on DTMs (or deterministic algorithms). However, if someonedevice can generate a perfect guess, then just by verifying that single perfectguess, we can answer the above question conclusively as a “Yes” or as a “No.”It is in complexity class NP, once again!

As a third example, consider the problem of checking whether a givenBoolean formula is satisfiable.

“Yes/no: the given formula is satisfiable.”

Page 310: Computation Engineering Applied Automata Theory and Logic

302 CHAPTER 18. UNDECIDABILITY

Given a perfect guess of the Boolean assignment to the variables, we cancheck whether the guess satisfies the formula within polynomial time. As afourth example, consider the task of detecting whether, in a directed graph,there is a Hamiltonian cycle.

“Yes/no: the given graph contains a Hamiltonian cycle.”

What is a Hamiltonian cycle? Suppose we are given a directed graph 〈V,E〉where V are the vertices and E ⊆ V ×V are the edges. A Hamiltonian cycleis a permutation p0p1 . . . pn−1 of the nodes of the graph, pi ∈ V for i ∈ |V |,such that 〈pi, p(i+1)mod|V |〉 ∈ E for i ∈ |V |. So, for a graph with three nodesV = A,B,C and edges

E = 〈A,B〉, 〈B,A〉, 〈A,C〉, 〈C,A〉

there is no such permutation, while for a graph with V = A,B,C,D and

E = 〈A,B〉, 〈B,A〉, 〈A,C〉, 〈C,A〉, 〈B,D〉, 〈D,C〉

the permutation ABDC is a Hamiltonian path. Now, given a perfect guessfor a permutation, the checking phase takes only polynomial time.

Two more examples that help clarify our uses of the words “Ptime” and“NPtime”:

• our DTM in Figure 18.1 implements a P-time algorithm - it is deter-ministic, and it takes a polynomial amount of time to answer whetherthe given string is of the form w#w.

• The NDTM in Figure 18.2 on the other hand represents an NP-timealgorithm.

Problems that remain difficult to solve despite non-determinism

Now let us consider the earlier-studied three problems once again. Considerthe complement of the problem of factoring large natural numbers.

“Yes/no: The given natural number is prime.

Prof. Vaugh Pratt of Stanford has proved that primality testing can be doneusing the guess-and-check method in polynomial time. In other words it is

Page 311: Computation Engineering Applied Automata Theory and Logic

18.2. NON-DETERMINISTIC TURING MACHINES (NDTM) 303

in NP. Thus, the composites number problem, and its dual (or complement),namely primality testing, are both in NP-time9.

Now take the complement of the second problem we discussed: we walkinto a large room filled with thousands of people and we have to answer thefollowing question:

“Yes/no: In this room, there aren’t some k people who know eachother.”

This is a problem that cannot be solved with a single guess. Basically, itappears that all the guesses must be examined, and none of the guessesmust reveal a k-clique. Basically, we have turned an assertion of the form“True/false: there exists a k-clique” to “True/false: there doesn’t exist a k-clique,” or equivalently “True/false: For all subsets of k of nodes, that subsetis not a k-clique.” The “forall” question forces all the evidence - in the formof guesses - to be examined. All the cases seem to be necessary to examine.Nobody knows whether this can be avoided. So it is not known whether thisproblem is in NP or not. It appears not to be in NP. Thus, non-determinismdoes not appear to help here. By the way, the “evidence” or “guesses” arecalled a certificate.

Now take the complement of the third problem we discussed:

“Yes or no: the given formula is unsatisfiable.”

This problem also appears to demand all possible assignments. It is notknown to be in NP. Thus, non-determinism does not appear to help.

Finally, take the complement of the fourth example we considered:

“Yes/no: the given graph does not contain a Hamiltonian cycle.”

Again, non-determinism does not seem to help.

P versus NP

It is clear that any problem in P is also in NP, since P is a special case ofNP. However, there are problems - for example, the k-Clique, Boolean Satis-fiability, and Hamiltonian Cycle - for which no algorithm in P is known, and

9A celebrated theorem says that if a problem and its complement are both in NP-time,then it is highly unlikely (but still not shown conclusively) that the problems are NP-complete. In summary, we observe that non-determinism helps even for the complementof the composite numbers problem. This does not appear to be the case for the otherthree problems we considered - as we now discuss.

Page 312: Computation Engineering Applied Automata Theory and Logic

304 CHAPTER 18. UNDECIDABILITY

yet all of these problems are in NP. Through their study of NP-completeness,scientists are on a quest to answer whether these problems are indeed alsoin P. For problems that don’t seem to be in NP (the complement problemsabove), scientists have created a complexity class called Co-NP. The study ofNP-completeness does involve the study of Co-NP problems also. We shallreturn to these issues in Section 18.4.

18.3 Computability and Undecidability

We now discuss four specific questions and provide answers. These relate tothe undecidability of the Halting problem and how diagonalization is usedfor that.

18.3.1 Halting is undecidable

First we introduce some terminology. A Turing recognizable language P isa language such that for some Turing machine M , P = L(M), i.e, P is M ’slanguage. Thus,

• A TMM1 that is designed to check its tape and halt in the accept stateif it sees a 1 and goes into an infinite loop if it sees any other numberhas language 1.

• A TMM2 that is designed to check its tape and halt in the accept stateif it sees a 1 and halts in the reject state if it sees any other numberalso has language 1.

In the above example, M1 is called a recognizer for the language 1, and M2

is called a decider for the same language. A recognizer may loop on someinput - a decider always halts, no matter what input. Since 1 has at leastone decider, it is a decidable language. In Figures 18.1 and 18.2, we gave twodeciders for the languages Lw#w and Lww, respectively.

Coming back to the question of whether halting is decidable, let us modelthe Halting problem as a language (refer to Chapter 1 for an explanation ofhow problems are modeled using languages). Is the language

L〈M,w〉 = 〈M,w〉 | M is the binary encoding of a TM that halts on input w

a decidable language? (Here, “M is a TM” means “M is the binary encodingof a TM - quite similar to an a.out or program.exe file.) We will show via

Page 313: Computation Engineering Applied Automata Theory and Logic

18.3. COMPUTABILITY AND UNDECIDABILITY 305

proof by contradiction that this language is not decidable. We will use thenotations “TMx” to refer to the TM represented by the binary code x.

Suppose L〈M,w〉 is decidable. We will then have a Turing machine TMh

with binary code h that accepts two inputs: the binary code M of a TM,and an input string w, and simulates TMM running on w. (This is verymuch like a programming language interpreter interpreting a programs ona certain input.) Essentially TMM will be run on input w by TMh. TMh

halts with 1 (representing ‘true’) on the tape if it finds that TMM halts oninput w, and TMh halts with 0 (representing ‘false’) on the tape if it findsthat TMM does not halt on input w. Since the act of running TMh on〈M,w〉 produces a result, we model it using the mathematical function fh(the “halting function”).

• Now define a new TM called Md, with binary code d, as follows:

procedure TM_d(x)

if f_h(x,x)

then loop

else accept

endif

end M_d

In other words, TMd runs the decider fh on x and x. (fh, in turn,simulates the machine TMx running on input x.) If fh returns ‘true,’TMd deliberately goes into a loop. Otherwise, it jumps to the acceptstate and halts.

• Now, let us examine the truth value of fh(d, d). In other words, whatwill happen if TMh were to simulate TMd running on d.

− Suppose fh(d, d) returns true.

− By the definition of fh, this means TMd halts on input d.

− Looking at the code for TMd, we see that this happens when TMd

reaches its “accept” label.

− However, this means that fh(d, d) returns false!

− Similarly, suppose fh(d, d) returns false.

− By the definition of fh, this means TMd does not halt on input d.

− Looking at the code for TMd, we see that this happens when TMd

reaches its “loop” label.

Page 314: Computation Engineering Applied Automata Theory and Logic

306 CHAPTER 18. UNDECIDABILITY

− However, this means that fh(d, d) returns true!

• Thus, the function fh - or equivalently the decider TMh - cannot exist.

Acceptance cannot be decided - diagonalization

In the same vein, we can show that acceptance - whether a TM accepts acertain input - is undecidable. Suppose not. Then there exists a deciderfa : TM × input → bool such that fa(x, y) iff TMx accepts y. Then also asimilar machine TMd can be defined.

procedure TM_d(x)

if f_a(x,x)

then reject

else accept

endif

end M_d

Now we can determine the truth-value of fa(d, d).Suppose fa(d, d) returns true. Then TMd accepts d. But, this happens

when fa(d, d) returns false. Suppose fa(d, d) returns false. Then TMd doesnot accept d. But, this happens when fa(d, d) returns true. Again, thecontradiction is complete.

Halting cannot be decided - reduction from Acceptance

If we have a decider for halting, fh, we can build a decider for acceptance,fa. But suppose we have independently shown that fa does not exist. Thenfh cannot also exist. This is a reduction based proof. For technical reasons,we say that the “old, already shown impossible problem, fa, is being reducedto the new problem fh - not the other way around.”

If fh exists, we can build fa as follows:

function f_a(x,y)

if f_h(x,y)

then run TM_x on y ;

if TM_x accepts y

then accept

else reject

endif

else reject

Page 315: Computation Engineering Applied Automata Theory and Logic

18.4. COMPLEXITY 307

endif

end f_a

Thus, reduction proofs are labor-saving devices. Once we show that accep-tance is undecidable, then we don’t need a proof by contradiction to showthat halting is undecidable - we can use reduction as above.

Acceptance cannot be decided - reduction from Halting

Lastly, ff fa exists, we can build fh thus:

function f_h(x,y)

Make a TM, TM_x’ which has its accept and reject labels swapped

with respect to TM_x.

if f_a(x,y) or f_a(x’,y)

then accept

else reject

end f_h

This proof “works” as follows. If fa exists, we check whether TMx acceptsy or TM ′

x accepts y. Now, TM ′x accepts y is equivalent to TMx rejecting y.

Thus, ‘TMx accepts y or TMx rejects y’ is equivalent to TMx halts on y.That is precisely what the reduction mechanism does. We now provide abrief glimpse of what the theory of NP-completeness looks like.

18.4 Complexity

In our discussions so far, we hinted at how NP-complete problems are defined.Here are a few additional details. We provide a narration of the highlights ofthe theory of NP-completeness. Let us examine a few motivating examplesand then proceed to examine the highlights of the theory of NP-completeness.Problem 1: Hamiltonian Cycle A salesperson has to start from some cityc, visit N-1 other cities, and return to c. The salesperson must visit themin some order without visiting any city twice. It costs a certain amount(completely arbitrary but known ahead of time and constant) travel costgoing city to city. What route would the salesperson take to minimize his/heroverall travel cost? This problem is otherwise known as one of determiningthe minimum cost Hamiltonian cycle. A related (and, surprisingly, equallyhard) problem is whether there is a Hamiltonian cycle in a graph.

Page 316: Computation Engineering Applied Automata Theory and Logic

308 CHAPTER 18. UNDECIDABILITY

Problem 2: 3-coloring of Graphs One is given an arbitrary undirectedgraph G. One has to color the nodes of the graph in such a way that adjacentnodes do not get assigned the same color. Can we color a given graph withonly 3 colors? (In other words, is the given graph G “3-colorable?”)

These two problems (and hundreds of “similar” others) have no knownpolynomial algorithms. All known algorithms are exponential. There arethree possible explanations:

1. Today’s CS researchers are not smart enough to figure out polynomialalgorithms for these problems;

2. it is mathematically impossible to have polynomial time algorithms forthese problems; or

3. not only are today’s researchers unable to figure out polynomial algo-rithms for these problems, but also all past CS researchers and (as isanticipated) future researchers.

No one yet knows don’t know which of these explanations is true! However,researchers have determined that these problems are not isolated! In fact,they are very strongly related to each other. Here is the amazing fact:

If one discovers a polynomial-time algorithm for any one of theseproblems, then all problems (hundreds) in this class will havepolynomial-time algorithms. This class of problems are calledNP-complete.

18.4.1 The Theory of NP-completeness

For functions f and g,f(n) = O(g(n))

if there exist c and n0, both natural numbers, such that for every n ≥ n0,f(n) ≤ cg(n). For example,

n2 = O(n3)

and alson2 = O(n2)

Note: We don’t have to say that f(n) is at-most O(t(n)) ever; the O notationcaptures the “at most”, and so simply say f(n) is O(t(n)). All inputs are

Page 317: Computation Engineering Applied Automata Theory and Logic

18.4. COMPLEXITY 309

assumed to be “reasonably encoded” – i.e., it is not assumed that we are not“bringing down the complexity by bloating the input representation.” Thisassumption was tacit in your Algorithms class.

The class TIME(t(n))

Now, we define time-complexity class, TIME(t(n)), as follows:

TIME(t(n)) = L | L is a language decided by an O(t(n)) time Turing machine

For example, the language

A = 0n1n | n ≥ 0

is of time-complexity class TIME(n2), because we can easily exhibit a deciderM that decides membership in A in O(n2) time.

The class P

The class P

P = ∪k TIME(nk)

is important in practice, for two reasons:

1. P is invariant for all models that are polynomially equivalent to thedeterministic single-tape TM, and

2. P roughly corresponds to the class of problems that are realisticallysolvable.

Example of a problem in P: two-colorability of an undirected graph: Theproblem itself is described as a language:

Twocolor = < G > | G is a directed graph that is twocolorable

Hint: Use breadth-first search. A language (or a problem) is said to beintractable if no polynomial-time algorithm can solve it. Whether or notNP-complete problems (to be defined soon) are actually intractable is one ofthe foremost questions of Computer Science. This is often phrased as “is P= NP?”

Page 318: Computation Engineering Applied Automata Theory and Logic

310 CHAPTER 18. UNDECIDABILITY

The class NP is defined as follows:

NP = the class of languages that have polynomial time verifiers, where

a verifier for a language A is an algorithm V , where

A = w | V accepts < w, c > for some string ci.e., a verifier is an algorithm V that can verify the membership in the setA of every string w, with the help of a certificate c. The certificate c simplysays “here’s how w qualifies to be in A”. For a polynomial-time verifier, thecertificate c is also of polynomial length (with respect to the length of w).There is a polynomial-time verifier V for a language in NP. One can equiv-alently think of a non-deterministic machine performing a non-deterministiccomputation - i.e., “guesses a certificate” and then verifies it in polynomialtime. Hence the name NP (non-deterministic polynomial-time). Anotherway to look at the class NP is “given the power of non-determinism - i.e.,that of executing all the non-deterministic possibilities of a non-deterministicTM in parallel - does it still take a polynomial amount of time to solve a cer-tain problem.” It is also the case that a language is in NP if and only if it isdecided by some NDTM in Polynomial time. Note that the above definitionof NP has a very direct bearing on how NP-complete problems are solved inpractice: they are solved simply via an exponentially expensive determinis-tic simulation of the non-deterministic TM! Clearly here is where heuristicscome into play. (I have a whole book describing approximate algorithms -i.e. heuristics - for known NP-complete problems.)

Let us now consider some examples of certificates. Consider the languageHAMPATH = < G, s, t > such that G is a directed graph with a Hamil-tonian path from s to t. A certificate for a tuple < G, s, t >∈ HAMPATH isthe Hamiltonian path from s to t. Given that certificate, we can see whetherthe path described by the certificate exists from s to t. Now consider the lan-guage HAMPATH = < G, s, t > such that G is a directed graph withouta Hamiltonian path from s to t. A certificate for < G, s, t >∈ HAMPATHwould consist of all paths in the graph from s to t. This is because we mustconsider each possible path to ultimately rule out the existence of any Hamil-tonian path10. Thus, it appears that even the “guess and check” paradigmis in danger because a “guess” produces an exponentially long certificate.At least, that is the best known result so far. It appears there cannot be

10Note that the complement of HAMPATH is really a set that also includes < G, s, t >where G does not encode a graph, and perhaps s and t also don’t encode legal node

Page 319: Computation Engineering Applied Automata Theory and Logic

18.4. COMPLEXITY 311

polynomial time verifiers for HAMPATH, because there doesn’t appear tobe any polynomial length certificate.

The class NP-complete

A language B is NP-complete if

1. B is in NP, and

2. every A in NP (such A’s are “old problems” known to be in NP) ispolynomial-time reducible to B. This reduction works exactly as inthe case of ≤m, the mapping reduction function we saw earlier.

Now, this portion of the theorem alone is called NP-hard. So an NP-hard language is not necessarily in NP, but is such that every languagein NP is polynomial-time reducible to that NP-hard problem.

Showing NP-completeness

If a language B is NP-complete, and B ∈ P , then P = NP . This is becauseevery problem in NP can then be polynomial-time transformed to B whichis ∈ P . To show that a new problem X is NP-complete, we first show that itis in NP , and then reduce a known NP-complete problem N to X througha polynomial-time transformation. This proof works because one can trans-form every problem in NP to problem X, via N . If B is NP-complete, itonly means that every problem in in NP (which includes P) can be “run”using an algorithm for B with no more than a polynomial increase in therunning cost. However, if B (even one B) is found to be in P (polynomial-time runnable), then every problem in NP can be run with no more than apolynomial increase in the running cost over a polynomial, i.e. in P-time. Asan example, to show that the Clique problem is NPC, one can reduce satis-fiability to Clique. Basically, given an instance of satisfiability, one producesan instance of Clique such that the Boolean formula is satisfiable if and onlyif the mapped Clique problem is satisfiable. Further, the mapping processmust take only a polynomial amount of time. In later chapters, we will showthis process in more detail.

numbers. We ignore this sublanguage as there are polynomial ways to decide whether astring is really in this “junk space” that does not encode a “legal anything”. Thus, tacitlywe represent the complement of HAMPATH as HAMPATH above.

Page 320: Computation Engineering Applied Automata Theory and Logic

312 CHAPTER 18. UNDECIDABILITY

Page 321: Computation Engineering Applied Automata Theory and Logic

Chapter 19

Basic Notions in Logic

This chapter serves to complete the discussion of a few important decidabilityresults pertaining to first-order logic validity in the vein of earlier chapters. Itthen sets the stage for subsequent chapters that discuss validity and relatednotions in the setting of propositional logic.

Mathematical logic is central to formalizing the notion of assertions andproofs. Classical logics include propositional (zeroth-order), first-order, andhigher order logics. Most logics separate the notion of proof and truth. Aproof is a sequence of theorems where each theorem is either an axiom or isobtained from previous theorems by applying a rule of inference. Usually aproof is done (or generated) with respect to the last theorem in the sequence.Informally speaking, it is intended that all theorems are “true.” This isformalized through the notion of a meaning function or interpretation whichmaps a well-formed formula (wff) to its truth value. If a wff is true underall interpretations, it is called a tautology (or equivalently, it is called valid).Thus when we later mention the term “the validity problem of first-orderlogic,” we refer to the problem of deciding whether a given first-order logicformula is valid. (This problem is later shown to be only semi-decidable.)

A logical system consisting of a collection of axioms and rules of inferenceis sound if every theorem is a tautology. A logical system is complete if everytautology is a theorem. A logical system has independent rules of inferenceif omitting any rule prevents certain theorems from being derived. It is quitenatural to consider the axioms together with rules being independent bytreating axioms as “zero-ary” rules (they are not applied to prior theorems,but in fact are themselves theorems.

Propositional logic includes Boolean variables that range over True and

313

Page 322: Computation Engineering Applied Automata Theory and Logic

314 CHAPTER 19. BASIC NOTIONS IN LOGIC

False (or 1 and 0), Boolean connectives, and rules of inference. First orderlogic additionally allows the use of individuals such as integers and stringsthat constitute infinite sets, variables that are quantified over such sets, aswell as predicate and function constants. Second (and higher) order logicsallow quantifications to occur over function and predicate spaces. In Chap-ter 6.2, we presented two principles of induction, namely arithmetic andcomplete; the formal statement of both these induction principles constituteexamples of higher order logic. There are also special classes of logics thatvary according to the kinds of models (interpretations) possible: these in-clude temporal logic, various process logics, and even highly specialized logicsthat are designed to model concurrent programs with shared data structures(e.g., separation logic [?]). In these logics, notions such as validity tend toacquire specialized connotations, such as validity under certain restrictions.We will however confine our discussions to “classical” logics in the rest ofthis chapter.

Section 19.1 discusses a Hilbert style axiomatization of propositional logicdue to Church. Section 19.2 begins with a discussion of quacks and doctors1,presents examples of interpretations for formulas, and closes off with a prooffor the fact that the validity problem of first-order logic is undecidable. Asemi-decidability result can be obtained by enumerating proofs, and printingthe theorems generated in the process.

We then turn our attention to the topic of satisfiability in the setting ofpropositional logic in Section 19.3. We approach the subject based on notions(and notation) popular in hardware verification - following how “Booleanlogic” is treated in those settings (a model-theoretic approach). We first ex-amine two normal forms, namely the conjunctive and the disjunctive normalform, and discuss converting one to the other. We then examine related topicssuch as 6=-satisfiability, 2-satisfiability, and satisfiability-preserving transfor-mations.

19.1 Axiomatization of Propositional Logic

We now present one axiomatization of propositional calculus following theHilbert style. Let the syntax of well formed formulas (wff) be as follows:

Fmla ::= Pvar | ¬Fmla | Fmla⇒ Fmla | (Fmla)

Page 323: Computation Engineering Applied Automata Theory and Logic

19.1. AXIOMATIZATION OF PROPOSITIONAL LOGIC 315

In other words, a formula is a propositional variable, the negation of a propo-sitional formula, an implication formula, or a paranthesized formula. Thisgrammar defines a complete set of formulas in the sense that all Booleanpropositions can be expressed in it. (Prove this assertion.) Following Church,a Hilbert calculus for propositional logic can be set-up based on three axioms(A1-A3) and two rules of inference (R1-R2) shown below. Here p and q standfor propositional formulas.

A1: p⇒ (q ⇒ p)

A2: (s⇒ (p⇒ q)) ⇒ (s⇒ p)⇒ (s⇒ q)

A3: (¬q ⇒ ¬p) ⇒ (p⇒ q)

R1 (Modus Ponens): If P is a theorem and P ⇒ Q is a theorem, concludethat Q is a theorem.

Example: From p and p⇒ (p⇒ q), infer p⇒ q.

R2 (Substitution): The substitution of wffs for propositional variables in atheorem results in a theorem. A substitution is a “parallel assignment”in the sense the newly introduced formulas themselves are not affectedby the substitution (as would happen if, for instance, the substitutionsare made serially).

Example: Substituting (p ⇒ q) for p and (r ⇒ p) for q in formula p ⇒ qresults in (p ⇒ q) ⇒ (r ⇒ p). It is as if p and q are replaced by freshand distinct variables first, which, in turn, are replaced by (p⇒ q) and(r ⇒ p) respectively.

We do not perform the substitution of r ⇒ p for q first, and then affectthe p introduced in this process by the substitution of (p⇒ q) for p.

A proof for a simple theorem such as p⇒ p can be quite involved:

P1: From A1, through substitution of p⇒ p for q, we obtain

p⇒ ((p⇒ p)⇒ p)

Page 324: Computation Engineering Applied Automata Theory and Logic

316 CHAPTER 19. BASIC NOTIONS IN LOGIC

P2: From A2, substituting p for s, p⇒ p for p, and p for q, we obtain

(p⇒ ((p⇒ p)⇒ p)) ⇒ (p⇒ (p⇒ p))⇒ (p⇒ p)

P3: Modus Ponens between P1 and P2 yields

(p⇒ (p⇒ p))⇒ (p⇒ p)

P4: From A1, substituting p for q, we obtain

p⇒ (p⇒ p)

Modus Ponens between P4 and P3 results in the desired theorem (p⇒ p).

It is straight-forward to verify that the above axiomatization is sound.This is because the axioms are true, and every rule of inference is truth-preserving. The axiomatization is also complete, as can be shown via a proofby induction. The take-away message from these discussions is that it pays tohone the axiomatization of a formal logic into something that is parsimoniousas well as enjoys the attributes of being sound, complete, and independentas explained earlier.

Exercise 19.1 Using the Hilbert-style axiomatization above, describe howto implement the familiar operators ∧, ∨, and ⊕ (exclusive-OR). Also im-plement the constants true and false.

19.2 First Order Logic (FOL) and Validity

Below, we will introduce many notions of first-order logic intuitively, throughexamples. We refer the reader to one of many excellent books in first-orderlogic for details. One step called skolemization merits some explanation.Basically, skolemization finds a witness to model the existential variable. Ingeneral, from a formula of the form ∃X.P (X), we can infer P (c) where c isa constant in the domain. Likewise, from P (c), we can infer ∃X.P (X). Inother words, for an unspecified constant c in the domain,

∃X.P (X) ⇔ P (c).

Page 325: Computation Engineering Applied Automata Theory and Logic

19.2. FIRST ORDER LOGIC (FOL) AND VALIDITY 317

We will use this equivalence in the following proofs. There is another useof skolemization illustrated by the following theorem (which we won’t haveoccasion to use):

∀X.∃Y.P (X,Y ) ⇔ P (X, f(X)).

Here, f is an unspecified (but fixed) function. This equivalence is validbecause of two reasons:

• The right-hand side leaves X as a free variable, achieving the sameeffect as ∀X goes, as far as validity goes (must be true for all X).

• The right-hand side employs f(X) to model the fact that the selectionof ∃Y may depend on X.

19.2.1 A Warm-up Exercise

Suppose we are given the following proof challenge: Some patients like everydoctor. No patient likes a quack. Therefore, prove that no doctor is a quack.It is best to approach problems such as this using predicates to model thevarious classes of individuals2 instead of employing ‘arbitrary constants’ suchas p and d to denote patients, doctors, etc. Introduce predicate p to carve outa subset of people (P ) to be patients, and similarly d for doctors. Let l (for“likes”) be a binary relation over P . Our proof will consist of instantiationof formulas, followed by modus-ponens, and finally proof by contradiction.

A1: The statement “Some patients like every doctor:”

∃x ∈ P : (p(x) ∧ ∀y : (d(y)⇒ l(x, y))).

A2: The statement “No patient likes a quack:”

∀x, y ∈ P : (p(x) ∧ q(y)⇒ ¬l(x, y)).

Proof-goal: “No doctor is a quack.”

∀x ∈ P : (d(x)⇒ ¬q(x)).

Negate the proof-goal: ∃x ∈ P : (d(x) ∧ q(x)).2Pun intended; in FOL, members of domains other than Booleans are called individuals.

Page 326: Computation Engineering Applied Automata Theory and Logic

318 CHAPTER 19. BASIC NOTIONS IN LOGIC

Skolemize negated proof-goal: (x0 ∈ P ∧ d(x0) ∧ q(x0).

Skolemize A1: c ∈ P ∧ (p(c) ∧ ∀y : (d(y) ⇒ l(c, y))) (we will suppressdomain membership assertions such as c ∈ P from now on)

Specialize: From A1, we specialize y to x0 to get

p(c) ∧ (d(x0)⇒ l(c, x0))

But since d(x0) is true in the negated proof-goal, we get:

p(c) ∧ l(c, x0) (more irrelevant facts suppressed)

Since q(x0) is true in the negated proof-goal, we also get:

p(c) ∧ l(c, x0) ∧ q(x0)

Use A2 and specialize x to c and y to x0 to get ¬l(c, x0).

Contradiction: Since we have l(c, x0) and ¬l(c, x0), we get a contradiction.

Exercise 19.2 Prove the following using a similar approach as illustratedabove (Sperschneider’s book, p. 107): Every barber shaves everyone whodoes not shave himself. No barber shaves someone who shaves himself. Provethat there exists no barber.

19.2.2 Examples of Interpretations

Zohar Manna’s book describes many basic notions through examples thatare summarized below. We focus on the notion of interpretations, which, incase of first-order logic: (i) chooses domains for constants, predicates, andfunction symbols to range over, and (ii) and assigns to them. Examplesbelow will clarify.

Example 1

Consider the formula

Fmla1 = ∃F.F (a) = b

∧(∀x).[p(x)⇒ F (x) = g(x, F (f(x)))]

Page 327: Computation Engineering Applied Automata Theory and Logic

19.2. FIRST ORDER LOGIC (FOL) AND VALIDITY 319

We will now provide three distinct interpretations for it.Interpretation 1.

D = Nat

a = 0

b = 1

f = λx.(x = 0→ 0, x− 1)

g = *

p = λx.x > 0

Interpretation 2.

D = Σ∗

a = ε

b = ε

f = λx.(tail(x))

g(x,y) = concat(y,head(x))

p = λx.x 6= ε

Interpretation 3.

D = Nat

a = 0

b = 1

f = λx.x

g(x,y) = y+1

p = λx.x > 0

Exercise 19.3 Considering the three interpretations above, determine whetherFmla1 is valid or not.

Example 2

Consider the formula

Fmla2 = ∀P.P (a)

∧(∀x).[(x 6= a) ∧ P (f(x))⇒ P (x)]

⇒ (∀x.P (x))

We can interpret this formula suitably to obtain the principle of inductionover many domains: for example, Nat, strings, etc.

Page 328: Computation Engineering Applied Automata Theory and Logic

320 CHAPTER 19. BASIC NOTIONS IN LOGIC

19.2.3 Validity of First Order Logic is Undecidable

Valid formulas are those that are true for all interpretations. For example,

∀x.f(x) = g(x)⇒ ∃a.f(a) = g(a).

We will now summarize Floyd’s proof that the validity problem for first-order logic is undecidable. First, an abbreviation: for σi ∈ 0, 1, use theabbreviation

fσ1,σ2,...,σn(a) = fσn

(fσn−1(. . . fσ1

(a)) . . .).

The proof proceeds by building a FOL formula for a given “Post system” (aninstance of the Post Correspondence Problem).Given a Post system S = (α1, β1), (α2, β2), . . . , (αn, βn), n ≥ 1 over Σ =0, 1, construct the wff WS (we will refer to the two antecedents of WS asA1 and A2, and its consequent as C1):

∧ni=1 p(fαi(a), fβi

(a)) (A1)

∧ ∀x ∀y [p(x, y)⇒ ∧ni=1 p(fαi(x), fβi

(y))] (A2)

⇒ ∃z p(z, z) (C1)

We now prove that S has a solution iff WS is valid.Part 1. (WS valid) ⇒ (S has a solution).

If valid, it is true for all interpretations. Pick the following interpretation:

a = ε

f0(x) = x0 (string ‘x’ and string ‘0’ concatenated)

f1(x) = x1 (similar to the above)

p(x, y) = There exists a non-empty sequence i1i2 . . . im such that

x = αi1αi2 . . . αim and y = βi1βi2 . . . βim

Under this interpretation, parts A1 and A2 of WS are true. Here is why:

• Under the above interpretation, fαi(a) = εαi = αi and similarly fβi

(a) =βi.

• Thus A1 becomes ∧ni=1 p(αi, βi). Each conjunct in this formula is trueby p’s interpretation; hence A1 is true.

Page 329: Computation Engineering Applied Automata Theory and Logic

19.2. FIRST ORDER LOGIC (FOL) AND VALIDITY 321

• The part [p(x, y) ⇒ ∧ni=1 p(fαi(x), fβi

(y))] reduces to the followingclaim: p(x, y) is true means that x and y can be written in the form x =αi1αi2 . . . αim and y = βi1βi2 . . . βim ; the consequent of this implicationthen says that we can append some αi and the corresponding βi to xand y, respectively. The consequent is also true by p’s interpretation.Thus A2 is also true.

• SinceWS is valid (true), C1 must also be true. C1 asserts that the Postsystem S has a solution, namely some string z that lends itself to be-ing interpreted as some sequence αi1αi2 . . . αim as well as βi1βi2 . . . βim .That is,

αi1αi2 . . . αim = z = βi1βi2 . . . βim .

Part 2. (WS valid) ⇐ (S has a solution).If S has a solution, let it be the sequence i1i2 . . . im. In other words,

αi1αi2 . . . αim = βi1βi2 . . . βim = Soln. Now, in order to show that WS isvalid, we must show that for every interpretation, it is true. We approachthis goal by showing that under every interpretation where the antecedentsof WS, namely A1 and A2, are true, the consequent, namely C1, is also true.(If any antecedent is false, WS is true, so this case is not considered.)

From A1, we conclude that

p(fαi1(a), fβi1

(a))

is true. Now using A2 as a rule of inference, we can conclude, throughmodus-ponens, that

p(fαi2(fαi1

(a)), fβi2(fβi1

(a))

is true. In other words,

p(fαi1αi2

(a), fβi1βi2

(a))

is true. We continue this way, applying the functions in the order dictactedby the assumed solution for S; in other words, we arrive at the assertion thatthe following is true (notice that the subscripts of f describe the order inwhich the solution to S considers the αs and βs):

p(fαi1αi2

...αim(a), fβi1

βi2...βim

(a)).

However, sinceαi1αi2 . . . αim = βi1βi2 . . . βim = Soln,

Page 330: Computation Engineering Applied Automata Theory and Logic

322 CHAPTER 19. BASIC NOTIONS IN LOGIC

we have essentially shown that

p(fSoln(a), fSoln(a)).

Notice that ∃z.p(z, z) is the disjunction ∨z0∈D : p(z0, z0), where D is therange of f . Thus, from p(fSoln(a), fSoln(a)), we can infer ∃z : p(z, z).

19.2.4 Valid FOL Formulas are Enumerable

First-Order Logic has complete axiomatizations, which means that there areaxiomatizations in which every valid formula has a proof. Thus, by enumerat-ing proofs, we can enumerate all valid first-order logic formulas. Thus the setof valid FOL formulas is recursively enumerable (or is Turing recognizable).

Exercise 19.4 One often falls into the following trap of claiming that FOLformulas are decidable: the negation of every FOL formula is an FOL formula;thus one can enumerate proofs, and see whether F or ¬F gets enumeratedin a proof, and hence decide the validity of F . What is the fallacy in thisargument? (Hint: Are the cases listed, namely F being valid and ¬F beingunsatisfiable exhaustive?)

Exercise 19.5 Argue that the set of all non-valid but satisfiable FOL for-mulas is not Recursively Enumerable.

19.3 Properties of Boolean Formulas

19.3.1 Normal Forms

We now discuss several properties of Boolean (or propositional) formulas toset the stage for discussions about the theory of NP-completeness. There aretwo commonly used normal forms for Boolean formulas: the conjunctive nor-mal form (CNF) and the disjunctive normal form (DNF)3. Given a Booleanexpression (function) over n variables x1 . . . xn, a sum-of-products (SOP) ordisjunctive normal form (DNF) expression for it is one where products of

3Strictly speaking, we should call the normal forms being discussed here the canonicalsum-of-products form and the canonical product-of-sums form, since each product-term orsum-term consists of n literals.

Page 331: Computation Engineering Applied Automata Theory and Logic

19.3. PROPERTIES OF BOOLEAN FORMULAS 323

literals are disjoined (OR-ed) – a literal being a variable or its negation. Forexample, nand(x, y) is expressed in SOP (DNF) as

nand(x, y) = ¬x¬y + ¬xy + x¬y.

A POS (CNF) expression for nand would be (¬x + ¬y). A systematic wayto obtain DNF and CNF expressions from truth-tables is illustrated below:basically, the DNF form for a Boolean function is obtained by disjoining(OR-ing) the min-terms at which the function is a 1, while the CNF formfor a Boolean function is obtained by conjoining (AND-ing) the max-termsat which the function is a 0. All these are illustrated in Table 19.1.

Row x y nand(x,y) Minterm Maxterm0 0 0 1 m0 = ¬x ¬y M0 = (x+ y)1 0 1 1 m1 = ¬x y M1 = (x+ ¬y)2 1 0 1 m2 = x ¬y M2 = (¬x+ y)3 1 1 0 m3 = x y M3 = (¬x+ ¬y)

Table 19.1: Min/Max terms for nand, whose DNF form is m0+m1+m2 andCNF form is M3

Here are important points pertaining to these normal forms:

• Conversion between the CNF and the DNF forms incurs an exponen-tial cost. This is best illustrated by a conversion program included inFigures 19.1 and Figures 19.2. You are encouraged to run this codeunder an Ocaml system (see Appendix for details) and see the resultsfor yourselves. You will see that an exponential growth in formula sizecan occur, as demonstrated in the tests at the end of Figure 19.2.

• CNF-satisfiability will be shown to be NP-complete in the next chapter,whereas DNF-satisfiability is linear-time (see exercise below). However,due to the exponential size blowup, one cannot “win” against NP-completeness by converting CNF-satisfiability to DNF-satisfiability.

Exercise 19.6 Argue that DNF satisfiability and CNF validity have linear-time algorithms.

Page 332: Computation Engineering Applied Automata Theory and Logic

324 CHAPTER 19. BASIC NOTIONS IN LOGIC

type literal = int (* 1, 3, -2, etc *)

type clause = literal list (* [1; 3; -2] -> [ ] means TRUE and [ [ ] ] means FALSE *)

type cnffmla = clause list (* [[1; 3; -2]; [1; -1]] -> (x1 \/ x3 \/ ~x2)/\(x1 \/ ~x1)*)

type fmla =

Ff | Tt | Pv of string | And of fmla * fmla | Or of fmla * fmla

| Eq of fmla * fmla | Imp of fmla * fmla | Not of fmla

type cnf_str = cnf:cnffmla; (* CNF formula as a list *)

nvgen:int; (* Variable allocation index *)

algen: (string * int) list (* Association list between variable-names

and integers representing them *)

let rec gencnf(fmla)(next_var_int)(var_al) =

match fmla with

Tt -> cnf=[ ]; nvgen=0; algen=[]

| Ff -> cnf=[[]]; nvgen=0; algen=[]

| Pv(s) ->

if (List.mem_assoc s var_al)

then cnf = [ [ (List.assoc s var_al) ] ];

nvgen = 0; algen = []

else cnf = [ [ next_var_int ] ]; nvgen = 1; (* 1 new var generated *)

algen = [(s,next_var_int)]

| And(q1,q2) ->

let cnf=cnf1; nvgen=nvgen1; algen=algen1

= gencnf(q1)(next_var_int)(var_al) in

let cnf=cnf2; nvgen=nvgen2; algen=algen2

= gencnf(q2)(next_var_int + nvgen1)(algen1 @ var_al) in

cnf=doAnd(cnf1)(cnf2); nvgen=nvgen1+nvgen2;

algen=algen1 @ algen2

| Or(q1,q2) ->

let cnf=cnf1; nvgen=nvgen1; algen=algen1

= gencnf(q1)(next_var_int)(var_al) in

let cnf=cnf2; nvgen=nvgen2; algen=algen2

= gencnf(q2)(next_var_int + nvgen1)(algen1 @ var_al) in

cnf=doOr(cnf1)(cnf2); nvgen=nvgen1+nvgen2;

algen=algen1 @ algen2

| Imp(q1,q2) -> gencnf(Or(Not(q1),q2))(next_var_int)(var_al)

| Eq(q1,q2) -> gencnf(And(Or(Not(q1),q2), Or(Not(q2),q1)))(next_var_int)(var_al)

| Not(Pv(s)) ->

if (List.mem_assoc s var_al)

then cnf = [ [ (0-(List.assoc s var_al)) ] ];

nvgen = 0; algen = []

else cnf = [ [ (0-next_var_int) ] ]; nvgen = 1; (* 1 new var generated *)

algen = [(s,next_var_int)]

| Not(q1) ->

let cnf=cnf1; nvgen=nvgen1; algen=algen1

= gencnf(q1)(next_var_int)(var_al) in

cnf=doNot(cnf1); nvgen=nvgen1;

algen=algen1

and

(* See PART-2 ... *)

Figure 19.1: A CNF Generator, Part-1

Page 333: Computation Engineering Applied Automata Theory and Logic

19.3. PROPERTIES OF BOOLEAN FORMULAS 325

doAnd(cnf1)(cnf2) =

(match (cnf1,cnf2) with

| ([],cnf2’) -> cnf2’

| (cnf1’,[]) -> cnf1’

| ([[]],_) -> [[]]

| (_,[[]]) -> [[]]

| _ -> List.append(cnf1)(cnf2) )

and

doOr(cnf1)(cnf2) =

(match (cnf1,cnf2) with

| ([],_) -> []

| (_,[]) -> []

| ([[]],cnf2’) -> cnf2’

| (cnf1’,[[]]) -> cnf1’

| ([cl1],[cl2]) -> [List.append(cl1)(cl2)]

| (cnf1’,[cl2]) -> doOr([cl2])(cnf1’)

| (cnf1’, (cl2::cls)) ->

let door1 = doOr(cnf1’)([cl2]) in

let door2 = doOr(cnf1’)(cls) in

doAnd(door1)(door2) )

and

doNot(cnf1) =

(match cnf1 with

| [] -> [[]]

| [[]] -> []

| (cl1::cls) ->

let compclause = comp_clause(cl1) in

let donot’ = doNot(cls) in

doOr(compclause)(donot’) )

and

comp_clause(clause) =

(match clause with

| [] -> []

| (lit::lits) ->

let cl = 0-lit in (* complement literal *)

let cl_cnf = [[cl]] in (* turn literal into CNF *)

let rest = comp_clause(lits) in

doAnd(cl_cnf)(rest) )

;;

(* To run tests, load these definitions, and type, e.g., gencnf(f5) 1 [];;

let f1 = And(And(Pv "x", Pv "y"), And(Pv "z", Pv "w"));;

let f2 = And(And(Pv "p", Pv "q"), And(Pv "r", Pv "s"));;

let f3 = Or(f1,f2);;

let f4 = Or(f3,f3);;

let f5 = Or(f4,f4);;

f5 is really (xyzw + pqrs + xyzw + pqrs + xyzw + pqrs + xyzw + pqrs)

List.length ( gencnf( f5 ) 1 [] ).cnf;;

- : int = 65536

Figure 19.2: A CNF Generator, Part-2

Page 334: Computation Engineering Applied Automata Theory and Logic

326 CHAPTER 19. BASIC NOTIONS IN LOGIC

19.3.2 Overview of Direct DNF to CNF Conversion

Consider Figure 19.2 in which some terms representing DNF formulas isgiven. In particular, note that formula f5 is a 4-DNF formula with eightproduct terms. The expression List.length ( gencnf( f5 ) 1 [] ).cnf

converts this formula to CNF, and measures its length, which is found to be65536, showing the exponential growth in length. Here is a brief explanationof this code4.

In Figure 19.1, the types for literals, clauses, and CNF formulas is given.The Ocaml List of lists [[1; 3; -2]; [1; -1]] stands for the CNF formula(x1 ∨ x3 ∨ ¬x2) ∧ (x1 ∨ ¬x1). The syntax of formulas, fmla, declares whatformula terms are permissible.

Conversion is driven by pattern-matching. Suppose fmla matches true(Tt); this results in an Ocaml record structure as shown in the following line:

Tt -> cnf=[ ]; nvgen=0; algen=[]

Note that [] is how true is represented. This is a list of lists with the outer-list empty. Since the outer-list is a list of conjunctions, an empty outer-listcorresponds to the basis case of a list of conjunctions, namely true. Likewise,[[]] represents false because the inner list is empty, and its basis case (for alist of disjunctions) is false (Ff). We also record the number of new variablesgenerated in nvgen, and an association list of variable names to their integervalues in algen. These are respectively 0 and [] for the present example.

A propositional variable is converted by the code under Pv(s). We look-up s in the association list var_al, and if found, generate its CNF withrespect to the looked-up result; else we generate a CNF clause containingnext_var_int, the next variable to be allocated, and also return nvgen=1

and algen suitably records the association between s and next_var_int.The remaining cases with propositional connectives is handled as shown:

for And, we recursively convert its arguments, and call doAnd (Figure 19.2).The first four cases of doAnd deal with one of the arguments being false ortrue. The last case simply appends cnf1 and cnf2, as they are already inCNF form. In case of doOr, we perform a more elaborate case analysis. Thecase

| ([cl1],[cl2]) -> [List.append(cl1)(cl2)]

4This code can convert arbitrary Boolean expressions to CNF - we choose to illustrateit on DNF.

Page 335: Computation Engineering Applied Automata Theory and Logic

19.3. PROPERTIES OF BOOLEAN FORMULAS 327

corresponds to two CNF clauses; here, we simply append the list of disjunctsto form a longer list of disjuncts. Given the case

| (cnf1’,[cl2]) -> doOr([cl2])(cnf1’)

This helps bring the first argument towards a single clause, at which time wecan apply the append rule described earlier.

Let us discuss the last case:

| (cnf1’, (cl2::cls)) ->

let door1 = doOr(cnf1’)([cl2]) in

let door2 = doOr(cnf1’)(cls) in

doAnd(door1)(door2) )

We recursively OR cnf1’ with the head of the second list, namely cl2, andthe tail of the list, namely cls, and call doAnd on the result. These are theplaces where an exponential size-growth is introduced.

19.3.3 CNF-conversion using Gates

To contain the size-explosion, one can resort to a circuit-based conversion ofDNF to CNF. This keeps the sizes of formulas linear, but introduces a linearnumber of intermediate variables. Theoretically, therefore, the exponentialcost remains hidden. In practice, we can often avoid suffering this cost.We illustrate this idea using an example. Consider converting the DNFexpression

xyzw + pqrs

to CNF. We build a circuit netlist as follows, by introducing temporary nodest1 through t7:

t1 = and(x,y) ; t2 = and(z,w) ; t3 = and(p,q) ; t4 = and(r,s)

t5 = and(t1,t2) ; t6 = and(t3,t4) ; t7 = or(t5,t6)

Now, we convert each gate to its own CNF representation. We illustrate oneconversion in detail now.

• t1 = x ∧ y.

• Treat it as (t1⇒ x) ∧ y and (x ∧ y)⇒ t1.

• The former is equivalent to (¬t1) ∨ (x ∧ y) which is equivalent to(¬t1 ∨ x) ∧ (¬t1 ∨ y).

Page 336: Computation Engineering Applied Automata Theory and Logic

328 CHAPTER 19. BASIC NOTIONS IN LOGIC

• The latter is equivalent to (¬x ∨ ¬y ∨ t1).

The following ML function accomplishes the conversion of AND gates, andthrough a similar reasoning, of OR gates:

let gen_andG_clauses(gatetype = Andg; inputs = (i1,i2); output = output as andG)

= [ [-i1; -i2; output]; [-output; i1]; [-output; i2] ]

let gen_orG_clauses (gatetype = Org; inputs = (i1,i2); output = output as orG)

= [ [i1; i2; -output]; [output; -i1]; [output; -i2] ]

The final result for our example will be as follows (numbering variables asindicated:)

x=1,y=2,z=3,w=4,p=5,q=6,r=7,s=8,t1=9,t2=10,t3=11,t4=12,t5=13,t6=14,t7=15

[ [-1; -2; 9] ; [-9; 1] ; [-9; 2] ] t1 = and(x,y)

[ [-3; -4; 10] ; [-10; 3] ; [-10; 4] ] t2 = and(z,w)

[ [-5; -6; 11] ; [-11; 5] ; [-11; 6] ] t3 = and(p,q)

[ [-7; -8; 12] ; [-12; 7] ; [-12; 8] ] t4 = and(r,s)

[ [-9; -10; 13] ; [-13; 9] ; [-13; 10] ] t5 = and(t1,t2)

[ [-11; -12; 14]; [-14; 11]; [-14; 12] ] t6 = and(t3,t4)

[ [13; 14; -15] ; [15; -13]; [15; -14] ] t7 = or(t5,t6)

[ 15 ] t7

19.3.4 DIMACS File Encoding

CNF formulas can be run through modern Boolean satisfiability tools (SAT).For instance, the above problem can be converted into the following ASCII fileand given to a tool called zchaff (one of the efficient modern SAT solvers).

p cnf 15 22 # Problem CNF format : Nvars and Nclauses

-1 -2 9 0 # Each line consists of the literals of a clause and ends with 0

-9 1 0

-9 2 0

-3 -4 10 0

-10 3 0

-10 4 0

-5 -6 11 0

-11 5 0

-11 6 0

-7 -8 12 0

-12 7 0

-12 8 0

-9 -10 13 0

-13 9 0

Page 337: Computation Engineering Applied Automata Theory and Logic

19.3. PROPERTIES OF BOOLEAN FORMULAS 329

-13 10 0

-11 -12 14 0

-14 11 0

-14 12 0

13 14 -15 0

15 -13 0

15 -14 0

15 0

Type command ‘‘zchaff CNF-File’’ - here the file is "cnf"

The result of feeding the above file is shown below.

[ganesh@localhost CH19]$ zchaff cnf

Z-Chaff Version: ZChaff 2003.11.04

Solving cnf ......

22 Clauses are true, Verify Solution successful. Instance satisfiable

1 2 3 4 -5 -6 -7 -8 9 10 -11 -12 13 -14 15

Max Decision Level 7

Num. of Decisions 8

Num. of Variables 15

Original Num Clauses 22

Original Num Literals 50

Added Conflict Clauses 0

Added Conflict Literals 0

Deleted Unrelevant clause 0

Deleted Unrelevant literals 0

Number of Implication 15

Total Run Time 0

SAT

The SAT tool Zchaff picked the assignment given by the line

1 2 3 4 -5 -6 -7 -8 9 10 -11 -12 13 -14 15

which stands for the Boolean assignment

x=1,y=1,z=1,w=1,p=0,q=0,r=0,s=0,t1=1,t2=1,t3=0,t4=0,t5=1,t6=0,t7=1

Suppose we force the assignment of x=0 by adding the clause ¬x: the assign-ment then becomes the one shown below, where p,q,r,s are set to 1:

-1 -2 -3 -4 5 6 7 8 -9 -10 11 12 -13 14 15

Page 338: Computation Engineering Applied Automata Theory and Logic

330 CHAPTER 19. BASIC NOTIONS IN LOGIC

p cnf 4 16

1 2 3 4 0

1 2 3 -4 0

1 2 -3 4 0

1 2 -3 -4 0

1 -2 3 4 0

1 -2 3 -4 0

1 -2 -3 4 0

1 -2 -3 -4 0

-1 2 3 4 0

-1 2 3 -4 0

-1 2 -3 4 0

-1 2 -3 -4 0

-1 -2 3 4 0

-1 -2 3 -4 0

-1 -2 -3 4 0

-1 -2 -3 -4 0

Figure 19.3: An Unsat CNF Instance

19.3.5 Unsatisfiable CNF Instances

Consider the unsatisfiable CNF formula represented by the DIMACS file inFigure 19.3:

We notice that in any unsatisfiable CNF formula, for any assignment,there will be one clause with all its variables false and another with all itsvariables true. This very important property will be exploited in many proofs.We can prove this fact by contradiction, as follows. Suppose Fmla is anunsatisfiable CNF formula and there is an assignment σ under which one ofthe following cases arise (both violating the ‘one clause with all its variablesfalse and another with all its variables true’ requirement):

• There are no clauses with all its literals assuming value true. In thiscase, we can complement the assignment σ, thus achieving a situationwhere no clause will have all false values.

• There are no clauses with all its literals assuming value false. In thiscase, Fmla is satisfiable.

Page 339: Computation Engineering Applied Automata Theory and Logic

19.3. PROPERTIES OF BOOLEAN FORMULAS 331

19.3.6 3-CNF, 6=-satisfiability, and general CNF

A 3-CNF formula is a CNF formula in which every clause has three literals.For example,

(a ∨ a ∨ ¬b) ∧ (c ∨ ¬d ∨ e)is a 3-CNF formula. As can be seen, there is no requirement on what variablesthe clauses might involve. Many proofs are rendered easier by standardizingon the 3-CNF form.

In many proofs, it will be handy to restrict the satisfying assignmentsallowed. One useful form of this restriction is the 6=-satisfiability restriction.Given a 3-CNF formula φ, a 6=-assignment is one under which every clausehas two literals with unequal truth-values. Given a set of N clauses wherethe ith clause is ci = yi1 ∨ yi2 ∨ yi3, suppose we transform each such clauseinto two clauses by introducing a new variable zi per original clause and asingle variable b for the whole translation:

ci1 = (yi1 ∨ yi2 ∨ zi) and ci2 = (¬zi ∨ yi3 ∨ b)

We can show that for any given formula Fmla = ∧i∈Nci, the above describedtransformation is a polynomial-time mapping reduction ≤P from 3SAT to 6=-sat.

Exercise 19.7 Prove that this transformation is indeed a polynomial-timemapping reduction ≤P from 3SAT to 6=-sat.

Finally, a general CNF formula is one where the number of literals ineach clause can vary (a special case where this number is ≤ 2 is discussed inSection ??). A conversion from general CNF to 3-CNF that does not increasethe length of the formula beyond a polynomial factor and takes no more thanpolynomial time is now described. (For each clause with less than 3 literals,replicating some literal can increase its size to 3; e.g., (a ∨ b) = (a ∨ b ∨ b).)Consider a clause with l literals:

a1 ∨ a2 ∨ . . . ∨ alThis clause can be rewritten into one with l − 2 clauses:

(a1 ∨ a2 ∨ z1) ∧ (¬z1 ∨ a3 ∨ z2) ∧ (¬z2 ∨ a4 ∨ z3) . . . (zl−3 ∨ al−1 ∨ al)

Applying these transformations to a general CNF formula results in one thatis equivalent as far as satisfiability goes.

Page 340: Computation Engineering Applied Automata Theory and Logic

332 CHAPTER 19. BASIC NOTIONS IN LOGIC

Exercise 19.8 Why does the conversion from general CNF to 3-CNF pre-serve only satisfiability? Why do the formulas not emerge to be logicallyequivalent?

19.3.7 2-CNF Satisfiability

A 2-CNF formula is one where each clause has two literals. One can showthat the satisfiability of 2-CNF is polynomial-time, as follows:

• Consider one of the clauses l1 ∨ l2. This can be written as two implica-tions (¬l1 ⇒ l2) and (¬l2 ⇒ l1).

• The complete algorithm is to process each clause of a 2-CNF formulainto such a pair of implications. Now, viewing each such implication asa graph edge, we can connect its source to its destination; for example,in (¬l1 ⇒ l2), connect node ¬l1 to node l2. In any situation, if adouble-negation is introduced, as in ¬¬a, we must simplify it to abefore proceeding.

• Finally, we connect-up pairs of edges such that the destination vertexof one edge is the source vertex of the other. For example, if (¬l1 ⇒ l2)is an edge, and (l2 ⇒ l3) is another edge, connect these edges at thecommon point l2.

Now, the following results can be shown:

• If the graph resulting from the above construction is cyclic and a cycleincludes p and ¬p for some literal p, the formula is unsatisfiable.

• If this situation does not arise, we can perform a value assignment tothe variables (find a satisfying assignment) as follows:

– If a literal x (for variable x) is followed by literal ¬x in the graph,assign x false

– If a literal ¬x (for variable x) is followed by x in the graph, assignx true

– Else assign x arbitrarily.

Exercise 19.9 Apply the above algorithm to the following 2-CNF formulas:

Page 341: Computation Engineering Applied Automata Theory and Logic

19.3. PROPERTIES OF BOOLEAN FORMULAS 333

• (a ∨ b) ∧ (a ∨ ¬b) ∧ (¬a ∨ b)

• (a ∨ b) ∧ (a ∨ ¬b) ∧ (¬a ∨ b) ∧ (¬a ∨ ¬b)

Page 342: Computation Engineering Applied Automata Theory and Logic

334 CHAPTER 19. BASIC NOTIONS IN LOGIC

Page 343: Computation Engineering Applied Automata Theory and Logic

Chapter 20

Complexity

335

Page 344: Computation Engineering Applied Automata Theory and Logic

336 CHAPTER 20. COMPLEXITY

Page 345: Computation Engineering Applied Automata Theory and Logic

Chapter 21

Automaton/Logic Connection:WS1S

337

Page 346: Computation Engineering Applied Automata Theory and Logic

338 CHAPTER 21. AUTOMATON/LOGIC CONNECTION: WS1S

Page 347: Computation Engineering Applied Automata Theory and Logic

Chapter 22

Automata on Infinite Words,Enumerative Model Checking

of states, a given Buchi automaton (deterministic or non-deterministic) will,when fed an infinitely long input string, traverse some cycle in its transitiongraph an infinite number of times. Such a run is an accepting run exactlywhen the number of occurrences of final states in the run is infinite.

...

339

Page 348: Computation Engineering Applied Automata Theory and Logic

340CHAPTER 22. AUTOMATAON INFINITEWORDS, ENUMERATIVEMODEL CHECKING

Page 349: Computation Engineering Applied Automata Theory and Logic

Chapter 23

Conclusions

341

Page 350: Computation Engineering Applied Automata Theory and Logic

342 CHAPTER 23. CONCLUSIONS

Page 351: Computation Engineering Applied Automata Theory and Logic

Bibliography

[1] Paul R. Halmos. Naıve Set Theory. Van Nostrand, 1968.

[2] Michael Gordon. Programming Language Theory and Implementation.Prentice-Hall, 1993.

[3] http://mathworld.wolfram.com/CollatzProblem.html.

[4] Edsger W. Dijkstra. The humble programmer. Communications of theACM, 15(10):859–866, 1972.

[5] http://mathworld.wolfram.com/ContinuumHypothesis.html.

[6] Joe Hurd. http://www.cl.cam.ac.uk/~jeh1004/compsci/lectures/.

[7] Jacques Loeckx and Kurt Sieber. The Foundations of Program Verifi-cation: Second Edition. John Wiley & Sons, 1987.

[8] Leslie Lamport. How to write a long formula. Formal Aspects of Com-puting, 3:1–000, 1994.

[9] S. J. Friedman and K. J. Supowit. Finding the optimal variable orderingfor binary decision diagrams. In Proceedings of the 24th ACM/IEEEconference on Design automation, pages 348–356, 1987. ISBN:0-8186-0781-5.

[10] F.L. Morris and C.B. Jones. An early program proof by alan turing.IEEE Annals of the History of Computing, 6(2):139–143, April-June1984.

[11] Henrik Reif Andersen. An introduction to binary decision diagrams,April 1998. Lecture Notes that come with the BED Tool.

343

Page 352: Computation Engineering Applied Automata Theory and Logic

344 BIBLIOGRAPHY

[12] Michael Sipser. Introduction to the Theory of Computation. PWS Pub-lishing Company, 1997.

[13] Jeffrey Jaffe. A necessary and sufficient pumping lemma for regularlanguages. In ACM SIGACT News archive, volume 10, pages 48–49.ACM Press, 1978. Issue 2, Summer 1978, ISSN:0163-5700.

[14] Donald F. Stanat and Stephen F. Weiss. A pumping theorem for regularlanguages. In ACM SIGACT News archive, volume 14, pages 36–37.ACM Press, 1982. Issue 1, Winter 1982, ISSN:0163-5700.

[15] E. Allen Emerson and Edmund M. Clarke. Using branching time tem-poral logic to synthesize synchronization skeletons. Science of ComputerProgramming, 2(3):241–266, 1981.

[16] Edmund M. Clarke, Orna Grumberg, and Doron Peled. Model Checking.MIT Press, December 1999.

[17] John Hopcroft, Rajeev Motwani, and Jeffrey D. Ullman. Introduction toAutomata Theory, Languages, and Computation. Addison-Wesley, 2001.

Page 353: Computation Engineering Applied Automata Theory and Logic

Index

<lex, 125=lex, 125>lex, 125CP , the set of all legal C programs,

64Eclosure function, 165IBS, infinite bit-sequences, 65R∗, given R, 79R+, given R, 79R0, given R, 79S/ ≡, 78TID DFA, 140⇐, 91Σ, 121Σε = Σ ∪ ε, 159δ, for a DFA, 137δ, for an NFA, 159δ, 140≤lex, 125|, Unix pipe, 173⊥, 115φ, 122¹, 84→, 164.=, 914=, 91ε-closure, 163`, 140`∗, 140as notation, 93, 130

q →, 164q0, 137’, quoting, 173ε→, 164ε→∗, 164‘∗,’ Kleene star, 129‘∗,’ Star, 129‘-’, Read standard input, 173“de-Fred”, 109, 110, 1133x+1 problem, 51

Accepting state, 135Aleph, 57Algorithm, 51Algorithmically computable, 50Alphabet, 121Ambiguity, 263

of CFGs , 239if-then-else , 239undecidability, 240

Ambiguity , 239Ambiguity and non-determinism, 263Ambiguous grammar, 239

Barter, 58basketball, 74Black-hole state, 137Bottom, 115

function, 117value, 115

345

Page 354: Computation Engineering Applied Automata Theory and Logic

346 INDEX

Cantor, 59Cardinality, 57

ℵ0, 57ℵ1, 57ℵ0.5??, 63trap, 59

Cartesian product, 41, 42examples of, 42

CFG, 229Fixed-points , 237left-linear , 249right-linear , 249

CFG to PDA, 263CFL

deterministic, 248Pumping Lemma, 252

CFLs and decidability, 275Characteristic sequence, 42

infinite, 123Chomsky Hierarchy , 240Chomsky normal form, 272CKY algorithm, 273Closure, 79

prefix, 133reflexive, 79reflexive and transitive, 79transitive, 79

Closure of CFLs, 275Closure properties of CFLs , 242co-TR, 9co-Turing recognizable, 9Cocke-Kasami-Younger algorithm,

273Collatz’s problem, 51Completeness, 232

of a CFG, 232undecidable , 232

Computation, 121

Computational process, 50Congruence

Radio receiver, 85Congruence relation, 85Consistency

of a CFG, 232Consistency , 232Constructor, 98Context-Free Grammar

Fixed-points, 237Context-free grammar, 229Continuum Hypothesis, 63Contrapositive rule, 93Conversion

NFA to DFA, 180

Definitional equality, 91DeMorgan’s law, 96

generalized, 96Derivation, 231

Leftmost , 231parse tree shape, 231Rightmost , 231

Derivation sequence , 230Derivation step , 230Determinism vs Ambiguity , 248Deterministic CFLs, 248DFA

Complementation, 186Intersection, 185Minimization, 194Myhill-Nerode Theorem, 194Relationship with BDDs, 194Ultimate Periodicity, 197Union, 183

DFA Transition function, 137Diagonalization, 59

proof, 61

Page 355: Computation Engineering Applied Automata Theory and Logic

INDEX 347

Disambiguation , 239Dog, 57

Fifi, 57Howard, 57

Dominance relation, 81DPDA and NPDA , 246Duality

between ∀ and ∃, 97

Effective procedure, 51Effectively computable, 50Equivalence, 77

class, 78Equivalence class, 78Exists

as infinite disjunction, 95Exponential DFA, 159

First-order logic, 95Fixed-point, 111

and automata, 119equation, 111immovable point, 111least, 118multiple, 117of a Xerox machine, 112on a calculator, 111

Fixed-pointsgreatest, 119

Forallas infinite conjunction, 94

Formal Verification, 154Fred, 45, 109Frontier , 230Function, 44

1-1, 48bijection, 48computable, 50

correspondences, 48domain, 44gappiest, 118injection, 48into, 48many to one, 48non computable, 50non-termination, 47one to one, 48onto, 48partial, 47range, 44signature, 44surjection, 48total, 47

Gamemastermind, 93

GNFA method, 190Grail, 172

|, 173dot, 173fa2grail.perl, 173fmcment, 173fmcross, 173fmdeterm, 173fmmin, 173ghostview, 173gv, 173retofm, 173

grail tool, 172

Halting ProblemDiagonalization, 72

Hilbert23 problems, 199first problem, 63problems, 63

Page 356: Computation Engineering Applied Automata Theory and Logic

348 INDEX

Hilbert’s problems, 63, 199Homomorphism, 130

inverse, 131

ID, 140Identity relation, 78Implication

conclusion, 89premis, 89

Individuals, 95Induction, 98

arithmetic, 100complete, 100noetherian, 102structural, 98, 103

Inductive, 97basis elements, 98closed under, 98constructor, 98definition, 97free, 99least set, 98set, 98

Infinite loop, 47Inherently ambiguous, 240Instantaneous Description, 140Interior node , 230Inverse Homomorphism, 131Irredundant name, 45, 110Ivory soap, 60

Lambda calculus, 45alpha conversion, 46alpha rule, 46associativity, 46beta reduction, 46beta rule, 46bindings, 46

function application, 46irredundant names, 45syntactic conventions, 54Y, 110

Language, 5, 121, 122cardinality, 123concatenation, 127context-free , 229empty, 122exponentiation, 128homomorphism, 130, 131intersection, 127of a CFG, 230prefix closure, 133recursive definition, 119reversal, 130setminus, 127star, 129symmetric difference, 127uncountably many, 123union, 127

Language , 229Lattice, 84

¹, 84all equivalences, 84glb, 84greatest lower bound, 84illustration on 2S, 84least upper bound, 84lub, 84

Left-to-right scan, 135Lexical analyzer, 168Lexicographic, 124

strictly before, 125Lexicographic order, 125Linear CFG to NFA , 251Linear CFGs, 249Logic

Page 357: Computation Engineering Applied Automata Theory and Logic

INDEX 349

⇒, 89∃, 94∀, 94if-then, 89axiom, 89if, 89implication, 89proof, 89Quantification, 94rule of inference, 89vacuous truth, 90

Machineswith one stack, 4with two stacks, 4with zero stacks, 3

Mechanical process, 50Modular, 130

homomorphism, 130substitution, 130

Myhill-Nerode Theorem, 194

Natural number, 30as set, 40

NFA, 159δ, 160→, 164ε moves, 162ε-closure, 163`, 160`∗, 160Concatenation, 186Homomorphism, 188ID, 160Instantaneous Description, 160Kleene-star, 186language, 165Prefix-closure, 189

Reversal, 187to DFA, 180to Regular Expressions, 190token game, 166Union, 183

NFA Transition function, 159Non-terminals , 229Nondeterminism, 153, 154Nondeterministic machines, 155Number

ℵ0.5??, 63cardinal, 57, 58integer, 35natural, 35real, 35

Numericstrictly before, 126

Numeric order, 125, 126

One-stop shopping., 237Order

partial, 76pre, 75

Overapproximation, 153

Parse tree , 230PDA, 257, 263

`, 258ID, 258Instantaneous description, 258

PDA acceptance, 259by empty stack, 262by final state, 259

PDA to CFG, 267Periodic

ultimate, 91Pigeon, 104Pigeon-hole principle, 104

Page 358: Computation Engineering Applied Automata Theory and Logic

350 INDEX

Power of machines, 3, 81Prefix closure, 133Production, 229

elementary, 231Proof, 89

reductio ad absurdum, 93axiom, 89by contradiction, 93machine-checked, 107mistakes, 105model-checker, 107of a statement, 89reliability, 105rule of inference, 89social process, 107theorem prover, 107

Purely left-linear , 250Purely right-linear , 250Push-down automata, 257Push-down automata , 246

RE, 152, 155closure, 152Complementation, 156DeMorgan’s Law, 156

Reachabilityin graphs, 79

Recognize, 140Recursion, 109

nonsensical, 110solution of equations, 115solving an equation, 110

Recursive definition, 92Reflexive Transitive closure, 79Regular Expression, 155

to NFA, 189Regular expressions, 152Rejecting state, 135

Relation, 52antisymmetric, 74asymmetric, 74binary, 52, 73codomain, 52complement, 53domain, 52equivalence, 77functional, 53functions viewed as, 53identity, 78inverse, 52irreflexive, 73partial functions as, 53partial order, 76preorder, 75reflexive, 73restriction, 53, 78single-valued, 53symmetric, 74ternary, 52total, 76total functions as, 53total order, 76transitive, 74unary, 52universal, 78

Resistor, 75Respect concatenation, 130Run, 121

Scanner, 168Telephone number, 168

Schroder-Bernstein Theorem, 64Nat→ Bool, 65all C programs, 64

Second-order logic, 95Sentence , 230

Page 359: Computation Engineering Applied Automata Theory and Logic

INDEX 351

Sentential form , 230Set, 35

Real versus Nat, 64cardinality, 57complement, 39comprehension, 36countable, 58empty, 36intersection, 39numbers as, 40powerset, 36, 42proper subset, 39recursively defined, 38subtraction, 39symmetric difference, 40union, 39unique definition, 38universal, 38, 39

Start symbol , 229String, 121, 122

ε, 122length, 124substr, 124empty, 122lexicographic order, 124numeric order, 125reversal, 130

Stringsin living beings, 122

Structural inductionproof by, 99

Symbol, 121

Term, 75Terminals , 229TR, 9, 124Transition function, 140

δ, 137

δ, 140for strings, 140

Triple, 41Tuple, 41Turing recognizable, 9, 124Type, 40

versus sets, 40

Ultimate Periodicity, 197Ultimately periodic, 91Unique definition, 38

function, 92, 110functions, 100sets, 38

Uniqueness, 92Universal relation, 78

Well-founded partial order, 102Wierd but legal C program, 65Wildcard, 155

Xerox machinefixed-point, 112image transformation, 112

Y function, 112