lecture 32: graph traversals

47
LECTURE 32: GRAPH TRAVERSALS CSC 213 – Large Scale Programming

Upload: yuval

Post on 22-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

CSC 213 – Large Scale Programming. Lecture 32: Graph TRAVERSALS. Today’s Goals. Make Britney sad through my color choices Revisit issue of graph terminology and usage Subgraphs , spanning, & connected graphs, oh my! Problems solved or why you should care about these - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 32: Graph TRAVERSALS

LECTURE 32:GRAPH TRAVERSALS

CSC 213 – Large Scale Programming

Page 2: Lecture 32: Graph TRAVERSALS

Today’s Goals

Make Britney sad through my color choices

Revisit issue of graph terminology and usage Subgraphs, spanning, & connected graphs,

oh my! Problems solved or why you should care

about these Just rats in a maze: using graphs to find

the exit Confident in your decisions: how to travel

depth-first Using bread-first searching to cover all the

bases 2 algorithms both alike in dignity: how do

they differ?

Page 3: Lecture 32: Graph TRAVERSALS

Today’s Goals

Make Britney sad through my color choices

Revisit issue of graph terminology and usage Subgraphs, spanning, & connected graphs,

oh my! Problems solved or why you should care

about these Just rats in a maze: using graphs to find

the exit Confident in your decisions: how to travel

depth-first Using bread-first searching to cover all the

bases 2 algorithms both alike in dignity: how do

they differ?

Page 4: Lecture 32: Graph TRAVERSALS

Graphs Solve Many Problems

Page 5: Lecture 32: Graph TRAVERSALS

Subgraphs

Subgraph of G contains edges & vertices from G

Graph

Page 6: Lecture 32: Graph TRAVERSALS

Subgraphs

Subgraph of G contains edges & vertices from G

Subgraph

Page 7: Lecture 32: Graph TRAVERSALS

Subgraphs

Subgraph of G contains edges & vertices from G

Spanning subgraph is subgraph containing all vertices in G

Subgraph

Graph

Page 8: Lecture 32: Graph TRAVERSALS

Subgraphs

Subgraph of G contains edges & vertices from G

Spanning subgraph is subgraph containing all vertices in G

Subgraph

Spanning subgraph

Page 9: Lecture 32: Graph TRAVERSALS

Connected Graphs & Subgraphs Connected if path

exists between all vertex pairs Requires path, not

edge, between vertices Connected Graph

Page 10: Lecture 32: Graph TRAVERSALS

Connected Graphs & Subgraphs Connected if path

exists between all vertex pairs Requires path, not

edge, between vertices Disconnected Graph

Page 11: Lecture 32: Graph TRAVERSALS

Connected Graphs & Subgraphs Connected if path

exists between all vertex pairs Requires path, not

edge, between vertices

Connected component is subgraph containing all connected vertices Often called

“maximally connected”

Disconnected Graph

Graph

Page 12: Lecture 32: Graph TRAVERSALS

Connected Graphs & Subgraphs Connected if path

exists between all vertex pairs Requires path, not

edge, between vertices

Connected component is subgraph containing all connected vertices Often called

“maximally connected”

Disconnected Graph

Graph with 2 connected components

Page 13: Lecture 32: Graph TRAVERSALS

Connected Graphs & Subgraphs Connected if path

exists between all vertex pairs Requires path, not

edge, between vertices

Connected component is subgraph containing all connected vertices Often called

“maximally connected”

Disconnected Graph

Graph with 2 connected components

Page 14: Lecture 32: Graph TRAVERSALS

Tree

Connected acyclic graph Resembles Tree

structureGraph

Page 15: Lecture 32: Graph TRAVERSALS

Tree

Connected acyclic graph Resembles Tree

structureTree

Page 16: Lecture 32: Graph TRAVERSALS

Tree

Connected acyclic graph Resembles Tree

structure

Don’t lose forest for trees Forest is graph with

multiple trees

Tree

Graph

Page 17: Lecture 32: Graph TRAVERSALS

Tree

Connected acyclic graph Resembles Tree

structure

Don’t lose forest for trees Forest is graph with

multiple trees

Tree

