22 lines
790 B
Plaintext
22 lines
790 B
Plaintext
= Depth First Search =
|
|
|
|
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.
|