Data Structures in Java

Similar documents
NP-Completeness. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

NP-Completeness. Until now we have been designing algorithms for specific problems

Analysis of Algorithms. Unit 5 - Intractable Problems

Easy Problems vs. Hard Problems. CSE 421 Introduction to Algorithms Winter Is P a good definition of efficient? The class P

Data Structures and Algorithms

ECS122A Handout on NP-Completeness March 12, 2018

Computational Complexity

Polynomial-time Reductions

Data Structures and Algorithms (CSCI 340)

CMSC 441: Algorithms. NP Completeness

CS/COE

Data Structures in Java. Session 22 Instructor: Bert Huang

Lecture 4: NP and computational intractability

Algorithms: COMP3121/3821/9101/9801

Tractability. Some problems are intractable: as they grow large, we are unable to solve them in reasonable time What constitutes reasonable time?

NP Complete Problems. COMP 215 Lecture 20

Theory of Computation CS3102 Spring 2014 A tale of computers, math, problem solving, life, love and tragic death

Unit 1A: Computational Complexity

Tractable & Intractable Problems

NP Completeness and Approximation Algorithms

Summer School on Introduction to Algorithms and Optimization Techniques July 4-12, 2017 Organized by ACMU, ISI and IEEE CEDA.

NP-Complete Problems and Approximation Algorithms

CHAPTER 3 FUNDAMENTALS OF COMPUTATIONAL COMPLEXITY. E. Amaldi Foundations of Operations Research Politecnico di Milano 1

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

NP-Complete Problems. More reductions

CSC 1700 Analysis of Algorithms: P and NP Problems

P,NP, NP-Hard and NP-Complete

Outline. 1 NP-Completeness Theory. 2 Limitation of Computation. 3 Examples. 4 Decision Problems. 5 Verification Algorithm

Computational Complexity. IE 496 Lecture 6. Dr. Ted Ralphs

Design and Analysis of Algorithms

CS 583: Algorithms. NP Completeness Ch 34. Intractability

P, NP, NP-Complete. Ruth Anderson

Limitations of Algorithm Power

COSE215: Theory of Computation. Lecture 20 P, NP, and NP-Complete Problems

1. Introduction Recap

P vs. NP. Data Structures and Algorithms CSE AU 1

In complexity theory, algorithms and problems are classified by the growth order of computation time as a function of instance size.

Introduction. Pvs.NPExample

Circuits. CSE 373 Data Structures

P and NP. Warm Up: Super Hard Problems. Overview. Problem Classification. Tools for classifying problems according to relative hardness.

Automata Theory CS Complexity Theory I: Polynomial Time

COMP/MATH 300 Topics for Spring 2017 June 5, Review and Regular Languages

CS 241 Analysis of Algorithms

CSCI3390-Lecture 14: The class NP

CS 350 Algorithms and Complexity

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: NP-Completeness I Date: 11/13/18

Computational Complexity and Intractability: An Introduction to the Theory of NP. Chapter 9

NP-complete problems. CSE 101: Design and Analysis of Algorithms Lecture 20

15.083J/6.859J Integer Optimization. Lecture 2: Efficient Algorithms and Computational Complexity

Algorithm Design Strategies V

Lecture 15 - NP Completeness 1

Algorithms and Theory of Computation. Lecture 19: Class P and NP, Reduction

VIII. NP-completeness

NP-Completeness. Subhash Suri. May 15, 2018

CS 350 Algorithms and Complexity

NP-Complete and Non-Computable Problems. COMP385 Dr. Ken Williams

Multiple Sequence Alignment: Complexity, Gunnar Klau, January 12, 2006, 12:

} } } Lecture 23: Computational Complexity. Lecture Overview. Definitions: EXP R. uncomputable/ undecidable P C EXP C R = = Examples

Algorithms Design & Analysis. Approximation Algorithm

P and NP. Inge Li Gørtz. Thank you to Kevin Wayne, Philip Bille and Paul Fischer for inspiration to slides

NP-Completeness Theory

CS 320, Fall Dr. Geri Georg, Instructor 320 NP 1

Check off these skills when you feel that you have mastered them. Write in your own words the definition of a Hamiltonian circuit.

CS 6505, Complexity and Algorithms Week 7: NP Completeness

Lecture 14 - P v.s. NP 1

NP Completeness. CS 374: Algorithms & Models of Computation, Spring Lecture 23. November 19, 2015

Limits of Feasibility. Example. Complexity Relationships among Models. 1. Complexity Relationships among Models

CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010

1 Computational problems

More on NP and Reductions

Computability and Complexity Theory

Some Algebra Problems (Algorithmic) CSE 417 Introduction to Algorithms Winter Some Problems. A Brief History of Ideas

