0% found this document useful (0 votes)
116 views10 pages

Breadth-First Search

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at a root node and explores all neighboring nodes at the present depth prior to moving on to the nodes at the next depth level. BFS uses a queue to keep track of nodes to visit at each level, marking them as discovered but not yet explored before visiting their unexplored neighbors. This process continues until all nodes are visited.

Uploaded by

Shah jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views10 pages

Breadth-First Search

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at a root node and explores all neighboring nodes at the present depth prior to moving on to the nodes at the next depth level. BFS uses a queue to keep track of nodes to visit at each level, marking them as discovered but not yet explored before visiting their unexplored neighbors. This process continues until all nodes are visited.

Uploaded by

Shah jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Breadth-First Search

Breadth-First Search

 BFS traverse a connected component of a given graph and defines a spanning


tree.
 Intuitively, the basic idea of the breath-first search is this: send a wave out from
source s. The wave hits all vertices 1 edge from s. From there, the wave hits all
vertices 2 edges from s. Etc.
 We use FIFO queue Q to maintain the wave front: v is in Q if and only if wave
has hit v but has not come out of v yet.
Overall Strategy of BFS
Algorithm

 Breadth-first search starts at a given vertex s, which is at level 0.

 In the first stage, we visit all the vertices that are at the distance of one edge
away. When we visit there, we paint as "visited," the vertices adjacent to the start
vertex s - these vertices are placed into level 1.
 In the second stage, we visit all the new vertices we can reach at the distance of
two edges away from the source vertex s. These new vertices, which are adjacent
to level 1 vertices and not previously assigned to a level, are placed into level 2,
and so on.
 The BFS traversal terminates when every vertex has been visited.
Overall Strategy of BFS
Algorithm
To keep track of progress, breadth-first-search colors each vertex. Each vertex of the
graph is in one of three states:

1. Undiscovered;

2. Discovered but not fully explored; and

3. Fully explored.

The state of a vertex, u, is stored in a color variable as follows:

1. color[u] = White - for the "undiscovered" state,

2. color [u] = Gray - for the "discovered but not fully explored" state, and

3. color [u] = Black - for the "fully explored" state.


Algorithm: Breadth-First
Search Traversal
 BFS(V, E, s)

 1.         for each u in V − {s}            ▷ for each vertex u in V[G] except s.


2.             do color[u] ← WHITE
3.                 d[u] ← infinity
4.                 π[u] ← NIL
5.         color[s] ← GRAY                 ▷ Source vertex discovered
6.         d[s] ← 0                               ▷ initialize
7.         π[s] ← NIL                           ▷ initialize
8.         Q ← {}                                ▷ Clear queue Q
9.         ENQUEUE(Q, s)
10        while Q is non-empty
11.             do u ← DEQUEUE(Q)                      ▷ That is, u = head[Q]
12.                 for each v adjacent to u            ▷ for loop for every node along with edge.
13.                         do if color[v] ← WHITE        ▷ if color is white you've never seen it before
14.                                 then  color[v] ← GRAY
15.                                          d[v] ← d[u] + 1
16.                                          π[v] ← u
17.                                          ENQUEUE(Q, v)
18.                 DEQUEUE(Q)
19.         color[u] ← BLACK
BFS simulation

a. After initialization (paint every vertex white, set d[u] to infinity for each vertex u,  and
set the parent of every vertex to be NIL), the source vertex is discovered in line 5. Lines 8-
9 initialize Q to contain just the source vertex s.

a
BFS simulation cont.

b. The algorithm discovers all vertices 1 edge from s i.e., discovered all vertices (w and
r) at level 1.

b c
BFS simulation cont.
d. The algorithm discovers all vertices 2 edges from s i.e., discovered all vertices (t, x,
and v) at level 2.

d e

f
BFS simulation cont.

g. The algorithm discovers all vertices 3 edges from s i.e., discovered all vertices (u and
y) at level 3.

g
h
BFS simulation cont.

i. The algorithm terminates when every vertex has been fully explored.

You might also like