BFS Example

Assuming that we push characters onto the frontier in alphabetical order (A before B ...), specify the order of the nodes that would be explored by BFS.

  • If we use the above source code
  • If we don't use the node_has_been_visited check

Assume that S is the initial node, while G is the goal node.

Note that the frontier is a queue, first we'll look at if the regular implementation is used:

expansion order frontier about to expanded
SSS
SB, CB
S-BC, A, D, EC
S-B-CA, D, E, EA
S-B-C-AD, E, E, FD
S-B-C-A-DE, E, FE
S-B-C-A-D-EE, F, DE
S-B-C-A-D-EF, DF
S-B-C-A-D-E-FD, GD
S-B-C-A-D-E-FGG
S-B-C-A-D-E-F-G

If we do it without cycle checking we get the following

expansion order frontier about to expand
SSS
SB, CB
S-BC, A, D, EC
S-B-CA, D, E, EA
S-B-C-AD, E, E, FD
S-B-C-A-DE, E, FE
S-B-C-A-D-EE, F, DE
S-B-C-A-D-E-EF, D, DF
S-B-C-A-D-E-E-FD, D, GD
S-B-C-A-D-E-E-F-DD, GD
S-B-C-A-D-E-E-F-D-DGG
S-B-C-A-D-E-E-F-D-D-G
Breadth First Search is Correct
Given any graph, breadth first search traverses all nodes in the graph
Enqueued
Suppose that v is a vertex in a graph being explored by BFS, then we say that v is enqueued, if it has been added to the queue.
Expanded
Suppose that v is a vertex in a graph being explored by BFS, then we say that v is expanded if all of it's successors have been added to the queue.
Depth
Suppose that v , w are two connected vertices in a graph, then we say that the depth of w with respect to v is the length of the shortest path from v to w in the graph, and we notate it by depth ( v , w )
Found
We say that a specific vertex v is said to be found if it has been expanded.
BFS Searches Less Deep Vertices First
Let s be the initial vertex that BFS is called on, and let us define the set X k := { v V : depth ( s , v ) = k } , then for any i < j N 0 BFS will expand everything in X i before it will expand anything in X j
BFS Expands by Depth Layer
Specifically given the following sequence of sets: X 0 , X 1 , X 2 , BFS will expand every node in the previous set before expanding any vertex in the next set.
BFS Expands All Enqueued Vertices
Suppose that the queue consists of some finite collection of vertices V , then BFS will expand every vertex in this set
BFS is Complete
Suppose that we have a potentially infinite graph G = ( V , E ) with an maximum branching factor of b . Given two specific vertices s , g V denoted as the start state and the goal state respectively if there exists a path from s to g then if BFS is initialized on s , it will expand g in a finite number of iterations.

Let X k be the collection of all nodes which have a path of length k connecting them to s and that X 0 = { s } . We will prove by that BFS will expand every node in X k by induction.

For the base case we know that X 0 = { s } will be expanded on the initial iteration, so the claim holds. Now assume that it holds true for every k N 0 , and we'll show that it holds true for k + 1 .

We know that BFS will expand every node in X k , we also know that any vertex that has depth k + 1 , is a neighbor of a node that had length k , therefore by expanding every node in X k , this means that X k + 1 will be contained on the queue, and therefore will expand every vertex in X k + 1

Consider a graph that has one root node, and then infinitely many child nodes, and then in the third layer all the child nodes lead to a singlar goal node. BFS will get stuck on this graph because it will be spending an infinite amount of time adding every child of the root node to the queue, and thus will never terminate even though there is a trivial path from the root to the goal node.

BFS Time Complexity
Suppose that BFS is run on a potentially infinite graph that has maximum branching factor b , then given two connected nodes s , g , such that d = depth ( s , g ) , then the number of steps taken by BFS by starting it on s until it expands g is O ( b b + 1 )

Observe that there are at most b k nodes in X k (symbolically | X k | b k ), therefore to expand everything in layer X k it will take at most b b k = b k + 1 steps to expand every vertex in X k while enqueuing their neighbors. Therefore, since we've proven that BFS expands by depth layer, then we would say that the runtime is upper bounded by

b 1 + b 2 + b k + 1

We'll also note that during the last layer, we could find our goal node immediately if it is the first thing we expand at depth d . But in the worst case, we iterate through every vertex in X d 1 , and finally add in the node g at the end of the queue, this means we will have to expand | X d | 1 vertices before we finally expand g , which results in at most b ( | X d | 1 ) b ( b d 1 ) = b d + 1 b d steps taken by BFS, which is why our upper bound involving just b k + 1 above is justified. Also we can simplify inside the big-o:

O ( b 1 + b 2 + + b k + 1 ) = O ( b k + 1 ) as needed.
BFS Space Complexity
The space complexity of BFS is given by O ( b d + 1 )
The space taken by BFS is directly given by the amount of space used up by the queue, we can assume that everything in the queue has constant size, but we note that in the worst case as we iterate over vertices in X d , g is the last one to be popped off, therefore the queue would contain b ( | X d | 1 ) b ( b d 1 ) vertices, which is upper bounded by b d + 1 vertices, as needed.

Note that if you run BFS on a graph with edge weights hoping that it will produce a minimum cost solution to the goal node, it will not.

BFS Cost Optimal?
Suppose that there is a solution path within a finite search space, is it true that BFS is cost-optimal if given any level of the search tree, all step costs are greater than the step costs in the previous level.
This is not true, because suppose we have the graph consisting of one root node and 3 child nodes, all of which are goal states, then there is no guarentee in which order the nodes are explored, and thus BFS may return a goal state which has a very high cost