Np completeness

27
NP-Completeness Design and Analysis of Algorithm Muhammad Saim (BSCS/S13/0104)

Transcript of Np completeness

Page 1: Np completeness

NP-CompletenessDesign and Analysis of AlgorithmMuhammad Saim (BSCS/S13/0104)

Page 2: Np completeness

Introduction• A problem is said to be polynomial

if there exists an algorithm that solves the problem in time

• T(n)=O(nc), where c is a constant.

• Examples of polynomial problems:– Sorting: O(n log n) = O(n2)– All-pairs shortest path: O(n3)– Minimum spanning tree: O(N log N)= O(N2)

Page 3: Np completeness

Intro (continued)

• The world of computation can besubdivided into three classes:

Polynomial problems (P)Exponential problems (E)Intractable (non-computable) problems (I)

Page 4: Np completeness

Even More Intro

• we know how to solve exponentially,• we don't know how to solve polynomially, and• we don't know if they can be solved polynomially at

all

There is a very large and important class of problems that

Page 5: Np completeness

NP-Completeness• Defination:

A problem is said to be Nondeterministically Polynomial (NP) if we can find a nodeterminsitic Turing machine that can solve the problem in a polynomial number of nondeterministic moves.

Page 6: Np completeness

Another DefinitionA problem is said to be NP if

• its solution comes from a finite set of possibilities, and

• it takes polynomial time to verify the correctness of a candidate solution

Page 7: Np completeness

AND Another• A problem is said to be NP if there

exists an NP algorithm for it.

begin /* The following for-loop is the guessing stage*/ for i=1 to N do X[i] := choose(i); endfor /* Next is the verification stage */ Write code that does not use "choose" and that verifies if X[1:N] is a correct solution to the problem.

end

Page 8: Np completeness

NP-Completeness

• Good news: such-and-such a problem can be solvedquickly

• Bad news: evidence that many important problems can't be

solved quickly

Page 9: Np completeness

Why Should We Care?• The sad news is these NP-complete

problems really come up all the time.

They are Hard:• stop beating your head against a wall & do

something better.

Page 10: Np completeness

The BIG PROBLEM

IS P=NP??That’s

Complexity Theory

So complex that many /ˌθɪərəˈtɪʃ(ə)n/ say it is false.

Page 11: Np completeness

N?? P?? NP??What are you talking about?

• N= Non-deterministic• P= Polynomial• NP= Non-deterministic Polynomial

(you guessed it)

Remember the Non-deterministic Turing machine I talked about earlier?

Come on it was only 5 slides back.

Slide 5Now you remember

Page 12: Np completeness

THE SOLUTIONAmazing Computer can do what normal Computers can't

• It is not a Super Computer (they are just very fast normal computers)• It is really a "Non-deterministic“ Computer

• We can solve a problem in P time using this “Amazing Compuer”

RESULT:

Page 13: Np completeness

Stop staring and listen

• N" computer can also do anything a normal computer can, we know that "P" problems are also in "NP".

• The easy problems are in "P" (and "NP"), but the really hard ones are *only* in "NP", and they are called "NP-complete".

TADA!!

Page 14: Np completeness

IF we really had such computers…

Page 15: Np completeness

One more thing

• it is believed that if anyone could *ever* solve an "NP-Complete" problem in "P" time

• *all* "NP-complete" problems could also be solved that way by using the same method

Page 16: Np completeness

More talking

• In computational complexity theory, the Cook–Levin theorem, also known as Cook's theorem, states that the Boolean satisfiability problem is NP-complete. That is, any problem in NP can be reduced in polynomial time by a deterministic Turing machine to the problem of determining whether a Boolean formula is satisfiable.

Blah…

Blah…

Blah…

BORING

Page 17: Np completeness

Lets do it…

• Travelling Salesman Problem

• Imagine you need to visit 5 cities on your sales tour.

• You know all the distances.• Which is the shortest round-trip to follow?

ABCEDA? ADECBA?

• An obvious solution is to check all possibilities.

Page 18: Np completeness

Lets take a brake now

So what do you think?Why did I choose “The traveling salesman” Problem?

Is it easy?Or because we already did it in class?

Actually NO!

Page 19: Np completeness

Because….

There is a movie on it… I bet you didn’t know!

Page 20: Np completeness

Back to work

• this only works for small problems

• If you add a new city it needsto be tried out in every previouscombination

• So this method takes “factorial time": t = n!

Actually t = (n-1)! but it is still factorial.

Page 21: Np completeness

Sounds easy, right?

• Lets say the program could solve a 20-city problem in 1 second, then

• a 21-city problem would take about 21 seconds to solve.

• And a 22-city problem would take about 462 seconds (over 7 minutes),

• and a 30-city problem would take 3 Million Years. Ouch!

Page 22: Np completeness

Thanks to “Richard E. Bellman”

• We have Dynamic programming• but the best still take "exponential time":

t = 2n

But if we had the “Amazing Computer”

• So a program that solved 20 cities in 1 second

• would take about 10 minutes to solve a 30-city

• problem and a 60-city problem would take 35,000 Years.

Page 23: Np completeness

And…

We could create multiple copies of theearlier sub-problems

solving each of those sub-problems justonce, and storing their solutions

In this way we can check for solutions of similar problems without repeating the actual process

Page 24: Np completeness

Hungry for more?

Check out:

Computers and Intractibility:A guide to the theory of NP-completenessMichael R. Garey and David S. JohnsonW. H. Freeman, 1979.

Page 25: Np completeness

In the End

Method to solve a NP-complete problem:

1. look for it in Garey and Johnson

2. find as similar a problem as you can in Garey and Johnson

3. you could always go back to the methods in Garey and Johnson

Page 27: Np completeness