As discussed in the previous posts about the breadth-first-search (bfs), the Depth-first-search (dfs) is another common brute-force searching algorithm. In the dfs, we start with an initial condition for one route, then gradually reach the end of one route; then we repeat the searching on the second route, the third route, ...
Therefore, we can make the following summary:
1. a dfs is one kind of recursion. When search from the current step to the next one, we will call the same function again;
2. the end condition (or base case) should be defined;
3. when the same function is called, the parameters should be updated converging to the end conditions;
4. a dfs should cover all the possible routes;
One possible optimization could be memoization, to record the result once a route is finished; then if we have the change to meet with the same route again in the searching, we could use the saved results directly without reaching the end. In short this method is usually called as dfs + memo, which is in general equivalent to dynamic programming (dp).
Comments
Post a Comment