26
Time Complexity Each visited vertex is put on (and so removed from) the queue exactly once. When a vertex is removed from the queue, we examine its adjacent vertices.  O(n) if adjacency matrix used  O(vertex degree) if adjacency lists used Total time  O(mn), where m is number of vertices in the component that is searched (adjacency matrix)

28
Path From Vertex v To Vertex u Start a breadth-first search at vertex v. Terminate when vertex u is visited or when Q becomes empty (whichever occurs first). Time  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

29
Is The Graph Connected? Start a breadth-first search at any vertex of the graph. Graph is connected iff all n vertices get visited. Time  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

30
Connected Components Start a breadth-first search at any as yet unvisited vertex of the graph. Newly visited vertices (plus edges between them) define a component. Repeat until all vertices are visited.

31
Connected Components

32
Time Complexity  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

34
Spanning Tree Start a breadth-first search at any vertex of the graph. If graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree (breadth-first spanning tree). Time  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

49
Depth-First Search Properties Same complexity as BFS. Same properties with respect to path finding, connected components, and spanning trees. Edges used to reach unlabeled vertices define a depth-first spanning tree when the graph is connected. There are problems for which bfs is better than dfs and vice versa.