BFS or breadth first search is one of the most commonly used algorithms to search for an item in non-linear data strcutures like trees and graphs.
Breadth-First Search algorithm for graphs (nonlinear data structures) is based on the idea of searching for an element across all the adjacent nodes of the current
node before traversing their respective neighboring nodes discovered along the way.
Consider the following illustration:
Assuming the start node to be the one with value 1
, the nodes that the algorithm will next traverse will be 2
and 4
(in any order) before their respective adjacent nodes, like 3 or 5, can be traversed.
An additional data structure (Queue) is required to temporarily hold intermediate nodes to block their processing until all the prior nodes in the sequence are processed. On a high level, BFS the algorithm can be represented as a sequence of the following steps:
Q
with the start node. This will hold all the discovered but not visited nodes.V
as a boolean array of size equal to the number of vertices.Q
is not empty, dequeue its head element
.element
is equal to the key
we are looking for.element
as visited in the V
array.adj
of element
:
adj
is not visited already in the V
and is not discovered in Q
, add it to Q
.Let’s try this algorithm for the given input for key 3:
In the next step, when we dequeue 3
from the head of the discovered queue, it will pass the comparison test and is returned to denote the presence.
public int search(int[][] graph, int start, int key){
boolean[] visited = new boolean[graph.length];
Queue<Integer> discovered = new LinkedList<>();
discovered.add(start);
while(!discovered.isEmpty()){
Integer visitedNode = discovered.poll();
if(visitedNode == key){
return visitedNode;
}
visited[visitedNode] = true;
// for each neighbor, check if the two vertices are connected
// if yes, and the neighbor is not visited already
// add it to the discovered list
for(int neighbor = 0; neighbor < graph.length; neighbor++){
// graph[visitedNode][neighbor] != 0 is neighbor check
// !visited[neighbor] - is not be visited already
// !discovered.contains(neighbor) - is not discovered already via some other node
if(graph[visitedNode][neighbor] != 0 && !visited[neighbor] && !discovered.contains(neighbor){
discovered.add(neighbor);
}
}
}
// return -1 to denote absence
return -1;
}
The overall complexity of this algorithm is dependent on the underlying data structure used to represent the graph:
Be notified of new posts. Subscribe to the RSS feed.