×

Search anything:

#### Graph Algorithms breadth first search

Reading time: 12 minutes | Coding time: 6 minutes

Breadth-first search (BFS) algorithm is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores along adjacent nodes and proceeds recursively.

### Complexity

• Worst case time complexity: `Î˜(V+E)`
• Average case time complexity: `Î˜(V+E)`
• Best case time complexity: `Î˜(V+E)`
• Space complexity: `Î˜(V)`

### Pseudocode

``````
BFS(v):
for each vertex i
do d[i] = inf
d[v] = 0
queue q
q.push(v)
while q is not empty
u = q.front()
q.pop()
if d[w] == inf
then d[w] = d[u] + 1, q.push(w)
``````

where distance of vertex u from v is d[u].

### Implementations

Implementation of Breadth First Search algorithm in 6 languages that includes `C`, `C++`, `Java`, `Ruby`, `Python` and `Swift`.

• C
• C++
• Java
• Ruby
• Python
• Swift

### C

``````
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
//main program
int main()
{
//input no of nodes: Num
int Num;
printf("Enter number of nodes\n");
scanf("%d",&Num);
for(int i=0;i<Num;i++)
{
for(int j=0;j<Num;j++)
{
}
}
//BFS traversing
//create a queue
struct node *Queue=NULL;
//create status array and set it to ready state
int status[Num];
for(int i=0;i<Num;i++)
{
}
status[0]=waiting;
int node=NULL;
printf("BFS traversing\n");
while(Queue!=NULL)
{
//get a node from queue, display it and change status to processed
del(&Queue,&node);
printf("%d ",node);
status[node]=processed;
for(int i=0;i<Num;i++)
{
{
status[i]=waiting;
}
}
}
printf("\n");
}
//queue functions
{
return 1;
struct node* temp;
free(temp);
return 0;
}
{
{
return 0;
}
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=(struct node*)malloc(sizeof(struct node));
if(temp->next==NULL)
return 1;
temp=temp->next;
temp->data=element;
temp->next=NULL;
return 0;
}
``````

### C++

``````
#include <iostream>
#include <map>
#include <set>
#include <queue>
/* Part of Cosmos by OpenGenus Foundation */
void bfs(int start,                             // the node that we are currently at
std::map<int, bool>& visited,          // the 'visited' state for each node
std::map<int, int>& distance           // the distance from the `start` node
) {
// First, clear the distance map, so that only nodes that we can actually reach from the `start` appear in it at the end
distance.clear();
// This queue wil hold pairs of numbers of the form (nodeNumber, distance)
std::queue<std::pair<int, int>> Q;
// Add the first node with distance 0 to the queue and mark it as visited
Q.push(std::make_pair(start, 0));
visited[start] = true;
// Repeat the algorithm until there are no more ndoes left to process
while (!Q.empty()) {
// Take the node from the front of the queue and pop it off the queue
auto entry = Q.front(); Q.pop();
int node = entry.first;
int dist = entry.second;
// Put the distance to this node into the distance map (this is the minimal distance to this node)
distance[node] = dist;
// Iterate over the neighbours of the current node
for (auto neighbour : adjList[node]) {
if (!visited[neighbour]) {
// Mark the neighbour node as visited
visited[neighbour] = true;
Q.push(std::make_pair(neighbour, dist + 1));
}
}
}
}
}
int main() {
/*
Do a breadth-first search in this graph:
1--2--4--7
| / \    |
|/   \   |
3     5--6
8--9
*/
std::map<int, bool> visited;
std::map<int, int> dist;
// Set all nodes as unvisited at first
for (int i = 1; i <= 8; i++) visited[i] = false;
// Perform a bfs from the node `1`
// Print the distance to all visited nodes
for (auto entry : dist) {
std::cout << "Distance from 1 to " << entry.first << " is " << entry.second << std::endl;
}
return 0;
}
``````

### Java

``````
// Java program to print BFS traversal from a given source vertex.
import java.io.*;
import java.util.*;
// Part of Cosmos by OpenGenus Foundation
// This class represents a directed Graph using adjacency list representation
class Bfs
{
private int V;   // No. of vertices
// Constructor of the class
Bfs(int v) {
V = v;
for (int i = 0; i < v; ++i) {
}
}
// Function to add an edge into the Graph
}
// prints BFS traversal from a given source s
void BFS(int s) {
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[V];
// Create a queue for BFS
// Mark the current node as visited and enqueue it
visited[s]=true;
while (queue.size() != 0) {
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print( s + " ");
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it visited and enqueue it
while( i.hasNext()) {
int n = i.next();
if ( !visited[n] ) {
visited[n] = true;
}
}
}
}
// Driver method to
public static void main(String args[]) {
Bfs g = new Bfs(4);
System.out.println("Following is Breadth First Traversal "+"(starting from vertex 2)");
g.BFS(2);
System.out.println();
}
}
``````

### Ruby

