2. CVR COLLEGE OF ENGINEERING
Vastunagar , Mangalpalli(V) , Ibrahimpatnam(M) , R.R.Dist.
Pin-501510
Department of COMPUTER SCIENCE AND ENGINEERING
AKULA RAJU
19B81A0599
3.
4.
5.
6. 1. Given a graph G=(V,E) and a distinguished source vertex ‘s’ , breadth-first
search systematically explores the edges of G to discover every reachable
vertex from ‘s’. It produces "Breadth First Search Tree" with root ‘s’ that
contains all reachable from vertices.
2. The algorithm works on both directed and undirected graphs.
Assumptions made in the algorithm are:
1. Input graph G=(V,E) is represented using adjacency list representation.
2. We use 3 different colors to distinguish between vertices.
3. White color specifies that the vertex is not yet discovered, Gray color
specifies that the vertex is discovered for the first time, & Black color
specifies that the vertex is fully discovered.
4. We store color of each vertex in u.coIor and predecessor of u in u.pi if no
predecessor u.pi=null and shortest distance of u from 's' in u.d . This
algorithm uses FIFO QUEUE for Gray vertices.
7.
8. Working of procedure BFS(G,s) is as follows:
Example: Progress of BFS on a sample graph is shown in the figure below :
Fig. l. Given Undirected graph Fig. 4. Search visits vertices ‘t’ and ‘x’
Fig. 2. Source vertex ‘s’ is ENQUEUED
Fig. 3. Search visits vertices ‘r’ and ‘w’
Fig. 5. Search visits vertices ‘v’
Fig. 6. Search visits vertices ‘u’
Fig. 7. Search visits vertices ‘y’
Fig. 8. Vertex ‘v’ is completely traversed
hence Dequeued
Fig. 9. Vertex ‘u’ is completely traversed
hence Dequeued’
9. Example 1 :Consider the given
directed graph given below
The output Tree:
Example 2 :Consider the given
directed graph given below
Output forest with multiple trees
when the root vertex is ‘m’ :
Output tree when root vertex is ‘p’ :
Output tree when root vertex is ‘r’ :
10. • We can observe that In Example 1 as graph was a Forest so it is
obvious that according to BFS algorithm the search would be
incomplete.
• But in example 2 input graph is directed Acyclic graph (Dag) and
still the BFS algorithm was unable to search the graph completely.
• These can be overcomed by the modified BFS Algorithm.
11. For a Given graph G=(V,E) and a distinguished source vertex 's’, with
modified BFS algorithm we can traverse the graphs, which we may not
traverse with existing BFS completely but the output may contain
multiple trees FORMING A SPANNING FOREST.
Assumptions made in the algorithm are:
1. Input graph G=(V,E) is represented using adjacency list
representation.
2. We use 3 different colors to distinguish between vertices.
3. White color specifies that the vertex is not yet discovered, Gray color
specifies that the vertex is discovered for the first time, a Black color
specifies that the vertex is fully discovered.
4. We store color of each vertex in u.color & predecessor of u in u.pi if
no predecessor
12. The Algorithm:
BFS (G, root) :
1. for each vertex u in G.V
2. u. color = WHITE
//Initializing all the vertices in the given
graph.
3. u.d = ∞
4. u. π = NIL
5. u = root
6. B_FS(G, u)
// calling B_FS algorithm with specified root
vertex.
7. for each vertex u ∈ G. v - {root}
8. if u. color = = white
//calling B_FS algorithm with unreached
vertices.
9. B_FS(G , u)
B_FS (G , u) :
1. u. color = GRAY /*Traditional BFS
algorithm that forms a BFS tree from */
2. u. d = 0 // tree from the specified root
vertex
3. u. π = NIL
4. Q ≠ Φ
5. ENQUEUE(Q, u)
6. While Q ≠ Φ
7. u = DEQUEUE (Q)
8. for each v ∈ G. Adj [u]
9. if v. color = = WHITE
10. v. color = GRAY
11. v. d = u.d + 1
12. v. π = u
13. ENQUEUE (Q, v)
14. u.color = BLACK
13. Given directed Acyclic Graph :
Output forest with multiple trees :
Given directed Acyclic Graph :
Output forest with multiple trees when root vertex is ‘m’