BBM402-Lecture 11: The Class NP

Optimization Prof. A. Goswami Department of Mathematics Indian Institute of Technology, Kharagpur. Lecture - 20 Travelling Salesman Problem

Graduate Algorithms CS F-21 NP & Approximation Algorithms

CSCI3390-Lecture 18: Why is the P =?NP Problem Such a Big Deal?

Problem Complexity Classes

Problems and Solutions. Decidability and Complexity

NP-completeness. Chapter 34. Sergey Bereg

The Beauty and Joy of Computing

1 Primals and Duals: Zero Sum Games

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

Undecidable Problems. Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, / 65

Exercises NP-completeness

Data Structures and Algorithms

Problems, and How Computer Scientists Solve Them Manas Thakur

CP405 Theory of Computation

P, NP, NP-Complete, and NPhard

Computational Complexity

Combinatorial Optimization

NP-Completeness. ch34 Hewett. Problem. Tractable Intractable Non-computable computationally infeasible super poly-time alg. sol. E.g.

Graph Theory and Optimization Computational Complexity (in brief)

Lecture 18: P & NP. Revised, May 1, CLRS, pp

NP-Completeness. Andreas Klappenecker. [based on slides by Prof. Welch]

CSC 421: Algorithm Design & Analysis. Spring 2018

Lecture 19: Finish NP-Completeness, conp and Friends

Essential facts about NP-completeness:

Computational Complexity

Introduction to Computer Science and Programming for Astronomers

Transcription:

Data Structures in Java Lecture 21: Introduction to NP-Completeness 12/9/2015 Daniel Bauer

Algorithms and Problem Solving Purpose of algorithms: find solutions to problems. Data Structures provide ways of organizing data such that problems can be solved more efficiently Examples: Hashmaps provide constant time access by key, Heaps provide a cheap way to explore different possibilities in order When confronted with a new problem, how do we: Get an idea of how difficult it is? Develop an algorithm to solve it?

Problem Difficulty We can think of the difficulty of a problem in terms of the best algorithm we can find to solve the problem. Most problems we discussed so far have linear time solutions O(N), or slightly more than linear O(N log N). We often considered anything worse than O(N 2 ) to be a bad solution. For some problems we don t know efficient algorithms. What is the best algorithm we can hope for, for a given problem? (for instance, for comparison based sorting).

Polynomial and Exponential Time Two common classes of running time for algorithms: Polynomial: O(N k ) for some constant k. Exponential: O(2 N k ) for some constant k

Hamiltonian Cycle A Hamiltonian Path is a path through an undirected graph that visits every vertex exactly once (except that the first and last vertex may be the same). A Hamiltonian Cycle is a Hamiltonian Path that starts and ends in the same node. No hamiltonian path.

Hamiltonian Cycle Can check if a graph contains an Euler Cycle in linear time. Surprisingly, checking if a graph contains a Hamiltonian Path/Cycle is much harder! No polynomial time solution (i.e. O(N k ) ) is known. No hamiltonian path.

Traveling Salesman Problem (TSP) Given a complete, undirected graph G = (V,E), find the shortest possible cycle that visits all vertices. Optimal Traveling Salesman Tour through all 48 continental state capitals. Source: SAP

TSP - How many tours are there? Given a complete, undirected graph G = (V,E), find the shortest possible cycle that visits all vertices. 5 7 library C B 9 store 14 10 We can visit the vertices of the graph in ANY order. How many possibilities are there? D home 8 A post office

TSP - How many tours are there? Given a complete, undirected graph G = (V,E), find the shortest possible cycle that visits all vertices. 5 7 library C 9 B 14 store 10 We start at D. Because the graph is complete, we can go to any of the other N-1 nodes. D home 8 A post office

TSP - How many tours are there? Given a complete, undirected graph G = (V,E), find the shortest possible cycle that visits all vertices. 5 7 library C 9 B 14 store 10 We start at D. Because the graph is complete, we can go to any of the other N-1 nodes. Once we decide for a node, we can go to N-2 remaining nodes. D home 8 A post office

TSP - How many tours are there? Given a complete, undirected graph G = (V,E), find the shortest possible cycle that visits all vertices. 5 7 library C 9 B 14 store 10 We start at D. Because the graph is complete, we can go to any of the other N-1 nodes. Once we decide for a node, we can go to N-2 remaining nodes. D home 8 A post office Once we decide for a node, we can go to N-3 remaining nodes.

TSP - How many tours are there? Given a complete, undirected graph G = (V,E), find the shortest possible cycle that visits all vertices. 5 7 library C 9 B 14 store 10 We start at D. Because the graph is complete, we can go to any of the other N-1 nodes. Once we decide for a node, we can go to N-2 remaining nodes. D home 8 A post office Once we decide for a node, we can go to N-3 remaining nodes.

TSP - How many tours are there? Given a complete, undirected graph G = (V,E), find the shortest possible cycle that visits all vertices. D home 5 7 library C B 8 9 store 14 10 A post office D A C B = D B C A There are possibilities, but we can traverse complete tours in either direction. There are complete tours.

TSP - Brute Force Approach Try all possible tours and return the shortest one. Obviously this algorithm runs in

TSP - Brute Force Approach Try all possible tours and return the shortest one. Obviously this algorithm runs in Better algorithm: Dynamic Programming algorithm byheld-karp (1962) No polynomial time algorithm is known!

TSP - Greedy Approximation How about a greedy approximation? library C Start with node D, always follow the lowest edge until all 5 7 B 9 store 14 10 vertices have been visited. D home 8 A post office

TSP - Greedy Approximation How about a greedy approximation? library C Start with node D, always follow the lowest edge until all 5 7 B 9 store 14 10 vertices have been visited. D home 8 A post office

TSP - Greedy Approximation How about a greedy approximation? library C Start with node D, always follow the lowest edge until all 5 9 10 vertices have been visited. 7 B store 14 D home 8 A post office

TSP - Greedy Approximation How about a greedy approximation? library C Start with node D, always follow the lowest edge until all 5 9 10 vertices have been visited. 7 B store 14 D home 8 A post office

TSP - Greedy Approximation How about a greedy approximation? library C Start with node D, always follow the lowest edge until all 5 9 10 vertices have been visited. 7 B store 14 Unfortunately, this is not D home 8 A post office guaranteed to find an optimal solution. cost = 36

TSP - Greedy Approximation How about a greedy approximation? library C Start with node D, always follow the lowest edge until all 5 9 10 vertices have been visited. 7 B store 14 Unfortunately, this is not D home 8 A post office guaranteed to find an optimal solution. cost = 34

Combinatorial Optimization Many of the graph problems we discussed are combinatorial optimization problems. Select the best structure from a set of output structures subject to some constraints. Examples: Select the minimum spanning tree from the set of all spanning trees. Select the lowest-cost traveling salesman tour from the set of possible tours through a complete graph.

Bin Packing Problem You have: N items of sizes s1,, sn Any number of bins of fixed size V. Goal: pack the items into bins such that the number of bins needed is minimized. The sum of the item sizes in each bin must not exceed V. 120 120 120 30 45 60 85 90 120 70 70 30

Bin Packing Problem You have: N items of sizes s1,, sn Any number of bins of fixed size V. Goal: pack the items into bins such that the number of bins needed is minimized. The sum of the item sizes in each bin must not exceed V. 120 120 120 120 120 120 30 30 60 120 90 85 45 70 70

Knapsack Problem I can only carry weight 10. What should I take to maximize value. W=9 W=1 $400 $300 W=4 $1,000 W=5 $200 W=1 $900 W=2 $600 W=2 $10

Knapsack Problem Given N items, each with a value vi and a weight wi. Select a subset of the items to pack in a knapsack, such that the total weight does not exceed some limit W the sum of values is maximized.

Decision Problems A decision problem has, for each input, exactly two possible outcomes, YES or NO. Does this Graph contain an Euler Circuit Does this Graph contain a Hamiltonian Cycle

From Combinatorial Optimization to Decision Problems Any combinatorial optimization problem can be rephrased as a decision problem by asking if a decision that is better than a certain threshold exists. For instance, for TSP: Is there a simple cycle that visits all vertices and has total cost K Observation: Solving the optimization problem is at least as hard as solving the decision problem.

Deterministic and Non-Deterministic Machines The state of a computation consists of all current data (input, memory, CPU registers, ) and the last program instruction. Given any state, a deterministic machine goes to a unique next instruction. i1 i2 i3 S1 S2 S3

Deterministic and Non-Deterministic Machines A non-deterministic machine could be in ANY number of states at the same time. Equivalently, a non-deterministic machine contains an oracle that tells it the optimal instruction (of several multiple instructions) to execute in each state. i1 i2 i3 S1 S2 S3 i4 S4 i5 i6 S5

TSP with an Oracle State of the computation: Visited nodes, previous path. Same algorithm as greedy algorithm, but now the oracle tells us which edge to follow next. library C 5 7 B 9 store 14 10 D home 8 A post office

TSP with an Oracle State of the computation: Visited nodes, previous path. Same algorithm as greedy algorithm, but now the oracle tells us which edge to follow next. library C 5 7 B 9 store 14 10 D home 8 A post office

