Dream2503

BFS

Nov 17th, 2025
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.02 KB | Source Code | 0 0
  1. //  Q1. Implementation of Breadth-First-Search on an Undirected Graph with Adjacency List Representation.
  2.  
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. typedef enum { WHITE, GREY, BLACK } Color;
  8.  
  9. typedef struct AdjNode {
  10.     int vertex;
  11.     struct AdjNode* next;
  12. } AdjNode;
  13.  
  14. typedef struct GraphNode {
  15.     int vertex;
  16.     Color color;
  17.     int distance;
  18.     struct GraphNode* parent;
  19.     AdjNode* head;
  20. } GraphNode;
  21.  
  22. typedef struct {
  23.     int V;
  24.     GraphNode* nodes;
  25. } Graph;
  26.  
  27. typedef struct ListNode {
  28.     GraphNode* data;
  29.     struct ListNode* next;
  30. } ListNode;
  31.  
  32. typedef struct {
  33.     ListNode *front, *rear;
  34. } Queue;
  35.  
  36. Graph create_graph(int);
  37. void add_edge(Graph, int, int);
  38. void print_graph(Graph);
  39. void delete_graph(Graph);
  40. void enqueue(Queue*, GraphNode*);
  41. GraphNode* dequeue(Queue*);
  42. int empty(Queue);
  43. void BFS(Graph, int);
  44.  
  45. int main() {
  46.     int V, E, source, destination;
  47.     printf("Enter number of vertices: ");
  48.     scanf("%d", &V);
  49.     printf("Enter number of edges: ");
  50.     scanf("%d", &E);
  51.     Graph graph = create_graph(V);
  52.  
  53.     for (int i = 0; i < E; i++) {
  54.         printf("Enter the %dth edge: ", i + 1);
  55.         scanf("%d%d", &source, &destination);
  56.         add_edge(graph, source, destination);
  57.         add_edge(graph, destination, source);
  58.     }
  59.     print_graph(graph);
  60.     printf("\nEnter source vertex for BFS: ");
  61.     scanf("%d", &source);
  62.     BFS(graph, source);
  63.     delete_graph(graph);
  64.     return 0;
  65. }
  66.  
  67. void enqueue(Queue* queue, GraphNode* node) {
  68.     ListNode* new = (ListNode*)malloc(sizeof(ListNode));
  69.  
  70.     if (!new) {
  71.         printf("Memory was not allocated");
  72.         exit(0);
  73.     }
  74.     new->data = node;
  75.     new->next = NULL;
  76.  
  77.     if (queue->rear) {
  78.         queue->rear->next = new;
  79.     } else {
  80.         queue->front = new;
  81.     }
  82.     queue->rear = new;
  83. }
  84.  
  85. GraphNode* dequeue(Queue* queue) {
  86.     if (!queue->front) {
  87.         return NULL;
  88.     }
  89.     ListNode* temp = queue->front;
  90.     GraphNode* data = temp->data;
  91.     queue->front = queue->front->next;
  92.  
  93.     if (!queue->front) {
  94.         queue->rear = NULL;
  95.     }
  96.     free(temp);
  97.     return data;
  98. }
  99.  
  100. int empty(Queue queue) { return queue.front == NULL; }
  101.  
  102. Graph create_graph(int V) {
  103.     Graph graph = {V, (GraphNode*)malloc(V * sizeof(GraphNode))};
  104.  
  105.     if (!graph.nodes) {
  106.         printf("Memory was not allocated");
  107.         exit(0);
  108.     }
  109.     for (int i = 0; i < V; i++) {
  110.         GraphNode node = {i, WHITE, INT32_MAX, NULL, NULL};
  111.         graph.nodes[i] = node;
  112.     }
  113.     return graph;
  114. }
  115.  
  116. void add_edge(Graph graph, int source, int destination) {
  117.     AdjNode* node = (AdjNode*)malloc(sizeof(AdjNode));
  118.  
  119.     if (!node) {
  120.         printf("Memory was not allocated");
  121.         exit(0);
  122.     }
  123.     node->vertex = source;
  124.     node->next = NULL;
  125.     node->next = graph.nodes[destination].head;
  126.     graph.nodes[destination].head = node;
  127. }
  128.  
  129. void print_graph(Graph graph) {
  130.     printf("\nAdjacency List:\n");
  131.  
  132.     for (int i = 0; i < graph.V; i++) {
  133.         printf("%d: ", i);
  134.         AdjNode* current = graph.nodes[i].head;
  135.  
  136.         while (current) {
  137.             printf("-> %d ", current->vertex);
  138.             current = current->next;
  139.         }
  140.         printf("\n");
  141.     }
  142. }
  143.  
  144. void delete_graph(Graph graph) {
  145.     int i;
  146.     AdjNode* node;
  147.  
  148.     for (i = 0; i < graph.V; i++) {
  149.         while (graph.nodes[i].head) {
  150.             node = graph.nodes[i].head->next;
  151.             free(graph.nodes[i].head);
  152.             graph.nodes[i].head = node;
  153.         }
  154.     }
  155.     free(graph.nodes);
  156. }
  157.  
  158. void BFS(Graph graph, int source) {
  159.     int i;
  160.     AdjNode* adj;
  161.     GraphNode *node, *u, *v;
  162.     Queue queue = {NULL, NULL};
  163.     graph.nodes[source].color = GREY;
  164.     graph.nodes[source].distance = 0;
  165.     enqueue(&queue, &graph.nodes[source]);
  166.     printf("\nBFS Traversal starting from vertex %d: ", source);
  167.  
  168.     while (!empty(queue)) {
  169.         u = dequeue(&queue);
  170.         printf("%d ", u->vertex);
  171.         adj = u->head;
  172.  
  173.         while (adj) {
  174.             v = &graph.nodes[adj->vertex];
  175.  
  176.             if (v->color == WHITE) {
  177.                 v->color = GREY;
  178.                 v->distance = u->distance + 1;
  179.                 v->parent = u;
  180.                 enqueue(&queue, v);
  181.             }
  182.             adj = adj->next;
  183.         }
  184.         u->color = BLACK;
  185.     }
  186.     printf("\n\nAfter BFS:\n");
  187.  
  188.     for (i = 0; i < graph.V; i++) {
  189.         node = &graph.nodes[i];
  190.         printf("Vertex %d: color=", i);
  191.  
  192.         if (node->color == WHITE) {
  193.             printf("WHITE");
  194.         } else if (node->color == GREY) {
  195.             printf("GREY");
  196.         } else {
  197.             printf("BLACK");
  198.         }
  199.         printf(", distance=");
  200.  
  201.         if (node->distance == INT32_MAX) {
  202.             printf("INF");
  203.         } else {
  204.             printf("%d", node->distance);
  205.         }
  206.         printf(", parent=");
  207.  
  208.         if (node->parent) {
  209.             printf("%d", node->parent->vertex);
  210.         } else {
  211.             printf("NULL");
  212.         }
  213.         printf("\n");
  214.     }
  215. }
  216.  
Advertisement
Add Comment
Please, Sign In to add comment