-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
70 deletions.
There are no files selected for viewing
140 changes: 70 additions & 70 deletions
140
Unit - 2/Basic Traversal and Search Techniques/C implementation/BFSCyclic.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,98 @@ | ||
//Program to check if graph is cyclic or not using bfs | ||
#include<stdio.h> | ||
#define MAX 100 | ||
//Program to check if graph is cyclic or not using bfs | ||
#include<stdio.h> | ||
#define MAX 100 | ||
|
||
int count = 0, queue[MAX], front = 0, rear = -1; | ||
void insertQueue(int element) | ||
int count = 0, queue[MAX], front = 0, rear = -1; | ||
void insertQueue(int element) | ||
{ | ||
queue[++rear] = element; | ||
} | ||
|
||
void deleteQueue() | ||
{ | ||
queue[front++]; | ||
if(front > rear) | ||
{ | ||
queue[++rear] = element; | ||
front = 0; | ||
rear = -1; | ||
} | ||
} | ||
|
||
void deleteQueue() | ||
void readMatrix(int matrix[][MAX], int n) | ||
{ | ||
int i, j; | ||
for(i = 0; i < n; i++) | ||
{ | ||
queue[front++]; | ||
if(front > rear) | ||
for(j = 0; j < n; j++) | ||
{ | ||
front = 0; | ||
rear = -1; | ||
scanf("%d", &matrix[i][j]); | ||
} | ||
} | ||
} | ||
|
||
void readMatrix(int matrix[][MAX], int n) | ||
void markUnvisited(int visitedNodes[], int nodeCount) | ||
{ | ||
int i; | ||
for(i = 0; i < nodeCount; i++) | ||
{ | ||
int i, j; | ||
for(i = 0; i < n; i++) | ||
{ | ||
for(j = 0; j < n; j++) | ||
{ | ||
scanf("%d", &matrix[i][j]); | ||
} | ||
} | ||
visitedNodes[i] = 0; | ||
} | ||
} | ||
|
||
void markUnvisited(int visitedNodes[], int nodeCount) | ||
int bfs(int v, int graph[][MAX], int visitedNodes[], int nodeCount) | ||
{ | ||
insertQueue(v); | ||
while (front <= rear) | ||
{ | ||
int i; | ||
for(i = 0; i < nodeCount; i++) | ||
int frontElement = queue[front], w; | ||
deleteQueue(); | ||
if (visitedNodes[frontElement] != 0 || graph[frontElement][frontElement] == 1) | ||
{ | ||
visitedNodes[i] = 0; | ||
return 1; | ||
} | ||
} | ||
|
||
int bfs(int v, int graph[][MAX], int visitedNodes[], int nodeCount) | ||
{ | ||
insertQueue(v); | ||
while (front <= rear) | ||
count++; | ||
visitedNodes[frontElement] = count; | ||
for (w = 0; w < nodeCount; w++) | ||
{ | ||
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++) | ||
if (graph[frontElement][w] == 1 && visitedNodes[w] == 0) | ||
{ | ||
if (graph[frontElement][w] == 1 && visitedNodes[w] == 0) | ||
{ | ||
insertQueue(w); | ||
} | ||
insertQueue(w); | ||
} | ||
} | ||
return 0; | ||
} | ||
return 0; | ||
} | ||
|
||
int isCyclic(int graph[][MAX], int visitedNodes[], int nodeCount) | ||
int isCyclic(int graph[][MAX], int visitedNodes[], int nodeCount) | ||
{ | ||
int v; | ||
for(v = 0; v < nodeCount; v++) | ||
{ | ||
int v; | ||
for(v = 0; v < nodeCount; v++) | ||
if(visitedNodes[v] == 0) | ||
{ | ||
if(visitedNodes[v] == 0) | ||
{ | ||
if(bfs(v, graph, visitedNodes, nodeCount) == 1){ | ||
return 1; | ||
} | ||
if(bfs(v, graph, visitedNodes, nodeCount) == 1){ | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
return 0; | ||
} | ||
|
||
int main() | ||
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)) | ||
{ | ||
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; | ||
} | ||
printf("The graph is cyclic\n"); | ||
} | ||
else | ||
{ | ||
printf("The graph is acyclic\n"); | ||
} | ||
return 0; | ||
} |