2021-11-05 19:45:01 +00:00
|
|
|
= Depth First Search =
|
2021-11-06 19:00:01 +00:00
|
|
|
|
|
|
|
Depth First Search is an algorithm for searching a tree/graph for a node that
|
|
|
|
has a certain property. It starts at some chosen/implied root node, and
|
|
|
|
traverses the entire tree/graph. Depth first search can also be thought of as a
|
|
|
|
type of prefix traversal
|
|
|
|
|
|
|
|
== Algorithm ==
|
|
|
|
|
|
|
|
=== Tree ===
|
|
|
|
|
|
|
|
The algorithm is often implemented as a recursive function. For each node in
|
|
|
|
the graph, the function checks if the node's value is the desired value, and if
|
|
|
|
not immediately calls itself upon that nodes children. Due to this the
|
|
|
|
algorithm searches as deeply as possible first and foremost, before then
|
|
|
|
searching the width of the tree.
|
|
|
|
|
|
|
|
=== Graph ===
|
|
|
|
|
|
|
|
Graph traversal is similar, however a list of of visited nodes is required.
|
|
|
|
This is to ensure that we do not get stuck in loops.
|