Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions