1. NP-Completeness | Set 1 (Introduction)
We have beenwritingaboutefficientalgorithmstosolve complex problems,likeshortestpath,Euler
graph,minimumspanningtree,etc.Those were all successstoriesof algorithmdesigners.Inthispost,
failure storiesof computerscienceare discussed.
Can all computational problemsbe solvedbyacomputer?There are computational problemsthatcan
not be solvedbyalgorithmsevenwithunlimitedtime.ForexampleTuringHaltingproblem(Givena
program andan input,whetherthe programwill eventually haltwhenrunwiththatinput,orwill run
forever).AlanTuringprovedthatageneral algorithmtosolve the haltingproblemforall possible
program-inputpairscannotexist.A keypartof the proof is,Turingmachine wasusedas a mathematical
definitionof acomputerandprogram (Source HaltingProblem).
Statusof NPComplete problemsisanotherfailure story,NPcompleteproblemsare problemswhose
statusis unknown.Nopolynomial timealgorithmhasyetbeendiscoveredforanyNPcomplete problem,
nor has anybodyyetbeenable toprove thatno polynomial-timealgorithmexistsforanyof them.The
interestingpartis,if any one of the NPcomplete problemscanbe solvedinpolynomial time,thenall of
themcan be solved.
Attentionreader!Don’tstop learningnow.Getholdof all the importantDSA conceptswiththe DSA Self
PacedCourse at a student-friendlyprice andbecome industryready. Tocomplete yourpreparation
fromlearninga language toDS Algoandmany more, please referComplete InterviewPreparation
Course.
In case you wishtoattendlive classeswithexperts,please referDSA Live ClassesforWorking
ProfessionalsandCompetitive ProgrammingLive forStudents.
What are NP,P, NP-complete andNP-Hardproblems?
P isa setof problems thatcan be solvedbyadeterministicTuringmachine inPolynomial time.
NPis setof decisionproblemsthatcanbe solvedbya Non-deterministicTuringMachine inPolynomial
time.Pis subsetof NP(anyproblemthatcan be solvedbyadeterministicmachine inpolynomial time
can alsobe solvedbya non-deterministicmachine inpolynomial time).
Informally,NPisasetof decisionproblemsthatcanbe solvedbya polynomial-time viaa“Lucky
Algorithm”,amagical algorithmthatalwaysmakesa rightguessamongthe givensetof choices(Source
Ref 1).
2. NP-completeproblemsare the hardestproblemsinthe NPset. A decisionproblemLisNP-complete if:
1) L isin NP(AnygivensolutionforNP-complete problemscanbe verifiedquickly,butthere isno
efficientknownsolution).
2) EveryprobleminNPis reducible toLinpolynomial time (Reductionisdefinedbelow).
A problemisNP-Hardif itfollowsproperty2mentionedabove,doesn’tneedtofollowproperty1.
Therefore,the NP-Completesetisalsoasubsetof the NP-Hardset.
DecisionvsOptimizationProblems
NP-completenessappliestothe realmof decisionproblems. Itwasset upthis waybecause it’seasierto
compare the difficultyof decisionproblemsthanthatof optimizationproblems. Inreality,though,
beingable tosolve adecisionprobleminpolynomial time will oftenpermitustosolve the
correspondingoptimizationprobleminpolynomial time (usingapolynomial numberof callstothe
decisionproblem).So,discussingthe difficultyof decisionproblemsisoftenreallyequivalentto
discussingthe difficultyof optimizationproblems.(Source Ref 2).
For example,considerthe vertex coverproblem(Givenagraph,findoutthe minimumsizedvertexset
that coversall edges).Itisan optimizationproblem.Correspondingdecisionproblemis,given
undirectedgraphG andk, is there a vertex coverof size k?
3. What isReduction?
Let L1 andL2 be twodecisionproblems.Suppose algorithmA2solvesL2.That is,if y is an inputforL2
thenalgorithmA2will answerYesor NodependinguponwhetherybelongstoL2 or not.
The ideais to finda transformationfromL1to L2 so that algorithmA2can be part of an algorithmA1to
solve L1.
Learningreduction,ingeneral,isveryimportant.Forexample,if we have libraryfunctionstosolve
certainproblemsandif we can reduce a new problemtoone of the solvedproblems,we save alotof
time.Considerthe exampleof aproblemwhere we have tofindthe minimumproductpathina given
directedgraphwhere the productof path isthe multiplicationof weightsof edgesalongthe path.If we
have code for Dijkstra’salgorithmtofindthe shortestpath,we cantake the logof all weightsanduse
Dijkstra’salgorithmtofindthe minimumproductpathratherthanwritinga freshcode forthisnew
problem.
How to prove thata givenproblemisNPcomplete?
From the definitionof NP-complete,itappearsimpossible toprove thata problemLis NP-Complete. By
definition,itrequiresustothat showeveryprobleminNPinpolynomial timereducibletoL.
Fortunately,there isanalternate waytoprove it. The ideaisto take a knownNP-Complete problem
and reduce itto L. If polynomial timereductionispossible,we canprove that L isNP-Completeby
transitivityof reduction(If aNP-Complete problemisreducibletoL inpolynomial time,thenall
problemsare reducible toLinpolynomial time).
What was the firstproblemprovedasNP-Complete?
There mustbe some firstNP-Complete problemprovedbydefinitionof NP-Complete problems. SAT
(Booleansatisfiabilityproblem)isthe firstNP-Complete problemprovedbyCook(See CLRSbookfor
proof).
It isalwaysuseful toknowaboutNP-Completenessevenforengineers.Supposeyouare askedtowrite
an efficientalgorithmtosolve anextremelyimportantproblemforyourcompany.Afteralotof
4. thinking,youcanonlycome up exponential timeapproach whichisimpractical.If youdon’tknowabout
NP-Completeness,youcanonlysaythat I couldnot come withan efficientalgorithm.If youknow about
NP-Completenessandprove thatthe problemisNP-complete,youcanproudlysaythat the polynomial
time solutionisunlikelytoexist.If there isapolynomialtime solutionpossible,thenthatsolutionsolves
a big problemof computerscience manyscientistshave beentryingforyears.