nondeterministic (operational) models (1)home.deib.polimi.it/martinen/courses/api/04nd.pdf ·...

39
Nondeterminism 1 Nondeterministic (operational) models (1) Usually one thinks of an algorithm as a determined sequence of operations In a certain state with a certain input there is no doubt on the next step Example: let us compare if x>y then max:=x else max:=y with if x>=y then max:=x x<=y then max:=y fi

Upload: vudung

Post on 01-Sep-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Nondeterminism 1

Nondeterministic(operational)models(1)

• Usuallyonethinksofanalgorithmasadeterminedsequenceofoperations– Inacertainstatewithacertaininputthereisnodoubtonthenextstep

• Example:letuscompareif x>y then max:=x else max:=y

withif

x>=y then max:=xx<=y then max:=y

fi

Nondeterminism 2

Nondeterministic(operational)models(2)

• Isitonlyamatterofelegance?• Letusconsiderthecase constructofPascal:whynothavingsomethinglikethefollowing?

casex=y then S1z>y+3 then S2

… thenendcase

Nondeterminism 3

Blindsearch

Anotherformofnondeterminismthatisusually“hidden”

?

Nondeterminism 4

Searchalgorithms

• Searchalgorithmsarea“simulation”ofbasicallynondeterministicalgorithms– Istheelementsearchedforintheroot?

Ifyes,okOtherwise

SearchtheleftsubtreeSearchtherightsubtree

• Choiceofpriorityamongpathsisoftenarbitrary

Nondeterminism 5

Inconclusion

• Nondeterminism(ND)isamodelofcomputationoratleastamodelofparallelcomputing– Adaandotherconcurrentlanguagesexploitit

• Itisausefulabstractiontodescribesearchproblemsandalgorithms

• Itcanbeappliedtovariouscomputationalmodels• Important:NDmodelsmustnotbeconfusedwithstochasticmodels

Nondeterminism 6

Addingnondeterminsm

δ(q1,a)={q2,q3}

q1

q2

q3

a

a

Nondeterminism 7

NondeterministicFSAs

• AnondeterministicFSA(NDFSA)isatuple<Q,I,δ ,q0,F>,where

– Q,I,q0,Faredefinedasin(D)FSAs– δ:Q×I→ P(Q)

• Whathappenstoδ*?

Nondeterminism 8

Movesequence

• δ∗ isinductivelydefinedfrom δ

• Example:δ(q1,a)={q2,q3},δ(q2,b)={q4,q5},δ(q3,b)={q6,q5}

→ δ*(q1,ab)={q4,q5,q6}

