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

Post on 21-Jan-2016

224 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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.

Class P

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

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.

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)

Answers

• O(log(n)) 3

• O(n) 1000

• O(nlog(n)) 3000

• O(n2) 1000000

• O(n3) 1000000000

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.

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.

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.

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.

function gcd(x,y) {

do {

w = x % y; // x mod y

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

} until (y == 0)

return x;

}

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!

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

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.

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.

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.

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.

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).

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.

Verifiers

• A verifier for Composites multiplies the factors.

• A verifier for HAMPATH checks out the candidate path.

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.

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.

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.

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.

top related