Advertisement
Naimul_X

Untitled

May 19th, 2020
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include<stdio.h>
  2. #define size 500
  3. int no_of_vertex, i, graph[50][50], j, visited_seq = -1, f = 0, r = 0;
  4. char vertex[26];
  5. char visited_array[100];
  6.  
  7. char q[size+1];
  8.  
  9. void visit(char ch)
  10. {
  11. visited_seq++;
  12. visited_array[visited_seq] = ch;
  13. }
  14.  
  15. int is_visit(char ch)
  16. {
  17. for(i = 0; i <= visited_seq ; i++)
  18. {
  19. if(visited_array[i] == ch)
  20. {
  21. return 1; // If Found
  22. }
  23. }
  24. return 0; // If not found
  25. }
  26.  
  27. void print_array()
  28. {
  29. for(i = 0; i <= visited_seq ; i++)
  30. printf("%c ", visited_array[i]);
  31. }
  32. void enqueue(char ch)
  33. {
  34. r = (r+1) % (size + 1);
  35.  
  36. q[r] = ch;
  37.  
  38. }
  39. char dequeue()
  40. {
  41. f = (f+1) % (size+1);
  42.  
  43. char ch = q[f];
  44. q[f] = '\0';
  45. return ch;
  46. }
  47. void findAdjacentList(char ch)
  48. {
  49. for(i = 0; i < no_of_vertex ; i++)
  50. {
  51. if(vertex[i] == ch)
  52. {
  53. for(j = 0 ; j < no_of_vertex ; j++)
  54. {
  55. if(graph[i][j] == 1)
  56. {
  57. if( is_visit(vertex[j]) == 0 ){
  58.  
  59. enqueue(vertex[j]);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. void BFS()
  67. {
  68. enqueue(vertex[0]);
  69. while(f != r)
  70. {
  71.  
  72. char ch = dequeue();
  73.  
  74. findAdjacentList(ch);
  75. if(is_visit(vertex[j]) == 0)
  76. visit(ch);
  77. }
  78. }
  79.  
  80. void createGraph()
  81. {
  82. for(i = 0 ; i < 26 ; i++)
  83. vertex[i] = i+65;
  84.  
  85. printf("How many vertex: ");
  86. scanf("%d", &no_of_vertex);
  87.  
  88. for(i = 0 ; i < no_of_vertex ; i++)
  89. {
  90. for(j = 0 ; j< no_of_vertex ; j++)
  91. {
  92. graph[i][j] = 0;
  93. }
  94. }
  95.  
  96. for(i = 0 ; i < no_of_vertex ; i++)
  97. {
  98. for(j = i+1 ; j< no_of_vertex ; j++)
  99. {
  100.  
  101. scanf("%d", &graph[i][j]);
  102. if(graph[i][j] == 1)
  103. graph[j][i] = 1;
  104.  
  105. }
  106. }
  107. }
  108.  
  109. int main()
  110. {
  111. createGraph();
  112. BFS();
  113. print_array();
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement