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 shortest path and longest path in an undirected graph?

I have a general question regarding how to find shortest path and longest path in an undirected graph with simple edges where edges have no weights. Is it a correct conclusion that we need to use DFS algorithm to find the longest path in a graph,…
0
votes
1 answer

Transforming Weighted to Unweighted Graph for BFS/DFS

Are there any general or simple algorithms that convert a weighted graph into a non-weighted graph (where each edge has the same weight)? I know an algorithm called Djikstra's algorithm that works very similar to BFS on a weighted graph when finding…
0
votes
1 answer

Using breadth first search on NetworkX to find predecessors only return 1 predecessor for each node

I want to find all the direct predecessors of all nodes in a graph using NetworkX's breadth first search. Below is my code and the graph image: import networkx as nx import matplotlib.pyplot as plt nodes = ['0','1','2','3','4','5'] G =…
eng2019
  • 953
  • 10
  • 26
0
votes
0 answers

How do I reach all parts of a directed graph using breadth first search or depth first search?

Let's say we have the graph that is below and we want to print out all the vertices names that there are. How would I be able to reach vertice C if I start exploring at vertice A? I would assume we use the following implementation for a…
0
votes
0 answers

How do I find time complexity using summation?

Given Sum of (C + a_k) from k=1 to n, where C is the cost of running to return a list of edges and a_k the number of edges from from node k. How do I solve this summation to find its Theta Notation?
0
votes
0 answers

How to add nodes and leaf value in a graph using java

This is the class below to create a graph in java which I got from leetcode. How can I generate a graph(check image) using this class as after generating the graph I will debug it as I am learning Graphs, BFS? class Node { public int val; …
RajB009
  • 417
  • 2
  • 7
  • 19
0
votes
1 answer

Minimum number of steps to convert the matrix by swapping

I stumbled upon the below question and was unable to solve it, can someone tell whats the approach here
0
votes
1 answer

BFS with Constant Amount of Memory

Is it possible to do a breath first search using only (size of graph) + a constant amount of memory -- in other words, without recording which nodes have already been visited?
user562688
0
votes
1 answer

Breadth first traversal of arbitrary graph with minimal memory

I have an enormous directed graph I need to traverse in search for the shortest path to a specific node from a given starting point. The graph in question does not exist explicitly; the child nodes are determined algorithmically from the parent…
NounVerber
  • 135
  • 7
0
votes
1 answer

Maximum queue length in BFS

What will be the maximum length/space capacity of the queue when we run BFS on a tree? Is there a formula we can estimate it with? I have tried using geometric series to estimate the rate of queue expansion, but I am not sure if it is the right…
0
votes
3 answers

BFS traversal on multi-way trees?

I've seen samples, codes and references on bfs, however, all of them seems to focus on traversing through binary trees. I want to know if bfs traversal is possible for multi-way trees, if so where can I find coding references for it?
0
votes
0 answers

Is there a way to run BFS on a grid in linear time? i.e. with respect to the number of cols & rows-- O(rows*cols)

I learned that BFS has time complexity of O(E+V) on a graph. I found this SO question that mentioned that on a grid it would be O(row*col). I tried to code it using a BFS code that I found, but I think it's slower than that (since I'm checking the…
Penguin
  • 1,923
  • 3
  • 21
  • 51
0
votes
1 answer

Printing the shortest path with BFS

I am creating a maze solver which uses Breadth First Search algorithm and I'm wondering if I can somehow print the path on the grid instead of just printing the distance value it travelled in order to find the exit. Here is the code down below and…
ThuggyTM
  • 93
  • 1
  • 12
0
votes
0 answers

Find bug in my Knight's Move Python Program

Code I am trying to build a Knight's Move Program everything working fine but there is a hidden bug in this code that I cannot find. Can anybody spot the problem? from itertools import product def solution(src, dest): A = [[0, 1, 2, 3, 4, …
0
votes
1 answer

Connectivity of a graph in java

I've implemented a BFS algorithm from my textbook and I am trying to modify it to throw an exception when it discovers a non-connected graph. My BFS using an array of boolean to store if a node has been reached or not. After running the BFS from the…