From a81836bfc0fc3093ae4598694e80913f70b132b4 Mon Sep 17 00:00:00 2001 From: JoyM268 <125041935+JoyM268@users.noreply.github.com> Date: Sun, 9 Jun 2024 18:23:00 +0530 Subject: [PATCH] Add File --- Assignment programs/BFSCyclic.c | 119 ++++++++++++++++---------------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/Assignment programs/BFSCyclic.c b/Assignment programs/BFSCyclic.c index 0176465..c29b63d 100644 --- a/Assignment programs/BFSCyclic.c +++ b/Assignment programs/BFSCyclic.c @@ -1,98 +1,99 @@ -//Program to check if graph is cyclic or not using bfs -#include +// Program to check if graph is cyclic or not using bfs +#include #define MAX 100 int count = 0, queue[MAX], front = 0, rear = -1; void insertQueue(int element) { -queue[++rear] = element; + queue[++rear] = element; } void deleteQueue() { -queue[front++]; -if(front > rear) -{ - front = 0; - rear = -1; -} + queue[front++]; + if (front > rear) + { + front = 0; + rear = -1; + } } void readMatrix(int matrix[][MAX], int n) { -int i, j; -for(i = 0; i < n; i++) -{ - for(j = 0; j < n; j++) + int i, j; + for (i = 0; i < n; i++) { - scanf("%d", &matrix[i][j]); + for (j = 0; j < n; j++) + { + scanf("%d", &matrix[i][j]); + } } } -} void markUnvisited(int visitedNodes[], int nodeCount) { -int i; -for(i = 0; i < nodeCount; i++) -{ - visitedNodes[i] = 0; -} + int i; + for (i = 0; i < nodeCount; i++) + { + visitedNodes[i] = 0; + } } int bfs(int v, int graph[][MAX], int visitedNodes[], int nodeCount) { -insertQueue(v); -while (front <= rear) -{ - int frontElement = queue[front], w; - deleteQueue(); - if (visitedNodes[frontElement] != 0 || graph[frontElement][frontElement] == 1) - { - return 1; - } - count++; - visitedNodes[frontElement] = count; - for (w = 0; w < nodeCount; w++) + insertQueue(v); + while (front <= rear) { - if (graph[frontElement][w] == 1 && visitedNodes[w] == 0) + int frontElement = queue[front], w; + deleteQueue(); + if (visitedNodes[frontElement] != 0 || graph[frontElement][frontElement] == 1) { - insertQueue(w); + return 1; + } + count++; + visitedNodes[frontElement] = count; + for (w = 0; w < nodeCount; w++) + { + if (graph[frontElement][w] == 1 && visitedNodes[w] == 0) + { + insertQueue(w); + } } } -} -return 0; + return 0; } int isCyclic(int graph[][MAX], int visitedNodes[], int nodeCount) { -int v; -for(v = 0; v < nodeCount; v++) -{ - if(visitedNodes[v] == 0) + int v; + for (v = 0; v < nodeCount; v++) { - if(bfs(v, graph, visitedNodes, nodeCount) == 1){ - return 1; + if (visitedNodes[v] == 0) + { + if (bfs(v, graph, visitedNodes, nodeCount) == 1) + { + return 1; + } } } -} -return 0; + return 0; } int main() { -int nodeCount, graph[MAX][MAX], visitedNodes[MAX]; -printf("Enter the number of nodes: "); -scanf("%d", &nodeCount); -printf("Enter the adjacency matrix:\n"); -readMatrix(graph, nodeCount); -markUnvisited(visitedNodes, nodeCount); -if(isCyclic(graph, visitedNodes, nodeCount)) -{ - printf("The graph is cyclic\n"); -} -else -{ - printf("The graph is acyclic\n"); -} -return 0; + int nodeCount, graph[MAX][MAX], visitedNodes[MAX]; + printf("Enter the number of nodes: "); + scanf("%d", &nodeCount); + printf("Enter the adjacency matrix:\n"); + readMatrix(graph, nodeCount); + markUnvisited(visitedNodes, nodeCount); + if (isCyclic(graph, visitedNodes, nodeCount)) + { + printf("The graph is cyclic\n"); + } + else + { + printf("The graph is acyclic\n"); + } + return 0; } \ No newline at end of file