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
S
S
S
S
B, C
B
S-B
C, A, D, E
C
S-B-C
A, D, E, E
A
S-B-C-A
D, E, E, F
D
S-B-C-A-D
E, E, F
E
S-B-C-A-D-E
E, F, D
E
S-B-C-A-D-E
F, D
F
S-B-C-A-D-E-F
D, G
D
S-B-C-A-D-E-F
G
G
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
S
S
S
S
B, C
B
S-B
C, A, D, E
C
S-B-C
A, D, E, E
A
S-B-C-A
D, E, E, F
D
S-B-C-A-D
E, E, F
E
S-B-C-A-D-E
E, F, D
E
S-B-C-A-D-E-E
F, D, D
F
S-B-C-A-D-E-E-F
D, D, G
D
S-B-C-A-D-E-E-F-D
D, G
D
S-B-C-A-D-E-E-F-D-D
G
G
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 is a vertex in a graph being explored by BFS, then we say that is enqueued, if it has been added to the queue.
Expanded
Suppose that is a vertex in a graph being explored by BFS, then we say that is expanded if all of it's successors have been added to the queue.
Depth
Suppose that are two connected vertices in a graph, then we say that the depth of with respect to is the length of the shortest path from to in the graph, and we notate it by
Found
We say that a specific vertex is said to be found if it has been expanded.
BFS Searches Less Deep Vertices First
Let be the initial vertex that BFS is called on, and let us define the set , then for any BFS will expand everything in before it will expand anything in
BFS Expands by Depth Layer
Specifically given the following sequence of sets: 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 , then BFS will expand every vertex in this set
BFS is Complete
Suppose that we have a potentially infinite graph with an maximum branching factor of . Given two specific vertices denoted as the start state and the goal state respectively if there exists a path from to then if BFS is initialized on , it will expand in a finite number of iterations.
Let be the collection of all nodes which have a path of length connecting them to and that . We will prove by that BFS will expand every node in by induction.
For the base case we know that will be expanded on the initial iteration, so the claim holds. Now assume that it holds true for every , and we'll show that it holds true for .
We know that BFS will expand every node in , we also know that any vertex that has depth , is a neighbor of a node that had length , therefore by expanding every node in , this means that will be contained on the queue, and therefore will expand every vertex in
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 , then given two connected nodes , such that , then the number of steps taken by BFS by starting it on until it expands is
Observe that there are at most nodes in (symbolically ), therefore to expand everything in layer it will take at most steps to expand every vertex in 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
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 . But in the worst case, we iterate through every vertex in , and finally add in the node at the end of the queue, this means we will have to expand vertices before we finally expand , which results in at most steps taken by BFS, which is why our upper bound involving just above is justified. Also we can simplify inside the big-o:
as needed.
BFS Space Complexity
The space complexity of BFS is given by
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 , is the last one to be popped off, therefore the queue would contain vertices, which is upper bounded by 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