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

Trying to implement breadth first search from text file in python

So I was able to implement breadth first search like this. But Now I am trying to implement breadth first search from a text file that looks like this. Textfile: 1,1 1,1 4,1 4,5 5,1 This is the code: from collections import defaultdict class…
0
votes
0 answers

Improving this recursive algorithm for efficient path finding

I've been trying with limited success to find a more efficient way of discovering all paths along a tree - from multiple roots to multiple leaves. I had it working somewhat using mutable strings and snipping the heads and tails (like a blockchain)…
Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
0
votes
1 answer

how to get all neighbors of all nodes by using BFS in python

I have a graph: G = {'A': set(['B', 'C']), 'B': set(['A', 'D', 'E']), 'C': set(['A', 'E]), 'D': set(['B']), 'E': set(['B', 'C']), 'F': set(['G'])} I want to get all the distinct neighbors'combinations of each nodes in this…
velvetrock
  • 593
  • 1
  • 8
  • 18
0
votes
1 answer

Breadth First Search Infinite Loop

I'm creating the 8 puzzle AI game in bfs and it always ends up in an infinite loop , i'm using a queue to store the explored (not yet visited nodes) , and a list to store the explored and visited nodes to avoid visiting the same state multiple times…
0
votes
0 answers

Keep track of depth level of BFS in undirected graph

I found some answers on BFS and binary trees. Any ideas what is the good way to do it in general undirected connected graph? My final goal is determine whether a vertex is in the cycle of length less than 5, so I want to do 5-deep BFS from that…
0
votes
2 answers

Find all paths in a Tree represented as Graph, From any node to any node. ( Only in downward direction )

Given edges Edges: { {1,2}, {1,3}, {3,4}, {3,5}, {5,6}}; Find all possible paths from any node to any node ( Note moves only downwards from root node ). Expected…
0
votes
0 answers

breadth first search for characters instead of integers c++

I am trying to create a menu driven program to produce a depth first search and breadth first search for a graph. I was able to get it to work for integers but I need it to do the same for characters or strings instead of integers. I keep getting a…
0
votes
1 answer

My code exceeds the time limit. How can I make my code more optimized?

My code exceeds the time limit at test case 23. I have used dfs to traverse the connected houses. I did everything I could to optimize from my side. Kattis problem: where's my internet? import java.util.Scanner; import java.util.HashMap; import…
0
votes
0 answers

We have an NxM grid there are two animals one is ‘Horse’ and another is ‘Bishop’ which has different moving abilities

Please help me to solve this problem efficiently and calculate its time complexity. We have an NxM grid there are two animals one is ‘Horse’ and another is ‘Bishop’ which has different moving abilities. A ‘Horse’ can move 2.5 steps and a ‘Bishop’…
0
votes
0 answers

Need feedback on my Breadth First Search algorithm between two strings

I am attempting to find the shortest path in a route between two points. My adjacency list seems to be correct but the breadth-first search seems to be messing up somewhere. If no path exists between two points in the graph, "Not found" is printed.…
M.M.
  • 19
  • 2
0
votes
0 answers

True/False: If $v$ is a leaf in every spanning tree resulting from DFS(s), then $v$ is a leaf in every spanning tree resulting from BFS(s)

Let G = (V,E) be a connected undirected graph. Let s in V be a vertex in the graph. True/False: If v is a leaf in every spanning tree resulting from DFS(s), then v is a leaf in every spanning tree resulting from BFS(s) (v != s). I assume that this…
0
votes
0 answers

DFS n-queens problem. Why my code is so slow?

Recently got an assignment on solving the n-queens problem using BFS/DFS. My BFS stops at N=7 and I think it's understandable but DFS has a problem with N=8 and goes on for a few minutes. Do you have any suggestions on how to speed it up? Also, can…
0
votes
2 answers

How to do backtracking in BFS in Java

I am trying to write some BFS algorithm in Java and read online that we should maintain something like an array prev where prev[i] = j indicates that the node we visited before i was node j. So suppose I have such an array. How do I recover the…
AndW
  • 726
  • 6
  • 31
0
votes
1 answer

Finding a cycle and saving its vertices in an undirected, unweighted graph using BFS

I've been trying to make this program save the vertices that make the cycle in the graph. But I'm kind of a newbie in algorithms and achieving that functionality seems a bit complex when using BFS. The code below successfully finds cycles, but the…
0
votes
1 answer

Running Time of BFS vs Dijkstra

I have a unweighted, connected graph G with n vertices and m edges. m=O(n log n). I want to find the shortest path from vertex s to vertex v. I want to know if a BFS traversal or Dijkstra's algorithm would be asymptotically faster. BFS would start…