dimanche 22 avril 2018

BFS stops after the starting vertex's neighbors have been visited

I'm attempting to implement a BFS search on a maze, specifically the Apollo and Diana maze puzzle. Upon running my BFS algorithm the initial neighbors of the starting vertex are added however subsequent neighbors are added causing the algorithm to terminate after three iterations when run using the example maze below. The code for my algorithm is:

BFS_Algo(){

NodeQueue.push(NodeMatrix[0][0]);


while( !NodeQueue.empty() )
{
    Node current;
    current = NodeQueue.front();
    NodeQueue.pop();

    if( !current.visited )
    {
        current.visited = true;

        for ( int i = 0; i < current.neighbors.size(); ++i )
        {
            if ( !current.neighbors[i].visited )
            {
                NodeQueue.push(current.neighbors[i]);
                current.neighbors[i].predecessor = &NodeMatrix[current.x][current.y];
            }
        }
    }
}



}

I know that the neighbors vectors are properly filled since I have a helper function that prints the contents of all the vectors like so:

B E:  |R SE|R SW|
R SE: |empty vector
R SW: |B N|B N|
R SE: |B E|
B N:  |R SE|
R E:  |empty vector
B N:  |R SE|
B E:  |O O|
O O:  |empty vector

/empty vectors signify that there are no neighbors./

I don't believe this to be a logical error, but I feel this might be more in line with local references instead. Any guidance would be greatly appreciated.


Additional Info:
Ive read in the information for the graph from a file similar to this example:

B E |R SE|R SW|
R SE|B N |R E |
B N |B E |O O |

I have created a class that contains a node struct that holds all the appropriate data.

private:

struct Node{

    int x;                          position in graph
    int y;                          position in graph
    std::string color;              graph attr
    std::string direction;          graph attr
    bool visited;                   bfs bool
    Node * predecessor;             ancestor for path backtracking
    std::vector<Node> neighbors;    vector of neighbors

};

short rows;
short columns;
Node **NodeMatrix;
std::queue<Node> NodeQueue;

Aucun commentaire:

Enregistrer un commentaire