1 CSE 326: Data Structures: Graphs Lecture 23: Wednesday, March 5 th, 2003.

Slides:



Advertisements
Similar presentations
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,
Advertisements

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.
NP-Complete Problems Polynomial time vs exponential time
The Theory of NP-Completeness
CPE702 Complexity Classes Pruet Boonma Department of Computer Engineering Chiang Mai University Based on Material by Jenny Walter.
1 NP-Complete Problems. 2 We discuss some hard problems:  how hard? (computational complexity)  what makes them hard?  any solutions? Definitions 
Great Theoretical Ideas in Computer Science for Some.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 21 Instructor: Paul Beame.
CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007.
Analysis of Algorithms CS 477/677
CSE 421 Algorithms Richard Anderson Lecture 27 NP Completeness.
1 NP-Complete Problems Polynomial time vs exponential time –Polynomial O(n k ), where n is the input size (e.g., number of nodes in a graph, the length.
Circuits CSE 373 Data Structures Lecture 22. 3/12/03Circuits - Lecture 222 Readings Reading ›Sections and 9.7.
Chapter 11: Limitations of Algorithmic Power
Toward NP-Completeness: Introduction Almost all the algorithms we studies so far were bounded by some polynomial in the size of the input, so we call them.
Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs.
1 NP-Complete Problems (Fun part) Polynomial time vs exponential time –Polynomial O(n k ), where n is the input size (e.g., number of nodes in a graph,
The Theory of NP-Completeness 1. Nondeterministic algorithms A nondeterminstic algorithm consists of phase 1: guessing phase 2: checking If the checking.
Programming & Data Structures
NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems.
1 The Theory of NP-Completeness 2012/11/6 P: the class of problems which can be solved by a deterministic polynomial algorithm. NP : the class of decision.
Nattee Niparnan. Easy & Hard Problem What is “difficulty” of problem? Difficult for computer scientist to derive algorithm for the problem? Difficult.
1 The TSP : NP-Completeness Approximation and Hardness of Approximation All exact science is dominated by the idea of approximation. -- Bertrand Russell.
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.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 8: Complexity Theory.
CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007.
CSCI 2670 Introduction to Theory of Computing November 29, 2005.
Week 10Complexity of Algorithms1 Hard Computational Problems Some computational problems are hard Despite a numerous attempts we do not know any efficient.
CSE 024: Design & Analysis of Algorithms Chapter 9: NP Completeness Sedgewick Chp:40 David Luebke’s Course Notes / University of Virginia, Computer Science.
NP-COMPLETENESS PRESENTED BY TUSHAR KUMAR J. RITESH BAGGA.
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.
EMIS 8373: Integer Programming NP-Complete Problems updated 21 April 2009.
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.
NP-Complete Problems. Running Time v.s. Input Size Concern with problems whose complexity may be described by exponential functions. Tractable problems.
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.
NP-Completeness (Nondeterministic Polynomial Completeness) Sushanth Sivaram Vallath & Z. Joseph.
CS 3343: Analysis of Algorithms Lecture 25: P and NP Some slides courtesy of Carola Wenk.
CSE 589 Part V One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important. Bertrand Russell.
Fundamentals of Informatics Lecture 14 Intractability and NP-completeness Bas Luttik.
CS6045: Advanced Algorithms NP Completeness. NP-Completeness Some problems are intractable: as they grow large, we are unable to solve them in reasonable.
Lecture 25 NP Class. P = ? NP = ? PSPACE They are central problems in computational complexity.
1 CSE 326: Data Structures: Graphs Lecture 24: Friday, March 7 th, 2003.
NPC.
CSC 413/513: Intro to Algorithms
Young CS 331 D&A of Algo. NP-Completeness1 NP-Completeness Reference: Computers and Intractability: A Guide to the Theory of NP-Completeness by Garey and.
1 Ch 10 - NP-completeness Tractable and intractable problems Decision/Optimization problems Deterministic/NonDeterministic algorithms Classes P and NP.
CSCI 2670 Introduction to Theory of Computing December 2, 2004.
The Theory of NP-Completeness 1. Nondeterministic algorithms A nondeterminstic algorithm consists of phase 1: guessing phase 2: checking If the checking.
COSC 3101A - Design and Analysis of Algorithms 14 NP-Completeness.
1 The Theory of NP-Completeness 2 Review: Finding lower bound by problem transformation Problem X reduces to problem Y (X  Y ) iff X can be solved by.
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.
CSE 332: NP Completeness, Part II Richard Anderson Spring 2016.
Young CS 530 Ad. Algo. D&A NP-Completeness1 NP-Completeness Reference: Computers and Intractability: A Guide to the Theory of NP-Completeness by Garey.
The Theory of NP-Completeness
The NP class. NP-completeness
Richard Anderson Spring 2016
P & NP.
Richard Anderson Lecture 26 NP-Completeness
Richard Anderson Lecture 26 NP-Completeness
Chapter 11 Limitations of Algorithm Power
CSE 589 Applied Algorithms Spring 1999
Presentation transcript:

1 CSE 326: Data Structures: Graphs Lecture 23: Wednesday, March 5 th, 2003

2 Today All pairs shortest paths NP completeness –Will finish on Friday

3 All Pairs Shortest Path C ij = the weight of the edge i  j Let D k,ij = the cost of the cheapest path i  j of length  k –This is not the same as in Floyd-Warshall yet

4 All Pairs Shortest Path i j C ij D k,ij

5 All Pairs Shortest Path What is D 0,ij = ? What is D k+1,ij = (in terms of D k,ij ) ? What is the cost of the shortest path i  j ?

6 All Pairs Shortest Path What is D 0,ij = ? D 0,ij = 0 What is D k+1,ij = (in terms of D k,ij ) ? D k+1,ij = min 0  m<n (C im + D k,mj ) for k  0 What is the cost of the shortest path i  j ? D n,ij

7 All Pairs Shortest Path for (i = 0; i < n; i++) for (j = 0; j < n; j++) D[i][j] = 0.0; for (k = 0; k < n; k++) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) { D’[i][j] =  ; for (m=0; m<n; m++) D’[i][j] = min(D’[i][j], D[i][m]+C[m][j]); D = D’ } for (i = 0; i < n; i++) for (j = 0; j < n; j++) D[i][j] = 0.0; for (k = 0; k < n; k++) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) { D’[i][j] =  ; for (m=0; m<n; m++) D’[i][j] = min(D’[i][j], D[i][m]+C[m][j]); D = D’ } Run time = O(n 4 ) We could save space and use only D (not D’), but the main problem is the running time O(n 4 )

