Loading in 5 sec....

CSE 326: Data Structures: GraphsPowerPoint Presentation

CSE 326: Data Structures: Graphs

- By
**trent** - Follow User

- 65 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' CSE 326: Data Structures: Graphs' - trent

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### CSE 326: Data Structures: Graphs

Lecture 23:Wednesday, March 5th, 2003

Today

- All pairs shortest paths
- NP completeness
- Will finish on Friday

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

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 ?

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

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’

}

Run time = O(n4)

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

An Improvement

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

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

Done after log n steps

All Pairs Shortest Path

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’

}

Run time = O(n3 log n)

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

All Pairs Shortest Path

nodes0, 1, ..., k-1

Dk,ij

i

Here i, j < k

j

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

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 ?

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

Floyd-Warshall Algorithm

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’

}

Run time = O(n3)

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

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

NP-Completeness

- Really hard problems

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

L. Euler

(1707-1783)

W. R. Hamilton

(1805-1865)

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?

Historical Puzzle: Seven Bridges of Königsberg

PREGEL

KNEIPHOFF

Want to cross all bridges but…

Can cross each bridge only once (High toll to cross twice?!)

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

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…

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:
- Do a depth-first search (DFS) from a vertex until you are back at this vertex
- Pick a vertex on this path with an unused edge and repeat 1.
- Splice all these paths into an Euler circuit

- Running time = O(|V| + |E|)

Euler Circuit Example

A

B

C

B

C

G

G

G

D

E

D

E

D

E

F

DFS(G) :

G D E G

Splice

at G

DFS(B) :

B G C B

DFS(A) :

A B D F E C A

A BG D E GC B D F E C A

Splice at B

A B G C B D F E C A

C

B

C

G

G

D

E

D

E

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?

I

II

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

Analysis of our Exhaustive Search Algorithm

B

C

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

G

D

E

B

D G C

G E D E C G E

Etc.

Search tree of paths from B

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 BNis asymptotically worse thanany polynomial function Nk for any k
- For any k, Nk is (BN) for any constant B > 1

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.

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?

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)

- Hamiltonian circuit problem: Why is it in NP?

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?

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

- YES! All problems in P are also in NP
- 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

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?

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

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

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)

Boolean expressionsize = n5 x n5

Boolean expressionsize = n5 x n5

SAT is NP-Complete: ProofProgramcounter

Candidatesolution (unknown)

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

Input

Time = 0

Time = 1

Time = n5

Answer (0 or 1)

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

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 Presentation

Connecting to Server..