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 7092209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:17:05+00:00 2026-05-28T08:17:05+00:00

How do you trace the path of a Breadth-First Search, such that in the

  • 0

How do you trace the path of a Breadth-First Search, such that in the following example:

If searching for key 11, return the shortest list connecting 1 to 11.

[1, 4, 7, 11]
  • 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-28T08:17:06+00:00Added an answer on May 28, 2026 at 8:17 am

    You should have look at http://en.wikipedia.org/wiki/Breadth-first_search first.


    Below is a quick implementation, in which I used a list of list to represent the queue of paths.

    # graph is in adjacent list representation
    graph = {
            '1': ['2', '3', '4'],
            '2': ['5', '6'],
            '5': ['9', '10'],
            '4': ['7', '8'],
            '7': ['11', '12']
            }
    
    def bfs(graph, start, end):
        # maintain a queue of paths
        queue = []
        # push the first path into the queue
        queue.append([start])
        while queue:
            # get the first path from the queue
            path = queue.pop(0)
            # get the last node from the path
            node = path[-1]
            # path found
            if node == end:
                return path
            # enumerate all adjacent nodes, construct a 
            # new path and push it into the queue
            for adjacent in graph.get(node, []):
                new_path = list(path)
                new_path.append(adjacent)
                queue.append(new_path)
    
    print bfs(graph, '1', '11')
    

    This prints: ['1', '4', '7', '11']


    Another approach would be maintaining a mapping from each node to its parent, and when inspecting the adjacent node, record its parent. When the search is done, simply backtrace according the parent mapping.

    graph = {
            '1': ['2', '3', '4'],
            '2': ['5', '6'],
            '5': ['9', '10'],
            '4': ['7', '8'],
            '7': ['11', '12']
            }
    
    def backtrace(parent, start, end):
        path = [end]
        while path[-1] != start:
            path.append(parent[path[-1]])
        path.reverse()
        return path
            
    
    def bfs(graph, start, end):
        parent = {}
        queue = []
        queue.append(start)
        while queue:
            node = queue.pop(0)
            if node == end:
                return backtrace(parent, start, end)
            for adjacent in graph.get(node, []):
                if node not in queue :
                    parent[adjacent] = node # <<<<< record its parent 
                    queue.append(adjacent)
    
    print bfs(graph, '1', '11')
    

    The above codes are based on the assumption that there’s no cycles.

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

Sidebar

Related Questions

I have a Breadth-First algorithm that scans a graph of nodes. I am new
I would like to track the path that leads to a 404 with Google
I'm designing a public facing website that needs to track the path of users
How do I change the path for the default trace in SQL Server? Can
How do I trace the crash ? Process: iAddressGrabber [253] Path: /Users/egrabber/Library/Application Support/iPhone Simulator/User/Applications/DB3B2896-258C-4EC2-A490-802B1190A675/iAddressGrabber.app/iAddressGrabber
I have the following stack trace when trying to access the django admin default
My goal is to trace drawings that have a lot of separate shapes in
On a Vista machine with the valid path C:\Users\David, calling Directory.GetFiles(@C:\Users\David) throws the following
I'm using this code, and I get the stack trace that is listed below.
in upgrading rails app from 2.2.2 to 2.3.9 first error was: D:\web>ruby script/server -trace

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.