!y)(q,δq'

*

*

*

i),δ(q'y.i)(q,δ{q}ε)(q,δ

=

=

q1

q2

q3

a

a

q4

q5

q6

b

b

b

b

Nondeterminism 9

Acceptancecondition

x∈L⇔ δ*(q0,x)∩F≠∅

Amongthevariouspossibleruns(withthesameinput)oftheNDFSA,itissufficientthatoneofthemsucceedstoaccepttheinputstring

• Existential nondeterminism- Thereexistsauniversalinterpretation,too:δ*(q0,x)⊆F

Nondeterminism 10

DFSAvsNDFSA

• Startingfromq1 andreadingabtheautomatonreachesastatethatbelongstotheset{q4,q5,q6}

• Letuscallagain“state”thesetofpossiblestatesinwhichtheNDFSAcanbeduringtherun

q1

q2

q3

a

a

q4

q5

q6

b

b

b

b

Nondeterminism 11

Formally

• NDFSAshavethesamepowerasDFSAs• GivenaNDFSA,anequivalentDFSAcanbeautomatically synthesizedasfollowsIfAND =<Q,I,δ,q0,F>thenAD =<QD,I,δD,q0D,FD>,where– QD=P (Q )

– q0D ={q0}– FD={qD|qD∈ QD∧ qD∩F ≠∅}

δD(qD,i) = δ(q,i)q∈qD

Nondeterminism 12

Example(1)TransformthefollowingNDFSAintotheequivalentDFSA

q0 0 q1

q0 0 q2

q0 1 q1

q1 0 q3

q1 1 q3

q1 1 q1

q2 0 ⊥

q2 1 q0

q3 0 q2

q3 1 q0

q3q0

q1

q2

1

1

Nondeterminism 13

Example(2)

• Letusproceedrecursively

q0 0 q1

q0 0 q2

q0 1 q1

q1 0 q3

q1 1 q3

q1 1 q1

q2 0 ⊥

q2 1 q0

q3 0 q2

q3 1 q0

q0 0 q12

q0 1 q1

q1 0 q3

q1 1 q13

q2 0 ⊥

q2 1 q0

q3 0 q2

q3 1 q0

q12 0 q3

q12 1 q013

q13 0 q23

q13 1 q013

q013 0 q123

q013 1 q013

q23 0 q2

q23 1 q0

q123 0 q23

q123 1 q013

Nondeterminism 14

Example(3)

Graphically

q3q0

q1

q2

1

1

q0 q1

q12

q3 q2

q13q013

q23

q123

1 0 0

0

1

0 0

0

0

1

1

Nondeterminism 15

WhyND?

• NDFSAsarenomorepowerfulthanFSAs,buttheyarenotuseless– ItcanbeeasiertodesignaNDFSA– Theycanbeexponentiallysmallerw.r.t.thenumberofstates

• Example:aNDFSAwith5statesbecomesintheworstcaseaFSAwith25 states

Nondeterminism 16

NondeterministicTM

• TodefineanondeterministicTM(NDTM),weneedtochangethetransitionfunctionandthetranslationmapping

• Alltheotherelementsremainasina(D)TM• Thetransitionfunctionis

δ:(Q-F)xIxΓk →P (QxΓkx{R,L,S}k+1)andtheoutputmapping

η:(Q-F)xIxΓk →P (O x{R,S})

Nondeterminism 17

Computationtree

c32

c0

c22c21

c13c12c11

c26c25c23 c24

c31ckj

cim

C accepting

C halting but not accepting

Unfinished computation

Nondeterminism 18

Acceptancecondition

• Astringx∈I*isacceptedbyaNDTMifandonlyifthereexistsacomputation thatterminatesinanacceptingstate

• Itwouldseemthattheproblemofacceptingastringcanbereducedtoavisitofacomputationtree– Howshouldweperformthevisit?– WhatabouttherelationshipbetweenDTMsandNDTMs?

Nondeterminism 19

Visitingthecomputationtree

• Weknowdifferentkindsofvisits:– Depth-firstvisit– Breadth-firstvisit

• Adepth-firstvisitcannotworkinourproblembecausethecomputationtreecouldhaveinfinitepathsandthealgorithmmight“getstuck”init

• Weshouldadoptabreadth-firstvisitalgorithm

Nondeterminism 20

DTMvsNDTM

CanwebuildaDTMthatvisitsatreelevelbylevel?Itisalong(andboring)exercise,butYES– WecanbuildaDTMthatestablisheswhetheraNDTMrecognizesastringx

– GivenaNDTM,wecanbuildanequivalentDTMNDdoesnotaddpowertoTMs

Nondeterminism 21

ε MovesandPDAs

• ε-movescamewiththefollowingconstraint:Ifδ(q,ε,A)≠⊥,thenδ(q,i,A)=⊥ ∀i∈I

• Withoutthisconstraintthepresenceofε-moveswouldmakePDAsintrinsicallynondeterministic

q1

q2

q3

i, A/α

ε, A/β

Nondeterminism 22

AddingnondeterminismtoPDAs

• RemovingtheconstraintalreadymakesthePDAnondeterministic

• Additionally,wecanhavenondeterminismbychangingthetransitionfunctionofaPDAandconsequently:– transitionsamongconfigurations– acceptancecondition

Nondeterminism 23

Definition

AnondeterministicPDA(NDPDA)isatuple<Q,I,Γ,δ,q0,Z0,F>– Q,I,Γ,q0,Z0,Fasin(D)PDA– δ isthetransitionfunctiondefinedas

δ:Q×(I∪{ε})×Γ→PF(Q×Γ*)– WhatistheF inPF?

– WhyF?

Nondeterminism 24

Transitionfunction

δ:Q×(I∪{ε})×Γ→PF(Q×Γ*)

• PF indicatesthefinite subsetsofQ×Γ*

– WhydidwenotspecifyitforNDTM?• Graphically:

q1

q2

q3

i, A/α

i, A/β

Nondeterminism 25

Transitionamongconfigurations

Relation⊢ onQ×I*× Γ*× Q×I*× Γ*isdefinedbyc=<q,x,γ>⊢ c’=<q’,x’,γ’>ifandonlyif– Case1

• x=iy,x’=y• γ=Aβ,γ’=αβ• <q’,α>∈δ(q,i,A)

– Case2• x’=x• γ=Aβ,γ’=αβ• <q’,α>∈δ(q,ε,A)

Nondeterminism 26

Acceptancecondition

• GivenaNDPDAP∀x∈I* (x∈L(P)⇔ c0=<q0,x,Z0>⊢* cF=<q,ε,γ>andq∈F)

• Informally,astringisacceptedbyaPDAifthereisapath coherentwithxonthePDAthatgoesfromtheinitialstatetoafinalstate– Theinputstringhastobereadcompletely

Nondeterminism 27

Effectsofnondeterminism

• NDdoesnotaddexpressivepowerto– TMs– FSAs

• DoesNDaddexpressivepowertoDPDAs?

Nondeterminism 28

NDPDAsvsDPDAs(1)

• ObviouslyaNDPDAcanrecognizeallthelanguagesrecognizablebyDPDAs

• NDallows

soNPDAscanrecognize{anbn |n≥1}U{anb2n |n≥1}

q1

q2

q3

i, A/α

i, A/β

Nondeterminism 29

{anbn |n≥1}U{anb2n |n≥1}

q0

q1

q3 q4

q5a,Z0/AAZ0a,A/AAA

b,A/ε

b,A/ε

ε,A/ε

b,A/ε

ε,Z0/Z0

b,A/ε

Branch for anbn

Branch for anb2n

Nondeterminism 30

NDPDAsvsDPDAs(2)

LanguagesrecognizablebyNDPDAsarecalledcontext-free languages

Regular languages

Languages recognizable by DPDA

anbn∪anb2n

Languages recognizable by NDPDA

Nondeterminism 31

NDPDAvsTM

• (a)and(c):NO!– A(N)DTMcansimulateaNDPDAbyusingthetapeasastack

• (d):NO!– Thestackisstilladestructivememory– anbncnisnotrecognizablebyaNDPDA

TMNDPDA

OR OR ?OR ≡(a) (b) (c) (d)

Nondeterminism 32

Thebull’s-eye

Regular languages

Deterministic context-free languages

Context-free languages

Recursively enumerable languages

Nondeterminism 33

ClosurepropertiesinDPDAs

• InDPDAswehave– Closurew.r.t.complement– Non-closurew.r.t.union,intersection,difference

• Doeschangingthepoweroftheautomatachangetheirbehaviorw.r.t.operations?– Itcanhappen– Newpower,newlanguage

Nondeterminism 34

Union(1)

• NDPDAsareclosed underunion– Intuition:

• GiventwoNDPDAs,P1 andP2,wecanalwaysbuildaNDPDAthatrepresentstheunionbycreatinganewinitialstatethatisconnectedtobothinitialstatesofP1 andP2 withanε-move

q1

q2

q3

i, A/α

i, A/β

Nondeterminism 35

Union(2)

q’0

q’’0

q’0

q’’0

q0

εZ0|Z0

εZ0|Z0

Nondeterminism 36

Intersection

• Theclosurew.r.t.intersectionstilldoesnothold

• Consider– {anbnc*}– {a*bncn}botharerecognizableby(N)DPDAs,but{anbnc*}∩{a*bncn}={anbncn} isnotrecognizablebyanyNDPDA

Nondeterminism 37

Complement(1)

• Ifaclassoflanguagesisclosedw.r.t.union,butnotw.r.t.intersectionitcannotbeclosedw.r.t.complement– Wecanwriteintersectionintermsofunionandcomplement

• NDPDAsarenotclosed w.r.tcomplement

Nondeterminism 38

Remarks

• Ifamachineisdeterministicanditscomputationterminates,thecomplementcanbeobtainedby– Completingthemachine– Swappingacceptingandnonacceptingstates

• Nondeterminism,likenon-termination(infinitecomputation),makesthisapproachunfeasible

Nondeterminism 39

Complement(2)

• ForNDPDAs,computationscanalwaysbemadeterminating(asforDPDAs)

• However,NDcancausethisproblem:Onecanhavetwocomputations:

– c0=<q0,x,Z0>⊢* c1=<q1,ε,γ>– c0=<q0,x,Z0>⊢* c2=<q2,ε,γ>

withq1∈Fandq2∈F→ evenifweswapacceptingandnonacceptingstate,xisstillaccepted