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
0 answers

Optimizing a bfs solution

I am solving this problem on hacker earth: https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/just-c3-has-ended-1/description/ . It clearly seems like a BFS Problem. I solved a few test cases but…
0
votes
0 answers

Debugging BFS, counting the minimum time until all oranges are rotten

Problem: In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the value 1 representing a fresh orange; the value 2 representing a rotten orange. Every minute, any fresh orange that is adjacent…
Snorrlaxxx
  • 168
  • 1
  • 3
  • 18
0
votes
0 answers

DFS and BFS C++

I can't find the error in my code. When I run it in the terminal, it says a warning message, not an error, but still doesn't display any results. I am working on a dfsrecursive, BFS, bfs helper, and DFS function. Please help! void Graph::dfs() { …
0
votes
1 answer

How can I manage to get the requested output in this shortest path problem?

I have made this code which should solve the knight's shortest path problem . The problem is that I don't know how to count the depth level it reaches on the graph. # n = size of the board # start = starting position for example [0,0] # …
Tortar
  • 625
  • 5
  • 15
0
votes
1 answer

Find largest file on computer with dfs and bfs in java

I have been having some trouble with a class project lately and could really use some help. My teacher wants the class to create a program written in java that will locate the largest file (file with the longest path) on our computers using depth…
0
votes
1 answer

Java assignment to use Dijkstras search method, breath-first and depth first

Hello StackOverflow community, need your help. I have a final for my java class and its asking for: Generate a graph with 100,000 nodes, where each node randomly has between 1 and 5 connections to other nodes. Each node should contain within it a…
0
votes
2 answers

Cannot get "stack" to populate in DFS based task ordering program

I am writing a program that uses the recursive BFS algorithm to determine dependencies in an undirected graph. I am using a 5x5 array as an adjacency matrix to represent the graph. While debugging, I noticed that my "stack s" variable is remaining…
0
votes
2 answers

How to solve black & white knights problem in 3×3 grid

Here is a test of Artificial Intelligence informed and uninformed search algorithms. We have a 3×3 grid where B means BLACK KNIGHT and W is WHITE KNIGHT of chess. +---+---+---+ +---+---+---+ | W | | W | | B | | B…
0
votes
1 answer

How to find connectors in a graph?

I'm struggling a bit here because quite honestly my brain is fried and I'm clueless as to what to do. My task is to find connector's in a undirected, unweighted, graph. The task claims that: In an undirected graph, vertex v is a connector if there…
0
votes
1 answer

My Find Ladders solution is unable to update the list representing the path to the current node

This is the problem description: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed…
aemach
  • 33
  • 2
0
votes
1 answer

Finding area of subspace of 2d array?

I am trying to make a function that given a 2d array of either 0's or 1's and a set of coordinates, returns the area of the selected region with the same value as the given coordinates. For example given the array: [[0, 1, 0, 0, 0], [0, 1, 0, 0,…
sjpratt
  • 76
  • 1
  • 7
0
votes
1 answer

Finding the diameter of m-ary tree - C

I have to analyze an m-ary tree in C - using namely BFS. There are some requirements I don't succeed to implement for a while: 1. Find the diameter of the tree. 2. Given two vertices in the tree - find the shortest simple path between them. As for 1…
HelpMe
  • 91
  • 10
0
votes
0 answers

BFS for m-ary tree - C

I'm taking this term a course in C, and I have got an assignment which deals namely with pointers - building an m-ary tree. Some description: We receive at the command line arguments: the file name of a text and two numbers which represent the keys…
HelpMe
  • 91
  • 10
0
votes
1 answer

Longest path in a two-dimensional array of letters

I have tried to solve this problem for about 2 hours. I am not capable of solving. Does anyone have an idea on how to go about solving it? I tried using Python v. 3+ Any help would be greatly appreciated: Given a two-dimensional array of letters,…
0
votes
0 answers

"Fatal error in PMPI_Waitall: Invalid count, error stack" with MPI_Alltoall

I'm trying to implement a 1d partitioning breadth first search using MPI_Alltoall communication (to simulate distributed memory). But compiling the code gives the following error: PMPI_Waitall(392): MPI_Waitall(count=-1, req_array=0x7fff6c8c17a0,…
TinyNinja
  • 1
  • 1
1 2 3
99
100