Advertisement
ruhul0

BFS

Aug 25th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #define size 50
  4.  
  5. char visited_array[size];
  6. int visited_seq = -1, f = 0, r = 0, graph[50][50], n;
  7. char q[50];
  8. char vertex[26];
  9.  
  10. void visit(char ch)
  11. {
  12.     visited_seq++;
  13.     visited_array[visited_seq] = ch;
  14. }
  15.  
  16. int is_visit(char ch)
  17. {
  18.     int i;
  19.     for(i = 0 ; i <= visited_seq ; i++)
  20.     {
  21.         if(ch == visited_array[i])
  22.             return 1; //Already visited
  23.     }
  24.     return 0; //Not visited before
  25. }
  26.  
  27. void enqueue(char ch)
  28. {
  29.     int s;
  30.     s = (r+1) % (size+1);
  31.     if(s == f)
  32.         return;
  33.     else{
  34.         q[s] = ch;
  35.         r = s;
  36.     }
  37. }
  38.  
  39. char dequeue()
  40. {
  41.     if(f == r)
  42.         return '\0';
  43.     else
  44.     {
  45.         char ch;
  46.         f = (f+1) % (size+1);
  47.         ch = q[f];
  48.         q[f] = '\0';
  49.         return ch;
  50.     }
  51. }
  52.  
  53. void find_adjecency(char ch)
  54. {
  55.     int i, j;
  56.     for(i = 0 ; i < n ; i++)
  57.     {
  58.         if(ch == vertex[i])
  59.         {
  60.             for(j = 0 ; j < n ; j++)
  61.             {
  62.                 if(graph[i][j] == 1)
  63.                 {
  64.                     printf("%c ", vertex[j]);
  65.                     if(is_visit(vertex[j]) == false)
  66.                         enqueue(vertex[j]);
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74. void bfs()
  75. {
  76.     char ch;
  77.     int x;
  78.     printf("\nFrom which vertex you want to start?(1-50): ");
  79.     scanf("%d",&x);
  80.     enqueue(vertex[x-1]);
  81.  
  82.     while(f!=r)
  83.     {
  84.         ch = dequeue();
  85.         if(is_visit(ch) == 0)
  86.             {
  87.                 visit(ch);
  88.             }
  89.         find_adjecency(ch);
  90.     }
  91. }
  92.  
  93. void displayVisitedArray()
  94. {
  95.     int i;
  96.     printf("\nVisited Sequence is:  ");
  97.     for(i=0; i<= visited_seq ; i++)
  98.         printf("%c ", visited_array[i]);
  99. }
  100.  
  101. void createGraph()
  102. {
  103.     int i, j;
  104.     for(i = 0 ; i < 26 ; i++)
  105.         vertex[i] = i+65;
  106.  
  107.     printf("How many Vertex: ");
  108.     scanf("%d", &n);
  109.     for(i = 0 ; i < n ; i++)
  110.     {
  111.         for(j = 0 ; j < n ;j++)
  112.             graph[i][j] = 0;
  113.     }
  114.  
  115.     for(i = 0 ; i < n ; i++)
  116.     {
  117.         for(j = i+1 ; j < n ;j++)
  118.         {
  119.             printf("%c-%c exist: ", vertex[i], vertex[j]);
  120.             scanf("%d", &graph[i][j]);
  121.             if(graph[i][j] == 1)
  122.                 graph[j][i] = 1;
  123.         }
  124.     }
  125. }
  126. int main()
  127. {
  128.     createGraph();
  129.     bfs();
  130.     displayVisitedArray();
  131.     getch();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement