Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:02:43+00:00 2026-05-17T01:02:43+00:00

I’m trying to make a program using the BFS algorithm so I put every

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:02:43+00:00Added an answer on May 17, 2026 at 1:02 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.