0% found this document useful (0 votes)
37 views11 pages

Breadth-First Search: Presentation For Use With The Textbook,, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Uploaded by

sumi kanna
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)
37 views11 pages

Breadth-First Search: Presentation For Use With The Textbook,, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Uploaded by

sumi kanna
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/ 11

Presentation for use with the textbook, Algorithm Design and

Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Breadth-First Search
L0
A

L1
B C D

L2
E F

© 2015 Goodrich and Tamassia Breadth-First Search 1


Breadth-First Search
q  Breadth-first search q  BFS on a graph with n
(BFS) is a general vertices and m edges
technique for traversing takes O(n + m ) time
a graph
q  BFS can be further
q  A BFS traversal of a
extended to solve other
graph G
n  Visits all the vertices and
graph problems
edges of G n  Find and report a path
n  Determines whether G is with the minimum
connected number of edges
n  Computes the connected between two given
components of G vertices
n  Computes a spanning n  Find a simple cycle, if
forest of G there is one
© 2015 Goodrich and Tamassia Breadth-First Search 2
BFS Algorithm
q  The algorithm uses “levels” Li and a mechanism for setting and getting
“labels” of vertices and edges.

© 2015 Goodrich and Tamassia Breadth-First Search 3


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

© 2015 Goodrich and Tamassia Breadth-First Search 4


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

© 2015 Goodrich and Tamassia Breadth-First Search 5


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

© 2015 Goodrich and Tamassia Breadth-First Search 6


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
L0
of Gs A
Property 3
For each vertex v in Li L1
B C D
n  The path of Ts from s to v has i
edges L2
n  Every path from s to v in Gs has at E F
least i edges
© 2015 Goodrich and Tamassia Breadth-First Search 7
Analysis
q  Setting/getting a vertex/edge label takes O(1) time
q  Each vertex is labeled twice
n  once as UNEXPLORED
n  once as VISITED
q  Each edge is labeled twice
n  once as UNEXPLORED
n  once as DISCOVERY or CROSS
q  Each vertex is inserted once into a sequence Li
q  Method incidentEdges is called once for each vertex
q  BFS runs in O(n + m) time provided the graph is
represented by the adjacency list structure
n  Recall that Σv deg(v) = 2m
© 2015 Goodrich and Tamassia Breadth-First Search 8
Applications
q  We can use the BFS traversal algorithm, for a
graph G, to solve the following problems in
O(n + m) time
n  Compute the connected components of G
n  Compute a spanning forest of G
n  Find a simple cycle in G, or report that G is a
forest
n  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

© 2015 Goodrich and Tamassia Breadth-First Search 9


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
© 2015 Goodrich and Tamassia Breadth-First Search 10
DFS vs. BFS (cont.)
Back edge (v,w) Cross edge (v,w)
n  w is an ancestor of v in n  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
© 2015 Goodrich and Tamassia Breadth-First Search 11

You might also like