390 likes | 407 Views
This midterm review covers topics such as computability, Turing machines, finite state machines, and pushdown automata. It includes examples of different sorting algorithms and explores the notion of computability and complexity. The review also delves into the history of theoretical computer science and the study of computability.
E N D
Computability, etc. Midterm review. Turing machines. Finite state machines. Push down automata. Homework: FSA, PDA, TM problems (can work in teams)
Midterm • go over…
Theoretical Computer Science • Notion of computability • what it means to be computable • models of different levels • Complexity • what it takes in terms of resources to solve a class of problems • Example: one sorting algorithm takes O(n2), another takes O(n*log(n))
Benefits Practical as well as theoretical • Sharpen logical/thinking/problem solving skills • Learn history • Appreciate when good solutions may be appropriate
Background • The study of computability preceded the invention of computers. • What does it mean for something (a function, a set of numbers) to be computed/calculated/evaluated in a mechanical, procedural way? • Related to a problem posed by Hilbert in 1900 • Solved by Alan Turing, and others (Post, Church) in the early 1930s.
Posting topic? • How old are computers?
Turing work • Defined an abstract machine (is this a contradiction in terms?): came to be called a Turing Machine for computing specific functions… • ALSO defined set of functions called the recursive functions, made up by a starter set of functions with certain ways of building on functions to get new functions • Note: this definition of a recursive function is more general than "a function that calls itself". Primitive recursion is one of the building methods. • Proved any function computed by a Turing Machine was a recursive function AND any recursive function can be computed by a Turing Machine. • Also proved these two definitions equivalent to one made by Post • Many other functions proved equivalent
Turing machine Imagine • An [infinite] tape with cells or slots. Each cell can hold a symbol (from a finite alphabet) or a blank • A specific cell is under the machine head where it can be read • The machine is in one of a finite number of states • For each state reading a given symbol, there is a fixed set of actions: • Optionally, erase and write a new symbol + • Optionally, change to another state + • Move left, right, or stay fixed (halting)
Turing machine function • A given Turing machine computes a function F(input) output by • representing the input on the tape and placing the head to the left of the input • Starting the TM in a special state called the Start state • If the function halts, then the content on the tape represents the output using an agreed upon representation. • Related formulism: a TM recognizes a set of numbers or a set of strings of symbols (aka a language) if the TM halts in a state designated as an accepting state on each number/string in the set AND halts in a state designated as a rejecting state on numbers/strings not in the set.
Example: adding 1 • Let the alphabet be B (for blank) and 1 • Encode positive integers (0, 1, 2,…) by N+1 1s for input (so zero can be represented) • Encode (interpret) output by counting up all the 1s on the tape (alternative: require one more 1.) • States are Start state S, W, F for final • Start head at start of input
Example: Adding 1Definition of output exact # of 1s Three states: S, W, F • State S: • Sees a 1, moves to the right and changes to State W. • Sees a blank, does nothing • State W: • Sees a 1, moves to the right. • Sees a blank, changes to State F. • State F: does nothing (halts) May not be needed CHECK IT with a couple of examples.
Example: adding 1Definition of output: 1 more Three states: S, W, F • State S: • Sees a 1, moves to the right and changes to State W. • Sees a blank, does nothing (invalid input) • State W: • Sees a 1, moves to the right. • Sees a blank, writes a 1, changes to State F. • State F: does nothing (halts) May not be needed CHECK IT with a couple of examples.
Classwork:adding two integers • Encode the two integers N and M by placing N+1 1s followed by 1 blank followed by M+1 1s • Make output be (N+M)+1 1s contiguously on the tape • Alternative: use the encoding of exactly N+M 1s anywhere on the tape.
Overview • Define different types of machines. • abstract machines • Machines are fed (my term) strings in a specified alphabet. • Machines scan the strings and say yes or no (accept or reject) • Each machine accepts strings in a certain pattern. We call the set of strings in that pattern a language.
Overview • Repeat: a language is a set of strings. • This set may be finite or infinite.
Example • The [written, formal] English language is an infinite set of sentences made up of valid words, spaces and punctuation. • Why infinite?
Example • The set of all legal (acceptable, no syntax errors) Processing programs is a language. • It also is infinite.
Overview, cont. • Each machine a language • Each type of machine set of languages • Presumably, more powerful machines (machines with more capability) could accept a larger set of languages. • It will turn out that some changes are less powerful than you would think…
Aside • Turing Machines represented/implemented functions: • get input, produce output: an answer. • These other machines: the input is a string of symbols in a fixed alphabet. The output is yes or no • or some other indication of yes, this is a string in the language; or no it is not.
Alphabets • 2 symbols 0 and 1 or • abcdefghijklmnopqrstuvwxyz or • subset of the alphabet, say {a, b, c} or • all symbols used in languages such as JavaScript, Processing, etc. • It will turn out that alphabets can be represented by strings of 0 and 1…
Finite State Machine • also called finite state automaton. Plural is automata. • A FSM has a finite number of states! • express by writing out s0, s1, s2, etc. or drawing circles. • One state is specified as the initial state. • One or MORE states are specified as Final or Accepting states.
FSM, cont. • For each state, there is a state transition table: for each symbol in the alphabet, there is a switch to another state or to stay in the same state. • Draw this by a graph: representing the states by nodes and the edges by the state transition table.
Exercise • Draw the graph • Initial state s0: 0 go to s1, 1 go to s2 • s1: 0 go to s1, 1 go to s3 • s2: 0 go to s3, 1 go to s2 • s3: 0 go to s3, 1 go to s3 • s1 and s2 are accepting states. • What strings does it accept?
Problem • Let's design a FSM that will accept strings over {0,1} of length 3 and nothing else.
Approach Take problem and think about different conditions that could arise and you need to keep track of… These will be the different states, or at least a first guess at the different states. • For the "length 3" problem • start • length 1 • length 2 • length 3 • more than length 3
Problem • Alphabet {a, b, c, d}. Language is all strings with at least one instance inside the string of abc. • What are possible states?
Start of approach for abc States • start • a • ab • abc • a and then something not a b. But maybe that can put us back at the start. • ab and then something not a c. But maybe that can put us back at the start • abc and anything else. But that can just keep us at the abc stage.
Finish the problem • For the language containing all strings that have at least one abc inside. • States: • start • a • ab • abc What are the accepting state(s)? What are the edges?
Your turn • Each student draw a FSM over {0,1} • Find a partner and try each other's FSM on sample strings.
Tricky but important • The empty string is a string! • Some languages may contain it, some may not. • Design a FSM that accepts the empty string and only the empty string.
Definition • A language is called a regular language if there is a finite state automaton that accepts it. • feed in a string in the language L and the FSM ends in an accepting state. • feed in a string that is NOT in the language and the FSM ends in a non-accepting state. • You will need to check both things.
Cautions • Empty set is not the empty string. • The set consisting of one string, the empty string, is NOT empty. • The set with no members, that is, consists of no strings, is a regular language. • What is a FSM for this language? • Some regular languages are finite sets and some are infinite sets.
Stack • Standard data structure used in programming. • SOMETIMES it is built-in, say a Stack class. • stack.push(x) adds x to the end • x = stack.pop(); removes top element • Last In: First Out (LIFO) • NOTE: queue is the name for data structure LILO or FIFO
Push down automata • FSM + a stack • stack is last in / first out. Reading and adding done only at the top. Adding is called pushing. Reading is done by popping the first item • Non-deterministic Push Down automata recognize MORE languages than deterministic push down automaton. • Non deterministic PDA are equivalent to Context Free languages so we focus on them and call them PDA…
Definition of PDA • Finite set of States. Input alphabet. Stack alphabet. Starting state. Subset of States are accepting states. • Transition function:State, Input or ∊,Top of Stack or ∊ State and Stack That is, for each combination of state, reading input, reading top of stack, the transition can be to new state, pushing a new symbol onto stack. Plus machine can do something without reading an input symbol and/or top of stack.
A PDA = (Q,A,K,d,q0,F) • Q are the states. A is the input alphabet, K is the stack language (it can contain all or some of A plus other symbols), d is the transition function, q0 is the first state, F is the subset of Q that are the accepting states. NOTE: x's here indicate what is called the cartesian product. • d is a function from Q x A∊ x K∊ to Q x Kd(q,a, k) (r,m)Function d for any state, input letter or ∊, top of stack MAY change state or remain at current state and may pop or push or pop AND push symbol onto the stack.
PDA accepts a string A string of letters w=w1w2..wk in A is accepted by the PDA • if there is a sequence of states r1,r2,..rm and strings s0s1..sm (representing the stack) • where r1 is q0 and s0 is ∊ (Start at the start state and with an empty stack.) • rm is in F (an accepting state). • For all i, (ri+1,b) is in d(ri,wi+1,a) where si = at and si+1 = bt for some a, b in K∊ and t in K* That is, the sequences obey the transition function. The transition function may have multiple values!
Preview • There are different definitions/constructions/concepts that define the same set of functions or languages. • So-called recursive functions (more later) correspond to computable (TM) functions. • Languages defined by a Regular Expression (look it up) are languages recognized by a FSA. • Languages defined by a Context Free Grammar (look it up) are languages recognized by a PDA.
Homework • Define a FSA, alphabet {A,blank}, that accepts strings with even number of As. Define a second FSA that accepts strings with an odd number of As. • Define a PDA, alphabet {A, blank,(, ),+} that accepts A, (A, A+A, (A+A), (A+(A+A)) etc. NOT A+ , not unbalanced ( and ). • Define TM that subtracts 1. If input is zero, then tape is cleared. • Define Turing machine to subtract two numbers a and b, represented by a+1 1s followed by a blank followed by b+1 1s • if (a>=b), put (a-b) + 1 1s on the tape • if a<b, leave zero 1s in the tape OR return something else if a<b ???