140 likes | 202 Views
Nondeterminism and Nodeterministic Automata. Nondeterminism and Nondeterministic Automata.
E N D
Nondeterminism and Nondeterministic Automata The computational machine models that we learned in the class are deterministic in the sense that the next move is uniquely determined by the current state and the input (i.e., external stimulus). Thus, the state transition graphs of DFA's have one (and only one) edge for each input symbol. (Recall that, just for convenience, we agreed not to draw the edge for a transition with an input symbol leading to the dead state.) Deterministic machines follow our intuition, because the models are based on the determinism, which propose that, for a given cause (current state of the system and external input), the effect (state change and the output of the system) is uniquely determined. Nondeterminism does not go along our intuition because it allows multiple conflicting effects for a given cause. Consider the state transition graph shown on the following page, which has transitions which violate the determinism. For example, the transition (1, a) = {1, 2} says that in state 1 the automaton, reading input symbol a, enters states 1 and 2. (Note that the machine enters states 1 and 2 at the same time.We call such machines nondeterministic finite automata (NFA).
a, b b 2 a 4 a b 1 a b 3 Nondeterministic Automata (cont’ed) Nodeterministic FA: Nondeterministic transitions: (1, a) = {1, 2} (4, b) = {2, 3, 4} * (2, ) = {2, 3} Deterministic transitions: (1, b) = {3} (3, a) = {4} (4, a) = {2} * Notice that in any state p, the machine stays in the same state while there is no input, which implicitly there is (p, ) = {p}. Therefore, if there is an explicit transition, like from state 2 to state 3 in the above graph, the transition is nondeterministic, because (2, ) = {2, 3}. We will deal with such -transitions later as a separate topic. Until then we study nondeterministic automata with no -transitions.
Nondeterministic Automata (cont’ed) If an FA has transition (p, a) = {q, r}, we say that the machine, in state p reading a, nondeterministically enters states q and r. After taking this transition, the machine is in both q and r. We think that two machines, one in qand the other in r, continue the computation independently for the same input. Formally an NFA M is defined by the same tuple M = (Q, , , q0, Z0, F ) as for DFA. The only difference is the transition function; : Q 2Q ,which implies that in a state in Q, reading a symbol in , the machine can enter simultaneously in some states (i.e., a subset of Q). Notice that DFA is a special case of NFA which for an input symbol, takes a transition to a single state. For a string x, let (q0, x) = S, where S is the set of states that the machine enters after reading string x. The language accepted by an NFA M is defined as follows; L(M) = {x| x {a,b}*, (q0, x) = S, and S F }. Notice the accepting condition, S F . This implies that if there exists at least one sequence of transition that ends up in an accepting state, then we say the input xis accepted. All non-accepting cases are ignored. If there is no accepting case (i.e., S F =), then we say the input is rejected.
q0 b b q0 q1 b b b a,b q1 b q0 q1 X b b b a b q0 q0 q1 X a a q2 b q1 q2 Figure (a): An NFA Figure (b): The profile of nondeterministic transition for input string bbba. Nondeterministic Automata (cont’ed) For example, consider the NFA shown in Figure (a) below. Figure (b) shows all possible sequences of transitions for input bbba, where nodes with label X denote the dead state which is not shown on the state transition graph. String bbba is accepted by the machine since there exists a sequence of transitions, which ends in an accepting state (q2 in the figure). Note that NFA's are similar to parallel machines in the sense that they explore an answer simultaneously in more than one way. However, nondeterministic machines do not produce (or announce) the final result out of all possible sequences of operations. We, the user, must search all possible terminating states of the machine and decide whether it satisfies the accepting condition or not.
a a, b a {xaa | x {a, b}* } b a b a, b a a, b a, b {x | x {a, b}* and there is aa followed by bb somewhere in x} Nondeterministic Automata (cont’ed) Designing NFA:
(b, a/ba) 2 (a, b/ab), (, b/ ) Nondeterministic transitions: (1, a, b) = {(2, aa), (3, aa)} (3, b, a) = {(3, ), (3, ba)} (4, a, b) = {(4, ab)} (4, , b ) = {(2, )} Deterministic transitions: (2, a, b) = {(3, ab)} (3, a, b) = {(4, ab)} (3, b, b) = {(2, bb)} (4, b, a) = {(2, ba)} (a, b/aa) 4 (a, b/ab) }* (b, b/bb) 1 (a, b/ab) 3 (a, b/aa) (b, a/ ), (b, a/ba), (a, a/aa) Nondeterministic Automata (cont’ed) Nodeterministic PDA: Notice that a PDA is nondeterministic if there is a state q, an input symbol t, and a stack symbol s such that either (a) | (q, t, s) | > 1, or (b) | (q, t, s) | 0 and | (q, , s) | 0 (see case * above)
(Y, X/YX ) X, Y {a, b} ( , Z0/Z0 ) q1 q3 (X, Z0/X Z0 ) (, X/X ) q2 start q0 (X, X/ ) Figure (a): NPDA accepting {xxR | x {a, b}+ } Nondeterministic Automata (cont’ed) Designing NPDA:
(a, a/ aa) (b, a/) (b, a/) (c, Z0/ Z0) (a, Z0/aZ0) (c, Z0/ Z0) (b, b/bb) (a, Z0/Z0) (, Z0/ Z0) (c, b/ ) start (b, Z0/bZ0) (a, Z0/Z0) (c, b/ ) Nondeterministic Automata (cont’ed) Designing NPDA: Figure (b): NPDA accepting {aibjck| i, j, k > 0, and i = j or j = k }
(b, a, R), (b, b, R) (b, a, R) (a, b, R) 2 (b, a, L) 4 1 (a, b, R) (a, b, R) 3 (a, b, R), (a, b, L), (b, a, L) Nondeterministic Automata (cont’ed) Nodeterministic TM and LBA: Nondeterministic transitions: (1, a) = {(2, b, R), (3, b, R)} (2, b) = {(2, a, R), (2, b, R), (2, a, L)} (3, a) = {(4, b, R), (4, b, L)} Deterministic transitions: (3, b) = {(4, a, L)} (4, a) = {(4, b, R)} (4, b) = {(2, a, R)} Notice that if there is a pair of a state q and a symbol t such that | (q, t) | > 1, then the automaton is nondeterministic.
Nondeterminism and nondeterministic Algorithms Nondeterministic automata are different from probabilistic machines which change their state based on some probability. If an NFA has nondetermininistic transition (p, a) = {q, r}, then reading a in state p the machine definitely enters both states q and r. The same concept of nondeterminism can be applied to algorithm design. Instead of nondeterministic transition, we use “nondeterministic guess (or choose)” in nondeterministic algorithms. For a finite set S, let nondet_choose(S) mean nondeterministically choosing an element in S. For example, suppose that in an algorithm we have the following statement. . . x = nondet_choose (q, r) ; . .
. . x = nondet_choose (q, r) ; . . . . x = nondet_choose (q, r) ; {proceed with x = q) . . . x = nondet_choose (q, r) ; (proceed with x = r) . Nondeterministic Algorithms Upon execution of this assignment statement the algorithm forks into two independently running program instances, one with x = q, and the other with x = r. This way nondeterministic algorithms can search the solution space of the given problem simultaneously. As in the nondeterministic automata, if there is no instances of computation generating the answer, we say the algorithm fails to give the answer.
Nondeterministic Algorithms (cont’ed) If we assume that the statement nondet_choose( ) takes constant time, we can solve many, so called, intractable problems, for which only exponential-time algorithms are available, can be solved in polynomial time. For example, consider the sum-of-subset problem, which is given as follows; Given a set S of n integers and an integer M, is there any sum of subset of S that is equal to M? Given S = {8, 21, 13, 20, 7, 11, 5} and M = 34, for example, the answer should be yes, since 8 + 21 + 5 = 34. This problem, which is one of the well known intractable problems, can be solved in linear-time, if we use the function nondet_choose( ). Suppose that the set S is given in an array. The nondeterministic algorithm in the following slide solves the problem in linear-time.
Nondeterministic Algorithms (cont’ed) Nondet-Sum-of-Subset (int S[ ], int n, int M) // S is an integer array of size n. { int i, sum = 0; boolean selected; for ( i = 0; i < n; i++) { selected = nondet_choose(true, false); if (selected) sum += S[i]; } if (sum = = M) return “yes”; else return “no”; } Notice that in the algorithm above, by selected =nondet_choose(true, false) for each element S[i], we are considering both cases of adding and not adding it to the sum. Thus the algorithm examines all possible sums of subsets. If there is an output “yes”, then we can say the answer is yes, ignoring all other “no” answers.