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

  • Home
  • SEARCH
  • 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 7783875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:58:11+00:00 2026-06-01T19:58:11+00:00

I’m attempting to implement BFS in Python, I understand how the algorithm works but

  • 0

I’m attempting to implement BFS in Python, I understand how the algorithm works but I don’t have much programming experience. I’ve spent hours thinking about the best way to represent everything and how to be as efficient as possible. I’ve been unable to figure out how to get the path from my start node, to goal node.

I’ve spent ages googling around and looking at other peoples implementation of the algorithm, but my application is slightly different and this confuses me. When people are implementing BFS, they are assuming they have a graph given to them (as does the wikipedia article on BFS). In my problem, I have an initial state and a goal state I want to reach, but no graph or tree so I’m generating the nodes as I go.

For example:

def BFS(initial, goal):

    q = [initial]
    visited = []

    while q:
        n = q.pop()
        states = generate_states(n)

        for state in states:
            if state == goal:
                pass #placeholder
            q.append(state)
            visited.append(state)

It’s not properly fleshed out because I’m having trouble with something, I’m not sure what it specifically is either… If initial and goal are nodes, and I write a struct type class elsewhere in my code such as:

class node:
    state = None
    parent = None

I think this is a suitable way to represent a node. So when I pop a node object off my queue, it has information regarding where it originated which will be initialized by the generate_states function. Then the for loop will append each of these new nodes onto the queue, and visited queue and it will repeated under one of the generated nodes has a state which matches my goal state.

Now that I’ve found the goal node, I have a list of visited nodes, but if I trace the path the path backwards from the goal node, aren’t I slowing down the algorithm? Once a goal has been found I would look at its parent, locate its parent in the visited list, then look at the parent of the parent, etc… until I had a path = [node object, node object, …] from the initial node to the goal node.

This brings me to another problem, when I create a node object it only lasts for one iteration of the while loop. How am I meant to store the objects in an array, they will each need a unique name and there is no obvious way (to me) to do this. This was the problem I mentioned earlier that I wasn’t sure about. So it looks like I’m creating nodes but then only storing the node.state in the queue, which is pointless because I require the node object to access node.parent…

Why am I finding this so difficult, am I missing something obvious or making this too complicated?

Thanks for reading.

  • 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-06-01T19:58:13+00:00Added an answer on June 1, 2026 at 7:58 pm

    I can’t comment on most of this, as I don’t fully understand what you’re trying to do – as you say, normally a BFS would have the graph there already, so I’m not sure how you are proposing to construct it as you go. But I must reply to this bit:

    How am I meant to store the objects in an array, they will each need a unique name

    This is definitely false. There is absolutely no need to give something a name if you just want to store it in a list – you can just append it. If you’re worried about being able to find it later, then the normal thing to do with graphs is just give each node a number, via a single counter that you increase each time you define one. Again, if you’re just storing the nodes in a list, then they automatically get a unique number: their position in the list.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
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 just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.