8 An Improvement D 2k,ij = min 1  m  n (D k,im + D k,mj ) for k > 1 Hence, compute D 1, D 2, D 4, D 8,... Done after log n steps

9 All Pairs Shortest Path Run time = O(n 3 log n) for (i = 0; i < n; i++) for (j = 0; j < n; j++) D[i][j] = C[i][j]; /* need to start from k=1 */ for (k = 1; k < N; k = 2*k) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) { D’[i][j] =  ; for (m=0; m<n; m++) D’[i][j] = min(D’[i][j], D[i][m]+D[m][j]); D = D’ } for (i = 0; i < n; i++) for (j = 0; j < n; j++) D[i][j] = C[i][j]; /* need to start from k=1 */ for (k = 1; k < N; k = 2*k) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) { D’[i][j] =  ; for (m=0; m<n; m++) D’[i][j] = min(D’[i][j], D[i][m]+D[m][j]); D = D’ }

10 Floyd-Warshall Algorithm C ij = the weight of the edge i  j Let D k,ij = the cost of the cheapest path i  j that goes only through nodes 0, 1,..., k-1

11 All Pairs Shortest Path i j D k,ij nodes 0, 1,..., k-1 Here i, j  k

12 All Pairs Shortest Path i j D k,ij nodes 0, 1,..., k-1 Here i, j < k The cases i < k  j and j < k  i are also possible

13 Floyd-Warshall Algorithm What is D 0,ij = ? What is D k+1,ij = (in terms of D k,ij ) ? What is the cost of the shortest path i  j ?

14 Floyd-Warshall Algorithm What is D 0,ij = ? D 0,ij = C ij What is D k+1,ij = (in terms of D k,ij ) ? D k+1,ij = min(D k,ij, D k,ik + D k,kj ) for k > 0 What is the cost of the shortest path i  j ? D n,ij

15 Floyd-Warshall Algorithm Run time = O(n 3 ) for (i = 0; i < n; i++) for (j = 0; j < n; j++) D[i][j] = C[i][j]; for (k = 0; k < n; k++) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) D’[i][j] = min(D’[i][j], D[i][k]+C[k][j]); D = D’ } for (i = 0; i < n; i++) for (j = 0; j < n; j++) D[i][j] = C[i][j]; for (k = 0; k < n; k++) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) D’[i][j] = min(D’[i][j], D[i][k]+C[k][j]); D = D’ } Notice that D k+1, ik = D k, ik and D k+1, kj = D k, kj ; hence we can use a single matrix, D i, j ! Read the book D k+1,ij = min(D k,ij, D k,ik + D k,kj ) for k > 0

16 NP-Completeness Really hard problems

