csc 213 – large scale programming. today’s goals make britney sad through my color choices ...
TRANSCRIPT
LECTURE 32:GRAPH TRAVERSALS
CSC 213 – Large Scale Programming
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?
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?
Graphs Solve Many Problems
Subgraphs
Subgraph of G contains edges & vertices from G
Graph
Subgraphs
Subgraph of G contains edges & vertices from G
Subgraph
Subgraphs
Subgraph of G contains edges & vertices from G
Spanning subgraph is subgraph containing all vertices in G
Subgraph
Graph
Subgraphs
Subgraph of G contains edges & vertices from G
Spanning subgraph is subgraph containing all vertices in G
Subgraph
Spanning subgraph
Connected Graphs & Subgraphs Connected if path
exists between all vertex pairs Requires path, not
edge, between vertices
Connected Graph
Connected Graphs & Subgraphs Connected if path
exists between all vertex pairs Requires path, not
edge, between vertices
Disconnected Graph
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
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
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
Tree
Connected acyclic graph Resembles Tree
structure
Graph
Tree
Connected acyclic graph Resembles Tree
structure
Tree
Tree
Connected acyclic graph Resembles Tree
structure
Don’t lose forest for trees Forest is graph with
multiple trees
Tree
Graph
Tree
Connected acyclic graph Resembles Tree
structure
Don’t lose forest for trees Forest is graph with
multiple trees
Tree
Tree
Tree
Connected acyclic graph Resembles Tree
structure
Don’t lose forest for trees Forest is graph with
multiple trees
Tree
Forest
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Graph
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Tree
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Spanning subgraph
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Spanning tree
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?
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
DFS and Maze Traversal
Classic maze strategy Intersection, corner,
& dead end are vertices
Corridors become edges
Traveled corridors marked
Marks trace back to start
DFS Example
DB
A
C
E
discovery edgeback edge
A visited vertex
A unexplored vertex
unexplored edge
DFS Example
DB
A
C
E
discovery edgeback edge
A visited vertex
A unexplored vertex
unexplored edge
DFS Example
DB
A
C
E???
discovery edgeback edge
A visited vertex
A unexplored vertex
unexplored edge
DFS Example
DB
A
C
E
discovery edgeback edge
A visited vertex
A unexplored vertex
unexplored edge
DFS Example
DB
A
C
E
discovery edgeback edge
A visited vertex
A unexplored vertex
unexplored edge
DFS Example
DB
A
C
E
discovery edgeback edge
A visited vertex
A unexplored vertex
unexplored edge
DFS Example
DB
A
C
E
discovery edgeback edge
A visited vertex
A unexplored vertex
unexplored edge
Properties of DFS
Connected component’s vertices & edges visited
Edges visited form spanning tree for component
DB
A
C
E
Properties of DFS
Connected component’s vertices & edges visited
Edges visited form spanning tree for component
DB
A
C
E
L0
BFS Example
DB
A
C
E
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
F
cross edge
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L2
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L2
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L2
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L2
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
L2
L1
L0
BFS Example
DB
A
C
E
F
discovery edge
A visited vertex
A unexplored vertex
unexplored edge
cross edge
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
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
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?