Questions tagged [breadth-first-search]

In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

BFS is an uninformed search method that aims to expand and examine all nodes of a graph or combination of sequences by systematically searching through every solution. In other words, it exhaustively searches the entire graph or sequence without considering the goal until it finds it. It does not use a heuristic algorithm.

Algorithm

From the standpoint of the algorithm, all child nodes obtained by expanding a node are added to a FIFO (i.e., First In, First Out) queue. In typical implementations, nodes that have not yet been examined for their neighbors are placed in some container (such as a queue or linked list) called open and then once examined are placed in the container closed.

  1. Enqueue the root node
  2. Dequeue a node and examine it
    • If the element sought is found in this node, quit the search and return a result.
    • Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.
  3. If the queue is empty, every node on the graph has been examined – quit the search and return "not found".
  4. If the queue is not empty, repeat from Step 2.

In the figure below, you can see the animated figure of BFS.

enter image description here

Applications

  • Finding all nodes within one connected component
  • Copying Collection, Cheney's algorithm
  • Finding the shortest path between two nodes u and v (with path length measured by number of edges)
  • Testing a graph for bipartiteness

Source

Wikipedia

2216 questions
0
votes
1 answer

Leetcode Same Tree Iterative Solution

Question at hand: https://leetcode.com/problems/same-tree/ Can someone point out why my JavaScript solution can pass the test cases but fails during the actual submission? var isSameTree = function(p, q) { let queue = []; queue.push(p); …
Mike Chan
  • 167
  • 1
  • 11
0
votes
2 answers

How do you use stl iterator and a queue to implement a breadth first traversal of a graph?

while I understand the BFS conceptually I haven't grasped how to use an iterator and a queue to implement the BFS. I'd really appreciate it if somebody could give me some insight how to properly use the iterator in my code. Thank you. I'm not even…
user11219148
0
votes
1 answer

What is the order for BFS in adjacency matrix?

Given an adjacency matrix int[][] grid = new int[5][5]; Recursively, the DFS order would be //y = Row //x = Column //Down grid[y + 1][x]; //Up grid[y - 1][x]; //Right grid[y][x + 1]; //Left grid[y][x - 1]; Iteratively, the DFS order would…
user3587180
  • 1,317
  • 11
  • 23
0
votes
0 answers

Using bfs and creating a graph

I have to find shortest path and chose BFS, but nodes are described in x,y coordinates. Input data: 3 9 1 6 2 3 8 2 5 7 First two numbers are height and width, then first number in each row describes how many points in that row and other are x…
0
votes
1 answer

Does the starting node matter for Breadth First Search and Depth First Search in order to visit all of the nodes?

For DFS and BFS should should we start at the root always in order to make sure we traverse all the nodes?
0
votes
0 answers

Clarification required in Breadth First Search code as output doesn't match my understanding

I found this code from techiedelight [https://www.techiedelight.com/breadth-first-search/] for BFS the code is as below: #include #include #include using namespace std; // Data structure to store graph edges struct Edge…
Somesh Gupta
  • 324
  • 3
  • 13
0
votes
1 answer

is this python code using BFS or any other searching algorithm?

hi I am working on this python code and wanted to know which searching algorithm its using to find goal state. I want to know if the following code is using BFS or any other searching algorithm this code is solving classic farmer, wolf, cabbage,…
0
votes
1 answer

Coding exercise for practicing adjacency list and BFS

I have a coding exercise with a row of trampolines, each with a minimum and maximum "bounciness". I have the index of a starting trampoline and an end trampoline, and with this, I need to find the minimum amount of jumps required to reach the end…
jesseb0rn
  • 823
  • 3
  • 14
0
votes
1 answer

Why is this 8-puzzle solvable?

So i am writing a program to solve 8 puzzle using BFS,A*, and UCS. The goal state is fixed according to the assignment which is given as: Goal State: |1 2 3| |8 4| |7 6 5| My Initial State: | 1 2| |8 4 3| |7 6 5| However, upon writing a…
0
votes
0 answers

Three water jugs puzzle using BFS by C++ work on MacOS, but not on Ubuntu

Problem: Let’s say that we have 3 jugs, namely A, B, and C. Jug C will always start out completely full. Furthermore, we will define a goal state where jug A will contain a gallons, B will contain b gallons, and C will contain c gallons upon…
0
votes
1 answer

Breadth First Search: Thread 1: Swift runtime failure: force unwrapped a nil value

I try programming a "Circle the dot" game. Therefor I have to do a Breath First Search over a 2D-Array of UIButtons to find the shortest path from the playerButton to the border. Unfortunately I sometimes get a Thread 1: Swift runtime failure: force…
SwiftHobby
  • 67
  • 5
0
votes
1 answer

How to make graph traversal algorithm to work with large graph?

I am writing an algorithm to traverse through the graph when given a source and destination. The algorithm should traverse and show all possible paths from source to destination in the graph. I am using Breadth First Search to do this and it worked…
0
votes
1 answer

Puzzle solving algorithm to move a piece into the Exit spot

I'm looking for some suggestions as to what's a good algorithm I could use for solving the puzzle on the attached image with Javascript, preferably if the algo is documented and could contain a JS example. The white pieces can be moved one at a…
0
votes
2 answers

Determine whether any 3 integers from a data set sum up to a target value in O(n^2) time

So I'm working on this project and it has me stumped. We're given 2 arrays of integers. The first array is the set of items we will use and the second array is a set of target values. From the first array, I am supposed to find if there are any 3…
0
votes
0 answers

BFS prepare the bunnies escape

Prompt You have maps of parts of the space station, each starting at a prison exit and ending at the door to an escape pod. The map is represented as a matrix of 0s and 1s, where 0s are passable space and 1s are impassable walls. The door out of the…