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

Maximum path in a grid with obstacles, going right, left or down

Given a grid of m*n where each cell contains either 0 or 1. 0 means obstacle, you cannot pass through a cell with 0. We have to find a path from (0,0) to (m,n) that has the maximum number of ones. Allowed moves are right, left, bottom(down). If…
0
votes
0 answers

BFS and DFS by using adjacency matrix C#

How to implement BFS (Breadth First Search) and DFS (Depth First Search) using an adjacency matrix if the vertices in the graph are specified in string format (not integer)? Available code: https://github.com/surbacim/graph-c-/blob/main/graph
Adam
  • 1
  • 1
0
votes
1 answer

Two implementation methods of BFS for finding the shortest path, which one is the obvious winner?

There are two ways of implementing BFS to find the shortest path between two nodes. The first is by using a list of lists to represent the queue of paths. Another is to maintain a mapping from each node to its parent, and when inspecting the…
0
votes
1 answer

Why I get this error using TSQL to find a connected graph?

nodes(nodeID int) is a list of all nodes. edges(fromNodeID int, toNodeID int) is the relationship between two nodes. Assume the graph is undirected, find all the connected components. CREATE PROCEDURE getGraph AS BEGIN CREATE TABLE…
0
votes
0 answers

given a not oriented graph G, find an algorithm that verify if is possible to partition the set of nodes into two sets V1 C V and V2 C V (continue)

given a not oriented graph G=(V,E) represented with adjacency list,find an algorithm(that solve the problem in linear time) that verify if is possible to partition the set of nodes into two sets V1 C V and V2 C V with this condition: V1 U V2 = V V1…
0
votes
1 answer

Longest distance from a random vertex to the end points of the longest path

I have a tree-like graph/structure with the vertices as cities and let's say that the longest path is from 's' and 't'. Now, I want to prove that the longest path from any other city (let's say 'w') with end at either s or t. What I am thinking…
0
votes
1 answer

I modified BFS to find shortest path in weighted undirected graph instead using Dijkstra's algo and it worked

To find shortest path in undirected weighted graph I was comparing BFS and dijkstra's algo to understand why we need priority queue. I wrote some code modifying BFS to find the shortest path to all nodes in a given graph. Problem link :-…
0
votes
2 answers

Graph problem for checking whether a permutation A of 1 to N number can be converted to a permutation B if we are given good pairs

I am not sure whether this is an appropriate question to post on this platform. The problem statement is present here: https://www.interviewbit.com/problems/permutation-swaps/ Other than that, here's a short summary of this graph-based…
0
votes
0 answers

Seeking advise on create a maze to solve with BFS in Python

Seeking advise on how structure python to create a maze, solve it with BFS and to have basic navigation within maze with number of moves required to navigate. Use move along the path, Up, Left, Right, Down. Below is some code that I mangled together…
0
votes
1 answer

Apply BFS, with goal node what is the path?

I is the starting node and E is the goal node. What will be the path that would be return by BFS? I know this is easy the path that I found is I-H-E but my teacher is saying that the correct path is I-F-H-C-E. So I really don't know how did she…
0
votes
1 answer

Tree and Graph on same set of vertices

Suppose you are given an undirected graph G and a tree T on the same set of vertices. We would like to know whether it is possible to have an adjacency list representation of G (note that there are multiple options here as the adjacency list can…
0
votes
1 answer

How to wait after an iteration of a while loop in Java Swing

I'm programming a path finder visualizer with Kotlin and Java Swing and I have this Breadth-First Search function: fun bfs(): MutableList? { Grid.resetNodes() val queue: Queue = LinkedList() queue.add(Grid.start!!) …
0
votes
1 answer

Grid nearest neighbour BFS slow

Im trying to upsample my image. I fill the upsampled version with corresponding pixels in this way. pseudocode: upsampled.getPixel(((int)(x * factorX), (int)(y * factorY))) = old.getPixel(x, y) as a result i end up with the bitmap that is not…
JRazek
  • 191
  • 2
  • 11
0
votes
2 answers

How to use a queue to count the number of graph components?

I have the queue functions and I have to use them to count the number of graph components. These are my functions: struct TQueue { int value; TQueue * next; }; void QueueInit(TQueue * & a_head, TQueue * & a_tail) { a_head = NULL; …
ZlyVlk
  • 29
  • 7
0
votes
1 answer

Breadth - first traversal only returns one node

I am learning about Breadth-first traversal in python. I have had a problem when writing this code. ''' from collections import deque class Tree: def __init__(self,data): self.data = data self.right = None self.left =…
jacobdavis
  • 35
  • 6