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

Problem with BFS in graph java . It is n`t returning anything

I am making a project like google map in which I have to construct a graph in which vertices are intersections and edges are roads . In my code I was able to add intersections and roads but I`m not able to return a path through breadth first search…
0
votes
1 answer

Breadth-First Search vs A * Algorithm - Real World Example

I'm looking for a real-world example (by which I mean a software solution for a real-world problem), where the A* search algorithm is used because it radically outperforms Breadth-First Search for the same task. Suggestions please?
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
0
votes
2 answers

Traverse a tree depth-wise instead of branch-wise

I have a deep array of objects like {id, followers} where followers repeat the pattern: { id: 0, followers: [{ id: 1, followers: [{ id: 11, followers: [{ id: 111, followers: [...] }] }] }, { id: 2, followers: [{ …
0
votes
1 answer

Python - Coming up with a 'connections graph' of 5 ^ 5?

I'd really appreciate your help. I've been trying for a while and can't figure out how to programattically create a list where you start with one level in a list of people - MY friends- such as graph["me"] = ["alison", "bob", "candy", "duane",…
dbs5
  • 133
  • 2
  • 9
0
votes
1 answer

level order traversal of binary tree using queue (array implementation)

I am getting unexpected output from my function that print tree nodes data in level order. I was following queue implementation of level order traversal without using STL library. enter code here #include using namespace std; struct…
Lakshay
  • 1
  • 1
0
votes
1 answer

Having an issue with my Level Order Traversal function in Python

EDIT: I have overall finished the original problem. However I have a small problem. I will print out all of my code to show you all. The objective is print out all of the binary search tree parts in a list, plus every empty spot. The empty spots…
0
votes
0 answers

Program error for frog jump using breadth first search in c++

There are total of 7 spaces, 3 leftmost spaces contain a frog each of one family while the 3 rightmost spaces contain a frog each of second family. The 3 frogs on left need to be transferred to 3 rightmost positions while 3 frogs on right need to be…
0
votes
0 answers

Find all paths between source and destination using threads

How can I find all the paths between its source and destination using thread (for source_node) and thread (for other_nodes) and queue sharing in Python (every node send mesg (path content) for his neighbors until find destination et. Finaly…
0
votes
1 answer

BFS solution giving wrong answer to network time problem

There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target. Now,…
0
votes
1 answer

What if I just dont visit the children of the queue_front node but just push them into the queue? Will it still be BFS?

https://www.hackerrank.com/challenges/ctci-bfs-shortest-reach/problem In this problem, I tried BFS by just (first approach) visiting the queue_front node and pushing its children into the queue and not visiting its children nodes instead of (second…
0
votes
1 answer

Floating point exception during Breadth First Search

Everything else seems to work just fine, but it returns a Floating point exception during execution of BFS in this graph. Earlier, the program worked fine during debugging, BFS worked successfully but crashed during runtime. And now, that too isn't…
Rupesh
  • 81
  • 7
0
votes
1 answer

find shortest path between 2 vertices using BFS algorithm

I have this question for homework: prove/disprove: A. Given an unintentional bound graph G (V, E) and a minimum spanning tree for this graph, and 2 vertices u, v, the shortest path between u and v in graph G can be found by performing BFS on the…
0
votes
0 answers

Fifteen puzzle, BFS vs DFS difference

I would like to double-check my answer to a question from a textbook, as I'm still new to the concept of the Depth-first search and the Breadth-first search. The question regards the Fifteen puzzle and goes as follows: "For the given starting order…
0
votes
1 answer

Distance of nearest cell having 1

Given a binary matrix of size N x M. The task is to find the distance of nearest 1 in the matrix for each cell. The distance is calculated as |i1 – i2| + |j1 – j2|, where i1, j1 are the row number and column number of the current cell and i2, j2 are…
0
votes
2 answers

Got runtime error while using BFS algorithm

There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w. Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from…