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

Implementing shortest path in racket using BFS in a purely functional way?

How would we go about implementing get the shortest path between the start vertex and the end vertex? The program should return a list of edges (shortest path) using BFS. (define (new-paths path node net) (map (lambda (n) (cons n path)) (cdr…
0
votes
1 answer

Simple Graph database

I know about Neo4j, RedisGraph, DGraph, ArangoDB. But I don't want a so heavy client/server application. I just need to load 4 millions nodes and 10 millions relationships and request for the shortest path between 2 nodes. The graph in unweighted…
JeffProd
  • 3,088
  • 1
  • 19
  • 38
0
votes
1 answer

Count number of chain reactions

Suppose there is a new social page like facebook but every time someone publishes something, the publication automatically is re-published by all it's friends. This function causes a chain reaction, causing every friends of the first person's…
0
votes
1 answer

Does this BFS-based algorithm work for find shortest path in weighted graph

I know that plain BFS search can be used for find shortest path in unweighted graph or graph with same edge weight, and Dijkstra should be used in weighted graph, and Dijkstra can be seen as a variant of BFS. But I wonder if I push the node to the…
codesavesworld
  • 587
  • 3
  • 10
0
votes
1 answer

Error reconstructing path in a grid using BFS

The problem I am facing is the following: I have a function based on the BFS search algorithm that I use in a NxM grid, the mission of this function is to return the following Direction from a set of possible Directions = {Up, Down, Left , Right}…
0
votes
0 answers

My two BFS attempts to solve LeetCode 417 both encounter Errors

So I've been trying to solve LeetCode 417. Pacific Atlantic Water Flow for almost 5 hours this afternoon, and I'm now very exhausted and frustrated cuz I really have no idea why my code doesn't work. My DFS soluton passed LeetCode OJ (thank God) but…
kimmi
  • 33
  • 8
0
votes
1 answer

Level order binary tree insert with 0-children subtrees

I'm trying to create algorithm that inserts values to binary tree in level order but when the value is 0 then the node has no children Example: Elements: [7,6,4,3,0,1,1,2,0,0,0,0,0,5,0,0] How values in tree would be placed: 7 / \ …
jikix
  • 39
  • 1
  • 5
0
votes
1 answer

Why do I need to assign false to each element in BFS but not in DFS

Function definitions are : // DFS algorithm void Graph::DFS(int current) { visited[current] = true; cout<
0
votes
1 answer

Shortest path for robot moving on a grid

I have a problem with assignment. I wrote a code, but it gives wrong answer on some input (I can't see the input). However, the code works fine for all examples I tried. Here's the problem. The robot delivers mail to various corners of X. Country X…
0
votes
1 answer

C# BFS last visited node

I'm trying to get BFS working to find an optimal path on a grid system. Currently, it works until the end node is found. But no path is sent out because I'm not sure how to do this. BFS How would I record the best node to take for the path? Function…
Seitzy
  • 23
  • 1
  • 4
0
votes
1 answer

Search not working for river crossing problem in python

The problem of the river crossing description: We have a farmer, a wolf, a goat and a cabbage and they need to cross the river with the following restrictions: The wolf can’t be on the shame side with the goat The goat can’t stay at the same side…
0
votes
1 answer

How to traverse nodes efficiently in postgresql?

I'm trying to traverse nodes from the specific one with the recursive clause in PostgreSQL.(Btw I'm new to Postgresql) Here's a simple version of my db tables: table: nodes |--------------|------------------| | id | node_name …
0
votes
0 answers

Algorithm for finding least weight spanning tree of G that contains forest

I have a graph having the definition of G = (V;E) . This graph is a weighted undirected connected graph. F is a forest on the vertices V using edges in E. So I need to have an algorithm to compute a least weight spanning tree of G that contains F.…
0
votes
0 answers

Trying to implement DFS instead of BFS

i have implemented BFS to search for a mango seller. However i am about to try it with DFS now - but find it a bit hard. I have tried to to use pop() and unshift() instead, but i still get the same output. I was hoping to see different output. As…
0
votes
0 answers

Store all paths from start to end node using BFS

I have the implemetation of BFS but it stores only one path. How can I modify this code to store all paths from a starting to node to the end. Any ideas? def BFS(G, user1, user2): path = [] for v in G: …