1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

16
1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014

description

3 NP Complexity

Transcript of 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

Page 1: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

1

CPSC 320: Intermediate AlgorithmDesign and Analysis

July 30, 2014

Page 2: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

2

Course Outline• Introduction and basic concepts• Asymptotic notation• Greedy algorithms• Graph theory• Amortized analysis• Recursion• Divide-and-conquer algorithms• Randomized algorithms• Dynamic programming algorithms• NP-completeness

Page 3: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

3

NP Complexity

Page 4: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

4

Recap• We are working on problems, not algorithms• Our focus now is on decision problems (yes/no), not optimization problems• We distinguish “finding” a solution and “checking” a solution• Classes:

• P: decision problems that are solvable in polynomial time• NP: decision problems for which a given certificate can be checked in

polynomial time• NP hard: at least as “hard” as any problem in NP• NP complete (NPC): both NP and NP hard

Page 5: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

5

NP complete• NPC problems:

• have no known algorithm that runs in polynomial time• There is no proof that such an algorithm doesn’t exist

• Examples:• Hamiltonian path: path that traverses all nodes exactly once• 3-SAT: assign values to Boolean variables that satisfy a set of clauses• Graph coloring: assign colors to graphs, adjacent nodes have different colors

• Cook’s theorem (paraphrased): if the satisfiability problem can be solved in polynomial time, any problem in NP can be solved in polynomial time

Page 6: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

6

Problem Reduction• Some times the solution to a problem is to reduce it into another• Reduction: given a problem A, reduce it to B by:

• providing a way to transform the input (instance) of A into a valid input of B in polynomial time

• providing a translation from the result of B into the result of A• proving that this transformation results in a valid solution for A

• Example: stable matching problem, co-op setting, multiple jobs in a company• transform a company with jobs into companies with one job each, all with

same preferences• each student lists the companies instead of the one company in any order• in the result, translate matches for these companies into matches for original

company

Page 7: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

7

NPC Proof• How do we prove a problem is NP hard?

• Reduce any known NPC problem into in polynomial time• If is NPC, and we can solve by reducing it into , then is bound in time

complexity by the problem • We say that

• How do we prove a problem is NPC?• Prove it is NP, and prove it is NP hard

• Clarification: reduce the known NPC problem into the problem to test

Page 8: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

8

Graph Coloring• Problem: can we color all nodes in a graph using at most colors, such that

adjacent nodes (i.e., nodes linked by an edge) have different colors?• Application: exam scheduling, map coloring

• This is in NP, since, given a coloring, we can verify it’s valid in • Is this NP complete?

• Let’s reduce the satisfiability problem into a 3-coloring problem (prove that 3SAT Coloring)

Page 9: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

9

Graph Coloring – NPC proof• From an instance of 3-SAT, create a new graph with:

• Three nodes , and , all connected to each other;• For each variable , create two nodes: and , connected to each other and to ;• For each disjunction , create new nodes as described in the image below.

• This graph can be constructed in polynomial time.

𝑥 𝑦

𝑥∨ 𝑦

Page 10: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

10

Graph Coloring – NPC proofLemma: Assume a 3-coloring attempt. Assume , and have colors , and . Then, if the nodes representing and are both , then the node for is colored .Proof: By exhaustion.Lemma: In the same setting, if one of the nodes representing and is , then the node for can be colored .Proof: By exhaustion.

Page 11: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

11

Graph Coloring – NPC proofTheorem: The graph is 3-colorable if and only if is satisfiable• If is satisfiable:

• Assign color / to node if is true or false in solution to • Assign color / to node if is true or false in solution to • By previous lemmas, every node of the graph can be colored with //

• If is colorable:• Assign true/false to variable if node is colored /• Since bottom node is , at least one of its variables must be

• QED

Page 12: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

12

Clique• Problem: Given a graph , is there a set (“clique”) of nodes for which there is an

edge for every pair of nodes (a fully-connected subgraph)?• Is this NP?

• Given a set of nodes, we can check if it forms a clique in • Is this NPC?

• Can we reduce an NPC problem into it?

Page 13: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

13

Clique – NPC proof• We will try to prove that 3-SAT Clique• Given an instance of 3-SAT, with clauses, build a graph such that:

• For each clause , build one node for each literal in • Build an edge between and if and the corresponding literals are not

negations of each other (e.g., they are not and )• We can build this graph in polynomial time

Page 14: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

14

Clique – NPC proof• We want to prove that is satisfiable if and only if there is a clique with size • If is satisfiable:

• At least one of the literals in each clause is true• The node for a true literal in each clause is connected to the true literal in all

other clauses• There are clauses, so this clique has nodes

• If there is a clique with size • Each node in the clique is in a different clause• The nodes in the clique are not contradictory• We can assign true/false for nodes that correspond to variables/negations, and

any value for other variables• CQD

Page 15: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

15

Vertex Cover• Problem: given a graph , is there a subset of nodes such that, for every edge in ,

either , or , or both?• Is this problem NP?• Is this problem NPC?

Page 16: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.

16

Vertex Cover – NPC proof• We will try to prove that Clique Vertex Cover• Given an instance of the clique problem with graph (with nodes) and clique size ,

let’s reduce this problem into a vertex cover with size on a graph with:• exactly the same nodes as • the complement of the edges in (i.e., )

• If has a clique of size :• There is no edge in that connects two nodes not in • So is a vertex cover with size

• If has a vertex cover of size :• There is an edge in for every pair of nodes in • So is a clique with size