Top Banner
Graph Traversals CSC 172 SPRING 2004 LECTURE 21
56

Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements Project 3 is graded handed back Tuesday Grad spam, tonight – if you are really anxious.

Dec 21, 2015

Download

Documents

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: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Graph Traversals

CSC 172

SPRING 2004

LECTURE 21

Page 2: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Announcements Project 3 is graded

handed back TuesdayGrad spam, tonight – if you are really anxious see me

after class I will be out of town until Thursday, 15th

Read Chapter 14 Project 5 Due 16th

Page 3: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Traversing graphs

Depth-First Searchlike a traversal of a tree

Breath-First SearchLess like tree traversal

Use a stack

Page 4: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Exploring a Maze

A depth-first search (DFS) in an undirected graph G is like wandering in a maze with a string and a can of paint – you can prevent yourself from getting lost.

Page 5: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 6: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

DFS1. Start at vertex s

Tie the end of the string to s and mark “visited” on sMake s the current vertex u

2. Travel along an arbitrary edge (u,v) unrolling string

3. If edge(u,v) leads to an already visited vertex v then return to u else mark v as “visited”, set v as current u, repeat @ step 2

4. When all edges lead to visited verticies, backtrack to previous vertex (roll up string) and repeat @ step 2

5. When we backtrack to s and explore all it’s edges we are done

Page 7: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

DFS Pseudocode (labels edges)DFS( Vertex v)

for each edge incident on v do:if edge e is unexplored then

let w be the other endpoint of eif vertex w is unexplored then

label e as a discovery edgerecursively call DFS(w)

elselabel e as a backedge

Page 8: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

EXAMPLE

Page 9: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 10: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 11: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 12: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 13: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 14: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 15: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 16: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 17: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 18: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 19: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 20: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 21: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 22: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 23: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 24: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 25: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 26: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 27: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 28: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 29: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 30: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 31: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 32: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

DFS Tree

A B C D

E F G H

I J K L

M N O P

Page 33: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

DFS Properties

Starting at sThe traversal visits al the vertices in the connected

component of s

The discovery edges form a spanning tree of the connected component of s

Page 34: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

DFS Runtime

DFS is called on each vertex exactly once

Every edge is examined exactly twice (once from each of its vertices)

So, for ns vertices and ms edges in the connected component of the vertex s, the DFS runs in O(ns+ms) if:

The graph data structure methods take constant time

Marking takes constant time

There is a systematic way to examine edges (avoiding redundancy)

Page 35: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Marking Verticies

Extend vertex structure to support variable for marking

Use a hash table mechanism to log marked vertices

Page 36: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Breadth-First SearchStarting vertex has level 0 (anchor vertex)

Visit (mark) all vertices that are only one edge away

mark each vertex with its “level”One edge away from level 0 is level 1

One edge away from level 1 is level 2

Etc. . . .

Page 37: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

EXAMPLE

Page 38: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

Page 39: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0

Page 40: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0

Page 41: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1

Page 42: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1

Page 43: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2

Page 44: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2

Page 45: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

Page 46: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

Page 47: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

4

Page 48: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

4

Page 49: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

4

Page 50: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

4

Page 51: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

4

5

Page 52: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Example

A B C D

E F G H

I J K L

M N O P

0 1 2 3

4

5

Page 53: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

BFS Tree

A B C D

E F G H

I J K L

M N O P

0 1 2 3

4

5

Page 54: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

BFS PseudocodeBSF(Vertex s)

initialize container L0 to contain vertex si 0

while Li is not empty do

create container Li+1 to initially be empty

for each vertex v in Li doif edge e incident on v do

let w be the other endpoint of eif w is unexplored then

label e as a discovery edge

insert w into Li+1

elselabel e as a cross edge

i i+1

Page 55: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

BSF PropertiesThe traversal visits all vertices in the connected

component of s

The discover edges form a spanning tree of the cc

For each vertex v at level I, the path of the BSF tree T between s and v has I edges and any other path of G between s and v has at least I edges

If (u,v) is an edge that is not in the BSF tree, then the level number of u and v differ by at most one

Page 56: Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.

Run Time

A BSF traversal takes O(n+m) time

Also, there exist O(n+m) time algorithms base on BFS which test forConnectivity of graph

Spanning tree of G

Connected component

Minimum number of edges path between s and v