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

Crossoword Visualizer Breadth first search with direction

I'm currently working on a little project of mine, a crossword Solver. Given an Input of 64 characters, an algorithm displays them on a 8x8 board and then goes on to look if any of the word match. So far I've written an algorithm that for each…
0
votes
0 answers

Problem implementing a BFS algorithm in R

I'm trying to implement a bfs breadth first search algorithm in R. I know about the graph::bfs function and do_bfs from DiaGrammer. I think my problem is in the "for" of the bfs function. The input would be a graph as the following 1 …
0
votes
1 answer

how to choose starting point in breadth first search?

In the book I am reading it tells me to choose a vertex with depth 0 but I do not understand how depth is calculated in a graph. Looking at above example, it chooses vertex A as its starting point and explains that it has depth 0. In my…
haneulkim
  • 4,406
  • 9
  • 38
  • 80
0
votes
1 answer

How to trace the path that visits all nodes in bfs/dfs

This is similar to How to trace the path in a Breadth-First Search?, but the method described in the answers in that post doesn't work for my case, it seems. By path here, I essentially mean a sequence of connected nodes to get from a beginning to…
24n8
  • 1,898
  • 1
  • 12
  • 25
0
votes
0 answers

What is the most efficient way to find the sum of all nodes' values in an n-ary tree, where each node's value is the sum of it's children +1?

Suppose we have an n-ary tree. All leaves in the tree have a value of k. All other nodes have a value of 1 + the sum of its children's values. What is the most efficient way to find the sum of the values of all nodes in the tree? Side question:…
G.Baldjiev
  • 73
  • 1
  • 5
0
votes
0 answers

How to improve DFS based maze game performance?

I have coded a game based on JS and DFS algorithm. It's similar to pacman, only both computer and the player are trying to get as many gold coins as possible. I use DFS to calculate the computer path to the coin, then using setTimeout initialize the…
0
votes
1 answer

What makes Breadth First Search algorithm slower than Depth First Search(both are shown below)?

I am using DFS and BFS to solve mouse in a maze problem where the mouse is allowed to move either towards right or upwards. Since the branching factor is just two with the maze being a 5x5 matrix i expected BFS to be way quicker than DFS but turns…
0
votes
1 answer

Getting segmentation fault while doing BFS in c++

I've started learning graphs theory and was doing a question from Hackerrank https://www.hackerrank.com/challenges/bfsshortreach/problem, which is basically asking to perform a BFS, mark all elements in same level as 6*level and mark all…
Xgh05t
  • 230
  • 2
  • 11
0
votes
3 answers

Algorithm for Finding Graph Connectivity

I'm tackling an interesting question in programming. It is this: we keep adding undirected edges to a graph, until the graph (or subgraph) is connected (i.e. we can use some path to get from each vertex to any other vertex in that subgraph). We stop…
TheLeogend
  • 99
  • 4
0
votes
0 answers

Python graph dfs, bfs solving returns runtime error

I try to solve this problem, but my code returns a runtime error. I tried with many changes, but I don't know where returns runtime error exactly. I need some advice or some special cases please... graph = {} visited = None not_visited_vertex =…
0
votes
1 answer

Short version of DFS/BFS algorithms

Constantly confronted with olympiad problems on graphs, I always wrote a rather long code for implementing DFS, and it took a lot of time to debug it. I wrote constructs like this: class Graph { int V; list *adj; public: …
Mouvre
  • 274
  • 2
  • 10
0
votes
1 answer

Ocaml: path in a graph is repeated even if this has already been found

I wrote some function to search the list of possible paths from a starting node to an ending node. The function list_of_paths correctly returns all the possible paths from a starting point to an ending point but the same path inside the list is…
0
votes
1 answer

What's wrong with this implementation of Breadth First Search?

Here is my whole program: #include #include #include using namespace std; void addEdge(vector adjList[], int u, int v) { adjList[u].push_back(v); adjList[v].push_back(u); } void bfs(int s, vector
arujbansal
  • 117
  • 2
  • 12
0
votes
2 answers

In a DAG, how to find vertices where paths converge?

I have a type of directed acyclic graph, with some constraints. There is only one "entry" vertex There can be multiple leaf vertices Once a path splits, anything under that path cannot reach into the other path (this will become clearer with some…
0
votes
1 answer

Find all paths between to vertices in a graph

I have been using the solution posted here https://www.geeksforgeeks.org/find-paths-given-source-destination/ in python and it works fairly well for several input graphs but for this particular input I am not able to generate paths. Is there a…
sudiksha
  • 55
  • 1
  • 6