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

How to find the nearest node using BFS?

Let G(V,E) be an undirected unweighted graph and r be a subset of V. Now node root is added to G and edges are added between root and all the nodes of r. Now for each node of V-r I want to find the nearest node of r using BFS. Please help. I have…
0
votes
0 answers

Valgrind error with function sizing issue?

Evening, I think misunderstanding how bfs works in a maze solver. I trying to figure out this error originating from my enqueue function. Did I allocate the array incorrectly? It may be design flaw with my maze solver. Am I accessing a possible…
0
votes
0 answers

Knight's tour in Java with rocks

I am working on the following problem: Write a method that, given a chessboard with one knight, rocks on some of the squares, and a target position, indicates whether or not the knight can reach the target without landing on any rocks, and if so,…
ppan92
  • 1
  • 1
0
votes
1 answer

Dijkstra's Algorithm complexity vs BFS complexity

I've been practicing various algorithms and I just completed Dijkstra's algorithm to calculate the shortest distance between nodes on a graph. After completing the exercise leveraging an indexed minHeap, I also completed it leveraging BFS (solution…
0
votes
0 answers

How can we implement Priority Queue instead of Queue to Breadth-First Traversal?

We know that for Breadth First Traversal we should use 'Queue'. Example: For this graph, if we start from '0', the visit order will be like this : 0 1 3 8 7 2 4 5 6. What would be the visit order if we use a priority queue? Edit: Priority Queue…
0
votes
1 answer

Unweighted directed graph distances

Let's say I have an unweighted directed graph. I was wondering if there was a way to store all the distances between a starting node and all the remaining nodes of the graph. I know Dijkstra's algorithm could be an option, but I'm not sure this…
mikcnt
  • 55
  • 5
0
votes
0 answers

What is wrong in my code for finding if a graph is Bipartite or not?

I am doing a simple BFS and marks the neighbouring elements for the vertices which comes under the current traversing vertice and if it's again found in the next vertices of the current traversing vertice then it's not a bipartite. #include…
0
votes
1 answer

BFS Maze doesn't show shortest path only

I'm currently working on a little maze solver project where you find the shortest path in a maze so as to get a better understanding of path searching algorithms, like in this example the breadth-first search algorithm. My implementation of it uses…
Codexing
  • 133
  • 1
  • 11
0
votes
1 answer

Find minimum no. of perfect squares that sums to n using BFS algo

We are given an integer n, we have to determine the least no. of perfect squares which sum to n. Example : n = 12 and perfect squares up to n are 1, 4, 9, 16, 25... up to n in this case these will be 1, 4, 9 so output will be 3 as 12 = 4 + 4 +…
0
votes
1 answer

(C++) Leetcode: why is my code so much slower than sample top solution? (547. Number of Provinces)

https://leetcode.com/problems/number-of-provinces/ I was pretty excited when I solved this problem on my very first try, within only 20/30 minutes, though when I submitted my code, I ended up in 8.43 percentile. I looked at how the fastest solutions…
haosmark
  • 1,097
  • 2
  • 13
  • 27
0
votes
1 answer

breadth first search. Can a vertex have a an adjacent vertex with a different parent and is discovered but unprocessed

When traversing an undirected graph using breadth first search that has no parallel edges and self loops, there exists a vertex x with an adjacent vertex y. When processing vertex "x" it sees that y is already discovered/visited and has a…
bsobaid
  • 955
  • 1
  • 16
  • 36
0
votes
0 answers

Breadth-first search algorithm running an infinite loop in Python

I am trying to create a maze with the breadth-first search algorithm in python. However, my program is going into an infinite loop. It should check each space, up, down, left, and to the right of it. Then, that space gets put in the visited list.…
James
  • 83
  • 1
  • 6
0
votes
1 answer

Defining key of map as custom struct when implementing graph in C++

I am learning some graph algorithms and decided to implement a graph as an adjacency list. I am not very comfortable with C++ and I think I am making some syntactic mistakes on how to go about creating a graph represented by a map, with vertex as…
0
votes
1 answer

How i make bfs more efficient in cpp

I have problem in unweighted undirected graph the question ask if any path between two point in binary matrix, the user insert the matrix and the cell in matrix to test bool BFS(vector>mat, Point src, Point dest,int n,int m) { …
0
votes
0 answers

Multi-source BFS with traveling graph problem with expanding cells

You are given a M x M grid (7 <= M <= 1000) where every cell can either be occupied or is inaccessible. You are also given the locations of several starting points where you can start moving from. You can start at any starting location. In one unit…