43
CSC 172 P, NP, Etc

CSC 172 P, NP, Etc

  • Upload
    ringo

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

CSC 172 P, NP, Etc. “Computer Science is a science of abstraction – creating the right model for thinking about a problem and devising the appropriate mechanizable technique to solve it.” Aho & Ullman (1995). The process of abstraction. - PowerPoint PPT Presentation

Citation preview

Page 1: CSC 172  P, NP, Etc

CSC 172 P, NP, Etc

Page 2: CSC 172  P, NP, Etc

“Computer Science is a science of abstraction – creating the right model for thinking about a problem and devising the appropriate mechanizable technique to solve it.”

Aho & Ullman (1995)

Page 3: CSC 172  P, NP, Etc

The process of abstractionYou can solve problems by wiring up

special purpose hardware (hand calculator)

Turing showed that you could abstract hardware configurations

Von Neumann showed that you could abstract away from the hardware (machine languages)

High level languages are an abstraction of low level languages (JAVA/C++ rather than SML)

Page 4: CSC 172  P, NP, Etc

The process of abstraction

Data structures are an abstractions in high level languages (“mystack.push(myobject)”)

So, now we can talk about solutions to whole problems

“Similar” problems with “similar” solutions constitute the next level of abstraction

Page 5: CSC 172  P, NP, Etc

Hard, Harder, Impossible Some well-formed computational problems

don't have computational solution. They are “undecidable”. Trick: liar's paradox:

“This sentence is false”. Halts?(x) is a subroutine that is T or F if x (a

program) halts or loops. Oops(x) is if Halts?(x) then loop-forever() else halt. Oops(Oops) ??

Page 6: CSC 172  P, NP, Etc

Some DefinitionsExponential time: you have to test every

possibilityO(kn)

Polynomial time: you have some clever algorithmO(nk) Note even n100 is better than

exponential

Page 7: CSC 172  P, NP, Etc

P: A problem that can be solved quickly (in polynomial time) O(nc)

NP: A problem whose solution can be checked for correctness in polynomial time.

NP-hard: A problem such that every problem in NP reduces to it. (Not in NP)

NP-complete (NPC): A problem that is both NP hard and in NP

Page 8: CSC 172  P, NP, Etc

P: A problem that can be solved quickly (in polynomial time) O(nc)

NP: A problem whose solution can be checked for correctness in polynomial time.

NP-hard: A problem such that every problem in NP reduces to it. (Not in NP)

NP-complete (NPC): A problem that is both NP hard and in NP

Page 9: CSC 172  P, NP, Etc

The class of problems “P”

“P” stands for “Polynomial”The class “P” is the set of problems that

have polynomial time solutionsSome problems have solutions that run in

O(nc) time testing for cycles, MWST, Conn. Comps, shortest path, simple sorting,....

Page 10: CSC 172  P, NP, Etc

Not in the class of problems “P”?

On the other hand, some problems seem to take time that is exponential O(2n) or worseTSP, satisfiability, graph coloring

Page 11: CSC 172  P, NP, Etc

Famous Examples Travelling Salesman Prob: In undirected weighted

graph find path starting and ending at specified vertex, visiting each vertex once, costing <= K.

Satisfiability: e.g. what a, b, c, d, e values make (a v b v ~c)^(d v ~a ve)^(~c v b v d)^(c v ~d v e) true? (3-SAT)

How many colors needed to color graph vertices so no nbrs have same color? How color (= sudoku)?

Page 12: CSC 172  P, NP, Etc

Same problem if polynomially reducible

Assume I have boolean fn satisfiable(String expression)

How do I write tautology(String expression)

Page 13: CSC 172  P, NP, Etc

Easy Conversion

tautology(exp) = no var. assts such that satisfiable(exp) is false.

Page 14: CSC 172  P, NP, Etc

The class NP“NP” stands for “Nondeterministic Polynomial”Nondeterministic computation: “guess” or

“parallelize”A problem can be solved in nondeterministic

polynomial time if: given a guess at a solution for some

instance of size n we can check that the guess is correct in polynomial time (i.e. the check runs O(nc))

Page 15: CSC 172  P, NP, Etc

NP takes P time to Check TSP: does path hit all vertices, cost <= K? linear. SAT: evaluate the boolean expression with constant

(0,1) values: linear. Colorability: check all edges for diff. colored ends.

linear. Hamiltonian Path: path hits all vertices? linear.

Page 16: CSC 172  P, NP, Etc

P NP

NP

PP NP

Page 17: CSC 172  P, NP, Etc

NPCNPC stands for “NP-complete”Some problems in NP are also in P

-they can be solved as well as checked in O(nc) time

Others, appear not to be solvable in polynomial time