``````
#   This class represents a node in a graph.
#   It has a NAME and an ID and each node knows which nodes it is connected to.
class GraphNode
require 'set'
@@id = 0
def initialize(name)
@name = name
@id = (@@id += 1)
@connections = Set.new
end
def connect(node)
return false unless node.is_a? GraphNode
@connections << node
end
def equal?(other)
return false unless other.is_a? GraphNode
@name == other.name && @id == other.id
end
def to_s
"<GraphNode:#{name}>"
end
def to_str
to_s
end
end
#   A simple function as a default function performed for each node in bfs()
def goalNode?(node)
node.name == 'Goal'
end
#   Given a start node and a function or code block this performs a bread first search and will exit upon function or code block returning true, or returns :goal_node_not_found when it has visited the whole graph.
#
#   visited is a set containing all nodes that has been checked.
#   frontier is a queue were new nodes are appended, making this a breadth first search.
def bfs(start, goal = :goalNode?)
require 'set'
visited = Set.new
frontier = []
frontier << start
until frontier.empty?
current = frontier.shift
next if visited.include? current
visited << current
found = false
found = if block_given?
yield(current)
else
send(goal, current)
end
if !found
current.connections.each do |node|
frontier << node
end
else
return current
end
end
:goal_node_not_found
end
#   A small test that will use bfs() for root node First, then for the Second node.
#   The bfs accepts a code block, and here we just print each visited node and will never find the goal-node.
#
#   The tests output:
#   Testing from root node 'First'
#     <GraphNode:First>
#     <GraphNode:Second>
#     <GraphNode:Third>
#     <GraphNode:Fourth>
#     <GraphNode:Fifth>
#     <GraphNode:Goal>
#     <GraphNode:Sixth>
#   goal_node_not_found
#   Testing from second node 'Second'
#     <GraphNode:Second>
#     <GraphNode:First>
#     <GraphNode:Fourth>
#     <GraphNode:Fifth>
#     <GraphNode:Third>
#     <GraphNode:Goal>
#     <GraphNode:Sixth>
#   goal_node_not_found
def test
puts 'Building Graph for tests...'
#         Simple graph
#
#            First
#           /     \
#       second    third
#      /     |    |    \
#   fourth fifth  goal sixth
#
# Construct graph
first = GraphNode.new 'First'
second = GraphNode.new 'Second'
third = GraphNode.new 'Third'
fourth = GraphNode.new 'Fourth'
fifth = GraphNode.new 'Fifth'
sixth = GraphNode.new 'Sixth'
goal = GraphNode.new 'Goal'
first.connect second
first.connect third
second.connect first
second.connect fourth
second.connect fifth
third.connect first
third.connect goal
third.connect sixth
fourth.connect second
fifth.connect second
goal.connect third
sixth.connect third
# Perform tests
puts "Testing from root node 'First'"
puts(bfs(first) { |node| puts '  ' + node })
puts "Testing from second node 'Second'"
puts(bfs(second) { |node| puts '  ' + node })
end
test if \$PROGRAM_NAME == __FILE__
``````

### Python

``````
""" Part of Cosmos by OpenGenus Foundation"""
import collections
"""
Wrapper function for the print function.
Used as the default visitFunc for bfs
"""
def visitPrint(i):
print(i)
"""
A class representing a undirected graph of nodes.
*This class assumes all edge weights are equal
"""
class Graph:
def __init__(self):
"""
Given a 'start' node and a 'graph', call visitFunc
sequentially on the current node, and then its children
and so forth.
When visiting each node, mark it as visited by adding it to the hashmap.
Then queue up all of its children to be visited next.
"""
def bfs(start, graph, visitFunc=visitPrint):
visited = collections.defaultdict(bool)
queue = collections.deque()
queue.append(start)
while(len(queue) > 0):
current = queue.popleft()
if (not visited[current]):
visited[current] = True
visitFunc(current)
queue.append(neighbor)
# Testing the breadth first search implementation
if __name__ == "__main__":
# Testing on this tree
#      1
#     / \
#    /   \
#   2     3
#  / \   / \
# 4   5 6   7
g = Graph()
print("Test 1:")
bfs(1, g)
print("\nTest2:")
bfs(2, g)
"""Output:
Test 1:
1
2
3
4
5
6
7
Test2:
2
1
4
5
3
6
7
"""
``````

### Swift

``````
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var queue = Queue<Node>()
queue.enqueue(source)
var nodesExplored = [source.label]
source.visited = true
while let current = queue.dequeue() {
for edge in current.neighbors {
let neighborNode = edge.neighbor
if !neighborNode.visited {
queue.enqueue(neighborNode)
neighborNode.visited = true
nodesExplored.append(neighborNode.label)
}
}
}
return nodesExplored
}
``````

### Applications

• Shortest Path and Minimum Spanning Tree for unweighted graph
• Find neighboring nodes like in Peer to Peer Networks, Crawlers in search engine, potential friends in social networks, GPS navigation system, broadcasting in network and many others.
• In Garbage Collection: Breadth First Search is used in copying garbage collection using Cheneyâ€™s algorithm.
• Cycle detection in undirected graph
• Fordâ€“Fulkerson algorithm
• To test if a graph is Bipartite
• Path Finding

#### Alexa Ryder

Hi, I am creating the perfect textual information customized for learning. Message me for anything.

Improved & Reviewed by: