You are on page 1of 1

BFS

1. The code includes the necessary libraries and defines a constant N with a
value of 6, representing the size of the graph.
2. The visited array is declared with a size of N and initialized with zeros. This
array keeps track of visited nodes during the traversal.
3. A queue q of integers is declared to store the nodes to be processed during
the BFS traversal.
4. A 2D array graph of size N x N is declared to represent the graph. The user is
expected to input the graph's adjacency matrix in the main function.
5. The bfs function is defined, which takes a starting vertex v as an argument. It
performs the BFS traversal starting from the given vertex.
6. Inside the bfs function: a. The visited array is updated to mark the current
vertex as visited. b. The value of the current vertex is printed. c. A loop is used
to check all the vertices connected to the current vertex. d. If a connected
vertex i is found and it has not been visited yet, it is added to the queue q and
marked as visited in the visited array. e. After checking all connected vertices,
the function checks if the queue q is empty. f. If the queue is not empty, it
retrieves the front element of the queue into num, removes it from the queue,
and recursively calls the bfs function with num as the argument. This step
continues the BFS traversal from the next vertex in the queue.
7. The main function is defined, which is the entry point of the program. a. The
user is prompted to enter the graph's adjacency matrix. b. The matrix is read
using nested loops and stored in the graph array. c. The user is prompted to
enter the starting vertex for the BFS traversal. d. The bfs function is called with
the starting vertex. e. Finally, the program returns 0 to indicate successful
execution.

In summary, this code implements BFS traversal on a graph represented by an


adjacency matrix. It takes user input for the graph and starting vertex, and then
performs BFS traversal to print the order in which vertices are visited.

You might also like