Pseudocode and Algorithms
Previously, we looked at how to solve search problems by hand. Now, it's time for the next step. It is time to help your computer to solve the search problems also.
Pseudocode / Informal Algorithms
Now that you have a good idea on how to solve these various search problems, you
need to translate them so that a computer can solve it. Before we get into the
code, let's solve it generally using pseudocode.
Depth First Search
The pseudocode for depth first search based on what we did earlier is:
Declare two empty lists: Open and Closed.
Add Start node to our Open list.
While our Open list is not empty, loop the following:
Remove the first node from our Open List.
Check to see if the removed node is our destination.
If the removed node is our destination, break out of the loop, add the node to our Closed list, and return the value of our Closed list.
If the removed node is not our destination, continue the loop (go to Step c).
Extract the neighbors of our above removed node.
Add the neighbors to the beginning of our Open list, and add the removed node to our Closed list. Continue looping.
Breadth First Search
The pseudocode for breadth first first search is similar to the above and
similar to what we did earlier:
Declare two empty lists: Open and Closed.
Add Start node to our Open list.
While our Open list is not empty, loop the following:
Remove the first node from our Open List.
Check to see if the removed node is our destination.
If the removed node is our destination, break out of the loop, add the node to our Closed list, and return the value of our Closed list.
If the removed node is not our destination, continue the loop (go to Step c).
Extract the neighbors of our above removed node.
Add the neighbors to the end of our Open list, and add the removed node to our Closed list.