Tables 1.3 - Analysis Setting/getting a vertex/edge label...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 - 126 - Last Updated: 4/1/10 10:16 AM Applications BFS traversal can be specialized to solve the following problems in O(|V|+|E|) time: 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 CSE 2011 Prof. J. Elder - 127 - Last Updated: 4/1/10 10:16 AM Application: Shortest Paths on an Unweighted Graph Goal: To recover the shortest paths from a source node s to all other reachable nodes v in a graph. The length of each path and the paths themselves are returned. Notes: There are an exponential number of possible paths Analogous to level order traversal for graphs This problem is harder for general graphs than trees because of cycles! ? CSE 2011 Prof. J. Elder s - 128 - Last Updated: 4/1/10 10:16 AM Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouring vertices: Undiscovered vertices are coloured black Just discovered vertices (on the wavefront) are coloured red. Previously discovered vertices (behind wavefront) are coloured grey. CSE 2011 Prof. J. Elder - 129 - Last Updated: 4/1/10 10:16 AM 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 - 130 - Last Updated: 4/1/10 10:16 AM ...
View Full Document

Ask a homework question - tutors are online