Presentation is loading. Please wait.

Presentation is loading. Please wait.

P, NP, and NP-Complete Problems Section 10.3 The class P consists of all problems that can be solved in polynomial time, O(N k ), by deterministic computers.

Similar presentations


Presentation on theme: "P, NP, and NP-Complete Problems Section 10.3 The class P consists of all problems that can be solved in polynomial time, O(N k ), by deterministic computers."— Presentation transcript:

1

2 P, NP, and NP-Complete Problems Section 10.3

3 The class P consists of all problems that can be solved in polynomial time, O(N k ), by deterministic computers (the ones that you have used all your life!). Examples: Adding two numbers: Extracting the element with the highest priority from a heap: Looking for an element in an array: Looking for the MST: O(1) “constant” O(log N) “logarithmic” (thus, O(N) because N  log N ) O(N) “lineal” O(N log N) (thus, O(N 2 ) )

4 What does Deterministic Computer Means? (Idea) (For details: see CSE 318) 1. Computer Memory At every computational cycle we have the so-called state of the computation: 3. Program … …. 3. Program … …. State = (1. memory, 2. location of memory being pointed at, 3. program, 4. current instruction) 2 4

5 What does Deterministic Computer Means? (Idea- II) Computer Memory In a deterministic computer we can determine in advance for every computational cycle, the output state by looking at the input state Program … …. Program … …. InputOutput

6 Our Favorite Example search(el, A, i) { if (A[i] = el) then return i else return search(el, A, i+1) } 7 5 3 8 9 3 el = 9 Complexity: O(N) //input: an array A[1..N] and an element el that is in the array //output: the position of el in A

7 Djikstra’s Shortest Path Algorithm A 6 B 3 C 3 D 2 E H 4 If the source is A, which edge is selected in the next iteration? Complexity: O(N log N) ( N = number of edges + vertices)

8 What does NonDeterministic Computer Means? Computer Memory In a nondeterministic computer we may have more than one output state. Program … …. Program … …. Output-1Output-2 The computer “chooses” the correct one (“nondeterministic choice”)

9 Nondeterministic Computers Seem More powerful Than Deterministic Ones nonDeterministicSearch(el, A) { return i } 7 5 3 8 9 3 el = 9 There may be problems that are solvable by nondeterministic computers which are not solvable by deterministic ones Informally: Calls an Oracle who makes the right choice i  ohOracle(el, A, 1)

10 But They Are Not More Powerful nonDeterministicSearch(el, A) { i  ohOracle(el, A, 1) return i } ohOracle(el, A, i) { if (A[i] = el) then return i else return ohOracle(el, A, i+1) } Key result: Every program for a nondeterministic computer can be simulated by a deterministic one (CSE 318) Thus, they solve the same problems

11 But What About the Complexity? search(el, A, i, N) { if (A[i] = el) then return i else return search(el, A, i+1,N) } Complexity: O(N) OracleSearch(el, A, N) { i  Oracle(el, A, N) return i } Complexity: O(1)

12 Formal Definition: Nondeterministic Algorithms Phase1(el, A) { return i } A nondeterministic algorithm for a problem A is a two- stage procedure. In the first phase, a procedure makes a guess about the possible solution for A. In the second phase, a procedure checks if the guessed solution is indeed a solution for A i  random(1..N) Phase2(i,el, A) { return } A[i] == el Note: the actual solution must be included among the possible guesses of phase 1

13 NP Complexity The class NP consists of all problems that can be solved in polynomial time by nondeterministic algorithms If A is a problem in P then A is a problem in NP because The key question is are there problems in NP that are not in P or is P = NP? We don’t know the answer to the previous question (that is, both phase 1 and phase 2 run in polynomial time) Phase 1: use the polynomial algorithm that solves A Phase 2: write a constant time procedure that always returns true

14 NP Complexity (II) How to proof that a problem A is in NP: 1. Show that A is in P, or 2. Write a nondeterministic algorithm solving A that runs in polynomial time

15 Example Showing that searching for an element in an array is in P: 1. Write the procedure search(el, A, i) (Slide 5) which runs in lineal time, or 2. Write a non-deterministic algorithm solving search (Slide 11; both Phase 1 and Phase 2 run in constant time)

16 Homework Both sections: write a nickname so I can post your grades on the web and you remain anonym Section 010:  Section 9.4: 3.b, 4, 6  Construct a nondeterministic algorithm computing the MST for an input graph (pseudocode). For phase 2 assume that the minimum cost is known in advance. What is the complexity of this algorithm?  Construct a nondeterministic algorithm solving the traveling salesman person problem (pseudocode). For phase 2 assume that the minimum cost is known in advance. What is the complexity of this algorithm? Section 011:  Section 9.4: 3.a, 5.a, 5.b  Construct a nondeterministic algorithm computing the shortest path from a node u to a node v in an input graph (pseudocode). For phase 2 assume that the minimum cost is known in advance. What is the complexity of this algorithm?  Construct a nondeterministic algorithm solving the Knapsack problem (pseudocode). For phase 2 assume that the minimum cost is known in advance. What is the complexity of this algorithm?


Download ppt "P, NP, and NP-Complete Problems Section 10.3 The class P consists of all problems that can be solved in polynomial time, O(N k ), by deterministic computers."

Similar presentations


Ads by Google