Top Banner
6.006 Introduction to Algorithms Lecture 25: Complexity Prof. Erik Demaine
27

Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Jul 08, 2019

Download

Documents

lykhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

6.006IntroductiontoAlgorithms

Lecture25:ComplexityProf.ErikDemaine

Page 2: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Today• Reductionsbetweenproblems• Decisionvs.optimizationproblems• Complexityclasses

–P,NP,co‐NP,PSPACE,EXPTIME,…• NP‐completeness

Page 3: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Reductions

http://cdn.zedomax.com/blog/wp‐content/uploads/2010/02/reuse_reduce_recycle.jpg

design by Gary Andersonhttp://en.wikipedia.org/wiki/File:Recycle001.svg

balsamicreductionhttp://www.allthingsolive.ca/2011/01/pecorino‐di‐fossa‐cheese‐pears‐and‐cinnamon‐pear‐balsamic/

Page 4: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

HowtoDesignanEfficient Algorithm?

1. Definecomputational problem2. Abstract irrelevant detail3. Reducetoaproblemyoulearnhere

(or6.046oralgorithmicliterature)4. Elsedesignusing“algorithmictoolbox”5. Analyzealgorithm’sscalability6. Implement & evaluate performance7. Repeat(optimize,generalize)

Page 5: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Reductions• Insteadofsolvingaproblemfromscratch,convertyourproblemintoaproblemyoualreadyknowhowtosolve

• Examples:– Min‐productpath shortestpath(takelogs)– Longestpath shortestpath(negateweights)– Minmultiple‐of‐5path shortestpath( )– Unweighted weightedshortestpath(weight1)– 2Dpathplanning shortestpath(visibilitygraph)

Page 6: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Polynomial‐TimeReductions

• Considertwoproblems &• Polynomial‐timereduction :

– Solutionto usingsolutionto– Polynomial‐timealgorithmfor ,withfreecallstosubroutinetosolve

• Write :“ isnoharderthan ”(uptopolynomialoverhead)

Page 7: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

One‐CallReductions• Commonpolynomial‐timereduction :

1. Giveninputtoproblem2. Polynomial‐timepreprocessing3. Onecalltosolveproblem4. Polynomial‐timepostprocessing

• Moreinterestingthan“reduceanyproblemtobasicoperationsinmodelofcomputation”– Example: sortingreducestoelementcomparisons

Page 8: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

DecisionProblems• Decisionproblem=anyproblemwhoseanswerisonebit:“yes”or“no”

• Examples:– Dotheselinesegmentshaveanintersection?– DoesthisSuperMarioBros.levelhaveasolution?– Doesthe Rubik’sCubealwayshaveasolutionin20moves?

– Givenasequenceofcards,isthereaCrazyEightssubsequencetrickofatleast17cards?

– Doesgivenweightedgraphhaveanegativecycle?

Hamlet(1948)

Page 9: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Optimization Decision• Anyoptimizationproblemcanbeconvertedintoadecisionproblem

• Addinput :boundonoptimalsolution– Maximizationproblem isalowerbound– Minimizationproblem isanupperbound

• Examples:– Givensequenceofcards&number ,isthereaCrazyEightstrickof cards?

– Givenaweightedgraph,vertices & ,number ,istherean pathofweight ?

Page 10: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

WhyDecisionProblems?• Metaclaim: Everycomputationalproblemhasadecisionversionofroughlyequalcomputationaldifficulty

• Maximization/minimizationproblems:binarysearchonbound tofindoptimal– Logarithmicoverhead

• Example: Iskey inthisbinarysearchtree?• Example: Givenunsorted and & ,does sorttorank inasortedarray?

Page 11: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

KarpReductions• Fordecisionproblems and• Simplest(andmostcommon)typeofpolynomial‐time reduction:1. Giveninputtoproblem2. Polynomial‐timepreprocessing3. Onecalltosolveproblem4. Returnthesameanswer (nopostprocessing)

RichardKarp

Page 12: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

ComplexityClasses

Page 13: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

TimeClasses• P =classofalldecisionproblemssolvablebypolynomial‐timealgorithms–

