0% found this document useful (0 votes)
17 views

BFS DFS and BICONNECTED programs

The document provides a C program that implements Breadth-First Search (BFS) and Depth-First Search (DFS) for graphs represented as either an adjacency matrix or an adjacency list. It includes functions for creating nodes, managing a queue, and performing the traversals, as well as functions for inputting graph data and displaying the results. Additionally, it features a separate section for finding biconnected components in a graph using DFS.

Uploaded by

Sekhar Muthangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

BFS DFS and BICONNECTED programs

The document provides a C program that implements Breadth-First Search (BFS) and Depth-First Search (DFS) for graphs represented as either an adjacency matrix or an adjacency list. It includes functions for creating nodes, managing a queue, and performing the traversals, as well as functions for inputting graph data and displaying the results. Additionally, it features a separate section for finding biconnected components in a graph using DFS.

Uploaded by

Sekhar Muthangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Aim: Implement BFT and DFT for given graph when graph is represented by

a)Adjacency matrix b)Adjacency list

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

// Node structure for adjacency list

typedef struct Node {

int vertex;

struct Node* next;

} Node;

// Queue structure for BFS

typedef struct Queue {

int items[MAX];

int front;

int rear;

} Queue;

// Function to create a new node for the adjacency list

Node* createNode(int v) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->vertex = v;

newNode->next = NULL;

return newNode;

// Queue functions for BFS

