computability heap exercise. the class p. the class np. verifiers. homework: review relprime proof....

26
Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP.

Upload: jack-powers

Post on 21-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Computability

Heap exercise. The class P. The class NP. Verifiers.

Homework: Review RELPRIME proof. Find examples of problems in NP.

Page 2: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Heap sort

• Heap sort:– make whole set into a heap, starting at the top– Loop: remove root: swap with last member.

Remove (don't think about this position any more). Then restore heap property by possibly pushing value down. We only need to fix up one of the two subtrees at each stage.

• http://nova.umuc.edu/~jarc/idsv/lesson3.html

Page 3: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Formalities

• The Sipser text does everything in terms of Turing machines.

• We may talk more on some general model, such as encoding in a programming language

Page 4: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Multiple tape TM

Let M be a multiple tape TM that takes t(n) time, t(n)>=n. Then M can be simulated by a single tape TM N taking at most O(t2(n)).

Informal proof: Material on all the tapes must be bounded by t(n).1) Use O(n) to set up single tape and put in markers for the k tapes.2) Simulation of each step bounded by t(n)3) So c* t(n) * t(n) is the bound.

Page 5: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Class P

• All languages that are decidable in polynomial time on a single tape, deterministic Turing machine.

Page 6: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Alternative definition

• Consider a standard programming language, an algorithm is in the class P if the running time for an input (size of input is n) is bounded by a polynomial p(n).

• All the sorts, searches, shuffles discussed so far are in P.

Page 7: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Practicality

• In practical terms, there is a big difference between algorithms O(log(n)),O(n), O(nlog(n)), O(n2), etc.

• Say n = 1000, then what is ??– O(log(n)) use 10 as the base– O(n)– O(nlog(n))– O(n2)– O(n3)

Page 8: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Answers

• O(log(n)) 3

• O(n) 1000

• O(nlog(n)) 3000

• O(n2) 1000000

• O(n3) 1000000000

Page 9: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

TerminologyBUT• bigger difference between polynomial and

exponential– 1000 versus 10003 (1 billion) versus 21000 (more than

the number of atoms in the universe…)• So polynomial time algorithms are called easy,

tractable, practical and the exponential algorithms called hard, intractable, impractical.

• Note: these are all solvable. That is, there are deciders for the language accepting programs or programs that halt with an answer for the functions.

Page 10: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

RELPRIME

• For two natural numbers, x and y, x and y are relatively prime if 1 is the largest number that is a factor of both.

• Neither may be prime, but relatively prime means they don't share any factors.

• Note: if each is prime, then they are relatively prime.

Page 11: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Examples

• 2 and 3 Yes! easy, each is prime

• 4 and 9 Yes, try the factors 2 and 3.

• 20 and 64 No! 2 is a factor. Two even number are not relatively prime.

• 75 and 14 Yes, factors 3 & 5 versus 2 & 7

Now, each of you supply 1 pair that is relatively prime and 1 that is not.

Page 12: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

How to determine Relprime?

• Brute force (which is what I did):– determine factors of each and compare.– this is exponential…

• Alternative:– Use Euclidean Algorithm for greatest common

divisor of x & y. – If gcd is 1, return yes (true) else return false.

Page 13: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

function gcd(x,y) {

do {

w = x % y; // x mod y

x = y; y = w; // swap x and y

} until (y == 0)

return x;

}

Page 14: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Example

• x = 9, y = 4– w = 9 % 4 ; // w = 1– x = 9, y = 1;– w = 9 % 1; // w = 0– x = 1, y = 0

– return 1 relatively prime!

Page 15: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Example

• x = 6, y = 9• w = 6 % 9; //w = 6• x = 9; y = 6• w = 9 % 6; // w = 3• x = 6; y = 3;• w = 6 % 3; // w = 0• x = 3; y = 0• return 3 NOT relatively prime

Page 16: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Analysis

• After possibly the first stage, each stage cuts the value of x in half.

• Since values are swapped, each of x and y are halved every two times.

• So number of stages is proportional log(x) plus log(y). This is polynomial…Actually O(n)

• LOOK UP AND GIVE IMPROVED PROOF.

Page 17: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Non-deterministic algorithm

• Think of TM with multiple possibilities OR

• Using the programming language model, think of making guesses from a finite set of choices.

Page 18: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Example: Composites

• Composites = {x | x = pq where p, q >1}– is x not a prime?

• Factoring is difficult (more on this later) but given a number X, if you can produce a candidate set p and q, then it is easy to check if x = p * q.

Page 19: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Hamiltonian circuit

• Let G be a directed graph. A Hamiltonian circuit is a path using the edges of G that goes through each vertex exactly once.

• HAMPATH = {<G, s, t>| there is a Hamiltonian path from s to t}– language definition.

• No good way (so far) to determine if there is such as path for any <G, s, t> , that is, is <G,s, t> in the language.

Page 20: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Verifier / checker

• Some problems have the feature that finding a solution may be difficult, but checking it may be easy / easier.

• That is, one way to do the problem may involve guessing (the algorithm may be exponential)

• …. but checking can be done relatively easily (polynomial).

Page 21: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Formal definition: verifier

• Given a language A, a verifier V is an algorithm such that

A = {w | V accepts <w,c> for some string c}

The string c is the answer or certificate or proof that w is in A.

Page 22: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Verifiers

• A verifier for Composites multiplies the factors.

• A verifier for HAMPATH checks out the candidate path.

Page 23: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

NP

• is the class of languages that have polynomial time verifiers.

• This would include all the languages that are decided in polynomial time plus some others.

Page 24: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Theorem

• A language is in NP if and only if it can be decided by a polynomial time Turing Machine– or, using a program model, a program that

makes choices among a finite number of options at certain stages.

Page 25: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

P = NP???

• It is not known, and one of, if not the most important problems in theoretical computer science, whether or not P = NP.

• If true, this means that for each NP problem, there is a P algorithm.

• If false, this means that for some NP problems (more on this later), there is no P algorithm.

Page 26: Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP

Homework

• Review RELPRIME algorithm and analysis and give [more] formal proof that it is polynomial.

• Find another problem that is NP.

• Read about P and NP.