This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: BFS Example (cont.)
L0
L1 L0 A B C
E L0
L1 D
F L0 C
E CSE 2011
Prof. J. Elder B L2 A B L2 L1 L1 D  81  C
E D
F A B L2 F A C
E D
F Last Updated: 4/1/10 2:37 PM BFS Example (cont.)
L0
L1 L0
L1 A B L2 C
E L1 D B L2 F A
C
E D
F A B L2 L0 C
E CSE 2011
Prof. J. Elder D
F
 82  Last Updated: 4/1/10 2:37 PM Analysis
Setting/getting a vertex/edge label takes O(1) time
Each vertex is labeled three times
once as BLACK (undiscovered)
once as RED (discovered, on queue)
once as GRAY (finished) Each edge is considered twice (for an undirected graph)
Each vertex is inserted once into a sequence Li
Thus BFS runs in O(V+E) time provided the graph is
represented by an adjacency list structure CSE 2011
Prof. J. Elder  83  Last Updated: 4/1/10 2:37 PM BFS Algorithm with Distances and Predecessors
BFS(G,s)
Precondition: G is a graph, s is a vertex in G
Postcondition: d [u ] = shortest distance [u ] and
[u] = predecessor of u on shortest paths from s to each vertex u in G
for each vertex u V [G]
d [u ]
[u ] null
color[u] = BLACK //initialize vertex
colour[s]
RED
d [s ] 0
Q.enqueue(s )
while Q
u Q.dequeue()
for each v Adj[u ] //explore edge (u,v )
if color[v ] = BLACK
colour[v] RED
d [v ] d [u ] + 1 colour [u ]
CSE 2011
Prof. J. Elder [v ] u
Q.enqueue(v )
GRAY
 84  Last Updated: 4/1/10 2:37 PM SingleSource (Weighted) Shortest Paths CSE 2011
Prof. J. Elder  85  Last Updated: 4/1/10 2:37 PM ...
View
Full
Document
This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.
 Fall '11
 Elder
 Data Structures

Click to edit the document details