vimwiki/tech/BFS.wiki

25 lines
972 B
Plaintext

= Breadth First Search =
Breadth 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. Therefore, BFS is often performed in O(|V| +
|E|) time.
== Algorithm ==
=== Tree ===
The algorithm first starts by creating a Queue, Q, and a pointer to the current
node, curr. Starting at the root of the tree, all child nodes of the current
node are placed onto Q, then curr is changed to the first element of Q, which
is dequeued. This will traverse the tree from left to right, layer by layer. If
desired, a condition can be added for the desired node.
This method of traversal *does not* take advantage of the structure of a tree,
and therefore does not have any of the lookup advantages of a [[binary_tree]].
=== Graph ===
Graph traversal is similar, however a list of visited nodes is required. This
is to ensure that we do not get stuck in loops