There is some pseudocode on the Wiki:
BFS(start_node, goal_node) { return BFS'({start_node}, ∅, goal_node); } BFS'(fringe, visited, goal_node) { if(fringe == ∅) { // Целевой узел не найден return false; } if (goal_node ∈ fringe) { return true; } return BFS'({child | x ∈ fringe, child ∈ expand(x)} \ visited, visited ∪ fringe, goal_node); } But it has a lot of incomprehensible, namely the last line of the return function.
The essence of the job is to write a program to bypass the graph in width to find all the shortest paths. The essence of the question is to write pseudocode, which can pass the graph with the criteria above.
Now I have a working code that returns one shortest answer and that is to say the least very badly.
How it works?
- We get a new vertex .;
- We drive it into a function, mark it with black;
- Check adjacent vertices and if they are white, we throw into the queue;
- For each element, we remember its parents in a separate array of parents;
- Next, spin in a circle until the queue is empty;
- After the function is completed, we start the path recovery function, which restores the path reversely, starting from the final vertex and ending at the initial one, using the array with the parents.
It remains only to figure out how to make the program find all the shortest paths and do a normal search for an answer to the points passed, if this is really done (for the reverse search code looks disgusting).