Need help in the BFS Breadth First Search algorithm I know t

Need help in the BFS (Breadth First Search?) algorithm, I know the u stands for a vertex, but certain symbols such as {s} and Q I\'m not sure what that is referring to.

Basically could you explain each line from line 6 onward? Also need help in understanding how it is traced? I have pictures below showing algorithm and an illustration of how algorithm is traced.

BFS 1 for each vertex u E G.V- u color WHITE NIL 5 s color GRAY 9 ENQUEUE(0, s) 10 while Q DE QUEUE(0) 12 for each v E G.Adj[u] if v color WHITE 13 v. color GRAY 16 ENQUEUE v) color BLACK

Solution

Initially from line 1 to 4, he has taken every vertex u, which belongs to G to a set. and distance from s(starting vertex to all the other vertex as infinity, and parent () to null and every color of vertex to white(initial state).

Then in line 5 to 7 taking s as starting vertex, making it as grey(secondary state i.e.visiting state) and its parent as null and distance 0(as distance from vertex to itself is 0).

In line 8 taking a queue, initializing with NULL. and in line 9 enqueue starting vertex s in queue Q.

From line 10 to 18 its loop finding distance of every vertex from s(staring state) till queue is empty(exit condition)

line 11-> dequeue every vertex in queue

line 12 to line 17 its a loop relating to each vertex dequeued from line 11.

line 12-> find adjacent vertex of u(dequeued vertex)

line 13-> every afjacent vertex v of u if that color is white(i.e. if not visited) then make it gray(visiting state) -> line 14

line 15-> making the distance from u->v as 1 totally from s by incresing by 1.

line 16-> and making parent of v as u as v is neighbor for u and could come to v from u.

line 17-> enqueue v.

line 18-> make the visited vertex to black.

Ex:

starting from s. All distance are infinity, and colors to white(figure a)

neighbors of s r ans w, making their distance to 1 and if totally visited making their color black.......

In figure i we could find distance from s to every node and all are colored black as all are visted

Need help in the BFS (Breadth First Search?) algorithm, I know the u stands for a vertex, but certain symbols such as {s} and Q I\'m not sure what that is refer

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site