CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Introduction to Algorithms NP-Complete
What is Intractable? Some problems seem too hard to solve efficiently. Question 1: Does an efficient algorithm exist?  An O(a ) algorithm, where a > 1,
1 NP-completeness Lecture 2: Jan P The class of problems that can be solved in polynomial time. e.g. gcd, shortest path, prime, etc. There are many.
1 Lecture 5 (part 2) Graphs II Euler and Hamiltonian Path / Circuit Reading: Epp Chp 11.2, 11.3.
CPE702 Complexity Classes Pruet Boonma Department of Computer Engineering Chiang Mai University Based on Material by Jenny Walter.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CSE 326: Data Structures Lecture #20 Really, Really Hard Problems Henry Kautz Winter Quarter 2002.
CSE332: Data Abstractions Lecture 27: A Few Words on NP Dan Grossman Spring 2010.
1 CSE 326: Data Structures: Graphs Lecture 22: Monday, March 3 rd, 2003.
An Euler Circuit is a cycle of an undirected graph, that traverses every edge of the graph exactly once, and ends at the same node from which it began.
CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007.
Analysis of Algorithms CS 477/677
Approximation Algorithms for the Traveling Salesperson Problem.
CSE 421 Algorithms Richard Anderson Lecture 27 NP Completeness.
Circuits CSE 373 Data Structures Lecture 22. 3/12/03Circuits - Lecture 222 Readings Reading ›Sections and 9.7.
Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Euler Paths and Circuits. The original problem A resident of Konigsberg wrote to Leonard Euler saying that a popular pastime for couples was to try.
GRAPH Learning Outcomes Students should be able to:
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley.
Programming & Data Structures
NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
EECS 203: It’s the end of the class and I feel fine. Graphs.
Computational Complexity Polynomial time O(n k ) input size n, k constant Tractable problems solvable in polynomial time(Opposite Intractable) Ex: sorting,
Nattee Niparnan. Easy & Hard Problem What is “difficulty” of problem? Difficult for computer scientist to derive algorithm for the problem? Difficult.
Complexity Classes (Ch. 34) The class P: class of problems that can be solved in time that is polynomial in the size of the input, n. if input size is.
חישוביות וסיבוכיות Computability and Complexity Lecture 7 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.
1 Lower Bounds Lower bound: an estimate on a minimum amount of work needed to solve a given problem Examples: b number of comparisons needed to find the.
CSE332: Data Abstractions Lecture 24.5: Interlude on Intractability Dan Grossman Spring 2012.
1 The Theory of NP-Completeness 2 Cook ’ s Theorem (1971) Prof. Cook Toronto U. Receiving Turing Award (1982) Discussing difficult problems: worst case.
Data Structures & Algorithms Graphs
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
CSE373: Data Structures & Algorithms Lecture 22: The P vs. NP question, NP-Completeness Lauren Milne Summer 2015.
1 Chapter 34: NP-Completeness. 2 About this Tutorial What is NP ? How to check if a problem is in NP ? Cook-Levin Theorem Showing one of the most difficult.
NP-COMPLETE PROBLEMS. Admin  Two more assignments…  No office hours on tomorrow.
Lecture 6 NP Class. P = ? NP = ? PSPACE They are central problems in computational complexity.
NP-Complete problems.
CS 3343: Analysis of Algorithms Lecture 25: P and NP Some slides courtesy of Carola Wenk.
Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP.
AND.
Associated Matrices of Vertex Edge Graphs Euler Paths and Circuits Block Days April 30, May 1 and May
Graph theory and networks. Basic definitions  A graph consists of points called vertices (or nodes) and lines called edges (or arcs). Each edge joins.
Euler Paths and Circuits. The original problem A resident of Konigsberg wrote to Leonard Euler saying that a popular pastime for couples was to try.
Topological Sort: Definition
Chapter 6: Graphs 6.1 Euler Circuits
CSCI 2670 Introduction to Theory of Computing November 17, 2005.
CS 461 – Nov. 18 Section 7.1 Overview of complexity issues –“Can quickly decide” vs. “Can quickly verify” Measuring complexity Dividing decidable languages.
1 CSE 326: Data Structures: Graphs Lecture 24: Friday, March 7 th, 2003.
Lecture. Today Problem set 9 out (due next Thursday) Topics: –Complexity Theory –Optimization versus Decision Problems –P and NP –Efficient Verification.
COSC 3101A - Design and Analysis of Algorithms 14 NP-Completeness.
CS 154 Formal Languages and Computability May 10 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
The NP class. NP-completeness Lecture2. The NP-class The NP class is a class that contains all the problems that can be decided by a Non-Deterministic.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3
Great Theoretical Ideas in Computer Science.
1 CSE 326: Data Structures: Graphs Lecture 23: Wednesday, March 5 th, 2003.
Richard Anderson Spring 2016
Graphs Chapter 20.
EECS 203 Lecture 19 Graphs.
Lecture 2-2 NP Class.
Euler Paths and Circuits
EECS 203 Lecture 20 More Graphs.
ICS 353: Design and Analysis of Algorithms
Intro to NP Completeness
Graph Theory.
Chapter 11 Limitations of Algorithm Power
Euler and Hamilton Paths
CSE 589 Applied Algorithms Spring 1999
Presentation transcript:

CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007

2 Today’s Agenda Solving two pencil-on-paper puzzles –Euler Circuits –Hamiltonian circuits Hamiltonian circuits and NP complete problems The NP = P problem –Your chance to win a Turing award! Weiss Chapter 9.7 W. R. Hamilton ( ) L. Euler ( )

3 It’s Puzzle Time! Which of these can you draw without lifting your pencil, drawing each line only once? Can you start and end at the same point?

4 Historical Puzzle: Seven Bridges of Königsberg KNEIPHOFF PREGEL Want to cross all bridges but… Can cross each bridge only once

5 A “Multigraph” for the Bridges of Königsberg Find a path that traverses every edge exactly once

6 Euler Circuits and Tours Euler tour: a path through a graph that visits each edge exactly once Euler circuit: an Euler tour that starts and ends at the same vertex Named after Leonhard Euler ( ), who cracked this problem and founded graph theory in 1736 Some observations for undirected graphs: –An Euler circuit exists iff the graph is connected and each vertex has even degree (= # of edges on the vertex) –An Euler tour exists iff the graph is connected and either all vertices have even degree or exactly two have odd degree

7 Euler Circuit Problem Problem: Given an undirected graph G, find an Euler circuit How can we check if one exists in linear time? Given that an Euler circuit exists, how do we construct an Euler circuit for G?

8 Finding Euler Circuits Given a graph G = (V,E), find an Euler circuit in G –Can check if one exists in O(|V|+|E|) time (check degrees) Basic Euler Circuit Algorithm: 1.Do an edge walk from a start vertex until you are back to the start vertex. You never get stuck because of the even degree property. 2.The walk is removed leaving several components each with the even degree property. Recursively find Euler circuits for these. 3.Splice all these circuits into an Euler circuit Running time = O(|V| + |E|)

9 Euler Circuit Example A B C D E F G Euler(A) :

10 Euler Circuit Example A B C D E F G Euler(A) : A B G E D G C A

11 Euler Circuit Example A B C D E F G Euler(A) : A B G E D G C A B C D E F Euler(B)

12 Euler Circuit Example A B C D E F G Euler(A) : A B G E D G C A B C D E F Euler(B): B D F E C B

13 Euler Circuit Example A B C D E F G Euler(A) : A B G E D G C A B C D E F Euler(B): B D F E C B Splice A B D F E C B G E D G C A

14 Data Structure?

15 Euler with a Twist: Hamiltonian Circuits Euler circuit: A cycle that goes through each edge exactly once Hamiltonian circuit: A cycle that goes through each vertex exactly once Does graph I have: –An Euler circuit? –A Hamiltonian circuit? Does graph II have: –An Euler circuit? –A Hamiltonian circuit? B C D E G B C D E G I II

16 Finding Hamiltonian Circuits in Graphs Problem: Find a Hamiltonian circuit in a graph G –Sub-problem: Does G contain a Hamiltonian circuit? –No known easy algorithm for checking this… One solution: Search through all paths to find one that visits each vertex exactly once –Can use your favorite graph search algorithm (DFS!) to find various paths This is an exhaustive search (“brute force”) algorithm Worst case  need to search all paths –How many paths??

17 Analysis of our Exhaustive Search Algorithm Worst case  need to search all paths –How many paths? Can depict these paths as a search tree B C D E G B D G C G E D E C G E Etc. Search tree of paths from B

18 Analysis of our Exhaustive Search Algorithm Let the average branching factor of each node in this tree be B |V| vertices, each with  B branches Total number of paths  B·B·B … ·B = O(B |V| ) Worst case  Exponential time! B D G C G E D E C G E Etc. Search tree of paths from B

19 Exponential Time PC, since Big Bang PC, 1 day

20 Review: Polynomial versus Exponential Time Most of our algorithms so far have been O(log N), O(N), O(N log N) or O(N 2 ) running time for inputs of size N –These are all polynomial time algorithms –Their running time is O(N k ) for some k > 0 Exponential time B N is asymptotically worse than any polynomial function N k for any k

21 When is a problem easy? We’ve seen some “easy” graph problems: –Graph search –Shortest-path –Minimum Spanning Tree Not easy for us to come up with, but easy for the computer, once we know algorithm.

22 When is a problem hard? Almost everything we’ve seen in class has had a near linear time algorithm But of course, computers can’t solve every problem quickly. In fact, there are perfectly reasonable sounding problems that no computer could ever solve in any amount of time.

23 Shortest vs. Longest Path Finding the shortest path is easy--that is, we know an efficient algorithm. Namely DFS or BFS. How do we find the longest path?

24 Longest Path Again, no choice but to enumerate all paths. Q: Why doesn’t DFS work? –A node is visited only once, therefore only one path through each node is considered. But as we saw, there could be exponentially many paths. DFS is exploring only one per node.

25 Subset Sum We saw 4 number sum in homework: Given a list of N integers and target k, are there 4 numbers that sum to k? General Subset Sum: Given N integers and a target k, is there some subset of integers that sum to k?

26 Solving Subset Sum Only thing to do is try every possible combination. How many possible subset are there of N integers? –2 N. So again, exponential in input size. For 4 numbers there are N choose 4 possible subsets to try. Approx. N 4.

27 The Complexity Class P The set P is defined as the set of all problems that can be solved in polynomial worse case time –Also known as the polynomial time complexity class –All problems that have some algorithm whose running time is O(N k ) for some k Examples of problems in P: sorting, shortest path, Euler circuit, etc.

28 The Complexity Class NP Definition: NP is the set of all problems for which a given candidate solution can be tested in polynomial time Example of a problem in NP: –Hamiltonian circuit problem : Why is it in NP?

29 The Complexity Class NP Definition: NP is the set of all problems for which a given candidate solution can be tested in polynomial time Example of a problem in NP: –Hamiltonian circuit problem : Why is it in NP? Given a candidate path, can test in linear time if it is a Hamiltonian circuit – just check if all vertices are visited exactly once in the candidate path

30 Why NP? NP stands for Nondeterministic Polynomial time –Why “nondeterministic”? Corresponds to algorithms that can guess a solution (if it exists)  the solution is then verified to be correct in polynomial time –Nondeterministic algorithms don’t exist – purely theoretical idea invented to understand how hard a problem could be Examples of problems in NP: –Hamiltonian circuit: Given a candidate path, can test in linear time if it is a Hamiltonian circuit –Satisfiability: Given a circuit made out of AND, OR, NOT gates: is there an input that makes it output “1”? –All problems that are in P (why?)

31 Your Chance to Win a Turing Award It is generally believed that P  NP, i.e. there are problems in NP that are not in P –But no one has been able to show even one such problem! –This is the fundamental open problem in theoretical computer science –Nearly everyone has given up trying to prove it. Instead, theoreticians prove theorems about what follows once we assume P  NP ! Alan Turing ( )