Breadth First Search in C++
CoderSource.net
Breadth First Search in C++ - Article by breakaleg
Level: AdvancedType: Article
Rating: Page: 2 of 3

Date: 7/27/2005 12:00:00 AM

Environment: C++

  One possibility is to gather an infinite number of people (this is for instructional purposes only) and put them in the start position of the maze.

Then everytime we are to make a decision, leave one single person in the current position and split the group into N smaller ones, where N is the number of the current possible decisions to make. These groups each go a different way and so on, until someone reaches the finish. It is clear that the path found by this group of people is of minimum length, since every group has to make only one decision at a time (the length of each step is the same for everyone) and this group made it first to the finish. Note that passing through a point that already has one person standing there is not permitted.

  To reconstruct the minimum length path, let us assume that every person that is left in a certain point knows exactly the position where the group came from. For example the person in point 2 knows that the group came from the 1st position before it left him there. Now if we start with the last position of the winning group (the finish) we may go backwards as we know its previous position, and so on until we reach the start. We have now constructed the exact minimum path (in terms of decisions to make) to arrive at the finish of the maze, but in reversed order. The last operation is to reverse the path in order to find the correct one.

  This is a version of Lee's algorithm for finding the shortest path between two particular vertices in an undirected graph. But let us now concentrate on the method of traversal, which in terms of graph theory is known as Breadth First Search ( BFS in short ). I assume you already know the algorithm, it is all in the way the groups of people move from one point to another - at each step the groups separate into smaller ones, then go to the next positions and also leave a person behind. Remember it is not allowed to pass through a point with one person already standing there. This separation process continues until there are no more available positions to visit.

  For a better understanding let us try and traverse the next graph starting from vertex 3 using this method. The steps are already described graphically below but let us make a few comments.

  First there is a group of 5 people (which is enough for this situation) positioned in vertex 3. Then one person goes to vertex 1, one to vertex 5, two persons go to vertex 2 and one stays in 3. At the next phase of the process we clearly see that the person in vertex 1 has nowhere to go, since both its next possible positions (2 and 3) are occupied. Because 2 is smaller than 5 (considering the integer numbers order) we will search first for its available positions. The single one is vertex 4, so we leave one person in 2 and send one to 4. This seems like our last move - no person can move from its current position anymore since all are occupied.

1 2 3

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us