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

Iteration through HashSet vs a Linked Hashset

I am thinking about ways to represent a graph in memory? I was thinking to use a hash maps of hash maps so that it will behave similar to an adjacency matrix, but we can use Comparable edge labels instead of just integers. In Breadth First Search…
mk3009hppw
  • 101
  • 1
  • 8
0
votes
1 answer

Interesting Python data structure problem involving disjoint sets, hashing, and graphs

Problem: You are planning an around-the-world trip with your two best friends for the summer. There is a total of n cities that the three of you want to visit. As you are traveling around the world, you are worried about time zones and airport…
0
votes
2 answers

How to print out the path taken by the BFS?

I'm working on a problem from Leetcode where we're given a lock combination to reach from the starting combination of 0000. You can only rotate one lock wheel at a time, and you must do it using as few turns as possible. I have my solution to do…
0
votes
1 answer

Determine if two squares on a board are contiguous/connected by squares of the same turn()

I wrote the code below as well as the isAdjacentColor method. The isAdjacentColor method returns true if two squares are adjacent (e.g. one square's queen move away from each other) and have the same color or turn(). I think I have implemented a…
0
votes
1 answer

BFS with python, can't find solution when endPoint is far from startPoint

MAIN ALGORITHM for BFS is given below. It takes a very long time when the endRow and endColumn are far from startRow and startColumn to find a path between the startPoint and endPoint in 10x10 grid. Any logical explanation on how to reduce…
0
votes
2 answers

Understanding a python breadth-first-search algorithm

I'm trying to understand this Breadth First Search python implementation and I understand most of it shown in my commenting but I don't get this line here: for dx, dy in [(-1, 0), (0, +1), (+1, 0), (0, -1)]: a, b = current[0] + dx,…
0
votes
2 answers

inserting into a queue while enumerating it

I want to do a breadth first search of a tree using a Queue var q = new Queue(); q.Enqueue(Root); foreach(T root in q) { foreach(T t in root.Children) q.Enqueue(t); } However I get a "Collection was modified after the enumerator was…
BCS
  • 75,627
  • 68
  • 187
  • 294
0
votes
0 answers

Python Graph BFS Coding Question: Generate Course Sequence

I'm having trouble with a coding question. I have attempted a solution (see below), but am stuck on one part and would like suggestions. Question: Consider an academic program that consists of N courses. Some courses have others as prerequisites.…
0
votes
1 answer

Breadth First Search Algorithm Python too slow

I tried to program an AI. I made this script for path finding with Breadth First Search but it seems to be slow. I don't how to make it faster. Please help me. By the way if you need i used this tutorial…
0
votes
0 answers

Breadth First Search in matrix from source to destination

I have a numpy 2d Matrix that look like this : [ ['' '' '' '' '' '' '' '' '' ''] ['' '' '' '' '' '' '' 'c' '' ''] ['' '' '' '' '' '' '' '' '' ''] ['' '' '' '' '' '' '' '' '' ''] ['' '' 't' '' '' '' '' '' '' ''] ['' '' '' '' '' '' '' 'c' ''…
Joy
  • 145
  • 2
  • 9
0
votes
0 answers

How to use BFS and DFS to calculate shortest path between two coordinates of grid

I have a Dictionary that contains this : grid = { (6, 7): 'cheese', (9, 2): 'cheese', (0, 2): 'mouse', (9, 1): 'cat' } My starting coordinate is : (9,1) and destination is : (0,2). I want to use BSF and DSF to calculate shortest path using…
Joy
  • 145
  • 2
  • 9
0
votes
0 answers

path between two nodes in a graph breadth first search

I am trying to write a python program that takes input as "adjacency list (or dictionary) to represent an undirected graph", "source vertex" and "destination vertex". The program should use Breadth First Search and output all possible paths from…
0
votes
1 answer

How to write BFS function in C++?

#include #include #include using namespace std; void BFS(const string&, const string[], int[][10]); int main() { const int CAP = 10; string states[CAP] = { "Arizona", "California", "Idaho", "Nevada", "Oregon",…
gly
  • 21
  • 5
0
votes
1 answer

I am getting wrong answer for prime path problem can anyone help me in finding the issue?

Problem Link: https://www.spoj.com/problems/PPATH/ Brief explanation of the problem, 1) Construct a graph with prime numbers between 1000 and 9999. 2) Add an undirected edge between two numbers 'a' and 'b', if they differ only by one digit. EX:…
Giri
  • 31
  • 5
0
votes
0 answers

Keeping Track of Nodes in BFS - Python, Maze

I am implementing a BFS on a maze represented by a 2D numpy array in Python 3.7.3. This code generates the maze: for row in range(dim): # Add an empty array that will hold each cell # in this row grid.append([]) for…
arnavlohe15
  • 332
  • 5
  • 16