FA Transition Graphs

Embed Size (px)

Citation preview

  • 8/3/2019 FA Transition Graphs

    1/9

    Mathematical Foundations

    Finite Automata (FA)

    (Based on Chapter 5 of the textbook Cohen (1997) Introduction to Computer Theory, 2ndEdition, New York, NY: John Wiley & Sons)

    Consider a wall switch that turns a light on and off. If the switch is up the light is on. If itis down the light is off. There are two possible states the switch can be in. Now considera hair dryer with two switches that can each be either up or down. The hair dryer can bein one of four states at any point in time (up/up, up/down, down/up, down/down.)

    Now consider a game of tic-tac-toe. Each possible combination of X's and O's on aboard is a different state. In tic-tac-toe the empty board is always the starting state. At

    each step of the game one player makes a move which consists of putting an X or an O(depending on whose turn it is) into one of the 9 possible positions. If we think of a gameof tic-tac-toe as a machine which receives input, the inputs consist of a character and aposition. The machine starts in the empty state and changes states every time itreceives an input. Certain states are special in that when they are reached, the game isover. We call such states final states. Certain final states mean a win for X and othersmean a win for O. Still others mean the game is a draw.

    Instead of the term final state, we might use terminal state, or accept state, which isthe preferred term.

    The general model of such a machine is called a finite automaton. The word finite

    refers to the fact that the machine has a finite number of states. Formally we define afinite automaton as follows.

    A finite automaton is a collection of three things:

    1. A finite set of states, one of which is a start state, and some of which may bedesignated as accept states.

    2. An alphabet .3. A finite set of transitions that tell for each state and for each letter of the

    alphabet which state to go to next.

    The input to a finite automaton is a string of letters from the alphabet, finite in length, thatis read left to right one letter at a time. Beginning in the start state, the machine reads aletter and makes a transition over and over until the string ends. If this process ends withthe machine in an accept state, we say that the string is accepted. Otherwise the stringis rejected.

  • 8/3/2019 FA Transition Graphs

    2/9

    We draw a Finite Automaton (FA) in the following manner: The machines states arerepresented by circles and transitions are represented by arrows. Every transition is

    marked by a symbol from the alphabet . The start state is marked by a sign andthe final states are marked by a + sign as shown in the FA for a*ba*.

    The finite automaton given above has one start state. Of course, every finite automatonhas exactly one start state by definition. The above finite automaton has one final statealthough finite automata are allowed to have zero or more final states. The set of stringsaccepted by a finite automaton is referred to as the language accepted by the finiteautomaton (or the regular expression defined by the finite automaton). The above finiteautomaton accepts the language defined by a*ba*. The intuitive explanation is that themachine starts at the start state and can take the loop transition marked by the symbola from the start state zero or more times and then take the arrow marked by b andreach the final state where it can take the loop transition marked by a as many time asit likes and finish at the final state marked by +. From the final state, if the transitionmarked by b is taken then the machine will reach the non-final state that has two loops.However, it does not add to the language the machine accepts. Because, a finiteautomatas accepted language is defined by tracing the paths from the start stateto the final state(s). Paths ending in the non-final states do not contribute to theaccepted language of the machine.

    We draw another Finite Automaton (FA) below which accepts any string from ab*.

  • 8/3/2019 FA Transition Graphs

    3/9

    Please note that the Non-Deterministic Finite Automaton (NFA) for the same regularexpression, ab*, is given below which looks simpler. Why? Because in order to meet

    the definition of FA there should be one transition for every element of from eachstate. .

    The above notation for finite automata is given in Daniel I. Cohen, 1997,Introduction to Computer Theory , 2nd Edition. Finite automata can be drawnwith different notations. Please note that some books show the start state withan incoming arrow that is not attached to any other state (instead of a sign).Accept states or final states are often drawn as double concentric circles in somebooks (instead of a + sign).

    We draw another finite automaton which accepts all strings from a*bb*aa*(ba*bb*aa*)*.

  • 8/3/2019 FA Transition Graphs

    4/9

    Please note that some books show the start state with an incoming arrow that is notattached to any other state (instead of a sign). Accept states or final states are oftendrawn as double concentric circles in some books (instead of a + sign). States are givennames (0,1,2) to make discussions of the behavior of the machine easier. Transitionsare shown as labeled arcs from one state to another or from a state back to itself. In theabove, if the machine is in the start state 0 and it reads an a, the transition leaves state 0

    and reenters state 0. If it reads a b from state 0, the machine enters state 1.

    It is common for each state to have a transition leaving it for each letter of the alphabet.The above machine, when given the string bbabba will end up in state 2 and accept thestring. If it reads string abbbab it ends up in state 0 and does not accept the string. Wesay that the machine rejects abbbab.

    Note that the author uses a slightly different schema for drawing finite automata. He putsa minus sign in the start state and plus signs in the accept states.

    The set of strings accepted by a finite automaton is referred to as the languageaccepted by the finite automaton. We might describe a finite automaton as alanguage recognizer whereas a regular expression is a language generator.

    For each finite automaton there is a regular expression that defines the same language.Later we will learn an algorithm for determining the regular expression, but sometimeswe can figure it out using our common sense. Look back at the above finite automaton.A regular expression corresponding to that machine is a*bb*aa*(ba*bb*aa*)*. Note thatthe portion before the parentheses moves you from the start state to the accept state.The portion in the parentheses moves from the accept state back to the accept state andthis expression is "starred" because you can repeat this cycle as many times asnecessary.

    Another way to represent a finite automaton is with a transition table. Here is the tablefor the above machine. The rows correspond to states, the columns correspond tocharacters from the alphabet, and the cell contents correspond to the transitions of themachine.

    State a B

    0 0 1

    1 2 1

    2 2 0

    A drawing of a finite automaton is easier for a human to understand than a table, butimplementing a machine with a computer program requires storing the finite automaton'stransitions in a table. There is not just one finite automaton for a given language.Typically it is possible to draw the finite automaton in any of several different ways.

    There are languages for which it is not possible to draw any finite automaton. Forexample, there is no FA for L3 = { a

    nbn: where n > 0 }. Later we will show that anylanguage that can be described by a regular expression can also be described by a finiteautomaton and vice versa.

  • 8/3/2019 FA Transition Graphs

    5/9

    A regular expression for the above machine is (baa + abb)*. Can you find a regularexpression for this next one?

    If you try running some strings through the most recent machine you may notice that

    what it takes to get to the accept state is to see either two a's in a row or two b's. So theregular expression for the machine is (a+b)*(aa+bb)(a+b)*.

    Let's try one more:

    Notice that this machine will accept any string consisting entirely of a's. It will also acceptany string in which the number of b's is divisible by 3. Since the start state is not anaccept state, the machine does not accept . A regular expression for the machine is (a+ ba*ba*b)(a + ba*ba*b)*.

  • 8/3/2019 FA Transition Graphs

    6/9

    How do you create a finite automaton that will accept any string with an odd number ofa's? As the machine reads a's, you need to move back and forth between two states,one where the number of a's so far is even and one where the number is odd. Since weare not concerned with the number of b's, each state should have a self-transition for a b(a transition that leaves and enters the same state.) Which of the two states should bethe start state? Since 0 is even and at the beginning we have seen 0 a's, the start state

    should be the state that corresponds to having seen an even number of a's. The otherstate, the one for having seen an odd number of a's, is an accept state. A machine thataccepts strings with an even number of a's would look just like the one for odd a's exceptthat the other state is the accept state.

    What if we are concerned about the parity (oddness or evenness) of both a's and b's?For example, we might want to accept strings that contain an odd number of a's and aneven number of b's. To figure out how many states we need we must ask how manydifferent combinations of odd and even there are for two letters. The answer to thisquestion is four: even/even, even/odd, odd/even, and odd/odd. The even/even state isthe start state. Which states are accept states depends on the language we wish toaccept. If we want strings that contain an odd number of a's and an even number of b's

    then we make the state odd/even be the only accept state. If we want strings in whichthe parity of a's matches the parity of b's then we make both even/even and odd/odd beaccept states.

    Here is one final example of a finite automaton. What strings are accepted by this one?

    There are two accept states in this machine. In order to get to the top one, the first letterof the string must be a and the last letter must be b. In order to get to the bottom one,the first letter must be b and the last one a. So this machine accepts strings that startand end with a different letter. There is no way to do this job with a single accept state.The machine must remember what the first letter was by taking one or the other of thetransitions off of the start state. Once the machine has taken either the "high road" or the"low road" it cannot leave that portion of the machine or it would "forget" the first letter.

  • 8/3/2019 FA Transition Graphs

    7/9

    The following Non-deterministic Finite Automaton would accept any string from the

    regular expression b*aba*b.

    Non-deterministic Finite Automata are distinct from the Deterministic FA or FA

    because they do not require one outgoing transition for each element of from every

    state. However, it is proven by Kleene that Non-deterministic Finite Automata are

    equivalent to Deterministic FA. The FA (i.e. the Deterministic FA ) for b*aba*b isgiven below:

  • 8/3/2019 FA Transition Graphs

    8/9

    Chapter 6 - Transition Graphs

    In this chapter we define a new type of finite state machine called a transition graph.These machines were invented in 1957 by John Myhill in order to simplify the proof of atheorem known as Kleene's Theorem.

    A Transition Graph (TG) is a nondeterministic finite automaton with null transitions. Thefollowing features of TGs are emphasized:

    1. We allow the labels on the transitions of a transition graph to be strings inaddition to single characters (although we prefer single characters).

    2. We allow as a transition label. This is interpreted as a "free move" from onestate to another, a move that does not involve reading any input.

    3. We allow more than one start state.

    4. We allow the machine to read more than one character at a time.5. There can be two transitions leaving a state that both have the same label.

    Here is an example of a transition graph that illustrates some of the quirks of suchmachines. In this machine there are two different accepting paths that the machine maytake on the string baab. There is no path at all for strings other than baab, so the stringabb is rejected as are all strings other than baab.

  • 8/3/2019 FA Transition Graphs

    9/9

    If any path for a particular input leads to an accept state, we say the string is accepted.What if one path for string X leads to an accept state and one to a non-accept state?Then that string is accepted.

    Self-Checking Exercises: (You are encouraged to do these exercises. You arenot required to submit them)

    1. Construct a Finite Automaton that accepts only the word baa.

    2. Construct a Transition Graph that accepts only the word baa.

    3. Build a Transition Graph that accepts only those words that have more than 4letters.