Insight 34.7.1.
Depth-first search locks in edges in the search tree when a vertex is selected to be explored. Breadth-first search locks in edges when a vertex is first discovered.
DFS(graph, start):
Stack searchStack
searchStack.push(start)
Map parentMap
Set visitedSet
visitedSet.insert(start)
while not searchStack.isEmpty():
vertex = searchStack.pop()
if not visitedSet.contains(vertex):
visitedSet.insert(vertex)
for each neighbor of vertex:
if not visitedSet.contains(neighbor):
parentMap[neighbor] = vertex
searchStack.push(neighbor)
[1]
[2 3 5]
[0 3 5]
[3 5]
[7 5]
[4 5 6 5]
[5 5 6 5]
[5 6 5]
[6 5]
[5]
1 -> 5 -> 7 -> 3.
StartDFS(graph, start):
Map parentMap
Set visitedSet
DFS(graph, start, visitedMap, parentMap)
DFS(graph, vertex, visitedSet, parentMap):
if visitedSet.contains(vertex):
return
visitedSet.insert(vertex)
for each neighbor of vertex:
if not visitedSet.contains(neighbor):
parentMap[neighbor] = vertex
DFS(graph, neighbor, visitedSet, parentMap)
Known and Parent tables are updated.
Recursive, enter 0 as the Start Vertex in the input field and click Run DFS to see the recursive version of depth-first search produce the same search tree.
New Graph button to generate a new random graph. (You may need to fast forward to the end before being able to create a new graph.
> button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
<< : Skip to the beginning of the animation. < : Step backwards one step in the animation. > : Step forward one step in the animation. >> : Skip to the end of the animation.