0% found this document useful (0 votes)
89 views13 pages

s20 Bfs Dfs Apps

The document discusses applications of breadth-first search (BFS) and depth-first search (DFS) graph algorithms. It describes how BFS can be used to find the shortest path between vertices or detect cycles in an unweighted graph. It also explains that DFS can find paths between vertices or determine if a graph is connected. The document provides pseudocode for BFS and DFS algorithms.

Uploaded by

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

s20 Bfs Dfs Apps

The document discusses applications of breadth-first search (BFS) and depth-first search (DFS) graph algorithms. It describes how BFS can be used to find the shortest path between vertices or detect cycles in an unweighted graph. It also explains that DFS can find paths between vertices or determine if a graph is connected. The document provides pseudocode for BFS and DFS algorithms.

Uploaded by

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

4 April 2013 1

APPLICATIONS OF BFS
AND DFS
CSE 2011
Winter 2013

Some Applications of BFS and DFS


BFS
To find the shortest path from a vertex s to a vertex v in
an unweighted graph
To find the length of such a path
To find out if a graph contains cycles
To construct a BSF tree/forest from a graph
DFS
To find a path from a vertex s to a vertex v.
To find the length of such a path.
To construct a DSF tree/forest from a graph.

1
3

TESTING FOR CYCLES

Finding Cycles in Undirected Graphs


To detect/find cycles in an undirected graph, we need to
classify the edges into 3 categories during program
execution:
unvisited edge: never visited.
discovery edge: visited for the very first time.
cross edge: edge that forms a cycle.
Code fragment 13.10, p. 623.
When the BFS algorithm terminates, the discovery edges
form a spanning tree.
If there exists a cross edge, the undirected graph contains
a cycle.

2
BFS Algorithm (in textbook)
The algorithm uses a mechanism for Algorithm BFS(G, s)
setting and getting labels of L0 new empty sequence
vertices and edges L0.insertLast(s)
setLabel(s, VISITED)
i0
Algorithm BFS(G) while Li.isEmpty()
Input graph G Li +1 new empty sequence
Output labeling of the edges for all v Li.elements()
and partition of the for all e G.incidentEdges(v)
vertices of G if getLabel(e) = UNEXPLORED
for all u G.vertices() w opposite(v,e)
setLabel(u, UNEXPLORED) if getLabel(w) = UNEXPLORED
for all e G.edges() setLabel(e, DISCOVERY)
setLabel(w, VISITED)
setLabel(e, UNEXPLORED)
Li +1.insertLast(w)
for all v G.vertices() else
if getLabel(v) = UNEXPLORED setLabel(e, CROSS)
BFS(G, v) i i +1
5 Breadth-First Search

Example
L0
unexplored vertex A
A
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

6 Breadth-First Search

3
Example (2)
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
7 Breadth-First Search

Example (3)
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
8 Breadth-First Search

4
9

COMPUTING
SPANNING TREES

10

Trees
Tree: a connected graph without cycles.
Given a connected graph, remove the cycles a tree.
The paths found by BFS(s) form a rooted tree (called a spanning
tree), with the starting vertex as the root of the tree.

BFS tree for vertex s = 2

What would a level-order traversal of the tree tell you?

5
11

Computing a BFS Tree


Use BFS on a vertex
BFS( v ) with array
prev[ ]

The paths from source


s to the other vertices
form a tree

12

COMPUTING
SPANNING FORESTS

6
13

Computing a BFS Forest


A forest is a set of trees.
A connected graph gives a tree (which is itself a
forest).
A connected component also gives us a tree.
A graph with k components gives a forest of k
trees.

14

Example
A graph with 3 components
P
Q
N
L R
O
M D s
E

C
A
F
B
K
G

7
15

Example of a Forest
We removed the cycles
from the previous graph.
P
Q
N
L R
O
M D s
E

C A forest with 3 trees


A
F
B
K
G

16

Computing a BFS Forest


Use BFS method on a graph BFSearch( G ),
which calls BFS( v )

Use BFS( v ) with array prev[ ].


The paths originating from v form a tree.

BFSearch( G ) examines all the components to


compute all the trees in the forest.

8
17

Applications of DFS
Is there a path from source s to a vertex v?
Is an undirected graph connected?
Is a directed graph strongly connected?
To output the contents (e.g., the vertices) of a graph
To find the connected components of a graph
To find out if a graph contains cycles and report cycles.
To construct a DSF tree/forest from a graph

18

Finding Cycles Using DFS


Similar to using BFS.

For undirected graphs, classify the edges into 3


categories during program execution: unvisited
edge, discovery edge, and back (cross) edge.
Code Fragment 13.1, p. 613.
If there exists a back edge, the undirected graph
contains a cycle.

9
DFS Algorithm
q The algorithm uses a mechanism
for setting and getting labels Algorithm DFS(G, v)
of vertices and edges Input graph G and a start vertex v of G
Algorithm DFS(G) Output labeling of the edges of G
Input graph G in the connected component of v
as discovery edges and back edges
Output labeling of the edges of G
as discovery edges and setLabel(v, VISITED)
back edges for all e G.incidentEdges(v)
for all u G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w opposite(v,e)
for all e G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK)

2010 Goodrich, Tamassia Depth-First Search 19

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

2010 Goodrich, Tamassia Depth-First Search 20

10
Example (cont.)
A A

B D E B D E

C C

A A

B D E B D E

C C

2010 Goodrich, Tamassia Depth-First Search 21

22

DFS Tree
Resulting DFS-tree.
Notice it is much deeper
than the BFS tree.

Captures the structure of the


recursive calls:
- when we visit a neighbor w of v,
we add w as child of v
- whenever DFS returns from a
vertex v, we climb up in the tree
from v to its parent

11
23

Applications DFS vs. BFS


What can BFS do and DFS cant?
Finding shortest paths (in unweighted graphs)

What can DFS do and BFS cant?


Finding out if a connected undirected graph is biconnected
A connected undirected graph is biconnected if there are no vertices
whose removal disconnects the rest of the graph.
Application in computer networks: ensuring that a network is still
connected when a router/link fails.

24

A graph that is not biconnected

12
25

DFS vs. BFS


Applications DFS BFS
Spanning forest, connected

components, paths, cycles
Shortest paths

Biconnected components

L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS

26

Next lecture
Review

Final Exam
Wednesday, April 17, 9:00-12:00

13

You might also like