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
9
votes
2 answers

How to functionally generate a tree breadth-first. (With Haskell)

Say I have the following Haskell tree type, where "State" is a simple wrapper: data Tree a = Branch (State a) [Tree a] | Leaf (State a) deriving (Eq, Show) I also have a function "expand :: Tree a -> Tree a" which takes a…
wen
  • 3,782
  • 9
  • 34
  • 54
9
votes
4 answers

Complete graph with only two possible costs. What's the shortest path's cost from 0 to N - 1

You are given a complete undirected graph with N vertices. All but K edges have a cost of A. Those K edges have a cost of B and you know them (as a list of pairs). What's the minimum cost from node 0 to node N - 1. 2 <= N <= 500k 0 <= K <= 500k 1 <=…
9
votes
1 answer

Fast implementation of queues in Lua?

I am making a game using Lua and I need to use Breadth-first search to implement a fast path-finding algorithm which finds the shortest path between the enemy AI and the player. I will have up to 3 enemies using this algorithm at once and map is a…
Shashank
  • 13,713
  • 5
  • 37
  • 63
9
votes
2 answers

Maze solving with breadth first search

Can someone please explain how could I solve a maze using breadth first search? I need to use breadth first search to find shortest path through a maze, but I am so confused. This is the pseudo code from my book: void breadth_first_search(tree T) { …
user2348201
  • 93
  • 1
  • 1
  • 3
8
votes
1 answer

Where should I modify my breadth first search algo for finding the shortest path between 2 nodes?

I am taking a graph algo course, i am stuck with this problem of finding the shortest path between 2 vertices. The problem statement : Given an un-directed graph with n vertices and m edges and two vertices u and v, compute the length of the…
Ar7
  • 125
  • 4
8
votes
2 answers

Implementing DFS and BFS for binary tree

I'm trying to traverse a binary tree using depth first traversal and breadth first traversal, but I'm running into trouble. My node and tree implementation seems to be fine, I'm just not sure how to properly traverse the tree depth-wise and…
123
  • 8,733
  • 14
  • 57
  • 99
8
votes
8 answers

Level Order Traversal of a Binary Tree

void traverse(Node* root) { queue q; Node* temp_node= root; while(temp_node) { cout<value<left) q.push(temp_node->left); if(temp_node->right) …
brett
  • 5,379
  • 12
  • 43
  • 48
8
votes
1 answer

Breadth First Search in Prolog

I'm new to Prolog and currently implementing DFS (depth-first search) and BFS (breadth-first search) algorithms. My DFS works fine as the code below, but the BFS is terminated and aborted when it reaches the leaf node (it doesn't backtrack and…
8
votes
3 answers

BFS, DFS searches required to mark as Visited for trees?

Looking at the BFS and DFS algorithms they seem to mark the nodes as visited. If I am navigating trees only is it still necessary for my implementation to mark nodes as visited or not? I want to perform some action on every node exactly once. It…
roverred
  • 1,841
  • 5
  • 29
  • 46
8
votes
1 answer

Reconstruct a graph from BFS output in Haskell

I want to reconstruct the incidence structure of a graph in Haskell, which is given by the output of a breadth first traversal of it. Explicitly, the output consists of a root vertex and a list of neighborhoods (a neighborhood is a list of vertices…
Dune
  • 293
  • 1
  • 10
8
votes
3 answers

Sparse Graph Implementation & Performance in C++

I'm currently working on a directed graph data structure in C++ (no Boost GL for this project). The primary application will be identifying connected components and sinks. The graphs are expected to be sparse (E ~ 4V upper limit on num edges) and…
8
votes
2 answers

How to traverse graph in boost use BFS

I have problems getting to compile the BFS of a very simple graph. Whatever I do I get various compiler messages about unmatched method calls (I've tried boost::visitor and extending boost::default_bfs_visitor etc.) #include #include…
Maciej Piechotka
  • 7,028
  • 6
  • 39
  • 61
7
votes
2 answers

Why aren't my Words connecting in Undirected/Unweighted Graph when using BFS?

Issue in my code, not sure why NO connections are found in the Graph built with the words. ArrayList words = new ArrayList(); words.add("hello"); words.add("there"); words.add("here"); words.add("about"); Graph g =…
user14368465
7
votes
3 answers

Building a Binary Tree (not BST) in Haskell Breadth-First

I recently started using Haskell and it will probably be for a short while. Just being asked to use it to better understand functional programming for a class I am taking at Uni. Now I have a slight problem I am currently facing with what I am…
7
votes
1 answer

Can Dijkstra's Algorithm work on a graph with weights of 0?

If there exists a weighted graph G, and all weights are 0, does Dijkstra's algorithm still find the shortest path? If so, why? As per my understanding of the algorithm, Dijsktra's algorithm will run like a normal BFS if there are no edge weights,…
danielschnoll
  • 3,045
  • 5
  • 23
  • 34