Advertisement
zhenialeks

BFS

Apr 12th, 2020
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. typedef struct Node{
  6.     int n;
  7.     struct Node *next;
  8. } node;
  9.  
  10. typedef struct Queue{
  11.     node *first;
  12.     node *last;
  13. } queue;
  14.  
  15. int n;
  16.  
  17.  
  18. node *init_node(int n){
  19.     node *tmp = malloc(sizeof(node));
  20.     tmp->n = n;
  21.     tmp->next = NULL;
  22.     return tmp;
  23. }
  24.  
  25. //Queue:
  26.  
  27. queue *init_queue(){
  28.     queue *q = malloc(sizeof(queue));
  29.     q->first = q->last = NULL;
  30.     return q;
  31. }
  32.  
  33. int is_queue_empty(queue *q){
  34.     return (q->first == NULL);
  35. }
  36.  
  37. node *pop(queue *q){
  38.     if (is_queue_empty(q))
  39.         return NULL;
  40.     node *tmp = q->first;
  41.     q->first = q->first->next;
  42.  
  43.     if (tmp == q->last)
  44.         q->last = NULL;
  45.  
  46.     return tmp;
  47. }
  48.  
  49. void push_back(queue *q, node *el){
  50.     if (is_queue_empty(q)){
  51.         q->first = q->last = el;
  52.     } else {
  53.         q->last->next = el;
  54.         q->last = el;
  55.     }
  56. }
  57.  
  58. void free_queue(queue *q){
  59.     if (is_queue_empty(q)){
  60.         free(q);
  61.         return;
  62.     }
  63.     node *tmp2;
  64.     node *tmp;
  65.  
  66.     tmp = q->first;
  67.     if (tmp->next == NULL){
  68.         free(tmp);
  69.     } else {
  70.          tmp2 = tmp->next;
  71.         while (tmp2!= NULL){
  72.             free(tmp);
  73.             tmp = tmp2;
  74.             tmp2 = tmp2->next;
  75.         }
  76.         free(tmp);
  77.     }
  78.     free(q);
  79. }
  80.  
  81. //Main part:
  82.  
  83. int BFS(queue *q, char *visited, char *queued, int **G, int target_node) {
  84.     if (is_queue_empty(q)) return 0;
  85.     node *tmp = pop(q);
  86.  
  87.     visited[tmp->n] = 1;
  88.     if (tmp->n == target_node)
  89.         return 1;
  90.  
  91.     for (int i = 0; i < n; i++)
  92.         if (G[tmp->n][i] && !queued[i] && !visited[i]) {
  93.             push_back(q, init_node(i));
  94.             queued[i] = 1;
  95.         }
  96.     return BFS(q, visited, queued, G, target_node);
  97. }
  98.  
  99.  
  100.  
  101. int main(){
  102.     int   **G;
  103.     FILE *fin;
  104.  
  105.     fin = fopen("input.txt", "r");
  106.     if (!fin){
  107.         //handle this situation
  108.         exit(EXIT_FAILURE);
  109.     }
  110.  
  111.     fscanf(fin, "%d", &n);
  112.     G = (int **) malloc(sizeof(int *) * n);
  113.     for (int i = 0; i < n; i++)
  114.         G[i] = (int *) calloc(n, sizeof(int));
  115.  
  116.     for (int i = 0; i < n; i++)
  117.         for (int j = 0; j < n; j++)
  118.             fscanf(fin, "%d", &G[i][j]);
  119.  
  120.     //Entering set of vertexes to start BFS:
  121.     int tmp;
  122.     int target_node;
  123.     int is_reachable;
  124.  
  125.     char *queued = calloc(n, sizeof(int));
  126.     char *visited = calloc(n, sizeof(int));
  127.  
  128.     char ch;
  129.     queue *q = init_queue();
  130.  
  131.     while (fscanf(fin, "%d%c", &tmp, &ch) == 2) {
  132.         push_back(q, init_node(tmp - 1));
  133.         queued[tmp - 1] = 1;
  134.         if (ch == '\n')
  135.             break;
  136.     }
  137.     fscanf(fin, "%d", &target_node);
  138.     target_node--;
  139.  
  140.     is_reachable = BFS(q, visited, queued, G, target_node);
  141.     free_queue(q);
  142.     printf(is_reachable ? "YES" : "NO");
  143.  
  144.     getch();
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement