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

Track Second last item using BFS traversal java

I can track the last item by keeping the q.poll in a int but how can I track the second last item using this traversal? public Integer breadthFirstTraversal(Integer v) { Queue q = new LinkedList(); …
0
votes
1 answer

Shortest Source to Destination Path in a matrix n*m

Given a boolean 2D matrix (0-based index), find whether there is path from (0,0) to (x,y) and if there is one path, print the minimum no of steps needed to reach it, else print -1 if the destination is not reachable. You may move in only four…
0
votes
2 answers

Raising performance of BFS in Python

How can I increase speed performance of below Python code? My code works okay which means no errors but the performance of this code is very slow. The input data is Facebook Large Page-Page Network dataset, you can access here the dataset:…
boralim
  • 45
  • 10
0
votes
1 answer

Breadth's Algorithm Runs Forever Despite Possible Ending

So I saw this coding interview question and tried to solve it. I am trying to employ Breadth's Path Finding Algorithm to find the optimum flight routes from a given airport to all other airports; given the list of all airports and routes. An element…
user13527143
0
votes
0 answers

BFS logic connecting 6 dot

I got the error message: Index was outside the bounds of the array. It was fine when integer count was 5, now I'm adding 1 more dot and which makes the logic errors. Module Module1 Sub Main() Console.WriteLine("Algoritma BFS…
0
votes
1 answer

Request for Counter Testcase with Google Foobar Question - Prepare the Bunnies Escape

I recently came across GoogleFoobar's problem Prepare the Bunnies Escape, and I submitted a Shortest Path based solution. However, only 3 / 5 cases passed, and I am really intrigued to know why. I have attached my code below for reference. If anyone…
AwesomeGuy
  • 57
  • 2
  • 11
0
votes
1 answer

Graph BFS Traversal - C++

Given an undirected and disconnected graph G(V, E), print its BFS traversal. Here you need to consider that you need to print BFS path starting from vertex 0 only. V is the number of vertices present in graph G and vertices are numbered from 0 to…
John_Bradely
  • 1
  • 1
  • 3
  • 6
0
votes
1 answer

Updating variables in recursion

This is referring to this leetcode question here: https://leetcode.com/problems/path-sum-iii/ Basically I am given a binary tree in which each node contains an integer value. I have to find the number of paths that sum to a given value. Building up…
0
votes
0 answers

Can BFS reach the goal if the depth is infinite?

I have one scenario: what happen if the branch is only one and the goal node is at infinite depth? Can I actually assume that the goal node at infinite depth?
0
votes
1 answer

Does every matrix correspond to a graph conceptually?

I understand there are 3 common ways to represent graphs: Adjacency Matrix Adjacency List Edge list That said, problems I’ve solved on LeetCode often use matrices and the solution requires DFS or BFS. For example, given the matrix below, find if a…
jth_92
  • 1,120
  • 9
  • 23
0
votes
1 answer

My BreadthFirstTraversal implementation giving segmentation fault

I was trying to implement BFS on my own and wrote some code for it. I ran the code through debugger and all iterations of the while loop ran fine except the last one. When the last pointer is inside the queue (buffer) , the if condition : if…
0
votes
1 answer

How to perform breadth first search using python3 pandas dataframes

A single row in the dataframe looks like the following: source Bubble Sort target Sorting Algorithms Visualization : Bubble Sort edge …
0
votes
1 answer

How would I jump from one component to another in this undirected graph?

Since this distance learning thing started I've really struggled to understand data structures and this question really threw me for a loop. I have absolutely no idea how to even start with the code let alone get my point across. Any help at all…
0
votes
1 answer

Why is BFS more efficient than DFS in this scenario?

tldr; You start at 3 and want to end up at 4. There is always a guaranteed path. You can only hop onto 1's. You move like a knight, m units in one direction, and n units in another, every time. What is the least number of hops to get to your…
0
votes
1 answer

how to write a program to return bfs

I want to write a JavaScript function that takes 3 arguments: an adjacency matrix in the form of a 2D array, number of nodes, and starting vertex. The function returns the BFS traversal of the graph represented by the adjacency matrix. function…
DMaC
  • 11
  • 3