0% found this document useful (0 votes)
32 views30 pages

ch14 Graphs2

The document discusses subgraphs, connected components, spanning trees, and how graph search algorithms like breadth-first search (BFS) and depth-first search (DFS) can be used to analyze graph properties and find structures within graphs. BFS and DFS are used to determine if a graph is connected, find all connected components, and compute spanning trees.

Uploaded by

reddyraj036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views30 pages

ch14 Graphs2

The document discusses subgraphs, connected components, spanning trees, and how graph search algorithms like breadth-first search (BFS) and depth-first search (DFS) can be used to analyze graph properties and find structures within graphs. BFS and DFS are used to determine if a graph is connected, find all connected components, and compute spanning trees.

Uploaded by

reddyraj036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Subgraphs, Connected

Components, Spanning Trees

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 1


Subgraphs
 A subgraph S of a graph
G is a graph such that
 The vertices of S are a
subset of the vertices of G
 The edges of S are a Subgraph
subset of the edges of G
 A spanning subgraph of G
is a subgraph that
contains all the vertices
of G

Spanning subgraph

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 2


Connectivity
 A graph is connected if there is a path
between every pair of vertices
 A connected component ofConnected
a graph
graphG is
a maximal connected subgraph of G

Non connected graph with two


connected components

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 3


Trees and Forests
 A (free) tree is an
undirected graph T such
that
 T is connected
 T has no cycles
Tree
This definition of tree is
different from the one of
a rooted tree
 A forest is an undirected
graph without cycles
 The connected
components of a forest
are trees Forest

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 4


Spanning Trees and Forests
 A spanning tree of a
connected graph is
 a spanning subgraph that is
a tree
 A spanning tree
 is not unique unless the
graph is a tree Graph
 Spanning trees have
applications to the design
of communication
networks
 A spanning forest of a
graph
 is a spanning subgraph that
is a forest
Spanning tree
© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 5
Cycles
 How to detect that a graph has a cycle?
 Hint: BFS (or DFS)

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 6


Connectivity
 Determines whether G is connected
 How do we know G is not connected?
 Hint: BFS or DFS

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 7


Connected Components
 Computes the connected components of G
 How do we find all the connected
components?

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 8


BFS for an entire graph
 The algorithm uses a Algorithm BFS(G, s)
mechanism for setting and Queue  new empty queue
getting “labels” of vertices enqueue(Queue, s)
and edges while isEmpty(Queue)
v  dequeue(Queue)
Algorithm BFS(G) if getLabel(v) != VISITED
Input graph G visit v
Output labeling of the setLabel(v, VISITED)
edges neighbors  getAdjacentVertices(G, v)
and partition of for each w  neighbors
the if getLabel(w) !=
vertices of G VISITED // “children”
for each u  G.vertices()
setLabel(u, enqueue(Queue, w)
UNEXPLORED)
for each v  G.vertices()
if getLabel(v) =
UNEXPLORED
BFS(G, v)
© 2014 Goodrich, Tamassia, Goldwasser Breadth-First Search 9
DFS for an Entire Graph
 The algorithm uses a mechanism Algorithm DFS(G, v)
for setting and getting “labels” of visit v
vertices and edges setLabel(v, VISITED)
neighbors  getAdjacentVertices(G, v)
Algorithm DFS(G) for each w  neighbors
Input graph G if getLabel(w) != VISITED // “children”
Output labeling of the DFS(G, w)
edges of G
as discovery edges
and
back edges
for each u  G.vertices()
setLabel(u,
UNEXPLORED)
for each v  G.vertices()
if getLabel(v) =
UNEXPLORED
DFS(G, v)
© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 10
Spanning Tree
 Computes a spanning tree of G
 How do we find a spanning tree?

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 11


BFS Example
L0
A
A unexplored vertex
A visited vertex L1
B C D
unexplored edge
discovery edge E F
cross edge

L0 L0
A A

L1 L1
B C D B C D

E F E F

© 2014 Goodrich, Tamassia, Goldwasser Breadth-First Search 12


BFS Example (cont.)
L0 L0
A A

L1 L1
B C D B C D

L2
E F E F

L0 L0
A A

L1 L1
B C D B C D

L2 L2
E F E F

© 2014 Goodrich, Tamassia, Goldwasser Breadth-First Search 13


BFS Example (cont.)
L0 L0
A A

L1 L1
B C D B C D

L2 L2
E F E F

L0
A

L1
B C D

L2
E F

© 2014 Goodrich, Tamassia, Goldwasser Breadth-First Search 14


DFS Example
A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge

A A

B D E B D E

C C

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 15


DFS Example (cont.)
A A

B D E B D E

C C

A A

B D E B D E

C C

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 16


Spanning Forest
 Computes a spanning forest of G
 How do we find a spanning forest?

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 17


Path finding
 Given a start vertex s and a destination
vertex z
 Find a path from s to z

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 18


Path finding in a maze
 Vertex
 intersection, corner
and dead end

 Edge
 Corridor

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 19


BFS and path finding
 Hint: use the spanning tree

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 20


BFS and path finding
 Use the spanning tree
 Start from destination
 Go to its parent
 Until source is reached

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 21


BFS and path finding
 Use the spanning tree
 Start from destination
 Go to its parent
 Until source is reached
 Faster, any ideas?
 Hint: not the entire spanning tree

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 22


BFS and path finding
 Use the spanning tree
 Start from destination
 Go to its parent
 Until source is reached
 Faster, any ideas?
 Hint: not the entire spanning tree
 The found path is the shortest in terms
of number of edges --- Why?
© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 23
DFS and path finding
 Use the spanning tree
 Start from destination
 Go to its parent
 Until source is reached

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 24


DFS and path finding
 Use the spanning tree
 Start from destination
 Go to its parent
 Until source is reached
 Faster
 Hint: not the entire spanning tree

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 25


DFS and path finding
 Use the spanning tree
 Start from destination
 Go to its parent
 Until source is reached
 Faster
 Hint: not the entire spanning tree
 Less space, “no spanning tree,” ideas?
 Hint: use a stack

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 26


Path Finding
Algorithm pathDFS(G, v, z, S)
 find a path between two setLabel(v, VISITED)
given vertices v and z push(S, v)
if v = z
return elements(S)
 call DFS(G, v) with v as
path = null
the start vertex
neighbors  getAdjacentVertices(G, v)
for each w  neighbors
 use a stack S to keep if getLabel(w) !=
track of the path VISITED // “child”
path =
pathDFS(G, w, z, S)
 When destination vertex z if path != null // has path via a child
is encountered, return path
 return the path as the pop(S, v) //no path via a child or no children
contents of the stack return null

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 27


A

© 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 28


DFS vs. BFS
Applications DFS BFS
Spanning forest, connected components, paths,
 
cycles
Shortest paths 
Biconnected components (still connected after

removing one vertex, algorithm not discussed)
L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS
© 2014 Goodrich, Tamassia, Goldwasser Breadth-First Search 29
DFS vs. BFS (cont.)
Back edge (v,w) Cross edge (v,w)
 w is an ancestor of v in  w is in the same level as
the tree of discovery v or in the next level
edges

L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS
© 2014 Goodrich, Tamassia, Goldwasser Breadth-First Search 30

You might also like