Tree

Page 18: Lecture 32: Graph TRAVERSALS

Tree

Connected acyclic graph Resembles Tree

structure

Don’t lose forest for trees Forest is graph with

multiple trees

Tree

Forest

Page 19: Lecture 32: Graph TRAVERSALS

Spanning Tree

Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning

subgraph Tree connected without any cycles

Graph

Page 20: Lecture 32: Graph TRAVERSALS

Spanning Tree

Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning

subgraph Tree connected without any cycles

Tree

Page 21: Lecture 32: Graph TRAVERSALS

Spanning Tree

Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning

subgraph Tree connected without any cycles

Spanning subgraph

Page 22: Lecture 32: Graph TRAVERSALS

Spanning Tree

Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning

subgraph Tree connected without any cycles

Spanning tree

Page 23: Lecture 32: Graph TRAVERSALS

Spanning Tree

Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning

subgraph Tree connected without any cycles

Tree Than Spans Royals?

Page 24: Lecture 32: Graph TRAVERSALS

Depth- & Breadth-First Search Common graph traversal algorithms DFS & BFS traversals have common

traits Visits all vertices and edges in G Determines whether of not G is connected Finds connected components if G not

connected Heavily used to solve graph problems

Page 25: Lecture 32: Graph TRAVERSALS

DFS and Maze Traversal

Classic maze strategy Intersection, corner,

& dead end are vertices

Corridors become edges

Traveled corridors marked

Marks trace back to start

Page 26: Lecture 32: Graph TRAVERSALS

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

Page 27: Lecture 32: Graph TRAVERSALS

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

Page 28: Lecture 32: Graph TRAVERSALS

DFS Example

DB

A

C

E???

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

Page 29: Lecture 32: Graph TRAVERSALS

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

Page 30: Lecture 32: Graph TRAVERSALS

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

Page 31: Lecture 32: Graph TRAVERSALS

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

Page 32: Lecture 32: Graph TRAVERSALS

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

Page 33: Lecture 32: Graph TRAVERSALS

Properties of DFS

Connected component’s vertices & edges visited

Edges visited form spanning tree for component

DB

A

C

E

Page 34: Lecture 32: Graph TRAVERSALS

Properties of DFS

Connected component’s vertices & edges visited

Edges visited form spanning tree for component

DB

A

C

E

Page 35: Lecture 32: Graph TRAVERSALS

L0

BFS Example

DB

A

C

E

discovery edge

A visited vertexA unexplored vertex

unexplored edge

F

cross edge

Page 36: Lecture 32: Graph TRAVERSALS

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 37: Lecture 32: Graph TRAVERSALS

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 38: Lecture 32: Graph TRAVERSALS

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 39: Lecture 32: Graph TRAVERSALS

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 40: Lecture 32: Graph TRAVERSALS

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 41: Lecture 32: Graph TRAVERSALS

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 42: Lecture 32: Graph TRAVERSALS

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 43: Lecture 32: Graph TRAVERSALS

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 44: Lecture 32: Graph TRAVERSALS

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertexA unexplored vertex

unexplored edge

cross edge

Page 45: Lecture 32: Graph TRAVERSALS

Properties of BFS

Some properties similar to DFS: Visits all vertices & edges in connected

component Component’s spanning tree from discovery

edges For each vertex v in Li

Spanning tree has exactly i edges on path from s to v

All paths in G from s to v have at least i edges

L2

L1

L0

B

A

C

D

C

E

F

Page 46: Lecture 32: Graph TRAVERSALS

DFS vs. BFS

DFS BFSBack edge (v,w)

w is ancestor of v in tree of discovery edges

Cross edge (v,w) v in same or previous

level as w in tree of discovery edges

L2

L1

L0

B

A

C

D

C

E

F

DB

A

C

E

Page 47: Lecture 32: Graph TRAVERSALS

For Next Lecture

Weekly assignment available & due Tuesday Gives you good chance to check your

understanding

Next Fri. 1st check-in for program assignment #3 Planning now saves time later!

Read 13.4.1 – 13.4.3 for class on Monday When using directed edges, what changes

needed? Other computations possible for directed

graphs?