M4_graphsearchalgo
M4_graphsearchalgo
• the algorithm discovers all vertices at distance k from s before discovering any vertices at
distance k + 1
• It computes the distance (smallest number of edges) from s to each reachable vertex.
• It also produces a “breadth-first tree” with root s that contains all reachable vertices. For
any vertex reachable from s, the simple path in the breadth-first tree from s to
corresponds to a “shortest path” from s to in G, that is, a path containing the smallest
number of edges.
• The operations of enqueuing and dequeuing take O(1) time, and so the total time devoted to queue operations is O(V).
• Because the procedure scans the adjacency list of each vertex only when the vertex is dequeued, it scans each adjacency
list at most once.
• Since the sum of the lengths of all the adjacency lists is ‚(E), the total time spent in scanning adjacency lists is O(E).
• The overhead for initialization is O(V), and thus the total running time of the BFS procedure is O(V+E).
• Thus, breadth-first search runs in time linear in the size of the adjacency-list representation of G.
• Depth-first search explores edges out of the most recently discovered vertex that still has
unexplored edges leaving it.
• Once all of ’s edges have been explored, the search “backtracks” to explore edges leaving the
vertex from which was discovered.
• This process continues until we have discovered all the vertices that are reachable from the
original source vertex.
• If any undiscovered vertices remain, then depth-first search selects one of them as a new source,
and it repeats the search from that source.
• The algorithm repeats this entire process until it has discovered every vertex
Analysis
• The loops on lines 1–3 and lines 5–7 of DFS take time (V), exclusive of the time to execute the
calls to DFS-VISIT.
• The procedure DFS-VISIT is called exactly once for each vertex vV , since the vertex u on which
DFS-VISIT is invoked must be white and the first thing DFS-VISIT does is paint vertex u gray.
• Since