0% found this document useful (0 votes)
256 views9 pages

BFS DFS Applicatio PDF

This document discusses applications of breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. BFS can be used to find the shortest path between vertices in an unweighted graph and determine if a strongly connected directed graph contains cycles. DFS can determine if a path exists between vertices and find the connected components of a graph. Both algorithms can construct spanning trees or forests from graphs. BFS produces a breadth-first spanning tree while DFS produces a depth-first spanning tree. Finding cycles in a graph can be done using either algorithm.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
256 views9 pages

BFS DFS Applicatio PDF

This document discusses applications of breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. BFS can be used to find the shortest path between vertices in an unweighted graph and determine if a strongly connected directed graph contains cycles. DFS can determine if a path exists between vertices and find the connected components of a graph. Both algorithms can construct spanning trees or forests from graphs. BFS produces a breadth-first spanning tree while DFS produces a depth-first spanning tree. Finding cycles in a graph can be done using either algorithm.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 9

Applications of BFS and DFS

CSE 2011 Winter 2011

28 March 2011

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 strongly connected directed 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.
2

Computing Spanning Trees

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?

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

Computing Spanning Forests

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.

Example
A graph with 3 components
P N L O M D E C A F B G H
8

Q R s

Example of a Forest
We removed the cycles from the previous graph.
P N L O M D E C A F B G H
9

Q R s

A forest with 3 trees

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.

10

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

11

DFS Algorithm

Flag all vertices as not visited

Flag yourself as visited For unvisited neighbors, call RDFS(w) recursively


We can also record the paths using prev[ ]. Where do we insert the code for prev[ ]?
12

DFS Path Tracking


0 8
source

Adjacency List

Visited Table (T/F)


0 1

T T T T T T T T T T

8 9 1 3 3 5 6 2 8

2 1 3 7

2 3 4 5

6 5

6 7 8 9

DFS find out path too Try some examples. Path(0) -> Path(6) -> Path(7) ->

Pred

13

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

14

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.

15

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

16

DFS vs. BFS


Applications
Spanning forest, connected components, paths, cycles Shortest paths Biconnected components
A B E C F D

DFS

BFS

L0 L1

A C E F
17

L2

DFS

BFS

Final Exam
Final Exam
Sunday, April 17, 10:00-13:00

Materials:
All lectures notes and corresponding sections in the textbook from the beginning to todays lecture All assignments Homework questions
18

You might also like