Finding your way out of a maze with DFS
The Depth-First Search (DFS) algorithm is a path-finding technique suitable for low-memory devices. Another common use is to build mazes with some minor modifications to the list of nodes visited and discovered, but the main algorithm stays just the same.
Getting ready
This is a high-level algorithm that relies on each graph's implementation of the main functions (build
, init
, and so on), so that the algorithm is implemented in the Graph
class.
It is important to take a moment and verify when the recipe is manipulating actual objects, or vertex IDs.
How to do it...
Even though this recipe is only defining a function, please take into consideration the comments in the code for better understanding of the implementation and code flow:
- Declare the
GetPathDFS
function:
public List<Vertex> GetPathDFS(GameObject srcObj, GameObject dstObj) { // next steps }
- Establish whether input objects are
null
:
if (srcObj == null || dstObj == null) return new...