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

CSE 326: Data Structures: Graphs

Jan 27, 2016

Download

Documents

Trent

CSE 326: Data Structures: Graphs. Lecture 23: Wednesday, March 5 th , 2003. Today. All pairs shortest paths NP completeness Will finish on Friday. 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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 326: Data Structures:  Graphs

1

CSE 326: Data Structures: Graphs

Lecture 23:Wednesday, March 5th, 2003

Page 2: CSE 326: Data Structures:  Graphs

2

Today

• All pairs shortest paths

• NP completeness– Will finish on Friday

Page 3: CSE 326: Data Structures:  Graphs

3

All Pairs Shortest Path

• Cij = the weight of the edge ij

• Let Dk,ij = the cost of the cheapest path ij of length k– This is not the same as in Floyd-Warshall yet

Page 4: CSE 326: Data Structures:  Graphs

4

All Pairs Shortest Path

i

j

Cij

Dk,ij

Page 5: CSE 326: Data Structures:  Graphs

5

All Pairs Shortest Path

• What is D0,ij = ?

• What is Dk+1,ij = (in terms of Dk,ij) ?

• What is the cost of the shortest path i j ?

Page 6: CSE 326: Data Structures:  Graphs

6

All Pairs Shortest Path

• What is D0,ij = ?D0,ij = 0

• What is Dk+1,ij = (in terms of Dk,ij) ?Dk+1,ij = min0m<n (Cim + Dk,mj) for k 0

• What is the cost of the shortest path i j ?Dn,ij

Page 7: CSE 326: Data Structures:  Graphs

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(n4)

We could save space and use only D (not D’), but the main problemis the running time O(n4)

Page 8: CSE 326: Data Structures:  Graphs

8

An Improvement

D2k,ij = min1mn (Dk,im + Dk,mj) for k > 1

Hence, compute D1, D2, D4, D8, . . .

Done after log n steps

Page 9: CSE 326: Data Structures:  Graphs

9

All Pairs Shortest Path

Run time = O(n3 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’}

Page 10: CSE 326: Data Structures:  Graphs

10

Floyd-Warshall Algorithm

• Cij = the weight of the edge ij

• Let Dk,ij = the cost of the cheapest path ij that goes only through nodes 0, 1, ..., k-1

Page 11: CSE 326: Data Structures:  Graphs

11

All Pairs Shortest Path

i

j

Dk,ij

nodes0, 1, ..., k-1

Here i, j k

Page 12: CSE 326: Data Structures:  Graphs

12

All Pairs Shortest Path

i

j

Dk,ij

nodes0, 1, ..., k-1

Here i, j < k

The cases i < k jand j < k i are also possible

Page 13: CSE 326: Data Structures:  Graphs

13

Floyd-Warshall Algorithm

• What is D0,ij = ?

• What is Dk+1,ij = (in terms of Dk,ij) ?

• What is the cost of the shortest path i j ?

Page 14: CSE 326: Data Structures:  Graphs

14

Floyd-Warshall Algorithm

• What is D0,ij = ?D0,ij = Cij

• What is Dk+1,ij = (in terms of Dk,ij) ?Dk+1,ij = min(Dk,ij, Dk,ik + Dk,kj) for k > 0

• What is the cost of the shortest path i j ?Dn,ij

Page 15: CSE 326: Data Structures:  Graphs

15

Floyd-Warshall Algorithm

Run time = O(n3)

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 Dk+1, ik = Dk, ik and Dk+1, kj = Dk, kj;hence we can use a single matrix, Di, j !Read the book

Dk+1,ij = min(Dk,ij, Dk,ik + Dk,kj) for k > 0

Page 16: CSE 326: Data Structures:  Graphs

16

NP-Completeness

• Really hard problems

Page 17: CSE 326: Data Structures:  Graphs

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

1ie

W. R. Hamilton

(1805-1865)

L. Euler(1707-1783)

Page 18: CSE 326: Data Structures:  Graphs

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?

Page 19: CSE 326: Data Structures:  Graphs

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?!)

Page 20: CSE 326: Data Structures:  Graphs

20

A “Multigraph” for the Bridges of Königsberg

Find a path thattraverses every edgeexactly once

Page 21: CSE 326: Data Structures:  Graphs

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

Page 22: CSE 326: Data Structures:  Graphs

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…

Page 23: CSE 326: Data Structures:  Graphs

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

Page 24: CSE 326: Data Structures:  Graphs

24

Euler Circuit ExampleA

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 ASplice at B

Splice at G

Page 25: CSE 326: Data Structures:  Graphs

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

Page 26: CSE 326: Data Structures:  Graphs

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

Page 27: CSE 326: Data Structures:  Graphs

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

Page 28: CSE 326: Data Structures:  Graphs

28

How bad is exponential time?N log N N log N N2 2N

1 0 0 1 2

2 1 2 4 4

4 2 8 16 16

10 3 30 100 1024

100 7 700 10,000 1,000,000,000,000,00,000,000

,000,000,000

1000 10 10,000 1,000,000 Fo’gettaboutit!

1,000,000 20 20,000,000 1,000,000,000,000 ditto

Page 29: CSE 326: Data Structures:  Graphs

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(N2) running time for inputs of size N– These are all polynomial time algorithms– Their running time is O(Nk) for some k > 0

• Exponential time BN is asymptotically worse than any polynomial function Nk for any k– For any k, Nk is (BN) for any constant B > 1

Page 30: CSE 326: Data Structures:  Graphs

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(Nk) for some k

• Examples of problems in P: tree search, sorting, shortest path, Euler circuit, etc.

Page 31: CSE 326: Data Structures:  Graphs

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?

Page 32: CSE 326: Data Structures:  Graphs

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)

Page 33: CSE 326: Data Structures:  Graphs

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?

Page 34: CSE 326: Data Structures:  Graphs

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

Page 35: CSE 326: Data Structures:  Graphs

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?

a b a c b c

Page 36: CSE 326: Data Structures:  Graphs

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

TheoremSuppose that we can solve the SAT problem in polynomial time.Then there is a way to solve ANY NP problem in polynomial time !!!

TheoremSuppose that we can solve the SAT problem in polynomial time.Then there is a way to solve ANY NP problem in polynomial time !!!

Page 37: CSE 326: Data Structures:  Graphs

37

SAT is NP-Complete• Proof of Cook’s theorem:• Suppose we can solve SAT in time O(m7), where m

is the size of the formula

• Let some other problem in NP: we can check a candidate solution in, say, time O(n5), where n is the size of the problem’s input

Page 38: CSE 326: Data Structures:  Graphs

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(n5)• 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)

Page 39: CSE 326: Data Structures:  Graphs

39

SAT is NP-Complete: Proof

Time = 0

Memory(at most n5 memory words (why ?))

Programcounter Input

Candidatesolution (unknown)

Time = 1

Time = n5

Boolean expressionsize = n5 x n5

Boolean expressionsize = n5 x n5

Answer (0 or 1)

Page 40: CSE 326: Data Structures:  Graphs

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

Theorem If we can solve HC in polynomial time, then we can solve VC in polynomial time

Page 41: CSE 326: Data Structures:  Graphs

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