Docsity

Trace of DFS algorithm - Design and Analysis - Study Notes, Study notes of Digital Systems Design

Toggle sidebar
bg1
Figures 8.12 to 8.20 show a trace of the DFS algorithm applied to a graph. The figures
show the content of the stack during the execution of the algorithm.
Figure 8.12: Trace of Depth-first-search algorithm: source vertex โ€˜sโ€™
Figure 8.13: Trace of DFS algorithm: vertex โ€˜aโ€™ popped
Figure 8.14: Trace of DFS algorithm: vertex โ€˜cโ€™ popped
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Trace of DFS algorithm - Design and Analysis - Study Notes and more Study notes Digital Systems Design in PDF only on Docsity!

Figures 8.12 to 8.20 show a trace of the DFS algorithm applied to a graph. The figures show the content of the stack during the execution of the algorithm.

Figure 8.12: Trace of Depth-first-search algorithm: source vertex โ€˜sโ€™

Figure 8.13: Trace of DFS algorithm: vertex โ€˜aโ€™ popped

Figure 8.14: Trace of DFS algorithm: vertex โ€˜cโ€™ popped

Figure 8.15: Trace of DFS algorithm: vertex โ€˜fโ€™ popped

Figure 8.18: Trace of DFS algorithm: vertex โ€˜bโ€™ popped

Figure 8.19: Trace of DFS algorithm: vertexโ€˜dโ€™ popped

Figure 8.20: Trace of DFS algorithm: the final DFS tree

Each execution of line 3 or line 8 in the TRAVERSE -DFS algorithm takes constant time. So the overall running time is O(V + E). Since the graph is connected, V โ‰ค E + 1, this is O(E).

If we implement the bag by using a queue , we have breadth-first search (BFS). Each execution of line 3 or line 8 still takes constant time. So overall running time is still O(E).

If the graph is represented using an adjacency matrix , the finding of all the neighbors of vertex in line 7 takes O(V) time. Thus depth-first and breadth-first take O(V 2 ) time overall.

Either DFS or BFS yields a spanning tree of the graph. The tree visits every vertex in the graph. This fact is established by the following lemma:

The DFSVISIT routine is as follows: