0% found this document useful (0 votes)
78 views

Breadth First Search

Breadth First Search (BFS) is a graph traversal algorithm that begins at a starting node and explores all neighboring nodes. It uses a queue data structure to keep track of nodes to visit at each level, visiting all nodes at the current distance from the start before moving to the next level out. The result is a search tree showing the order all nodes are visited with their distances from the start node.

Uploaded by

VISHAL MUKUNDAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Breadth First Search

Breadth First Search (BFS) is a graph traversal algorithm that begins at a starting node and explores all neighboring nodes. It uses a queue data structure to keep track of nodes to visit at each level, visiting all nodes at the current distance from the start before moving to the next level out. The result is a search tree showing the order all nodes are visited with their distances from the start node.

Uploaded by

VISHAL MUKUNDAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Breadth First Search (BFS)

The Breadth First Search or BFS is a graph traversal technique. It can be used to
search for an element in a graph. The search starts at a randomly selected start
node of the graph. Each edge of the graph is considered to be of unit (1) length.
At first the nodes at unit distance from the start node are explored, followed by
exploration of nodes at the next level (or at distance 2 units from the start node)
and so on. The result of Breadth First Search will be a search tree which covers all
the nodes of the graph. There can be more than one BFS traversals beginning
from a start node depending on the order in which the nodes have been selected
at each level. It is a breadthwise exploration of a given graph.

Algorithm BFS(G,s)
Input: Graph G, Start node s
1 for each vertex v ɛ V[G] do
2 explored[v]  False
3 dist[v]  ꝏ
4 pred[v]  Nil
5 end for
6 explored[s]  true
7 d[s]  0
8 Q:= a queue data structure, initialized with s
9 while Q ≠ Ø do
10 u  remove vertex from the front of Q
11 for each v adjacent to u do
12 if not explored[v] then
13 explored[v]  True
14 dist[v]  dist[u] + 1
15 pred[v]  u
16 insert v to the end of Q
17 end if
18 end for
19 end while
In the above algorithm “explored” is used to keep track of the nodes in the graph
that are explored or visited. Initially all the nodes in the graph are marked as not
explored using the value “False” or “F”. When a node is visited or explored the
corresponding entry in “explored” is marked as “True” or “T”. The data structure
“dist” stores the distance of each node from the starting node. Each edge in the
graph is taken as unit (1) distance. It is initialized to infinity (ꝏ) for all the nodes in
the graph.The data structure “pred” stores the predecessor (a node coming
before a given node in the exploration path from the start node) of a node during
breadth first search. The queue data structure “Q” is used to store the nodes
visited during breadth first search.

Example:

BFS can be started from any node in the graph.

A queue is used to perform BFS

Let’s start from node ‘C’

We use an array “explored”. The size of the array will be equal to the number of
nodes in the graph G. This array indicates which all nodes in the graph has been
visited or explored as part of BFS. Initially all the nodes are marked as not yet
explored indicated by “false” or “F” in the array. The index of the array denotes
the node in the graph.

For the above given graph the content of “explored” array initially is as shown
below,

explored:

A B C D E F G H I J K L M N O P Q R
F F F F F F F F F F F F F F F F F F

dist:

A B C D E F G H I J K L M N O P Q R
ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R

A queue Q is also used, initially we will enqueue node ‘C’ the starting node (it can
be any random node) into the queue.

Q:

front rear

Next we output node ‘C’ and mark the node as explored in the array at index C
with “T” or “true”.

Output: Mark node ‘C’ , the starting node in the graph


explored:

A B C D E F G H I J K L M N O P Q R
F F T F F F F F F F F F F F F F F F

Since node ‘C’ is visited we will remove it from front

Q: of queue.

front rear

Next we will find the nodes adjacent to node ‘C’ in the graph (This can also be
done with the help of adjacency list). From the graph we find that nodes ‘B’ and
‘F’ are adjacent to node ‘C’. So we enqueue them into the queue. They can be
enqueued in any order. Also we mark them as explored.

explored:

A B C D E F G H I J K L M N O P Q R
F T T F F T F F F F F F F F F F F F

Q:

front rear

F B

dist:

A B C D E F G H I J K L M N O P Q R
ꝏ 1 ꝏ ꝏ ꝏ 1 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
pred:

A B C D E F G H I J K L M N O P Q R
C Nil C

Next we dequeue the the front element ‘F’ from queue.

Output: Mark node ‘F’ with the value in “dist” and join with predecessor node
mentioned in “pred”

The content of the queue becomes,

Q:

front rear

Next we find the nodes adjacent to the dequeued node ‘F’ from the graph. We
find that nodes ‘B’,’C’,’I’ and ‘J’ are adjacent to node ‘5’.But we know that nodes
‘B’ and ‘C’ are already explored. So only ‘I’ and ‘J’ are yet to be explored, we
enqueue them and mark them as explored.

The content of the queue becomes,

Q:

front rear

B I J

explored:

A B C D E F G H I J K L M N O P Q R
F T T F F T F F T T F F F F F F F F
dist:

A B C D E F G H I J K L M N O P Q R
ꝏ 1 ꝏ ꝏ ꝏ 1 ꝏ ꝏ 2 2 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R
C Nil C F F

Next we dequeue the the front element ‘B’ from queue.

The output now becomes

Output: Mark node ‘B’ with the value in “dist” and join with predecessor node
mentioned in “pred”

We will continue with these steps,

Dequeue ‘B’

Q:

front rear

I J

Enqueue adjacent nodes of ‘B’ that are not yet explored and mark them as
explored.

Q:

front rear

I J E
explored:

A B C D E F G H I J K L M N O P Q R
F T T F T T F F T T F F F F F F F F

dist:

A B C D E F G H I J K L M N O P Q R
ꝏ 1 ꝏ ꝏ 2 1 ꝏ ꝏ 2 2 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R
C Nil B C F F

Dequeue ‘I’

Output: Mark node ‘I’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

J E

Enqueue adjacent nodes of ‘I’ that are not yet explored and mark them as
explored.

Q:

front rear

J E H L
explored:

A B C D E F G H I J K L M N O P Q R
F T T F T T F T T T F T F F F F F F

dist:

A B C D E F G H I J K L M N O P Q R
ꝏ 1 ꝏ ꝏ 2 1 ꝏ 3 2 2 ꝏ 3 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R
C Nil B C I F F I

Dequeue ‘J’

Output: Mark node ‘J’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

E H L

Enqueue adjacent nodes of ‘J’ that are not yet explored and mark them as
explored.

Q:

front rear

E H L G K M N
explored:

A B C D E F G H I J K L M N O P Q R
F T T F T T T T T T T T T T F F F F

dist:

A B C D E F G H I J K L M N O P Q R
ꝏ 1 ꝏ ꝏ 2 1 3 3 2 2 3 3 3 3 ꝏ ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R
C Nil B C J I F F J I J J

Dequeue ‘E’

Output: Mark node ‘E’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

H L G K M N

Enqueue adjacent nodes of ‘E’ that are not yet explored and mark them as
explored.

Q:

front rear

H L G K M N A
explored:

A B C D E F G H I J K L M N O P Q R
T T T F T T T T T T T T T T F F F F

dist:

A B C D E F G H I J K L M N O P Q R
3 1 ꝏ ꝏ 2 1 3 3 2 2 3 3 3 3 ꝏ ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R
E C Nil B C J I F F J I J J

Dequeue ‘H’

Output: Mark node ‘H’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

L G K M N A

Enqueue adjacent nodes of ‘H’ that are not yet explored and mark them as
explored.

Q:

front rear

L G K M N A O
explored:

A B C D E F G H I J K L M N O P Q R
T T T F T T T T T T T T T T T F F F

dist:

A B C D E F G H I J K L M N O P Q R
3 1 ꝏ ꝏ 2 1 3 3 2 2 3 3 3 3 4 ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R
E C Nil B C J I F F J I J J H

Dequeue ‘L’

Output: Mark node ‘L’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Enqueue adjacent nodes of ‘L’ that are not yet explored and mark them as
explored.

Q:

front rear

G K M N A O

Since all nodes adjacent to node ‘L’ are already explored we can continue with the
next node at front of queue.

Dequeue ‘G’
Output: Mark node ‘G’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

K M N A O

Enqueue adjacent nodes of ‘G’ that are not yet explored and mark them as
explored.

Q:

front rear

K M N A O D

explored:

A B C D E F G H I J K L M N O P Q R
T T T T T T T T T T T T T T T F F F

dist:

A B C D E F G H I J K L M N O P Q R
3 1 ꝏ 4 2 1 3 3 2 2 3 3 3 3 4 ꝏ ꝏ ꝏ

pred:

A B C D E F G H I J K L M N O P Q R
E C Nil G B C J I F F J I J J H

Dequeue ‘K’
Output: Mark node ‘K’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

M N A O D

Enqueue adjacent nodes of ‘K’ that are not yet explored and mark them as
explored.

Q:

front rear

M N A O D R

explored:

A B C D E F G H I J K L M N O P Q R
T T T T T T T T T T T T T T T F F T
dist:

A B C D E F G H I J K L M N O P Q R
3 1 ꝏ 4 2 1 3 3 2 2 3 3 3 3 4 ꝏ ꝏ 4

pred:

A B C D E F G H I J K L M N O P Q R
E C Nil G B C J I F F J I J J H K

Dequeue ‘M’

Output: Mark node ‘M’ with the value in “dist” and join with predecessor node
mentioned in “pred”
Q:

front rear

N A O D R

Enqueue adjacent nodes of ‘M’ that are not yet explored and mark them as
explored.

Q:

front rear

N A O D R Q

explored:

A B C D E F G H I J K L M N O P Q R
T T T T T T T T T T T T T T T F T T

dist:

A B C D E F G H I J K L M N O P Q R
3 1 ꝏ 4 2 1 3 3 2 2 3 3 3 3 4 ꝏ 4 4

pred:

A B C D E F G H I J K L M N O P Q R
E C Nil G B C J I F F J I J J H M K

Dequeue ‘N’

Output: Mark node ‘N’ with the value in “dist” and join with predecessor node
mentioned in “pred”
Q:

front rear

A O D R Q

Enqueue adjacent nodes of ‘N’ that are not yet explored and mark them as
explored. Since all nodes adjacent to node ‘N’ are already explored we can
continue with the next node at front of queue.

Dequeue ‘A’

Output: Mark node ‘A’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

O D R Q

Enqueue adjacent nodes of ‘A’ that are not yet explored and mark them as
explored. Since all nodes adjacent to node ‘A’ are already explored we can
continue with the next node at front of queue.

Dequeue ‘O’

Output: Mark node ‘O’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Q:

front rear

D R Q
Enqueue adjacent nodes of ‘O’ that are not yet explored and mark them as
explored.

Q:

front rear

D R Q P

explored:

A B C D E F G H I J K L M N O P Q R
T T T T T T T T T T T T T T T T T T

dist:

A B C D E F G H I J K L M N O P Q R
3 1 ꝏ 4 2 1 3 3 2 2 3 3 3 3 4 5 4 4

pred:

A B C D E F G H I J K L M N O P Q R
E C Nil G B C J I F F J I J J H O M K

As we can see that all the nodes in the graph has been explored. Therefore
remove the front node from the queue and append it to the output.

Dequeue ‘D’

Output: Mark node ‘D’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Dequeue ‘R’

Output: Mark node ‘R’ with the value in “dist” and join with predecessor node
mentioned in “pred”
Dequeue node ‘Q’

Output: Mark node ‘Q’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Dequeue ‘P’

Output: Mark node ‘P’ with the value in “dist” and join with predecessor node
mentioned in “pred”

Final Output

You might also like