There is no proof that they cannot be solved in polynomial time

But, we have the next best thing to such proofA theory that says many of these problems are as hard as any in NPWe call these “NP-complete problems”

Page 18: CSC 172  P, NP, Etc

Not sure?

We work to prove equivalence of NPC problemsIf we could solve one of them in O(nc) time then

all would be solvable in polynomial time (P == NP)

What do we have?Since the NP-complete problems include many

that have been worked on for centuries, there is strong evidence that all NP-complete problems really require exponential time to solve.

Page 19: CSC 172  P, NP, Etc
Page 20: CSC 172  P, NP, Etc

Reductions

The way a problem is proved NP-complete is to “reduce” a known NP-complete problem to it

We reduce a problem A to a problem B by devising a solution that uses only a polynomial amount of time (to convert the data, make the correspondence) plus a call to a method that solves B

Page 21: CSC 172  P, NP, Etc

Easiest NPC Problem? Partition problem: partition list of integers into 2

parts such that sum(part1) = sum(part2) (3,1,1,2,2,1) -> (1,1,1,2) and (2,3). Version of subset-sum problem (is there a subset of

a list of ints that sums to zero?), Also NPC. Very good dynamic program and not bad greedy

approaches. hence “easy”.

Page 22: CSC 172  P, NP, Etc

Back to Graphs

By way of example of a class of problems considerCliques & Independent Sets in graphs

Page 23: CSC 172  P, NP, Etc

Cliques

A complete sub-graph of an undirected graphA set of nodes of some graph that has every possible edge

The clique problem:Given a graph G and an integer k, is there a clique of at least k nodes?

Page 24: CSC 172  P, NP, Etc

Example

Page 25: CSC 172  P, NP, Etc

Example

A B C D

E F HG

Page 26: CSC 172  P, NP, Etc

Example

A B C D

E F HG

Page 27: CSC 172  P, NP, Etc

Example

A B C D

E F HG

K == 4

ABEFCGHD

Page 28: CSC 172  P, NP, Etc

Independent SetSubset S of the nodes of an undirected graph such that

there is no edge between any two members of SThe independent set problem

given a graph G and an integer k, is there an independent set with at least k nodes

(Application: scheduling final exams)nodes == courses, edges mean that courses have one student in common.

Any guesses on how large the graph would be for UR?

Page 29: CSC 172  P, NP, Etc

Example independent set

A B C D

E F HG

K == 2

ACADAGAHB(D,G,H)Etc..

Page 30: CSC 172  P, NP, Etc

Checking Solutions

Clique, IS, colorability are examples of hard to find solutions “find a clique of n nodes”

But, it’s easy (polynomial time) to check a proposed solution.

Page 31: CSC 172  P, NP, Etc

Checking

Check a proposed clique by checking for the existence of the edges between the k nodes

Check for an IS by checking for the non-existence of an edge between any two nodes in the proposed set

Check a proposed coloring by examining the ends of all the edges in the graph

Page 32: CSC 172  P, NP, Etc

Checking

Check a proposed clique by checking for the existence of the edges between the k nodes

Check for an IS by checking for the non-existence of an edge between any two nodes in the proposed set

Check a proposed coloring by examining the ends of all the edges in the graph

Page 33: CSC 172  P, NP, Etc

Same Problem Reductions

Clique to IS Given a graph G and an integer k we want to know if there is a clique of size k in G

1. Construct a graph H with the same set of nodes as G and an edge wherever G does not have edges

2. An independent set in H is a clique in G3. Use the “IS” method on H and return its

answer

Page 34: CSC 172  P, NP, Etc

Same Problem Reductions

IS to CliqueGiven a graph G and an integer k we want to know if there is an IS of size k in G

1. Construct a graph H with the same set of nodes as G and an edge wherever G does not have edges

2. An independent set in H is a clique in G3. Use the “clique” method on H and return its

answer

Page 35: CSC 172  P, NP, Etc

Example

A B C D

E G

Page 36: CSC 172  P, NP, Etc

Example

A B C D

E G

Page 37: CSC 172  P, NP, Etc

Example

A B C D

E G

Page 38: CSC 172  P, NP, Etc

Example

A BC

D

E G

Page 39: CSC 172  P, NP, Etc

Example

ABC

D

E G

Page 40: CSC 172  P, NP, Etc

Example

ABC

D

E G

Page 41: CSC 172  P, NP, Etc

Example

ABC

D

E G

A B C D

E G

Page 42: CSC 172  P, NP, Etc

Hamiltonian cycle from TSPMake complete graph from input graph, give original edges cost 1,

added edges cost 2, and if there were K original edges solve TSP for K.

Page 43: CSC 172  P, NP, Etc