Mark Allen Weiss: Data Structures and Algorithm Analysis in Java
Lydia Sinapova, Simpson College
v Class P problems
v Class NP problems
v NP-complete problems
v Non-NP problems
Class P Problems
Ø Polynomial run time in the worst-case
Ø "tractable" problems
Ø Simple sorting algorithms
Ø Advanced sorting algorithm
Ø Topological Sort
Ø Shortest Paths
Ø Min Spanning Trees
Class NP Problems
NP-Problems: Problems for which there is no known
polynomial-time algorithms, "intractable" problems
NP means non-deterministically polynomial. If we
have a candidate for a solution, we can verify it in
polynomial time. The difficult part is to generate all
Example: (the satisfiability problem) Given a boolean expression of
n variables, find a set of variable assignments that satisfy the
Obviously, P Í NP, but P Ì NP (or P = NP) is an open question
An NP-Complete problem is an NP problem with the
property that any problem in NP can be reduced to it in
In 1971 Stephen Cook (currently working at the
University of Toronto, Ontario) showed that the
satisfiability problem is NP-complete by proving that
every other NP problem could be transformed to it.
Bin packing problem: How to pack or store objects of various sizes and
shapes with a minimum of wasted space?
Clique problem: A clique is a subset K of vertices in an undirected graph
G such that every pair is joined by an edge in G. The problems are:
Given a graph G, and an integer k, does G have a clique consisting of
Given a graph G, find a clique with as many vertices as possible.
Knapsack problem: You have a knapsack with a given size and several
objects with various sizes and values. The problem is how to fill your
knapsack with these objects so that you have a maximum total value.
Hamiltonian Cycle: a cycle that contains all nodes of the graph. Proving
that a graph has a Hamiltonian cycle is a NP-complete problem.
NP problems and non-NP problems:
For NP problems, given a candidate for a solution, we can verify it in
For non-NP problems, we cannot verify in polynomial time whether
a candidate for a solution is a solution or not.
Example: the problem to prove that a graph does not have a
Hamiltonian cycle is a non-NP problem.
NP-hard problems: NP-Complete, but not necessarily in NP class
Yes/no problems (decision problems)
A decision problem is a question that has two
possible answers yes or no. The question is
about some input.
Examples of Yes/No
Given a graph G and a set of vertices K, is K a
Given a graph G and a set of edges M, is M a
Given a set of axioms (boolean expressions)
and an expression, is the expression provable
under the axioms?
Decidable, semi-decidable, and
A problem is decidable if there is an algorithm
(P or NP) that says yes if the answer is yes,
and no otherwise.
A problem is semi-decidable if there is an
algorithm that says yes if the answer is yes,
however it may loop infinitely if the answer is
A problem is undecidable if we can prove that
there is no algorithm that will deliver an
Example of semi-decidable
Given a set of axioms, prove that an expression is true.
Problem 1: Let the axioms be:
To prove A we add ~A to the axioms.
If A is true then ~A will be false and this will cause a
contradiction - the conjunction of all axioms plus ~A will result in
(A v B) L ~A = B
B L (A v C) = (B L A) v (B L C)
B L ~ B = False 11
Example of semi-decidable
Problem 2: Let the axioms be:
We add A and obtain:
(A v C) L A = A
(A v B) L A = A
A L ~B = A L ~B
(A L ~B) L (A v B) = A L ~ B
This process will never stop, because the expressions we
obtain will always be different from False
Example of undecidable
The halting problem
Let LOOP be a program that checks other
programs for infinite loops:
LOOP(P) stops and prints "yes" if P loops infinitely
LOOP(P) enters an infinite loop if P stops
What about LOOP(LOOP)?