Shamba

BFS_Shamba

Mar 9th, 2022
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int V, count = 0;
  4. int queue[50], qpos = -1, qlen = -1;
  5.  
  6. //Function declarations
  7. void Init_Mat(int mat[50][50], int visit[50]);
  8. void Addedge(int mat[50][50], int source, int dest);
  9. void DisplayAdjMat(int mat[50][50]);
  10. void BFS(int node, int visit[50], int mat[50][50]);
  11.  
  12.  
  13. int main()
  14. {
  15.     int i, j, AdjMat[50][50];
  16.     int edge_add_choice = 1;
  17.     int visited[50];
  18.  
  19.     int Edge_A, Edge_B;
  20.  
  21.     //Taking input for the number of nodes in the graph
  22.     printf("Enter the amount of nodes : ");
  23.     scanf("%d", &V);
  24.  
  25.     //Initializing the Adjacency Matrix
  26.     Init_Mat(AdjMat, visited);
  27.  
  28.     //Do-While loop to take inputs for the graph
  29.     do
  30.     {
  31.         printf("\nEnter the Vertice 1 of the Edge : ");
  32.         scanf("%d", &Edge_A);
  33.         printf("Enter the Vertice 2 of the Edge : ");
  34.         scanf("%d", &Edge_B);
  35.  
  36.         Addedge(AdjMat, Edge_A, Edge_B);
  37.  
  38.         printf("\nWhat do you want to do now ?\n1. Add another Edge\n2. Stop adding edges\n");
  39.         scanf("%d", &edge_add_choice);
  40.  
  41.     } while (edge_add_choice == 1);
  42.  
  43.     DisplayAdjMat(AdjMat);    
  44.  
  45.     printf("\nThe required BFS for the graph will be : \n");
  46.  
  47.     BFS(0, visited, AdjMat);
  48.  
  49.     return 0;
  50. }
  51.  
  52. //Initialize the adjacency matrix and visited array with 0 in all elements
  53. void Init_Mat(int mat[50][50], int visit[50])
  54. {
  55.     for(int i = 0; i < V; i++)
  56.     {
  57.         visit[i] = 0;
  58.         for(int j = 0; j < V; j++)
  59.         {
  60.             mat[i][j] = 0;
  61.         }
  62.     }
  63. }
  64.  
  65. //Add adjacency values to the matrix by using the edges of the graph
  66. void Addedge(int mat[50][50], int source, int dest)
  67. {    
  68.     mat[source][dest] = 1;
  69.     mat[dest][source] = 1;
  70. }
  71.  
  72. //Display the Adjacency Matrix
  73. void DisplayAdjMat(int mat[50][50])
  74. {
  75.     printf("\nDisplaying the Adjacency Matrix : \n");
  76.     for(int i = 0; i < V; i++)
  77.     {
  78.         for(int j = 0; j < V; j++)
  79.         {
  80.             printf("%d ", mat[i][j]);
  81.         }
  82.         printf("\n");
  83.     }
  84. }
  85.  
  86. //Depth First Search
  87. void BFS(int node, int visit[50], int mat[50][50])
  88. {
  89.     int j;
  90.  
  91.     printf("%d", node);
  92.     count++;
  93.     if(count != V)
  94.     {
  95.         printf("->");
  96.     }
  97.     visit[node] = 1;
  98.  
  99.     for(j=0; j<V; j++)
  100.     {
  101.         if(mat[node][j] && !visit[j])
  102.         {
  103.             qlen++;
  104.             queue[qlen] = j;  
  105.             visit[j] = 1;        
  106.         }
  107.     }
  108.  
  109.    
  110.     if(count != V)
  111.     {
  112.         BFS(queue[++qpos], visit, mat);
  113.     }  
  114.  
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment