Breadth irst Search CSE Winter Graph raversal Application examples Given a graph representation and a vertex s in the graph ind all paths from s to the other vertices wo common graph traversal algorithms Breadth-irst Search (BS) Depth-irst Search (DS)
BS and Shortest Path Problem Given any 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 Examples: www.student.seas.gwu.edu/~idsv/idsv.html Code: similar to code for topological sort (see the next slide) flag[v] = false: we have not visited v flag[v] = true: we already visited v Why does BS need a flag for each vertex (topological sort does not)? he code works for both directed and undirected graphs
opological Sort ind all starting points Decrement indegree(w) Place new start vertices on the Q BS Algorithm flag[ ]: visited or not
BS Example Visited able (/) Q = { } Initialize visited table (all alse) Initialize Q to be empty Visited able (/) Q = { } lag that has been visited Place on the queue
Visited able (/) Mark neighbors as visited,, Q = {} {,, } Dequeue. Place all unvisited neighbors of on the queue Visited able (/) 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!
Visited able (/) Mark newly visited neighbors, Q = {,,, } {,,,, } Dequeue. -- Place all unvisited neighbors of on the queue. -- Only nodes and haven t been visited yet. Visited able (/) Q = {,,,, } {,,, } Dequeue. -- has no unvisited neighbors!
Visited able (/) Q = {,,, } {,, } Dequeue. -- has no unvisited neighbors! Visited able (/) Q = {,, } {, } Dequeue. -- has no unvisited neighbors!
Visited able (/) Mark new visited Vertex Q = {, } {, } Dequeue. -- place neighbor on the queue. Visited able (/) Mark new visited Vertex Q = {, } {, } Dequeue. -- place neighbor on the queue
Visited able (/) Q = {, } { } Dequeue. -- no unvisited neighbors of Visited able (/) Q = { } { } Dequeue. -- no unvisited neighbors of
Visited able (/) What did we discover? Q = { } SOP!!! Q is empty!!! Look at visited tables. here exists a path from vertex to all vertices in the graph Applications of BS What can we do with the BS code we just discussed? Is there a path from 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 o output the contents (e.g., the vertices) of a connected (strongly connected) graph What if the graph is not connected (weakly connected)? Add just a little bit of code and invoke method BS( ) discussed later.
Other Applications of BS o find the shortest path from a vertex s to a vertex v in an unweighted graph o find the length of such a path o find out if a graph contains cycles o find the connected components of a graph that is not connected o construct a BS tree/forest from a graph Running ime of BS Assume adjacency list 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 (cont d) 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? Applications of BS Is there a path from s to a vertex v? Is an undirected graph connected? Is a directed graph strongly connected? o output the contents (e.g., the vertices) of a connected (strongly connected) graph Discuss non-connected (weakly connected) graphs later o find the shortest path from a vertex s to a vertex v in an unweighted graph o find the length of such a path o find out if a graph contains cycles o find the connected components of a graph that is not connected o construct a BS tree/forest from a graph