3.44k likes | 3.55k Views
CS 461 – Aug. 24. Course overview Section 1.1 Definition of finite automaton (FA) How to draw FAs. Overview. What is Computational Theory? Not so concerned with specific HW/SW Search for general truths, models, imagination Ex. What is possible vs. impossible? Facets
E N D
CS 461 – Aug. 24 • Course overview • Section 1.1 • Definition of finite automaton (FA) • How to draw FAs
Overview • What is Computational Theory? • Not so concerned with specific HW/SW • Search for general truths, models, imagination Ex. What is possible vs. impossible? • Facets • Automata & Formal Languages (Ch. 1 – 3) There are 3 important models of computation. • Computability (Ch. 3 – 5) “Some problems can’t be solved.” • Complexity (Ch. 7) “Some problems take too long to solve.”
Theory • What is theory? • You have already dealt with abstract ideas in earlier courses • Difference between void, null, zero • Different kinds of zeros (int, float, positive, negative) • Infinity versus NaN • Correctness of an algorithm • Number of iterations in a loop • Does recursion really work?
Computation Models • Purpose of a model is to study what computers and programs can do. • Models range from simple to complete: • Model #1: Finite Automaton • Model #2: Pushdown Automaton • Model #3: Turing Machine • Assume programming essentially boils down to input outputs. • For simplicity / convenience, • input = any binary string • output = 0/1, T/F, accept/reject
Finite Automaton • Our first model – Chapter 1 • Purpose of FA is to take (binary) input string and either “accept” or “reject” it. • The set of all strings accepted by an FA is called the language of the FA. • “Language” simply means a set of strings, e.g. the set of all binary strings starting with “0”.
Example • Start state • Accept/happy state(s) • If we are in one of these when input is done, we accept string. • Transitions • “finite” refers to # of states. More on limitations later. 0 s1 s2 1 0, 1
Example • The FA could also be expressed in a table. • The table tells us where to go on each possible input bit. 0 s1 s2 1 0, 1
Example • Let’s try sample input like 101. • Can you figure out the language of this machine? • i.e. How do we get to the happy state? 0 s1 s2 1 0, 1
Example #2 0 1 • What is language of this FA? • Note: if we change which state is the accept state, we’d have a different language! s1 s2 s3 0 1 0, 1
CS 44 – Aug. 26 • Finish section 1.1 • Definition of FA • Examples • Regular languages • Union of 2 regular languages (handout)
Formal Definition A finite automaton has 5 things: • A set of states • An alphabet, for input • A start state • A set of accept states • Transition function: δ(state, input) = state • When we create/define an FA, must specify all 5 things. A drawing does a pretty good job.
Example 0 1 s1 s2 s3 0 1 0, 1
Let’s make FA’s • L = bit strings that have exactly two 1’s • L = starts with 01 • L = ends with 01 • L = has an even number of 1’s • L = starts and ends with same symbol *** Very good idea to give meaningful names to your states. Since L is a set… how would we create an FA for the complement of L?
Regular Language • A regular language is a set of strings accepted by some FA. • Examples • Starting with 01; ending with 01, containing 01. • Strings of length 2. • {0, 11} • Any finite set is regular. Infinite sets are more interesting. Yet, # states always finite!
Operations on sets • We can create new regular sets from existing ones, rather than starting from scratch. • Binary operations • Union • Intersection • Unary operations • Complement • Star: This is the concatenation of 0 or more elements. For example, if A = {0, 11}, then A* is { ε, 0, 11, 00, 1111, 011, 110, …} • “Closure property”: you always get a regular set when you use the above operations.
Union • Book shows construction (see handout) • We want to union two languages A1 and A2: M1 accepts A1 and M2 accepts A2. The combined machine is called M. • M pretends it’s running M1 and M2 at same time! δ((r1,r2),a) = (δ1(r1,a),δ2(r2,a)) • # states increases because it’s the Cartesian product of M1 and M2’s states. • Next section will show easier way to do union.
Wolf, Goat, Cabbage • A man would like to cross a river with his wolf, goat and head of cabbage. • Can only ferry 1 of 3 items at a time. Plus: • Leaving wolf & goat alone: wolf will eat goat. • Leaving goat & cabbage alone: goat will eat cabbage. • Yes, we can solve this problem using an FA! • Think about possible states and transitions.
CS 44 – Aug. 29 • Regular operations • Union construction • Section 1.2 - Nondeterminism • 2 kinds of FAs • How to trace input • NFA design makes “union” operation easier • Equivalence of NFAs and DFAs
Wolf, Goat, Cabbage • A man would like to cross a river with his wolf, goat and head of cabbage. • Can only ferry 1 of 3 items at a time. Plus: • Leaving wolf & goat alone: wolf will eat goat. • Leaving goat & cabbage alone: goat will eat cabbage. • Yes, we can solve this problem using an FA! • Think about possible states and transitions.
Operations on sets • We can create new regular sets from existing ones, rather than starting from scratch. • Binary operations • Union • Intersection • Unary operations • Complement • Star: This is the concatenation of 0 or more elements. For example, if A = {0, 11}, then A* is { ε, 0, 11, 00, 1111, 011, 110, …} • “Closure property”: you always get a regular set when you use the above operations.
Union • Book shows construction (see handout) • We want to union two languages A1 and A2: M1 accepts A1 and M2 accepts A2. The combined machine is called M. • M pretends it’s running M1 and M2 at same time! δ((r1,r2),a) = (δ1(r1,a),δ2(r2,a)) • # states increases because it’s the Cartesian product of M1 and M2’s states. • Next section will show easier way to do union.
Union • Book shows construction (see handout) • We want to union two languages A1 and A2: M1 accepts A1 and M2 accepts A2. The combined machine is called M. • M pretends it’s running M1 and M2 at same time! δ((r1,r2),a) = (δ1(r1,a),δ2(r2,a)) • # states increases because it’s the Cartesian product of M1 and M2’s states. • Next section will show easier way to do union.
Non-determinism • There are 2 kinds of FA’s • DFA: deterministic FA • NFA: non-deterministic FA • NFA is like DFA except: • A state may have any # of arrows per input symbol • You can have ε-transitions. With this kind of transition, you can go to another state “for free”. • Non-determinism can make machine construction more flexible. At first the theory seems more complex, but NFA’s will come in handy.
Example 1 0, ε 1 s1 s2 s3 s4 0, 1 0, 1
continued 1 0, ε 1 • See the non-determinism? • Remember, any time you reach a state that has ε-transitions coming out, take it! It’s free. • Let’s trace input 010110. s1 s2 s3 s4 0, 1 0, 1
1 0, ε 1 s1 s2 s3 s4 0, 1 0, 1
Moral • When tracing a word like 010110, we just want to know if there is any way to get to the accept state. • Language is anything containing 11 or 101. • Corresponding DFA would have more states.
CS 461 – Aug. 31 Section 1.2 – Nondeterministic FAs • How to trace input √ • NFA design makes “union” operation easier • Equivalence of NFAs and DFAs
NFA’s using “or” • Can you draw NFA for: { begin with 0 or end with 1 } ? Old start 1 ε New start Old start 2 ε
Amazing fact • NFA = DFA • In other words, the two kinds of machines have the same power. • Proof idea: we can always convert a DFA into an NFA, or vice versa. Which do you think is easier to do?
Formal NFA def’n • The essential difference with DFA is in the transition function: DFA δ: Q x Σ Q NFA δ: Q x Σε P(Q) • Thus, converting DFA NFA is easy. We already satisfy the definition!
NFA DFA construction • When creating DFA, states will be all possible subsets of states from NFA. • This takes care of “all possible destinations.” • In practice we won’t need whole subset: only create states as you need them. • “empty set” can be our dead state. • DFA start state = NFA’s start state or anywhere you can begin for free. Happy state will be any subset containing NFA’s happy state. • Transitions: Please write as a table. Drawing would be too cluttered. When finished, can eliminate useless states.
Example #1 • NFA transition table given to the right. • DFA start state is {1, 3}, or more simply 13. • DFA accept state would be anything containing 1. Could be 1, 12, 13, 123, but we may not need all these states.
continued • The resulting DFA could require 2n states, but we should only create states as we need them. Let’s begin: If we’re in state 1 or 3, where do we go if we read an ‘a’ or a ‘b’? δ(13, a) = 1, but we can get to 3 for free. δ(13, b) = 2. We need to create a new state “2”. Continue the construction by considering transitions from state 2.
NFA DFA answer Notice that the DFA is in fact deterministic: it has exactly one destination per transition. Also there is no column for ε.
Example #2 • NFA transition table given to the right. • DFA start state is A. • DFA accept state would be anything containing D.
continued Let’s begin. δ(A, 0) = A δ(A, 1) = AC We need new state AC. δ(AC, 0) = A δ(AC, 1) = ABC Continue from ABC…
NFA DFA answer
final thoughts • NFAs and DFAs have same computational power. • NFAs often have fewer states than corresponding DFA. • Typically, we want to design a DFA, but NFAs are good for combining 2+ DFAs. • After doing NFA DFA construction, we may see that some states can be combined. • Later in chapter, we’ll see how to simplify FAs.
CS 461 – Sept. 2 • Review NFA DFA • Combining FAs to create new languages • union, intersection, concatenation, star • We can basically understand how these work by drawing a picture. • Section 1.3 – Regular expressions • A compact way to define a regular set, rather than drawing an FA or writing transition table.
Example #2 • NFA transition table given to the right. • DFA start state is A. • DFA accept state would be anything containing D.
continued Let’s begin. δ(A, 0) = A δ(A, 1) = AC We need new state AC. δ(AC, 0) = A δ(AC, 1) = ABC Continue from ABC…
NFA DFA answer
Moral • NFAs and DFAs have same computational power. • NFAs often have fewer states than corresponding DFA. • Typically, we want to design a DFA, but NFAs are good for combining 2+ DFAs. • After doing NFA DFA construction, we may see that some states can be combined. • Later in chapter, we’ll see how to simplify FAs.
U and ∩ • Suppose M1 and M2 are DFAs. We want to combine their languages. • Union: We create new start state. √ • Do you understand formalism p. 60 ? • How can we also do intersection? Hint: A ∩ B = (A’ U B’)’
Concatenation • Concat: For each happy state in M1, turn it into a reject state and add ε-trans to M2’s start. • Example L1 = { does not contain 00 } L2 = { has even # of 1’s } Let’s draw NFA for L1L2. • Let’s decipher formal definition of δ on p. 61.
Star • We want to concat the language with itself 0+ times. • Create new start state, and make it happy. • Add ε-transitions from other happy states to the start state. • Example L = { begins with 1 and ends with 0 } Let’s draw NFA for L*. • Formal definition of δon p. 63.
Regular expression • A concise way to describe a language • Text representation, straightforward to input into computer programs. • Use alphabet symbols along with operators + means “or” * means repetition Concatenation • There is no “and” or complement.
Examples • What do you think these regular expressions mean? 0* + 1* 0*1* 00*11* (a shorthand would be 0+1+) (0 + 1)* • What’s the difference between 10*1 and 1(0+1)*1 ? Does this mean “anything with 1 at beginning and end?”
Practice • Words with 2 or more 0’s. What’s wrong with this answer: 1*01*01 ? • Words containing substring 110. • Every even numbered symbol is 0. • What’s wrong with: ((0 + 1)*0)* ? • Words of even length. • The last 2 symbols are the same. • What is the shortest word not in: 1*(01)*0* ? • True or false: (111*) = (11 + 111)*