 Hello and welcome to the lecture on graph traversal algorithm implementation using breadth first search algorithm. In this lecture we will be discussing about implementation of graph traversal using breadth first search approach using C programming language. The learning outcome of this video lecture is that the student is able to develop graph traversal using breadth first search algorithm using C programming language. Here the complete implementation or I can say programming I will be dividing into modules. Module 1 is the representation of graph using adjacency matrix and module 2 is a graph traversal of graph using breadth first search algorithm. Input in the module 1 is the number of nodes in the graph, second input is the label for each node and the third input will be the edges present between two nodes or not. The edges present user will enter one value and zero for the no edge between two nodes. Output of module 1 is the adjacency matrix of given graph which will be the input for the module 2. In module 2 set the status of each node to 1 initialize Q data structure and implement its operations like NQ, DQ, checking Q is empty or not and then follow the BFS algorithm. Let's start with the module 1. For a given graph G to represent or to store the information about the graph G we need input number of nodes and labels for each node. So here in variable declaration section we have declared N to store number of nodes information and array labels to store label for each node. To store graph information in adjacency matrix graph G is declared with size 10 by 10 and variable i and j declared for row index and column index. Now to read number of nodes from messages displayed and scan up statement to read N value. Just to read labels for each node using for loop construct in labels array within this loop we have set status array value to all elements to 1. This initialization of status is required to start with breadth first search algorithm. So in the same loop we are setting the status of all nodes to 1. As shown here in the stable labels array storing label for A, B, C, D, E nodes and status array value is set to 1 for each node. Here the flush all function is used to clear all buffers associated with input stream. So we can read the node information correctly. Now next is read age information in a graph and store in adjacency matrix G. And adjacency matrix is used to represent the nodes that are adjacent to one another that we have already discussed in previous videos. In adjacency matrix the rows and columns are labeled by graph vertices and entry in the adjacency matrix G will contain one at the ith row and jth column if there is a age present between these two nodes. As shown here now to read value in matrix G nested for loop is used in which the first loop is for a row index and second loop is for column index. Now you can see here printf statement is given here to ask the user and age information between two nodes. Here nodes label information is taken from the label in the label array and then next scanf statement to scan value and store at sale G of ij that is node at ith row and jth column. Now in undirected graph we are using here undirected graph if there is an age between node A to node B then there is an age from node B to node A also. So the value at cell G of ij is 1 it means the value at cell G of ji is also 1. So here we can add one condition so we can avoid one step of scanning from getting the information from user and to check that there is an age means the value 1 at G of ji then there is no age and then else you can have the ji value set to 1. If there is no age means 0 will be there and if there is age means we can set the value directly to 1 at the cell G of ij. Now here we are displaying the adjacency matrix for given graph so as we know in two-dimensional array how we can display the two-dimensional array using nested for loop the same way we are displaying the adjacency matrix here. Now in module 2 in which the given graph will be traversed using the by following the BFS algorithm here input to this module is an adjacency matrix representation of graph status array and queue data structure. First variable declaration related to the queue data structure that is character array queue with size 10 by 10 we are maintaining one more queue here that is index queue which is an integer array with the same size 10 and the queue is also with the size 1-dimensional array. We will discuss why index queue is required here in the next coming slide. Index queue front and rear is declared and initialized with 0 and one more variable is declared here is the index which is initialized to 1. Now queue operations are n queue d queue and checking queue is empty and full. Let us discuss implementation of queue operation here n queue function is defined in this slide. Here n queue operation is defined to perform n queue operation on a queue means inserting a new element in the queue with parameters character node and integer node index i node. In queue operation we need to check queue is full or not. So condition here is to check the queue is full or not rare reaches max size means then queue is full accordingly if condition is specified and if it queue is not full then we can insert the index information into the queue and similarly we can insert the node information into the index queue. After n queue operation increment the rear by 1 to point to the next location. So we are very much familiar with the queue operation. So now let us discuss the implementation of d queue operation that is a removing element from the queue. Here d queue function is defined which returns the character removed from the queue along with the index position. While d queue operation we need to check queue is empty or not. So condition here is if front is equal to rear then queue is empty. So we are not able to remove the any element from the queue. So accordingly this message is displayed here. And if queue is not empty then we are removing the node information from the queue and similarly we are removing the index that nodes index information from the index queue. So this we require index information we require to get the label information about that node. So that's why we are maintaining the index queue separately. There might be a different approach to have the node information and index information in the structure format also. After d queue operation increment front by 1 to point to the next location and update index queue's values to minus 1 value. Now let us discuss the implementation of checking queue is empty or not. Here is empty function is defined to check queue is empty or not. So if front is equal to rear then queue is empty. Now we will discuss the BFS algorithm by following this flow chart. Here you can pause the video for some time and write a c code by following BFS algorithm flow chart and using the functions and variables discussed in previous slides. Now we will see the implementation part. The first step in the BFS algorithm is set status 1 for each node in the g. So that we already finished when we are scanning the labels for each node. So we have set the status to 1 for each node in the array status. Then next step is enqueuing the starting node into the queue and update its status to 2. So that's why we are calling here enqueue function by passing the first node information and its index information. So these two will be enqueued into the queue and the index queue. And the status of the first node is updated to 2. Next step we are checking is queue is empty or not. So this while loop is the same and what it is checking the queue is empty or not. If it is not empty the block of code will be written here and if it is empty then it will be terminated. The program will stop here it will come out of the loop. So next step in the algorithm if queue is not empty dequeue a node and process it and set its status to 3. So we are calling here dequeue function. So character written by that we are displaying here as a visited or processed node. And index information what we are retrieving after this dequeue operation. So using that information the status of that node is updated to 3. Next one is enqueue all the neighbors of the node n which we have dequeued. The neighbors whose status is 1 and then set the status to 2. So for that using this index information we are checking this nodes neighbors that is at this column from 0 to n. If that is a neighboring node means the value in the adjacency matrix is 1 and the status is 1 then enqueue that node information and the index information into the queue and update that node status to 2. So this we are doing repeatedly for all the neighboring nodes of the node which we have just now dequeued from the queue. So this will be repeated and once you find the queue is empty your program will come out of the loop. So this is the implementation of the BSS algorithm. So let us see the execution till now whatever we have discussed the modules in this implementation part of the BSS let us see the execution part of that particular program. So here enqueue variable declaration enqueue function then dequeue function what we have discussed just now is empty function main function with the variable declaration reading number of nodes, read a node labels, reading the inch information between the nodes here the prong message to ask is there is a h between these two nodes then scan F to scan the value in the adjacency matrix then displaying the adjacency matrix information and then here from here your BFS implementation starts enqueuing on state first node then the value. So let us execute this particular program first compile then run. So here we are following the same example with the fine nodes A, B, C, D and E then we are asking for the age information there is no h between A to A nodes so 0 is entered there is a h between A to B, A to C so there is one values entered similarly the age information between different nodes as taken from the user. So here you can see when once we are asking for A to B there is no further prong message to ask from the age between B to A so it is considered if there is a h from A to B means there is a h from B to A this is the adjacency matrix after getting the information about the all ages and then the BFS by following the BFS node this is a node is displayed here A, B, C, D, E, A, B, C, E, D sorry. So this is the output of the program thank you for watching this video.