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

BFS algorithm in Python (return list with distances to other nodes)

I have a problem such that I need to implement a BFS method and return a list with the distances to other nodes, given the start node and an adjancecy matrix, as follows: import numpy as np import sys example.matrix = np.matrix('0 1 0 1; 1 0 0 0 ;…
0
votes
1 answer

Finding a Minimal set of vertices that satisfy the given constraints

Note: no need for formal proof or anything, just the general idea of the algorithm and I will go deeper myself. Given a directed graph: G(V,E), I want to find the smallest set of vertices T, such that for each vertex t in T the following edges don't…
user15862679
0
votes
1 answer

I am trying to do BFS search but get Runtime Error Index out Of Bounds Exception

Given a directed graph, the task is to do Breadth First Traversal of this graph starting from 0. Complete the Function bfsOfGraph() to return Breadth First Traversal of given graph. Here, V denotes the number of vertices. Here's the problem…
0
votes
0 answers

Expected size for queue in a breadth-first search algorithm

One of the most common and simple operations executed on unweighted graphs is the Breadth-first search. An aspect of the algorithm that is left to the practical implementation is how to implement the queue and especially, what capacity it should…
Luca Cappelletti
  • 2,485
  • 20
  • 35
0
votes
1 answer

finding a functional path through a graph with a twist

I was wondering whats the best way to go about finding a path through a simple, undirected graph where the path must go through nodes that contain a specific attribute. The allowable gap between the nodes that contain the said attribute can be…
0
votes
1 answer

Implementing a graph class

I'm having problems with the BFS method. When I call it shows me the error "cannot read 'length' of undefined" if I try to use the dequeue method on other queues it works, and it returns the node correctly. I also tried to use a number instead of…
0
votes
1 answer

Creating an array of distances from a starting vertex in a graph in java

I'm having trouble implementing a method to create an array of distances using BFS from a chosen starting vertex, it currently seems to work in some cases, but fails in larger graphs. The idea is each index in the array represents the corresponding…
0
votes
1 answer

BFS that returns shortest path on a 2nd array /unwighted graph

I know this has been asked before but I can't seem to figure out the solution from the examples and translate them to javascript. not even when following : https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm I have an unweighted graph or 2d array…
Shaked Tayeb
  • 112
  • 9
0
votes
2 answers

MPI sending 2D array

I am new to MPI. I am trying to assign an adjacency matrix to the processes so that I am able to implement the 1-D BFS algorithm given by https://ieeexplore.ieee.org/document/1559977. Assume that I have a 6*6 matrix like that with 4 processes: 0 0 0…
0
votes
1 answer

Maze pathfinding implementation (BFS) not giving correct path

I am trying to get the shortest path for a maze with a ball: the ball is rolling until it hits a wall. I use Dijkstra's algorithm using heapq for priority queue. However, I get a non-optimal path as result. Here is my code with sample input: maze =…
feedy
  • 1,071
  • 5
  • 17
0
votes
1 answer

I cannot understand BFS Tree Edges in Algorithm Design Manual

I can't understand what this mean The graph edges that do not appear in the breadth-first search tree also have special properties. For undirected graphs, non-tree edges can point only to vertices on the same level as the parent vertex, or to…
0
votes
1 answer

Breadth first search solution path

I had a question regarding BFS. After expanding nodes whether it be a graph or a tree what path will BFS take as the solution from the starting point to the goal? Does it take into account the cost of moving from one node to the other and takes the…
J458
  • 5
  • 3
0
votes
2 answers

Python BFS program not returning grid and path, on maze with obstacles and solutions

I have imported a text file with numbers as the following example: 3 0 0 0 0 1 0 0 3 3 3 0 3 0 0 0 0 0 3 3 3 0 3 0 0 0 0 0 3 3 3 0 3 0 0 0 0 0 3 3 3 0 3 0 0 0 0 0 3 3 3 0 3 0 0 0 0 2 3 3 3 0 3 0 0 0 0 3 3 3 3 0 3 0 0 0 0 3 3 3 3 0 3 2 2 0 0 3 3 3 3…
user15529787
0
votes
1 answer

Time Limit Exceeded: BFS

I need to output the shortest directions from 'P' to 'G'. I have used BFS for to implement it. The problem is as shown below: Input: 'N' dimension maze '#' - Wall 'G' - Ghost 'P' - Pacman //Input Maze 8 ######## # # # # ## # # # #G# #P …
0
votes
1 answer

Level Order Insertion in Binary Tree C#

Suppose we are given an array [20,8,22,5,3,4,25,null,null,10,14,null,null,null,null] and we want to construct a Binary Tree from it using Level Order , how can we do this in c# since it doesn't allow null int.
knowledgeseeker
  • 1,163
  • 4
  • 15
  • 32