























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























































David Galles
Department of Computer Science
University of San Francisco
Depth First Search Breadth First Search
Visited nodes cicrled in red
Visited nodes cicrled in red
0
Visited nodes cicrled in red
0
Visited nodes cicrled in red
0
Visited nodes cicrled in red
0
To visit every node in the graph: TraverseDFS(Edge G[]) {
int i;boolean Visited = new Edge[G.length];for (i=0; i<G.length; i++)
Visited[i] = false; for (i=0; i<G.length; i++)
if (!Visited[i])
DFS(G, i, Visited);
}
Keep track of what nodes we have left using astack Recursive version implicitly uses the system stack Can write DFS non-recursively, using our ownstack
DFS, using recursion
DFS: Look as
Deep
as possible, before looking
wide
Examine all descendants of a node, beforelooking at siblings BFS: Look as
Wide
as possible, before looking
deep
Visit all nodes 1 away, then 2 away, then threeaway, and so on