TSP with an Oracle State of the computation: Visited nodes, previous path. Same algorithm as greedy algorithm, but now the oracle tells us which edge to follow next. library C 5 7 B 9 store 14 10 D home 8 A post office

TSP with an Oracle State of the computation: Visited nodes, previous path. Same algorithm as greedy algorithm, D home but now the oracle tells us which edge to follow next. 5 7 library C B 8 9 store 14 10 This algorithm produces an optimal tour in linear time! A post office Unfortunately, a real oracle is not realistic. (But we can have a limited amount of parallelism).

The Class of NP Problems NP (Nondeterministic Polynomial Time) is the the class of problems for which a polynomial running time algorithm is known to exist on a nondeterministic machine. How do we know that a problem is in NP. Are there problems that are not in NP?

How Do We Know If a Problem Is in NP? Assume a decision problem produces YES on some input and some proof/ certificate for this result. A decision problem is in NP if we can verify, in deterministic polynomial time, that the proof for a YES instance is correct. Examples: An algorithm determines that a graph contains a Hamiltonian cycle and provides such a cycle as proof. A spanning tree of cost < K is proof that such a spanning tree exists in a graph.

Undecidable Problems Are there problems that are impossible to solve? Halting Problem: Given a program description and some input, determine if the program will terminate (halt) or run forever (loop). This problem is recursively undecidable. Turing 1936

The Halting Problem Assume we wrote a program called HALT(p,i) HALT outputs YES and halts if p halts on i. HALT outputs NO and halts if p loops on i. p i HALT p halts on i p loops on i output YES output NO halt halt

The Halting Problem p i HALT p halts on i p loops on i output YES output NO halt halt Write another program called LOOP(q) LOOP halts if HALT(q,q) returns YES LOOP loops if HALT(q,q) returns NO HALT(q,q) returns NO halt q LOOP HALT(q,q) returns YES

The Halting Problem q LOOP HALT(q,q) returns NO halt HALT(q,q) returns YES What happens if we run LOOP(LOOP)? Assume LOOP(LOOP) halts Then HALT(LOOP,LOOP) must have returned NO. Assume LOOP(LOOP) loops. Then HALT(LOOP,LOOP) must have returned YES.

A decidable problem that is (probably) not in NP Consider the problem of deciding if a graph does NOT have a hamiltonian cycle. No NP algorithm is known for this problem. Intuitively, a proof would require to list all possible cycles. Verifying the proof means to show that none of them is Hamiltonian, one by one.

NP Problems Decidable Problems NP Problems Undecidable Problems

P and NP Problems The class P contains all problems that are solvable in polynomial time on a deterministic machine (most of the problems discussed in this course are in P). Clearly, all problems in P are also in NP. Surprisingly, it is unknown if there are problems in NP (i.e. with proofs that can be verified in polynomial time), that cannot be SOLVED in polynomial time. vs.

P and NP Problems Decidable Problems P NP Problems if P NP

P and NP Problems Decidable Problems P = NP Problems if P = NP

NP-Complete Problems An NP problem is NP-Complete if it is at least as hard as any problem in NP. How de we know that a given problem p is NP complete? Any instance of any problem q in NP can be transformed into an instance of p in polynomial time. This is also called a reduction of q to p.

Reductions Provide a mapping so that any instance of q can be transformed into an instance of p. Solve p and then map the result back to q. These mappings must be computable in polynomial time.

Problem Classes Decidable Problems NP Complete P NP Problems if P NP

Importance of the NP-Complete Class Any other problem in NP can be transformed into an NP-Complete problem. If a polynomial time solution exists for any of these problems, there is a polynomial time solution for all problems in NP! To show that a new problem is NP-Complete, we show that another NP-complete problem can be reduced to it.

P and NP Problems Decidable Problems P = NP = NP Complete if P = NP

Example Reduction Assume we know the Hamiltonian Circuit problem is NP-Complete. To show that TSP is NP-Complete, we can reduce Hamiltonian Circuit to it. Hamiltonian Circuit (known to be NP-Complete) Traveling Salesman Problem

Reducing Hamiltonian Circuit to TSP We want to know if the input graph G = (V,E) contains a Hamiltonian Circuit. Construct a complete graph G over V. Set the cost of all edges in G that are also in E to 1.0. Set the cost of all other edges to 2.0.

Reducing Hamiltonian V1 Circuit to TSP V1 1 1 V2 V3 V4 V5 Input graph G for Hamiltonian Circuit 2 1 V2 V3 2 1 1 2 2 V4 1 V5 Input graph G for TSP Resulting TSP decision problem: Does G contain a TSP tour with cost V G contains a Hamiltonian Circuit if and only if G contains a TSP tour with cost = V