BFS DFS and BICONNECTED programs
BFS DFS and BICONNECTED programs
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
int items[MAX];
int front;
int rear;
} Queue;
Node* createNode(int v) {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = -1;
q->rear = -1;
return q;
}
int isEmpty(Queue* q) {
if (q->rear == MAX - 1)
printf("\nQueue is full!\n");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
int dequeue(Queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
return -1;
} else {
item = q->items[q->front];
q->front++;
return item;
Queue* q = createQueue();
printf("\nBFS Traversal: ");
visited[start] = 1;
enqueue(q, start);
while (!isEmpty(q)) {
int v = dequeue(q);
visited[i] = 1;
enqueue(q, i);
Queue* q = createQueue();
visited[start] = 1;
enqueue(q, start);
while (!isEmpty(q)) {
int v = dequeue(q);
while (temp) {
if (!visited[temp->vertex]) {
visited[temp->vertex] = 1;
enqueue(q, temp->vertex);
}
temp = temp->next;
visited[v] = 1;
visited[v] = 1;
while (temp) {
if (!visited[temp->vertex]) {
temp = temp->next;
printf("\nAdjacency Matrix:\n");
printf("\n");
printf("\nAdjacency List:\n");
while (temp) {
temp = temp->next;
printf("NULL\n");
int main() {
scanf("%d", &n);
scanf("%d", &e);
scanf("%d", &choice);
if (choice == 1) {
int u, v;
adjMatrix[u][v] = 1;
printAdjMatrix(adjMatrix, n);
} else if (choice == 2) {
int u, v;
newNode1->next = adjList[u];
adjList[u] = newNode1;
newNode2->next = adjList[v];
adjList[v] = newNode2;
printAdjList(adjList, n);
} else {
printf("Invalid choice!\n");
return 1;
}
scanf("%d", &choice);
scanf("%d", &start);
if (choice == 1) {
if (adjMatrix[0][0] != 0) {
bfs_matrix(adjMatrix, n, start);
} else {
bfs_list(adjList, n, start);
} else if (choice == 2) {
if (adjMatrix[0][0] != 0) {
} else {
} else {
printf("Invalid choice!\n");
return 0;
_ _ _ _ _ _ _ _ __ __ __ ___ __
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix:
01100
10110
11000
01001
00010
1. BFS
2. DFS
BFS Traversal: 0 1 2 3 4
1. Adjacency Matrix
2. Adjacency List
Adjacency List:
4: 3 -> NULL
1. BFS
2. DFS
DFS Traversal: 0 2 1 3 4
#include <stdio.h>
#include <stdlib.h>
int u, v;
} Edge;
// Global variables
Edge stack[MAX];
stack[++top].u = u;
stack[top].v = v;
// Function to pop an edge from the stack and print a biconnected component
top--;
top--;
int children = 0;
if (adj[u][v] == 1) {
if (!visited[v]) {
children++;
parent[v] = u;
pushEdge(u, v);
// Check if the subtree rooted at v has a connection back to one of u's ancestors
// If u is an articulation point, pop all edges from the stack till (u, v)
printBiconnectedComponent(u, v);
else if (v != parent[u]) {
pushEdge(u, v);
parent[i] = -1;
visited[i] = 0;
if (!visited[i]) {
top--;
printf("\n");
int main() {
int n, e;
scanf("%d", &n);
scanf("%d", &e);
int u, v;
adj[u][v] = 1;
adj[v][u] = 1;
findBiconnectedComponents(adj, n);
return 0;
Output:
01
02
12
13
34