Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CSE 326: Data Structures: Graphs Lecture 23: Wednesday, March 5 th, 2003."— Presentation transcript:

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

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

3 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 4 All Pairs Shortest Path i j C ij D k,ij

5 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 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 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 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 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 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 11 All Pairs Shortest Path i j D k,ij nodes 0, 1,..., k-1 Here i, j  k

12 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 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 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 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 16 NP-Completeness Really hard problems

17 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 (1805-1865) L. Euler (1707-1783)

18 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 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 20 A “Multigraph” for the Bridges of Königsberg Find a path that traverses every edge exactly once

21 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 (1707-1783), 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 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 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 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 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 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 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 28 How bad is exponential time? Nlog NN log NN2N2 2N2N 10012 21244 42816 103301001024 100770010,0001,000,000,000,0 00,00,000,000,0 00,000,000 10001010,0001,000,000Fo’gettaboutit! 1,000,0002020,000,0001,000,000,000,000ditto

29 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 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 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 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 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 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 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 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 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 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 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 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 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


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

Similar presentations


Ads by Google