Académique Documents
Professionnel Documents
Culture Documents
L2
DFS vs. BFS (§6.3.3)
E F Comparison of applications
Comparison of edge labels
discovery edge E F L2
E F E F
cross edge
L0 L0
A A L0 L0
A A
L1 L1
B C D B C D L1 L1
B C D B C D
E F E F
L2 L2
E F E F
1
Breadth-First Search 5/7/2002 11:06 AM
Analysis Applications
Setting/getting a vertex/edge label takes O(1) time
Using the template method pattern, we can
Each vertex is labeled twice
once as UNEXPLORED
specialize the BFS traversal of a graph G to
once as VISITED solve the following problems in O(n + m) time
Each edge is labeled twice Compute the connected components of G
once as UNEXPLORED Compute a spanning forest of G
once as DISCOVERY or CROSS
Find a simple cycle in G, or report that G is a
Each vertex is inserted once into a sequence Li
forest
Method incidentEdges is called once for each vertex
Given two vertices of G, find a path in G between
BFS runs in O(n + m) time provided the graph is
them with the minimum number of edges, or
represented by the adjacency list structure
report that no such path exists
Recall that Σv deg(v) = 2m
5/7/2002 11:06 AM Breadth-First Search 9 5/7/2002 11:06 AM Breadth-First Search 10