vimwiki/tech/BFS.wiki

25 lines
972 B
Plaintext
Raw Normal View History

2021-11-05 19:45:01 +00:00
= Breadth First Search =
2021-11-05 20:00:01 +00:00
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
2021-11-06 19:00:01 +00:00
traverses the entire tree/graph. Therefore, BFS is often performed in O(|V| +
|E|) time.
2021-11-05 20:00:01 +00:00
== 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]].
2021-11-06 19:00:01 +00:00
=== 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