Question
In BFS algorithm, in order to make sure a node is only processed one, we use a Boolean visited array. Due to avoiding complexity, the
In BFS algorithm, in order to make sure a node is only processed one, we use a Boolean visited array. Due to avoiding complexity, the assumption is that from the starting vertex, all vertices are reachable. Lets suppose a graph of 8 vertices. Make an algorithm in C++ for Breadth First Search Traversal for graphs.
The pseudo code for this algorithm is as follows:
BreadthFS(G,s) 1 for each vertex u in G.V - {s} 2 u.color = white 3 u.d = INF 4 u.p = NIL 5 s.color = green 6 s.d = 0 7 s.p = NIL 8 Q = NULL 9 ENQUEUE(Q,s) 10 while Q != NULL 11 u = DEQUEUE(Q) 12 for each v in G.Adj[u] 13 if v.color == white 14 v.color = green 15 v.d = u.d + 1 16 v.p = u 17 ENQUEUE(Q,v) 18 u.color = dark_green
For any arbitary starting vertex, the output of BFS should be correct.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started