tags:

views:

53

answers:

2

Hi,

I'm trying to make a program using the BFS algorithm so I put every node in a Queue and once every level is in the Queue I start comparing it to another node to see if they are equal or not, however the problem I'm having is that elements in my Queue are being modified so when I do the Dequeue I never get to an answer and I'm getting a stack overflow.

I'm not sure what part of my code had the problem since I the stack overflow happens all over the place, but I'll post a part of the Queuing and the Dequeuing.

So yeah basically after the second loop it starts messing up everything, it adds more than 1 "b" to my matrix and it modifies my Queue elements.

private void BFS(Nodo<string[,]> nodo)
{
  Array.Copy(nodo.getEA(), datos5, 9);
  temp = null;
  temp2 = null;
  temp3 = null;
  temp4 = null;
  Array.Copy(datos5, datos, 9);
  //There are a bunch of if and else if so I just posted one
  if (datos[1, 0].Equals("b"))
  {
    Array.Copy(datos, datos2, 9);
    Array.Copy(datos, datos3, 9);
    cont3=3;
    //UP from 1,0 a 0,0
    datos[1, 0] = datos[0, 0];
    datos[0, 0] = "b";
    temp = new Nodo<string[,]>(datos);
    temp.setCamino("U");
    temp.setPadre(nodo);
    myq.Enqueue(temp);
    temp = null;
    //Right from 1,0 a 1,1
    datos2[1, 0] = datos2[1, 1];
    datos2[1, 1] = "b";
    temp2 = new Nodo<string[,]>(datos2);
    temp2.setCamino("R");
    temp2.setPadre(nodo);
    myq.Enqueue(temp2);
    temp = null;
    //Down from 1,0 a 2,0
    datos3[1, 0] = datos3[2, 0];
    datos3[2, 0] = "b";
    temp3 = new Nodo<string[,]>(datos3);
    temp3.setCamino("D");
    temp3.setPadre(nodo);
    myq.Enqueue(temp3);
    fila();
   }

}

private void fila()
{     
  Nodo<string[,]> temp5;
  for (int i = 0; i < myq.Count; i++)
  {
    temp5 = null;
    temp5 = (Nodo<string[,]>)myq.Dequeue();
    if (objetivo(temp5, nodof))
    {
      if (!flag2)
      {
        boxResultado.AppendText("Problem solved");
        flag2 = true;
        break;
      }
      else
      {
        break;
      }          
    }
    else
    {
      if (!flag2)
      {
        BFS(temp5);
      }
      else
      {
        break;
      }
    }
  }
}
private bool objetivo(Nodo<string[,]> p, Nodo<string[,]> e)
{
  nodo1 = null;
  nodo2 = null;
  bool flag = false;
  nodo1 = p.getEA();
  nodo2 = e.getEA();
  for (int i = 0; i < 3; i++)
  {
    for (int f = 0; f < 3; f++)
    {
      if (nodo1[i, f] != nodo2[i, f])
      {
        flag = true;
      }
    }
  }
  if (flag)
  {
    return false;
  }
  else
  {
    return true;
  }
}

I know my code is pretty horrible but I've been trying to figure out that problem for the past 5 hours or so so I've been modifying here and there trying to locate the problem, but I'm getting pretty frustrated so I decided to ask here for help. Btw this is in C#

Thanks in advance

A: 

First of all, you didnt specify what does the Nodo class does. Anyway, the BFS is pretty simple. Ill tell you the general algorithm. Suppose you have a NxN adjacency matrix in a graph, you would have a size N array with marks of the nodes visited. The initial code would be

class Graph{

    //matrix[i,j] is true if there is a node from i to j
    bool[,] matrix;

    private void BFS( int startNode )
    {
        int n = matrix.GetLength(0);
        bool [] marks = new bool[n];

        Queue<int> nodes = new Queue();
        nodes.Enqueue(startNode);

        while ( !nodes.Empty() )
        {
            int node = nodes.Dequeue();

            //set visited
            marks[node] = true;

            List<int> adjs = GetAdyacents(node);

            foreach ( int adjacent in adjs ){
                if ( !mark[adjacent] )
                    nodes.Enqueue(adjacent);
            }

            Console.WriteLine("Visiting {0}", node);
        }
    }

}

The GetAdjacent() method depends if you are using a representation with matrix or adjacency lists. Anyway is simple, if you need to know how to do them, drop me a msg.

I haven't tested the code, but Im very sure it works (forgive any some syntax issues!)

Hope I can help Best of luck! DvD

David Conde
Thanks a lot I will try it with this and post back. And my "Nodo" class is just a Generic Node class I made.
Jadager
A: 

I haven't taken a deep look at your code, but I'm wondering if you'd benefit from using a Queue.

Christopher Hunt
Well I'm sure if would be easier without a Queue but BFS uses a Queue, so I want to make it that way.
Jadager
Sorry, I mean the .NET Queue class (http://msdn.microsoft.com/en-us/library/7977ey2c.aspx).
Christopher Hunt