17 Today’s Agenda Solving pencil-on-paper puzzles –A “deep” algorithm for Euler Circuits Euler with a twist: Hamiltonian circuits Hamiltonian circuits and NP complete problems The NP =? P problem –Your chance to win a Turing award! –Any takers? Weiss Chapter 9.7 W. R. Hamilton ( ) L. Euler ( )

18 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?

19 Historical Puzzle: Seven Bridges of Königsberg KNEIPHOFF PREGEL Want to cross all bridges but… Can cross each bridge only once (High toll to cross twice?!)

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

21 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 is only possible if the graph is connected and each vertex has even degree (= # of edges on the vertex) [Why?] –An Euler tour is only possible if the graph is connected and either all vertices have even degree or exactly two have odd degree [Why?]

22 Euler Circuit Problem Problem: Given an undirected graph G = (V,E), find an Euler circuit in G Note: Can check if one exists in linear time (how?) Given that an Euler circuit exists, how do we construct an Euler circuit for G? Hint: Think deep! We’ve discussed the answer in depth before…

23 Finding Euler Circuits: DFS and then Splice Given a graph G = (V,E), find an Euler circuit in G –Can check if one exists in O(|V|) time (check degrees) Basic Euler Circuit Algorithm: 1.Do a depth-first search (DFS) from a vertex until you are back at this vertex 2.Pick a vertex on this path with an unused edge and repeat 1. 3.Splice all these paths into an Euler circuit Running time = O(|V| + |E|)

24 Euler Circuit Example A B C D E F B C D E G G D E G DFS(A) : A B D F E C A DFS(B) : B G C B DFS(G) : G D E G A B G C B D F E C A A B G D E G C B D F E C A Splice at B Splice at G

25 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

26 Finding Hamiltonian Circuits in Graphs Problem: Find a Hamiltonian circuit in a graph G = (V,E) –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??

27 Analysis of our Exhaustive Search Algorithm Worst case  need to search all paths –How many paths? Can depict these paths as a search tree 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 C D E G B D G C G E D E C G E Etc. Search tree of paths from B

28 How bad is exponential time? Nlog NN log NN2N2 2N2N ,0001,000,000,000,0 00,00,000,000,0 00,000, ,0001,000,000Fo’gettaboutit! 1,000, ,000,0001,000,000,000,000ditto

29 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 –For any k, N k is  (B N ) for any constant B > 1

30 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: tree search, sorting, shortest path, Euler circuit, etc.

31 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?

32 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 (except start/finish vertex)

33 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 –Sorting: Can test in linear time if a candidate ordering is sorted –Are any other problems in P also in NP?

34 More Revelations About NP Are any other problems in P also in NP? –YES! All problems in P are also in NP Notation: P  NP If you can solve a problem in polynomial time, can definitely verify a solution in polynomial time Question: Are all problems in NP also in P? –Is NP  P? –I wished I could tell you that NP ⊈ P, but I can’t –Instead, I will tell you about “NP-Complete” problems

35 Another NP Problem SAT: Given a formula in Boolean logic, e.g. determine if there is an assignment of values to the variables that makes the formula true (=1). Why is it in NP?

36 SAT is NP-Complete Cook (1971) showed the following: In some sense, SAT is the hardest problem in NP We say that “SAT is NP-Hard” A problem that is NP-Hard and in NP is called NP- complete Theorem Suppose that we can solve the SAT problem in polynomial time. Then there is a way to solve ANY NP problem in polynomial time !!! Theorem Suppose that we can solve the SAT problem in polynomial time. Then there is a way to solve ANY NP problem in polynomial time !!!

37 SAT is NP-Complete Proof of Cook’s theorem: Suppose we can solve SAT in time O(m 7 ), where m is the size of the formula Let some other problem in NP: we can check a candidate solution in, say, time O(n 5 ), where n is the size of the problem’s input

38 SAT is NP-Complete: Proof To solve that other problem, do the following We have a program A that checks some candidate solution in time O(n 5 ) Construct a HUGE boolean formula that represents the execution of A: its variables are the candidate solution (which we don’t know) Then check if this formula is satisfiable (i.e. there exists some candidate solution)

39 SAT is NP-Complete: Proof Time = 0 Memory (at most n 5 memory words (why ?)) Program counter Input Candidate solution (unknown) Time = 1 Time = n 5 Boolean expression size = n 5 x n 5 Answer (0 or 1)

40 The Graph of NP-Completeness What is special about SAT ? Nothing ! There are hundreds of NP-complete problems: Vertex Cover (VC): Hamiltonean Cycle (HC): Theorem If we can solve VC in polynomial time, then we can solve SAT in polynomial time. Theorem If we can solve HC in polynomial time, then we can solve VC in polynomial time

41 A Great Book You Should Own! Computers and Intractability: A Guide to the Theory of NP-Completeness, by Michael S. Garey and David S. Johnson