csc 213 – large scale programming. today’s goals make britney sad through my color choices ...

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

Upload: clifton-hudson

Post on 29-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

LECTURE 32:GRAPH TRAVERSALS

CSC 213 – Large Scale Programming

Page 2: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Graphs Solve Many Problems

Page 5: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Subgraphs

Subgraph of G contains edges & vertices from G

Graph

Page 6: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Subgraphs

Subgraph of G contains edges & vertices from G

Subgraph

Page 7: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Subgraphs

Subgraph of G contains edges & vertices from G

Spanning subgraph is subgraph containing all vertices in G

Subgraph

Graph

Page 8: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Subgraphs

Subgraph of G contains edges & vertices from G

Spanning subgraph is subgraph containing all vertices in G

Subgraph

Spanning subgraph

Page 9: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Connected Graphs & Subgraphs Connected if path

exists between all vertex pairs Requires path, not

edge, between vertices

Connected Graph

Page 10: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Connected Graphs & Subgraphs Connected if path

exists between all vertex pairs Requires path, not

edge, between vertices

Disconnected Graph

Page 11: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Tree

Connected acyclic graph Resembles Tree

structure

Graph

Page 15: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Tree

Connected acyclic graph Resembles Tree

structure

Tree

Page 16: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Tree

Connected acyclic graph Resembles Tree

structure

Don’t lose forest for trees Forest is graph with

multiple trees

Tree

Graph

Page 17: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Tree

Connected acyclic graph Resembles Tree

structure

Don’t lose forest for trees Forest is graph with

multiple trees

Tree

Tree

Page 18: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Tree

Connected acyclic graph Resembles Tree

structure

Don’t lose forest for trees Forest is graph with

multiple trees

Tree

Forest

Page 19: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Spanning Tree

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

subgraph Tree connected without any cycles

Graph

Page 20: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Spanning Tree

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

subgraph Tree connected without any cycles

Tree

Page 21: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 27: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 28: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS Example

DB

A

C

E???

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 29: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 30: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 31: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 32: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS Example

DB

A

C

E

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 33: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Properties of DFS

Connected component’s vertices & edges visited

Edges visited form spanning tree for component

DB

A

C

E

Page 34: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

Properties of DFS

Connected component’s vertices & edges visited

Edges visited form spanning tree for component

DB

A

C

E

Page 35: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L0

BFS Example

DB

A

C

E

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

F

cross edge

Page 36: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 37: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 38: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 39: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 40: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 41: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 42: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 43: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 44: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

L2

L1

L0

BFS Example

DB

A

C

E

F

discovery edge

A visited vertex

A unexplored vertex

unexplored edge

cross edge

Page 45: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

DFS vs. BFS

DFS BFS

Back 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: CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,

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?