Lecture3.11-3.13 NP Theory and Non Deterministic Algorithm
Lecture3.11-3.13 NP Theory and Non Deterministic Algorithm
Program:
Input: number x L1. If x < 15, output 1. Output:
Otherwise, GOTO L1. 1 if x < 15,
Never stops
otherwise.
A couple of things to note
• There are lots of programs for any given problem.
• Some are faster than others.
• We can always artificially slow them down.
Back to P and NP
• P and NP are classes of solvable problems.
• Solvable means that there’s a program that takes an input, runs for a
while, but eventually stops and gives the answer.
Computation “trees” for solvable problems
2
100 10 , 000 2 100 1,267,650, 600,228,22 9,401,496, 703,205,37 6
Okay… so what about NP?
• The description involves non-deterministic programming.
• NP stands for “non-deterministic, polynomial-time computable”
• The examples we’ve seen so far are examples of deterministic
programs.
• By the way, P stands for “polynomial-time computable”
An example of non-deterministic
programming
• Non-deterministic programs use a new kind of command that normal
programs can’t really use.
• Basically, they can guess the answer and then check to see if the
guess was right.
• And they can guess all possible answers simultaneously (as long as it’s
only finitely many).
An example of non-deterministic
programming We branch when
there’s a guess; one
path for each
Input x = 7 guess.
Program:
Input x.
guess y = 1 guess y = 2 guess y = 4 guess y = 9
Guess y in {1, 2, 4, 9}.
If x+y > 10,
stop and output 0. x+y<10 x+y>10, x+y>10,
guess z guess z
Otherwise, =2 =3 Output 0 Output 0
If y is even,
y is odd,
Guess z in {2, 3}, x+z = 9 is x+z = 10 is
Output 0 odd, even
If x+z is odd, stop,
Output 1
output 1.
Otherwise, output 0. Output 0
Non-deterministic programming
• Convention:
• If any computation path ends with a 1, the answer to the problem is 1 (we
count this as “yes”).
• If all computation paths end with a 0, the answer to the problem is 0 (we
count this as “no”).
• Otherwise, we say the computation does not converge.
• Again, we’re only interested in problems where this third case never
happens – solvable problems.
The class NP
• If the computation halts on input x, the length of the longest path is
NTime(x).
• A problem is NP if it is solvable and there is a non-deterministic
program that computes it so that NTime(x)≤(Some polynomial).
Non-deterministic → Deterministic
• A non-deterministic algorithm can be converted into a deterministic
algorithm at the cost of time.
• Usually, the increase in computation time is exponential.
• This means, for normal computers, (deterministic ones), NP problems
are slow.
The picture so far…
NP
All solvable
problems
SAT (The problem of satisfiabity)
• Take a statement in propositional logic, (like , for example).
• The problem
p qis to determine
p q if it is satisfiable. (In other words, is
there a line in the truth table for this statement that has a “T” at the
end of it.)
• This problem can be solved in polynomial time with a non-
deterministic program.
SAT, cont.
• We can see this by thinking about the process of constructing a truth
table, and what a non-deterministic algorithm would do:
SAT, cont.
• The length of each path in the computation tree is a polynomial
function of the length of the input statement.
• SAT is an NP problem.
NP completeness
• If P≠NP, SAT is a witness of this fact, that is, SAT is NP but not P.
• It is among the “hardest” of the NP problems: any other NP problem
can be coded into it in the following sense.
If R is a non-deterministic, polynomial-time algorithm that solves another NP problem, then for any
input, x, we can quickly find a formula, f, so that f is satisfiable when R halts on x with output 1,
and f is not satisfiable when R halts on x with output 0.
NP complete problems
• Problems with this property that all NP problems can be coded into
them are called NP-hard.
• If they are also NP, they are called NP-complete.
• If P and NP are different, then the NP-complete problems are NP, but
not P.
The picture so far…
NP
All solvable
problems
The picture so far…
NP-complete
problems
NP
All solvable
problems
Question:
• Is there a clever way to change a non-deterministic polynomial time
algorithm into a deterministic polynomial time algorithm, without an
exponential increase in computation time?
• If we can compute an NP complete problem quickly then all NP
problems are solvable in deterministic polynomial time.
On to examples…
• SAT is NP-complete
• The Minesweeper Consistency Problem
• Tetris
Minesweeper
Minesweeper
• The Minesweeper consistency problem:
• Given a rectangular grid partially marked with numbers and/or mines – some
squares being left blank – determine if there is some pattern of mines in the
blank squares that give rise to the numbers seen. That is, determine if the
grid is consistent for the rules of the game.
Minesweeper
• This is certainly an NP problem:
• We can guess all possible configurations of mines in the blank squares and
see if any work.
• To see that it is NP complete is much harder.
• The trick is to code logical expressions into partially filled minesweeper grids.
Then we will have demonstrated that SAT can be coded into Minesweeper, so
Minesweeper is also NP-complete.
Tetris
Rules:
The pieces fall, you can rotate
them.
When a line gets all filled up, it
is “cleared”.
If 4 lines get filled
simultaneously, that’s a “Tetris”
– extra points!
You lose if the screen fills up so
no new pieces can appear.
Tetris, the “offline” version
• You get to know all the pieces (there will only be finitely many in this
version), and the order they will appear.
• You start with a partially filled board.
Tetris, offline
• The following problems are NP-complete:
• Maximizing the number of rows cleared.
• Maximizing the number of pieces placed before losing.
• Maximizing the number of Tetrises.
• Maximizing the height of the highest filled gridsquare over the course of
the sequence.
Tetris, offline
• It’s easy to see each of these is NP – just guess the different ways to
rotate and place each piece, and see which is largest.
• It’s (as usual) harder to show NP-completeness.
• It is shown by reducing the 3-Partition problem (which is known to be NP-
complete) to the Tetris problems.
Sudoku
Each row/column/square has a single instance
of each number 1-9.
Solving a board is NP-complete.
41
THANK YOU
For queries
Email: [email protected]
05/20/2022 42