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

Knight on Chess Board - Shortest Path

I'm trying to solve this problem: https://www.interviewbit.com/problems/knight-on-chess-board/# Basically, you're given a board, a start point and an end point and have to find the shortest path. I'm trying to do BFS on the the board using the 8…
0
votes
1 answer

How can I make my BFS algorithm run faster?

So I have a function that looks at a grid (2D array) and finds all the paths from the starting point to the end point. So far the algorithm works as intended and I get the values that I'm looking for. The problem is that it takes forever. It can run…
ESM
  • 175
  • 10
0
votes
0 answers

Clone a directed graph - Leetcode question

I'm having some trouble understanding the bug in my code and why it's timing out. The problem is to create a clone of a directed graph. Here's a link to the question: https://www.educative.io/m/clone-directed-graph My solution uses a queue twice.…
j.Doie
  • 91
  • 1
  • 6
0
votes
1 answer

BFS algorithm does not mark all nodes in the Rotten Oranges problem on Leetcode

I am working on the rotten oranges 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…
0
votes
1 answer

Breadth First Search falling one index short of desired outcome

I'm trying to implement a bread-first search algorithm in Python to find the shortest path from the top left corner of a matrix (2d list) and the number 9, wherever it may fall in the matrix. For some reason when I run the code below with the…
paoiherpoais
  • 334
  • 1
  • 10
0
votes
1 answer

BFS Graph loop execution

I am trying to traverse breadth first in graph formed . My graph is in adjacency list by arraylist of arraylist as below void bfs(int root,Graph g) { boolean[] visited = new boolean[g.al.size()]; Queue q = new…
0
votes
1 answer

Can anyone spot what is wrong with that code?

I have been trying to solve a problem from coursera. Problem description: Given an undirected graph with vertices and edges, check whether it is bipartite. Input Format. A graph is given in the standard format. Constraints. 1 ≤ ≤ 105, 0 ≤ ≤…
Partho KR
  • 112
  • 2
  • 10
0
votes
1 answer

Is variable length relationship is traversed in BFS manner in Neo4j?

If we have the following graph: And the following query: MATCH (me)-[:KNOWS*1..2]-(remote_friend) WHERE me.name = 'Filipa' RETURN remote_friend.name Is "Dilshad" guaranteed to be returned before "Anders", in other words, is :KNOWS*1..2 guaranteed…
adnanmuttaleb
  • 3,388
  • 1
  • 29
  • 46
0
votes
3 answers

a runtime error on my leetcode submission :heap use after free on address

#include using namespace std; #include #include struct word { string s; int level; word(string a, int b) : s(a) , level(b) { } }; bool isadj(string s1, string s2) { int…
venkat
  • 13
  • 6
0
votes
2 answers

BFS implementation C++

I was trying to implement BFS using Vertex classes and BFS class, since I wanted to learn implementation of classes along with algorithms. vertex.h #ifndef vertex_H_ #define vertex_H_ #include class Vertex{ private: int…
0
votes
1 answer

Can this problem done without bfs or storing edges?

You are given an undirected connected graph with n nodes and n edges. Now Given two nodes a and b. find the number of edges that lies in path from a to b but are not part of cycle. (as number of edges is n there will be a cycle for sure). Can this…
0
votes
1 answer

Constructing a graph with constraints

Well we all know that given n nodes and m edges we can find the no of possible ways to reach from 1 to n using some algorithm like dfs. But consider a case when we have to proceed from back ward side. I mean that we are given no of ways i.e x to…
0
votes
1 answer

Problem with elements at same level with same vertical height in Vertical Order Traversal of a Binary Tree

Pseudocode Created a class that will hold the node and its horizontal height Using BFS, so create a queue and inserted the first node having a horizontal height of 0 Popped the element from the queue, if the horizontal height doesn't exist in the…
user11202034
0
votes
1 answer

Creating a binary tree out of string. Ex: 1,2,3,#,#,4,5,#,#,#,#,

I am creating a binary tree out of string using BFS algorithm and I have a question: Why should I write "TreeNode node = tree.front();" after while loop instead of "root = tree.front();". Why I can't just use already created TreeNode root …
0
votes
1 answer

How to get shortest path between two nodes with Breadth First Search?

I am brushing up my algorithm and data structure knowledge and was wondering if someone could help me figure out how to find the shortest path between two nodes with BFS. So far I am have the following method, which returns the shortest parth in a…