Queue* createQueue() {

Queue* q = (Queue*)malloc(sizeof(Queue));

q->front = -1;

q->rear = -1;

return q;

}
int isEmpty(Queue* q) {

return q->rear == -1;

void enqueue(Queue* q, int value) {

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++;

if (q->front > q->rear) {

q->front = q->rear = -1;

return item;

// BFS function for adjacency matrix

void bfs_matrix(int adj[MAX][MAX], int n, int start) {

int visited[MAX] = {0};

Queue* q = createQueue();
printf("\nBFS Traversal: ");

visited[start] = 1;

enqueue(q, start);

while (!isEmpty(q)) {

int v = dequeue(q);

printf("%d ", v);

for (int i = 0; i < n; i++) {

if (adj[v][i] == 1 && !visited[i]) {

visited[i] = 1;

enqueue(q, i);

// BFS function for adjacency list

void bfs_list(Node* adjList[MAX], int n, int start) {

int visited[MAX] = {0};

Queue* q = createQueue();

printf("\nBFS Traversal: ");

visited[start] = 1;

enqueue(q, start);

while (!isEmpty(q)) {

int v = dequeue(q);

printf("%d ", v);

Node* temp = adjList[v];

while (temp) {

if (!visited[temp->vertex]) {

visited[temp->vertex] = 1;

enqueue(q, temp->vertex);

}
temp = temp->next;

// DFS function for adjacency matrix

void dfs_matrix(int adj[MAX][MAX], int visited[MAX], int v, int n) {

printf("%d ", v);

visited[v] = 1;

for (int i = 0; i < n; i++) {

if (adj[v][i] == 1 && !visited[i]) {

dfs_matrix(adj, visited, i, n);

// DFS function for adjacency list

void dfs_list(Node* adjList[MAX], int visited[MAX], int v) {

printf("%d ", v);

visited[v] = 1;

Node* temp = adjList[v];

while (temp) {

if (!visited[temp->vertex]) {

dfs_list(adjList, visited, temp->vertex);

temp = temp->next;

// Function to print adjacency matrix

void printAdjMatrix(int adj[MAX][MAX], int n) {

printf("\nAdjacency Matrix:\n");

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {

printf("%d ", adj[i][j]);

printf("\n");

// Function to print adjacency list

void printAdjList(Node* adjList[MAX], int n) {

printf("\nAdjacency List:\n");

for (int i = 0; i < n; i++) {

printf("%d: ", i);

Node* temp = adjList[i];

while (temp) {

printf("%d -> ", temp->vertex);

temp = temp->next;

printf("NULL\n");

int main() {

int n, e, choice, start;

int adjMatrix[MAX][MAX] = {0}; // Adjacency Matrix

Node* adjList[MAX] = {NULL}; // Adjacency List

printf("Enter the number of vertices: ");

scanf("%d", &n);

printf("Enter the number of edges: ");

scanf("%d", &e);

printf("Choose Graph Representation:\n1. Adjacency Matrix\n2. Adjacency List\nEnter your


choice: ");

scanf("%d", &choice);
if (choice == 1) {

printf("You chose Adjacency Matrix.\n");

// Input graph using adjacency matrix

for (int i = 0; i < e; i++) {

int u, v;

printf("Enter edge (u v): ");

scanf("%d %d", &u, &v);

adjMatrix[u][v] = 1;

adjMatrix[v][u] = 1; // Since it's an undirected graph

// Print the adjacency matrix

printAdjMatrix(adjMatrix, n);

} else if (choice == 2) {

printf("You chose Adjacency List.\n");

// Input graph using adjacency list

for (int i = 0; i < e; i++) {

int u, v;

printf("Enter edge (u v): ");

scanf("%d %d", &u, &v);

Node* newNode1 = createNode(v);

newNode1->next = adjList[u];

adjList[u] = newNode1;

Node* newNode2 = createNode(u); // Undirected graph

newNode2->next = adjList[v];

adjList[v] = newNode2;

// Print the adjacency list

printAdjList(adjList, n);

} else {

printf("Invalid choice!\n");

return 1;
}

printf("Choose Traversal Method:\n1. BFS\n2. DFS\nEnter your choice: ");

scanf("%d", &choice);

printf("Enter the starting vertex: ");

scanf("%d", &start);

if (choice == 1) {

printf("You chose BFS.\n");

if (adjMatrix[0][0] != 0) {

bfs_matrix(adjMatrix, n, start);

} else {

bfs_list(adjList, n, start);

} else if (choice == 2) {

printf("You chose DFS.\n");

int visited[MAX] = {0};

if (adjMatrix[0][0] != 0) {

printf("\nDFS Traversal: ");

dfs_matrix(adjMatrix, visited, start, n);

} else {

printf("\nDFS Traversal: ");

dfs_list(adjList, visited, start);

} else {

printf("Invalid choice!\n");

return 0;

_ _ _ _ _ _ _ _ __ __ __ ___ __

Sample Output Walkthrough:


Example 1 (Adjacency Matrix, BFS):

Enter the number of vertices: 5

Enter the number of edges: 5

Choose Graph Representation:

1. Adjacency Matrix

2. Adjacency List

Enter your choice: 1

You chose Adjacency Matrix.

Enter edge (u v): 0 1

Enter edge (u v): 0 2

Enter edge (u v): 1 2

Enter edge (u v): 1 3

Enter edge (u v): 3 4

Adjacency Matrix:

01100

10110

11000

01001

00010

Choose Traversal Method:

1. BFS

2. DFS

Enter your choice: 1

Enter the starting vertex: 0

You chose BFS.

BFS Traversal: 0 1 2 3 4

Example 2 (Adjacency List, DFS):

Enter the number of vertices: 5

Enter the number of edges: 5


Choose Graph Representation:

1. Adjacency Matrix

2. Adjacency List

Enter your choice: 2

You chose Adjacency List.

Enter edge (u v): 0 1

Enter edge (u v): 0 2

Enter edge (u v): 1 2

Enter edge (u v): 1 3

Enter edge (u v): 3 4

Adjacency List:

0: 2 -> 1 -> NULL

1: 3 -> 2 -> 0 -> NULL

2: 1 -> 0 -> NULL

3: 4 -> 1 -> NULL

4: 3 -> NULL

Choose Traversal Method:

1. BFS

2. DFS

Enter your choice: 2

Enter the starting vertex: 0

You chose DFS.

DFS Traversal: 0 2 1 3 4

How to Run the Program in Ubuntu Terminal:

1. Save the program in a file, e.g., graph_with_display.c.

2. Compile the program:

gcc graph_with_display.c -o graph_with_display

Run the program:


./
graph_with_display__________________________________________________________________
________________

Aim: write a program for finding the biconnected components

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

// A structure to represent an edge

typedef struct Edge {

int u, v;

} Edge;

// Global variables

int disc[MAX], low[MAX], parent[MAX], visited[MAX];

Edge stack[MAX];

int time = 0, top = -1;

// Function to add an edge to the stack

void pushEdge(int u, int v) {

stack[++top].u = u;

stack[top].v = v;

// Function to pop an edge from the stack and print a biconnected component

void printBiconnectedComponent(int u, int v) {

printf("Biconnected Component: ");

while (top != -1 && !(stack[top].u == u && stack[top].v == v)) {

printf("(%d, %d) ", stack[top].u, stack[top].v);

top--;

printf("(%d, %d)\n", stack[top].u, stack[top].v);

top--;

// Function to perform DFS and find biconnected components

void dfs(int u, int adj[MAX][MAX], int n) {


visited[u] = 1;

disc[u] = low[u] = ++time;

int children = 0;

for (int v = 0; v < n; v++) {

if (adj[u][v] == 1) {

if (!visited[v]) {

children++;

parent[v] = u;

pushEdge(u, v);

// Recur for DFS

dfs(v, adj, n);

// Check if the subtree rooted at v has a connection back to one of u's ancestors

low[u] = (low[u] < low[v]) ? low[u] : low[v];

// If u is an articulation point, pop all edges from the stack till (u, v)

if ((parent[u] == -1 && children > 1) || (parent[u] != -1 && low[v] >= disc[u])) {

printBiconnectedComponent(u, v);

// Update low value of u for parent function calls

else if (v != parent[u]) {

low[u] = (low[u] < disc[v]) ? low[u] : disc[v];

pushEdge(u, v);

// Function to find biconnected components in a graph

void findBiconnectedComponents(int adj[MAX][MAX], int n) {

for (int i = 0; i < n; i++) {

parent[i] = -1;
visited[i] = 0;

disc[i] = low[i] = -1;

// Perform DFS for each component

for (int i = 0; i < n; i++) {

if (!visited[i]) {

dfs(i, adj, n);

// Print any remaining edges in the stack

while (top != -1) {

printf("Biconnected Component: ");

while (top != -1) {

printf("(%d, %d) ", stack[top].u, stack[top].v);

top--;

printf("\n");

int main() {

int n, e;

int adj[MAX][MAX] = {0};

printf("Enter the number of vertices: ");

scanf("%d", &n);

printf("Enter the number of edges: ");

scanf("%d", &e);

printf("Enter the edges (u v):\n");

for (int i = 0; i < e; i++) {

int u, v;

scanf("%d %d", &u, &v);

adj[u][v] = 1;
adj[v][u] = 1;

findBiconnectedComponents(adj, n);

return 0;

Output:

Enter the number of vertices: 5

Enter the number of edges: 5

Enter the edges (u v):

01

02

12

13

34

Biconnected Component: (1, 3) (3, 4)

Biconnected Component: (0, 2) (1, 2) (0, 1)

You might also like