Breadth irst Search cse section. of textbook
Graph raversal (.) Application example Given a graph representation and a vertex s in the graph, find all paths from s to the other vertices. wo common graph traversal algorithms: Breadth-irst Search (BS) Idea is similar to level-order traversal for trees. Implementation uses a queue. Gives shortest path from a vertex to another. Depth-irst Search (DS) Idea is similar to preorder traversal for trees (visit a node then visit its children recursively). Implementation uses a stack (implicitly via recursion).
BS and Shortest Path Problem Given any source vertex s, BS visits the other vertices at increasing distances away from s. In doing so, BS discovers shortest paths from s to the other vertices. What do we mean by distance? he number of edges on a path from s (unweighted graph). Example Consider s=vertex s Nodes at distance?,,, Nodes at distance?,,, Nodes at distance?
How Does BS Work? Similarly to level-order traversal for trees. he BS code we will discuss works for both directed and undirected graphs.
Skeleton of BS Algorithm output v;
BS Algorithm flag[ ]: visited or not output v;
BS Example Adjacency List Visited able (/) source Initialize visited table (all alse) Q = { } Initialize Q to be empty
Adjacency List source Visited able (/) Q = { } lag that has been visited Place source on the queue
Adjacency List Visited able (/) Neighbors source Mark neighbors as visited,, Q = {} {,, } Dequeue. Place all unvisited neighbors of on the queue
Adjacency List Visited able (/) source Neighbors Mark newly visited neighbors, Q = {,, } {,,, } Dequeue. -- Place all unvisited neighbors of on the queue. -- Notice that is not placed on the queue again, it has been visited!
Adjacency List Visited able (/) Neighbors source Mark newly visited neighbors, Q = {,,, } {,,,, } Dequeue. -- Place all unvisited neighbors of on the queue. -- Only nodes and haven t been visited yet.
Adjacency List Visited able (/) source Neighbors Q = {,,,, } {,,, } Dequeue. -- has no unvisited neighbors!
Adjacency List Visited able (/) Neighbors source Q = {,,, } {,, } Dequeue. -- has no unvisited neighbors!
Adjacency List Visited able (/) source Neighbors Q = {,, } {, } Dequeue. -- has no unvisited neighbors!
Adjacency List Visited able (/) Neighbors source Mark new visited Vertex Q = {, } {, } Dequeue. -- place neighbor on the queue.
Adjacency List Visited able (/) source Neighbors Mark new visited Vertex Q = {, } {, } Dequeue. -- place neighbor on the queue
Adjacency List Visited able (/) source Neighbors Q = {, } { } Dequeue. -- no unvisited neighbors of
Adjacency List Visited able (/) source Neighbors Q = { } { } Dequeue. -- no unvisited neighbors of
Adjacency List Visited able (/) source What did we discover? Q = { } SOP!!! Q is empty!!! Look at visited tables. here exists a path from source vertex to all vertices in the graph
Assume adjacency list Running ime of BS V = number of vertices; E = number of edges Each vertex will enter Q at most once. dequeue is O(). he for loop takes time proportional to deg(v).
Running ime of BS () Recall: Given a graph with E edges Σ vertex v deg(v) = E he total running time of the while loop is: O( Σ vertex v ( + deg(v)) ) = O(V+E) his is the sum over all the iterations of the while loop! Homework: What is the running time of BS if we use an adjacency matrix?
BS and Unconnected Graphs L N P O Q R A graph may not be connected (strongly connected) enhance the above BS code to accommodate this case. M D E s A C G B K A graph with components H
Recall the BS Algorithm output ( v );
Enhanced BS Algorithm A graph with components N L M A B C H K We can re-use the previous BS(s) method to compute the connected components of a graph G. BSearch( G ) { i = ; // component number for every vertex v flag[v] = false; for every vertex v if ( flag[v] == false ) { print ( Component + i++ ); BS( v ); } }
Applications of BS What can we do with the BS code we just discussed? Is there a path from source s to a vertex v? Check flag[v]. Is an undirected graph connected? Scan array flag[ ]. If there exists flag[u] = false then Is a directed graph strongly connected? Scan array flag[ ]. If there exists flag[u] = false then
Next lecture Depth irst Search (DS) Review inal exam