Algorithms,Lecture3onNP:NondeterministicPolynomialTime
ProblemXispolytimereducibletoYX≤PY
ifcansolveXusingpolycomputationandapolynumberofcallstoanalgorithmsolvingY.
“Uptopolyfactors,XisatleastaseasyasY”“Uptopolyfactors,YisharderthanX”
DefinedNP:decision(yes/no)problemscancheckcertificatesforyesanswersinptime
Havepolytimeproofcheckers:ApolytimealgorithmAsothatifX(s)=yes,
thereexistsawsothatA(s,w)=yes,andifX(s)=no,
forallw,A(s,w)=no
Mostproblemsofform:doesthereexistwsothatsatisfiesconditionsimpliedbys?
DefinedNP-Complete:1.X∈ NP,and2.XisNP-hard
XisNP-hardif:1.forallY∈ NP,Y≤P X,or2.CircuitSat ≤P X,or3.Y≤P X,forsomeNP-HardY,suchas
SAT,IndependentSet,VertexCover
Lastclass:
Lastclass:ProvedCircuitSAT isNP-Hard.
ProvedCircuitSAT ≤P SAT.
Infact,provedCircuitSAT ≤P 3-SAT
Where3-SATisSAT,buteachclausehasatmost3terms.
Lastclass:ProvedCircuitSAT isNP-Hard.
ProvedCircuitSAT ≤P SAT.
Infact,provedCircuitSAT ≤P 3-SAT
Where3-SATisSAT,buteachclausehasatmost3terms.Naturallyfollowsfromtheconstruction.
SAT≤P 3-SAT,aself-containedproof
AorBisequivalentto∃ys.t. (Aory)and(Bory)
So,givenaclausewithmorethan3terms,like
Introduceanewvariabley,andreplacewithclauses:
x1Vx2Vx3 Vx4
x1Vx2VyandyVx3 Vx4
SAT≤P 3-SAT,aself-containedproof
AorBisequivalentto∃ys.t. (Aory)and(Bory)
So,givenaclausewithmorethan3terms,like
Introduceanewvariabley,andreplacewithclauses:
x1Vx2Vx3 Vx4
x1Vx2VyandyVx3 Vx4
2-SATisinP
Alsopossibletoforceeachvariabletoappearatmost3times
Sayavariablexappearsinkclauses.
Replacexwithknewvariables,x1,…,xk,oneforeachclauseinwhichitappears.
Addclausesx1Vx2 ,x2Vx3 ,…,xk-1Vxk ,xk Vx1
Onlysatisfiedifallareequal.
Today
WillprovemoreproblemsareNP-complete:
3-coloringHamiltonianCycleTravellingSalespersonProblem
k-Coloring
GivenagraphG=(V,E),doesthereexistf:V→ {1,2,…,k}(colors)
Sothatforall(u,v)∈ Ef(u) f(v)?
3-colorable Not3-colorable
6=
3-colorable Not3-colorable
k-Coloring
GivenagraphG=(V,E),doesthereexistf:V→ {1,2,…,k}(colors)
Sothatforall(u,v)∈ Ef(u) f(v)?6=
k-ColoringisNP-Complete
ClearlyinNP,becausecancheckaproposedcoloringToproveNP-hard,willshow3-SAT≤P 3-Coloring
GivenacollectionofclausesC1,…,Ck,eachwithatmost3terms,onvariablesx1,…,xnproducegraphG=(V,E)thatis3-colorableiff theclausesaresatisfiable
3-ColoringisNP-Complete– variablegadgets
Create3specialnodes:T,F,B(base),andonenodeforeachterm:xi andxi
x1x1 x2x2 xnxn
Inevery3-coloring,oneofxi andxiiscoloredTandoneiscoloredF
T F
B
xnxnx1
Create3specialnodes:T,F,B(base),andonenodeforeachterm:xi andxi
x1 x2x2
Inevery3-coloring,oneofxi andxiiscoloredTandoneiscoloredF
3-ColoringisNP-Complete– variablegadgets
FT
B
Considerclausex1Vx2 Vxn
3-ColoringisNP-Complete– clausegadgets
x1 x2 xn
Claim:3-colorableiff termscoloredtosatisfyclause
T
B
3-ColoringisNP-Complete– clausegadgets
x1 x2 xn
Claim:3-colorableiff termscoloredtosatisfyclause
1.IftermsallcoloredF,thencannot3-color
T
B
3-ColoringisNP-Complete– clausegadgets
x1 x2 xn
Claim:3-colorableiff termscoloredtosatisfyclause
1.IftermsallcoloredF,thencannot3-color
T
B
3-ColoringisNP-Complete– clausegadgets
x1 x2 xn
Claim:3-colorableiff termscoloredtosatisfyclause
1.IftermsallcoloredF,thencannot3-color
T
B
xnx2x1
3-ColoringisNP-Complete– clausegadgetsClaim:3-colorableiff termscoloredtosatisfyclause
2.Ifsometermtrue,can3-color
T
B
x2x1 xn
3-ColoringisNP-Complete– clausegadgetsClaim:3-colorableiff termscoloredtosatisfyclause
2.Ifsometermtrue,can3-color
T
B
xnx2x1
3-ColoringisNP-Complete– clausegadgetsClaim:3-colorableiff termscoloredtosatisfyclause
2.Ifsometermtrue,can3-color
T
B
3-ColoringisNP-Complete
x1x1 x2x2 xnxnx3x3
x1Vx2 Vxn
3-colorableiffsatisfiable
FT
B
3-ColoringisNP-Complete
x1x1 x2x2 xnxnx3x3
x1Vx2 Vxn x2Vx3 Vxn
3-colorableiffsatisfiable
FT
B
HamiltonianCycle:Acycleinagraphthathitseachvertexonce.
DirectedHamiltonianCycle:same,butinadirectedgraph
DirectedHamCycleisNP-Complete
ClearlyinNP,becausecancheckifacycleisHamiltonian
ToproveNP-hard,willshowSAT≤P DirectedHamCycle
ProducedirectedgraphG=(V,E)thathasHamCycleiff theclausesaresatisfiable
Start:creategraphwith2n HamCycles,thencreategadgetstorestrictthem
1:
2:
3:
n:
Start:creategraphwith2n HamCycles,thencreategadgetstorestrictthem
1:
2:
3:
n:
Start:creategraphwith2n HamCycles,thencreategadgetstorestrictthem
s
t
1:
2:3:
n:
Mustgotop-to-bottom,andcantraverseeachrowleft-to-right(True)orright-to-left(False)
Start:creategraphwith2n HamCycles,thencreategadgetstorestrictthem
Mustgotop-to-bottom,andcantraverseeachrowleft-to-right(True)orright-to-left(False)
s
t
1:
2:3:
n:
Clausegadgets TrueFalse
s
t
1:
2:3:
n:
clausex1 forcestraversefirstrow
Require:nootheredgestouchverticesingadget
x1Vx2 Vxn
Clausegadgets TrueFalse
s
t
1:
2:3:
n:
clause
Forcestraverse1,or2,orn
x1Vx2 Vxn
Clausegadgets TrueFalse
s
t
1:
2:3:
n:
clause
Forcestraverse1,or2,ornRequire:nootheredgestouchverticesingadget
x1Vx2 Vxn
Clausegadgets TrueFalse
2:
clause
Toseemustcomebacktosamerow,notethatifdonotisnoHamiltonianpaththroughunuseddown-link
x1Vx2 Vxn
Clausegadgets TrueFalse
1:
2:
n:
clause
Forcestraverse1,or2,orn
x1Vx2 Vxn
Hamcycleiff satisfiable TrueFalse
s
t
1:
2:3:
n:
Pf.Ifsatisfiable,traverseinorderindicatedbyvars,pickingupeachclauseonceusingsometrueterm.
x1Vx2 Vx3
x1Vx2 Vxn
Hamcycleiff satisfiable TrueFalse
s
t
1:
2:3:
n:
x1Vx2 Vx3Pf.IfHamCycle,mustgotoptobot
assignvars bydirection
ifvisiteachclausenode,thenismadetruebytermonrowfromwhichmakethevisit.
voutvin
DirectedHamCycle≤PHamCycle
1. Indirectedproblem,answersameifreverseallarrows.
2.Totransformtoundirected,replaceeachvertexvwiththreevertices:vin,vbase,vout
vbuoutuin ub
u vReplacedirected(u,v)edgewith(uout ,vin )
DirectedHamCycle≤PHamCycle
uoutuin ub
Claim:Iftheseareonlyedgestoub,thenineveryHamiltoniancycleub mustbeadjacenttouin
Proof:ifitisnot,thenonceenterubcannotgetout
voutvin vbuoutuin ub
Replacedirected(u,v)edgewith(uout ,vin )
DirectedHamCycleinoriginal->HamCycle
woutwin wb
u v
w
DirectedHamCycle≤PHamCycle
voutvin vbuoutuin ub
Replacedirected(u,v)edgewith(uout ,vin )
Lemma:EveryHamCycleintheundirectedgraphmustgoin,base,out,in,base,out,in,base,out,etc,mustcorrespondtoaHamCyc indirectedgraph
woutwin wb
u v
w
DirectedHamCycle≤PHamCycle
TSP(TravellingSalespersonProblem)
Givennlocations,adistancefunctiond(u,v)andatotaldistanceD,doesthereexistatourthroughalllocationsoftotaldistanceatmostL?
http://www.tsp.gatech.edu/usa13509/usa13509_info.html
TSP(TravellingSalespersonProblem)
Givennlocations,adistancefunctiond(u,v)andatotaldistanceD,doesthereexistatourthroughalllocationsoftotaldistanceatmostL?
http://www.tsp.gatech.edu/usa13509/usa13509_sol.html
RL5915 optimal solutionAn optimal solution for RL5915 is given by the following tour, which has length 565530.
http://www.tsp.gatech.edu/rl5915/rl5915_sol.html
TSPisNP-complete
HamCycle≤PTSP
GivengraphG=(V,E),createonelocationforeachvertex,
d(u,v)=1if(u,v)∈ E2otherwise
Targetdistance=|V|
Atourofalllocationsthatreturnstostartandhastotallength|V|mustuseexactly|V|edgesofG
TSPisNP-complete
HamCycle≤PTSP
GivengraphG=(V,E),createonelocationforeachvertex,
d(u,v)=1if(u,v)∈ E2otherwise
Thisisanabstractdistancefunction.Satisfiesd(u,v)=d(v,u)forallu,v andd(u,w)≤ d(u,v)+d(v,w)forallu,v,w
TSPisNP-complete
HamCycle≤PTSP
GivengraphG=(V,E),createonelocationforeachvertex,
d(u,v)=1if(u,v)∈ E2otherwise
Thisisanabstractdistancefunction.
RemainsNP-hardforintegerpointsinplane.
IssuewithPlanarTSP
Ifinputislocationsofpoints,insteadofdistances
TheproblemisnotknowntobeinNP,becausedonotknowifcancomparedistancesinpolynomialtime.
Forintegersx1,…,xn integert,donothavepolytimealgorithmtotestif
X
i
pxi t