End of Term Review 2.7

End of Term Review 2.7 - BFS Example (cont.) L0 L1 L0 A B C...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 Single-Source (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.

Page1 / 5

End of Term Review 2.7 - BFS Example (cont.) L0 L1 L0 A B C...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online