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
2 answers

How to search up-down-left-right in a 1D array Java

I am working on a 3x3 puzzle slider game which uses breadth first search to find the optimal solution from the initial_state to goal_state. At the moment, the gamestate method "possibleMoves()" does not search up, down, left and right. It only…
Avan e
  • 1
  • 3
0
votes
1 answer

Find explicitly islands in matrix

I am given a matrix of 0 and 1's and have to find the islands formed by one's. If found a reference : https://www.careercup.com/question?id=14948781 About who to compute the number of island but don't know at all how to adapt the algorithm to at the…
11house
  • 77
  • 7
0
votes
1 answer

Implementing parallel breadth first search using openmp

I want to implement parallel breadth first traversal using openmp. I read Parallelizing a Breadth-First Search. I am just trying to print the breadth-first traversal. But the code in the link provided above has almost all the traversal code in the…
0
votes
1 answer

I think my BFS adds all valid coordinates to list, not just shortest path

So I'm working on a BFS algorithm to find the shortest path in a 2d binary maze and print the path in the form of coordinates. The code is running, but there must be some mistakes somewhere. Basically the maze coordinates have either true or false…
DonnumS
  • 196
  • 1
  • 15
0
votes
1 answer

Why is the space complexity of depth-first search not expressed as O(n)?

Relatively new to algorithms, so please forgive if I've missed something obvious! I know that the space-complexity of depth-first search is commonly expressed as O(h), where h is the height of the tree, whereas breadth-first search's complexity is…
Bex
  • 27
  • 2
  • 6
0
votes
1 answer

List of children nodes in Node is lost during BFS traversal in C++

I am writing a piece of C++ code to perform a Breadth First traversal of a directed graph. In the main function I define a total of 7 nodes and establish the connections among them. One Node is a struct which contains a name, a value and a list of…
ismarlowe
  • 119
  • 2
  • 13
0
votes
1 answer

How to find all nodes distance k away from each other in directed graph (exploring every edge in a graph)?

0 I am working on a problem where I need to find all nodes at distance k from each other. So if k=3, then I need to find all nodes where they are connected by a path of distance 3. There are no self edges so if i has an edge pointing to s, s can't…
Citut
  • 847
  • 2
  • 10
  • 25
0
votes
1 answer

Breadth first search stuck in infinite loop

I am trying to implement breadth first search algorithm but due to some reasons it is getting stuck in infinite loop. I have tried debugging it by outputting various values but still no success. My Bfs Code int s=0; bool vis[n]; int…
0
votes
1 answer

Searching for the biggest area inside 2D matrix

so I've a algorytmical problem where i need to find the biggest area of a certain type of pixels inside a 2D matrix with following conditions: Each pixel can be connected either diagonally or adjacently. The area is considered coherent only if it…
nvio0
  • 9
  • 1
0
votes
0 answers

Why is my equals statement not working for my breadth first search

I am writing a program that is solving a 8 tile puzzle but I am not getting any out put, I am running the debugger and I am generating the right successors, one of which is the goal state that I want but when I compare them it does not say they are…
max
  • 13
  • 3
0
votes
2 answers

How can I access member functions of STL classes inside derived classes that aren't in the base class? (detailed explanation in body)

Right now I have a base class, class Base{}, with two classes deriving from it, BFS{} and DFS{}. BFS has queue, and DFS has stack, so they both have a member called "nodes", but the type is their respective std::queue and std::stack. My search…
0
votes
1 answer

In which circumstances would I want to run BFS or DFS instead of IDDFS?

Question is about tree search. I believe I understand the differences between DFS, BFS, and IDDFS. In regards to Optimality, Completeness, Time Complexity, and Space Complexity IDDFS has a better performance for tree searches. So, when would I want…
0
votes
1 answer

Can anyone explain me how this BFS code is working?

I am new to algorithms and data structures. This code is from the class I missed and now I am having difficulty understanding this. I could not understand what is happening after it asked for the initial vertex. Below is the…
Sayaji
  • 127
  • 1
  • 1
  • 8
0
votes
0 answers

Algorithm for finding a source equidistant from multiple destinations

Problem Statement: A Research team wants to establish a research center in a region where they found some rare-elements.They want to make it closest to all the rare-elements as close as possible so that they can reduce overall cost of research over…
argcv
  • 39
  • 6
0
votes
1 answer

How to index a large number of permutations?

I'm trying to code a program that finds for the God's Algorithm that solves a particular puzzle -- shortest sequence that leads current state until its solved state. To do this, I have to map all permutations/states (nodes) with its respectively…
1 2 3
99
100