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

15 BFS

bfsGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph intro

Uploaded by

nausheenshah175
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)
7 views13 pages

15 BFS

bfsGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph intro

Uploaded by

nausheenshah175
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

Breadth-First Search

L0
A

L1
B C D

L2
E F

5/22/2025 5:46 AM Breadth-First Search 1


Outline and Reading
Breadth-first search (§6.3.3) (Pg.no. 313-
316)
◼ Algorithm
◼ Example
◼ Properties
◼ Analysis
◼ Applications
DFS vs. BFS (§6.3.3)
◼ Comparison of applications
◼ Comparison of edge labels
5/22/2025 5:46 AM Breadth-First Search 2
Breadth-First Search
Breadth-first search BFS on a graph with n
(BFS) is a general vertices and m edges
technique for traversing takes O(n + m ) time
a graph
BFS can be further
A BFS traversal of a
graph G: extended to solve other
◼ Visits all the vertices and graph problems
edges of G ◼ Find and report a path
◼ Determines whether G is with the minimum
connected number of edges
◼ Computes the connected between two given
components of G vertices
◼ Computes a spanning ◼ Find a simple cycle, if
forest of G there is one
5/22/2025 5:46 AM Breadth-First Search 3
BFS Steps
BFS divides all the vertices of a graph into various levels
Initially all edges and vertices are UNEXPLORED
Let start vertex be s
Mark s as EXPLORED and Place s in a list at level 0 (L0)
Initialize i as 0
Create an empty list Li+1

5/22/2025 5:46 AM Breadth-First Search 4


BFS Steps
For every vertex v in list Li do
◼ For every edge (v,w) incident on v in G do

◼ If edge e is unexplored then

 If vertex w is unexplored then


◼ Mark edge as discovery edge

◼ Mark w as EXPLORED and place the vertex into level


Li+1
 Otherwise
◼ Mark edge as cross edge

5/22/2025 5:46 AM Breadth-First Search 5


BFS Algorithm

5/22/2025 5:46 AM Breadth-First Search 6


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

5/22/2025 5:46 AM Breadth-First Search 7


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

5/22/2025 5:46 AM Breadth-First Search 8


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

5/22/2025 5:46 AM Breadth-First Search 9


Properties
Notation A
Gs: connected component of s
Property 1 B C D
BFS(G, s) visits all the vertices and
edges of Gs
E F
Property 2
The discovery edges labeled by
BFS(G, s) form a spanning tree Ts of
L0
Gs A
Property 3
For each vertex v in Li L1
B C D
◼ The path of Ts from s to v has i
edges L2
◼ Every path from s to v in Gs has at E F
least i edges
5/22/2025 5:46 AM Breadth-First Search 10
Applications
BFS traversal of a graph G can be used to
◼ Compute the connected components of G
◼ Compute a spanning forest of G
◼ Find a simple cycle in G, or report that G is a
forest
◼ Given two vertices of G, find a path in G between
them with the minimum number of edges, or
report that no such path exists

5/22/2025 5:46 AM Breadth-First Search 11


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
5/22/2025 5:46 AM Breadth-First Search 12
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 in
edges the tree of discovery
edges

L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS
5/22/2025 5:46 AM Breadth-First Search 13

You might also like