views:

186

answers:

3

Can anybody tell me the algorithm for depth first search (DFS)? I need to write the code for DFS in python.

+1  A: 

See my answer on searching at http://stackoverflow.com/questions/3225878/how-can-i-reduce-this-problem-to-the-tower-of-hanoi-or-solve-it-in-the-least-num/3225916#3225916

The only difference between a BFS and a DFS is where in your to-check list you insert your next state.

At that link, I was talking about a game. So when I say "board", I really mean "state" or "node", and when I say "move", I mean "link" or "transition". It's just terminology.

Borealid
I have to search thru a Maze in pacman game. I dont know what mistake I have made in my coding part. I am using stack for dfs. I am putting up the question for you so that you can look my code and tell me the mistake that i have made. When I execute it, it just hanged up.I hope you can help in this part.
Shilpa
@Shilpa: note in my answer where I say that you have to check you're not visiting the same state repeatedly, in order to avoid an infinite loop.
Borealid
well, I dont think i am getting problem with this issue. I just sending you the question. Actually, it allows me to send another question after 20 min...so just wait for 5 mn more...u can look through my code...I know, I messed up with some coding mistakes. I am trying to make it work from last 2 days...Now I can see some hope as You are very familiar with this maza game.
Shilpa
http://stackoverflow.com/questions/3252217/help-in-executing-the-code
Shilpa
plz see this question now...given on the link and tell me the mistakes
Shilpa
@Borealid..where r u?? I neeed ur help sir...
Shilpa
A: 

The basic difference between DFS and BFS is whether you are checking the area directly around you first, or scouting out at a distance first. Imagine you are on a chessboard at the upper-left corner (0,0). In BFS, you would check the three squares next to you first: (1,0),(1,1),(0,1). Then, you'd check the squares next to (1,0) - making sure that you don't check one that you already checked! (This is important in BFS and DFS.) Then the ones next to (1,1) and the ones next to (0,1). Then you would start checking the next "layer" outwards, until you ran out of things that you haven't checked yet. DFS is similar, but instead of doing things one "layer at a time" outwards from the starting point, you "scout out" as far as possible first.

Another metaphor is imagine that you are paper-wrapping a whole oak tree in a park. If you wrapped things the BFS way, you would wrap the trunk, then all the really thick branches until they were done, then all the thinner branches until they were done, and finally all the leaves. If you did it in the DFS way, you would wrap the trunk, then pick a big branch and wrap that, then wrap the first small branch off that, then the first leaf off that... and then you'd "back off" just a little and wrap the nearby leaves... and then "back off" to the next small branch, and do all its leaves. Et ceteras.

eruciform
Iam sorry, bt it goes over my head. stackoverflow.com/questions/3252217/… – plz check this link and help in getting the right answer
Shilpa
http://stackoverflow.com/questions/3252217/help-in-executing-the-code
Shilpa
sorry, but this is as close as I can give. if this concept doesn't make sense, i can't imagine how you can get the code to function. you need to understand the basics of what you are asking of the computer program. if you have a particular issue with a line of code or a concept, please update your question. but "send me a working program" isn't what this site is for.
eruciform
i moved my answer to the other question. you shouldn't post a second question for the same problem just because you aren't getting enough attention quickly enough. this question will likely get closed / deleted.
eruciform
I did posted that question for other members here. I want to show them my solution, so i posted again with all the coding.
Shilpa
+2  A: 

There isn't really an algorithm for depth first search - it is an algorithm. It's also a common building block for other algorithms.

In depth first search, you keep pushing forward as far as you can before backtracking as little as possible to try alternatives. This is in contrast to breadth first search, where you examine every alternative from your current state before exploring any of the possible next-states you discover.

That is, in depth first search, as you find new places/states/intermediate solutions, you place them on a last-in-first-out stack. In a breadth-first search, you place them in a first-in-first-out queue.

Very often (but not always), the stack for a depth-first search is implicit (the processor stack). What this means is that the search is implemented by making recursive calls.

The following is a pseudo-C++ depth-first search of a binary tree (not a binary search tree - there is no ordering assumption)...

bool Find (position_t p_Start, position_t &p_Result)
{
  if (p_Start.Contains_Goal ())
  {
    p_Result = p_Start;
    return true;
  }

  return (Find (p_Start.Left_Child ()) || Find (p_Start.Right_Child ()));
    //  Note - second Find call only occurs if the first fails, due to the
    //         shortcut behaviour of ||
}

There's a fun DFS-based algorithm for generating random mazes - the description below seems pretty good.

http://www.mazeworks.com/mazegen/mazetut/index.htm

Steve314
+1: good example
eruciform