This preview shows pages 1–3. Sign up to view the full content.
Mehran Sahami
Handout #13
CS103B
January 28, 2009
Managing a Bestiary of Algorithms:
Complexity Classes and NPCompleteness
Thanks to Maggie Johnson for some portions of this handout.
A Quick Introduction to NPCompleteness
Imagine that you have decided to go on a skiing adventure across the Sierras.
Since there are no
McDonalds between Yosemite and Lee Vining, you will need to bring all your food with you in
your pack (um, yes, did I forget to mention that you are
backpacking
across the Sierras?!).
On
this trip, taste is a secondary consideration.
What matters is how much your food weighs and
how many calories it contains.
After raiding your kitchen (or your dorm dining hall), you
discover that you have roughly 200 different food items.
Each item has a certain weight and
contains a certain number of calories:
1.
Snickers Bar
200 calories
100 grams
2.
Diet Coke
1 calorie
200 grams
...
200. Dry Spaghetti
500 calories
450 grams
Given this selection of food, your task is to find the best subset that maximizes the number of
calories, but falls within the weight limit you can carry in your pack (say 15kg).
One algorithm
that definitely produces a solution is to simply try every possible combination.
Unfortunately,
there are 2
200
such combinations, and the Sierras would erode away before that algorithm
terminates.
Always a bummer when that happens.
Is there another algorithm that runs in reasonable time (
i.e
., a polynomial, as opposed to
exponential, number of steps in the size of the input)?
If there is, no one has found it yet.
This
might make you suspect that no polynomial time solution exists, but no one has ever been able to
prove that either.
This problem, known as the Knapsack Problem, is an example of a general
class of problems called the
NPcomplete
problems.
These are problems for which no
polynomial time solution currently exists, but it is unproven whether the problems necessarily
require exponential time.
In fact, there is currently a $1 million prize
1
for first person who can either find a polynomial
time solution to an NPcomplete problem, or prove that such a problem can only be solved with
an exponential time algorithm.
Sounds like something to consider as a summer project…
Characterizing Function Growth Rates
In studying algorithms, we have seen functions and bigOh notation used to characterize the
running time of algorithms.
There are some important terms used to classify problems, based on
1
The
“P vs. NP” problem is one of seven “Millennium Prize Problems” that have been named by The Clay
Mathematics Institute (CMI) of Cambridge, Massachusetts as worthy of a $1 million prize.
You may find more
information
about
all
seven
Millennium
Prize
Problems
at
the
web
site:
http://www.claymath.org/millennium/
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 2 
the running time of the algorithms that solve those problems.
First, we review the different
categories of functions for bigOh notation below.
The bigOh functions that involve n as an exponent (n! or 2
This is the end of the preview. Sign up
to
access the rest of the document.
 Winter '08
 SAHAMI,M
 Algorithms

Click to edit the document details