• EXPTIME =classofalldecisionproblemssolvablebyexponential‐timealgorithms–

• Ingeneral,if ,then

(TimeHierarchyTheorem)

Page 14: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

SpaceClasses• PSPACE =classofalldecisionproblemssolvableusingpolynomialstoragespace

• Example: IsthereasolutiontoagivenRubik’sCubeusing moves?

• EXPSPACE =classofalldecisionproblemssolvableusingexponentialstoragespace

• SpaceHierarchyTheorem:if,then

Page 15: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

NP• NP =classofalldecisionproblemssolvablebya“lucky”polynomial‐timealgorithm– In time,canguess betweentwochoices– Attheend,report“yes”or“no”– Ifanywaytosay“yes”,actuallyreturn“yes”(alwaysmakerightchoice)

• Example: IsthereasolutiontoagivenRubik’sCubeusing moves?

– Guessfirstmove,secondmove,…, th move– Return“yes”ifsolved

Page 16: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

NPRemix• Equivalently,NP =classofalldecisionproblemsverifiable inpolynomialtime– Every“yes”inputhasapolynomial‐lengthcertificate (mightbeveryhardtofind)

– Giveninputandcertificate,canconfirmthatansweris“yes”inpolynomialtime

• Example: IsthereasolutiontoagivenRubik’sCubeusing moves?

– Certificate=sequenceof movestosolution

Page 17: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

NPProblems• Givenagraph,doesithavevertexcoverofsize ?

• Givenagraph,isthereasimplepathoflength ?

• Givenagraph,canitbecoloredwith3colors?

• GivenaBooleancircuit,arethereinputsthatmaketheoutputtrue? OR

ANDOR

Page 18: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

NP‐Completeness• NP‐complete =NPproblemthatisatleastashardasallproblemsinNP– Formally: and forall– HardestproblemsinNP,allessentiallyequivalent

• Ifthereareanyproblemsin ,thenNP‐completeproblemsareamongthem

• IfanyNP‐completeproblemhasapolynomial‐timealgorithm,then

StephenCook

Page 19: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

ComplexityClasses

Page 20: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

PowerofReduction• If isNP‐complete, ,and ,then isNP‐complete

• Proof:– forall–– forall

Page 21: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

ProvingNP‐Completeness• StartwithanyknownNP‐completeproblem– Givenagraph,doesithavevertexcoverofsize ?

– Givenagraph,isthereasimplepathoflength ?

– Givenagraph,canitbecoloredwith3colors?

– GivenaBooleancircuit,arethereinputsthatmaketheoutputtrue?

• Prove and

Page 22: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

IndependentSet• Independentset =subsetofverticesinducingnovertices

• Problem: Givenagraph andinteger ,isthereanindependentsetofsize ?

Page 23: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

VertexCoverIndependentSet

• hasavertexcoverofsizeifandonlyif

hasanindependentsetofsize• So reduces• VCisNP‐complete ISis NP‐complete

VertexCover IndependentSet

Page 24: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

DominatingSet• Dominatingset =subsetofverticessuchthateveryothervertexisadjacenttosomeoneinthesubset

• Problem: Givenagraph andinteger ,isthereadominatingsetofsize ?

Page 25: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

VertexCoverDominatingSet

• hasavertexcoverofsizeifandonlyif

hasadominatingsetofsize• So reduces• VCisNP‐complete DSis NP‐complete

VertexCover DominatingSet

Page 26: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Phutball[Conway]

“Matein1”(canIwininonemove?)NP‐complete[Demaine,Demaine,Eppstein 2000]

a b c

a b c

a

a

b

b

c

c

(a b c) (a b c)

Page 27: Prof. Erik Demaine - Massachusetts Institute of Technology · – Does the Rubik’s Cube always have a solution in 20 moves? – Given a sequence of cards, is there a Crazy Eights

Bad&GoodNews• ManyproblemsareNP‐complete• Canoftenfindapproximatesolutionsinpolynomialtime– Within factorofoptimal(e.g.,VertexCover)

– Within0.0001%ofoptimal(e.g.,VertexCoverinplanargraphs)