Nondeterminism nd Nodeterministic Automt 61
Nondeterminism nd Nondeterministic Automt The computtionl mchine models tht we lerned in the clss re deterministic in the sense tht the next move is uniquely determined y the current stte nd the input (ie, externl stimulus) Thus, the stte trnsition grphs of DFA's hve one (nd only one) edge for ech input symol (Recll tht, just for convenience, we greed not to drw the edge for trnsition with n input symol leding to the ded stte) Deterministic mchines follow our intuition, ecuse the models re sed on the determinism, which propose tht, for given cuse (current stte of the system nd externl input), the effect (stte chnge nd the output of the system) is uniquely determined Nondeterminism does not go long our intuition ecuse it llows multiple conflicting effects for given cuse Consider the stte trnsition grph shown on the following pge, which hs trnsitions which violte the determinism For exmple, the trnsition δ(1, ) = {1, 2} sys tht in stte 1 the utomton, reding input symol, enters sttes 1 nd 2 (Note tht the mchine enters sttes 1 nd 2 t the sme timewe cll such mchines nondeterministic finite utomt (NFA) 62
Nondeterministic Automt (cont ed) Nodeterministic FA: 2 e 1 3, 4 Nondeterministic trnsitions: δ(1, ) = {1, 2} δ(4, ) = {2, 3, 4} * δ(2, e) = {2, 3} Deterministic trnsitions: δ(1, ) = {3} δ(3, ) = {4} δ(4, ) = {2} * Notice tht in ny stte p, the mchine stys in the sme stte while there is no input, which implicitly there is δ(p, e) = {p} Therefore, if there is n explicit trnsition, like from stte 2 to stte 3 in the ove grph, the trnsition is nondeterministic, ecuse δ(2, e) = {2, 3} We will del with such ε-trnsitions lter s seprte topic Until then we study nondeterministic utomt with no ε-trnsitions 63
Nondeterministic Automt (cont ed) If n FA hs trnsition δ(p, ) = {q, r}, we sy tht the mchine, in stte p reding, nondeterministiclly enters sttes q nd r After tking this trnsition, the mchine is in oth q nd r We think tht two mchines, one in qnd the other in r, continue the computtion independently for the sme input Formlly n NFA M is defined y the sme tuple M = (Q, Σ, δ, q 0, Z 0, F ) s for DFA The only difference is the trnsition function; δ : Q Σ 2 Q,which implies tht in stte in Q, reding symol in Σ, the mchine cn enter simultneously in some sttes (ie, suset of Q) Notice tht DFA is specil cse of NFA which for n input symol, tkes trnsition to single stte For string x, let δ(q 0, x) = S, where S is the set of sttes tht the mchine enters fter reding string x The lnguge ccepted y n NFA M is defined s follows; L(M) = {x x {,} *, δ(q 0, x) = S, nd S F } Notice the ccepting condition, S F This implies tht if there exists t lest one sequence of trnsition tht ends up in n ccepting stte, then we sy the input x is ccepted All non-ccepting cses re ignored If there is no ccepting cse (ie, S F = ), then we sy the input is rejected 64
Nondeterministic Automt (cont ed) For exmple, consider the NFA shown in Figure () elow Figure () shows ll possile sequences of trnsitions for input, where nodes with lel X denote the ded stte which is not shown on the stte trnsition grph String is ccepted y the mchine since there exists sequence of trnsitions, which ends in n ccepting stte (q 2 in the figure) Note tht NFA's re similr to prllel mchines in the sense tht they explore n nswer simultneously in more thn one wy However, nondeterministic mchines do not produce (or nnounce) the finl result out of ll possile sequences of opertions We, the user, must serch ll possile terminting sttes of the mchine nd decide whether it stisfies the ccepting condition or not q 0 q 0, q 1 q 2 q 0 q 1 q 0 q 0 q 1 q 2 q 1 q 1 X X Figure (): An NFA Figure (): The profile of nondeterministic trnsition for input string 65
Nondeterministic Automt (cont ed) Designing NFA:, {x x {, } * },,, {x x {, } * nd there is followed y somewhere in x} 66
Nondeterministic Automt (cont ed) (, /) 1 Nodeterministic PDA: 2 (, /) (, /) 3 (, /) (, /) (, /ε ), (, /), (, /) 4 (, /) (, /), (ε, /ε ) Nondeterministic trnsitions: δ(1,, ) = {(2, ), (3, )} δ(3,, ) = {(3, ε ), (3, )} d(4,, ) = {(4, )} }* d(4, e, ) = {(2, ε)} Deterministic trnsitions: δ(2,, ) = {(3, )} δ(3,, ) = {(4, )} δ(3,, ) = {(2, )} δ(4,, ) = {(2, )} Notice tht PDA is nondeterministic if there is stte q, n input symol t, nd stck symol s such tht either () δ(q, t, s) > 1, or () δ(q, t, s) 0 nd δ(q, ε, s) 0 (see cse * ove) 67
Designing NPDA: Nondeterministic Automt (cont ed) (Y, X/YX ) X, Y {, } (X, Z 0 /X Z 0 ) q 1 (e, X/X ) ( ε, Z 0 /Z 0 ) q 3 strt q 0 q 2 (X, X/ε ) Figure (): NPDA ccepting {xx R x {, } + } 68
Nondeterministic Automt (cont ed) Designing NPDA: (, / ) (, /ε) (, /ε) (, Z 0 /Z 0 ) (c, Z 0 / Z 0 ) (c, Z 0 / Z 0 ) strt (, Z 0 /Z 0 ) (, /) (c, / ε ) (ε, Z 0 / Z 0 ) (, Z 0 /Z 0 ) (, Z 0 /Z 0 ) (c, / ε) Figure (): NPDA ccepting { i j c k i, j, k > 0, nd i = j or j = k } 69
Nondeterministic Automt (cont ed) Nodeterministic TM nd LBA: (,, R) 1 (,, R) (,, R), (,, R) 3 2 (,, L) (,, R) 4 (,, R), (,, L), (,, L) (,, R) Nondeterministic trnsitions: δ(1, ) = {(2,, R), (3,, R)} δ(2, ) = {(2,, R), (2,, R), (2,, L)} δ(3, ) = {(4,, R), (4,, L)} Deterministic trnsitions: δ(3, ) = {(4,, L)} δ(4, ) = {(4,, R)} δ(4, ) = {(2,, R)} Notice tht if there is pir of stte q nd symol t such tht δ(q, t) > 1, then the utomton is nondeterministic 70
Nondeterminism nd nondeterministic Algorithms Nondeterministic utomt re different from proilistic mchines which chnge their stte sed on some proility If n NFA hs nondetermininistic trnsition δ (p, ) = {q, r}, then reding in stte p the mchine definitely enters oth sttes q nd r The sme concept of nondeterminism cn e pplied to lgorithm design Insted of nondeterministic trnsition, we use nondeterministic guess (or choose) in nondeterministic lgorithms For finite set S, let nondet_choose(s) men nondeterministiclly choosing n element in S For exmple, suppose tht in n lgorithm we hve the following sttement x = nondet_choose (q, r) ; 71
Nondeterministic Algorithms Upon execution of this ssignment sttement the lgorithm forks into two independently running progrm instnces, one with x = q, nd the other with x = r x = nondet_choose (q, r) ; x = nondet_choose (q, r) ; {proceed with x = q) x = nondet_choose (q, r) ; (proceed with x = r) This wy nondeterministic lgorithms cn serch the solution spce of the given prolem simultneously As in the nondeterministic utomt, if there is no instnces of computtion generting the nswer, we sy the lgorithm fils to give the nswer 72
Nondeterministic Algorithms (cont ed) If we ssume tht the sttement nondet_choose( ) tkes constnt time, we cn solve mny, so clled, intrctle prolems, for which only exponentil-time lgorithms re ville, cn e solved in polynomil time For exmple, consider the sum-of-suset prolem, which is given s follows; Given set S of n integers nd n integer M, is there ny sum of suset of S tht is equl to M? Given S = {8, 21, 13, 20, 7, 11, 5} nd M = 34, for exmple, the nswer should e yes, since 8 + 21 + 5 = 34 This prolem, which is one of the well known intrctle prolems, cn e solved in liner-time, if we use the function nondet_choose( ) Suppose tht the set S is given in n rry The nondeterministic lgorithm in the following slide solves the prolem in linertime 73
Nondeterministic Algorithms (cont ed) Nondet-Sum-of-Suset (int S[ ], int n, int M) // S is n integer rry of size n { } int i, sum = 0; oolen selected; for ( i = 0; i < n; i++) { selected = nondet_choose(true, flse); } if (selected) sum += S[i]; if (sum = = M) return yes ; else return no ; Notice tht in the lgorithm ove, y selected = nondet_choose(true, flse) for ech element S[i], we re considering oth cses of dding nd not dding it to the sum Thus the lgorithm exmines ll possile sums of susets If there is n output yes, then we cn sy the nswer is yes, ignoring ll other no nswers 74