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

BFS-DFS

The document provides C code implementations for Breadth-First Search (BFS) and Depth-First Search (DFS) traversals on a graph represented by an adjacency matrix. It includes functions to add edges, perform BFS and DFS, and handle user input for vertices and edges while ensuring input validation. The output demonstrates the order of visited vertices for both BFS and DFS based on user-defined starting points.

Uploaded by

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

BFS-DFS

The document provides C code implementations for Breadth-First Search (BFS) and Depth-First Search (DFS) traversals on a graph represented by an adjacency matrix. It includes functions to add edges, perform BFS and DFS, and handle user input for vertices and edges while ensuring input validation. The output demonstrates the order of visited vertices for both BFS and DFS based on user-defined starting points.

Uploaded by

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

Breadth-First Search (BFS)

Write a C program to perform BFS (Breadth-First Search) traversal on a graph.


Print the order of visited vertices.

Sample Solution:

#include <stdio.h>
#include <stdlib.h>

#define MAX_VERTICES 100

// Function to add an edge to the graph


void addEdge(int graph[MAX_VERTICES][MAX_VERTICES], int start, int end)
{

graph[start][end] = 1;
graph[end][start] = 1; // For undirected graph

// Function to perform BFS traversal


void BFS(int graph[MAX_VERTICES][MAX_VERTICES], int vertices, int
startVertex) {

int visited[MAX_VERTICES] = {0}; // Initialize all vertices as not visited

int queue[MAX_VERTICES];

int front = -1, rear = -1;


// Mark the startVertex as visited and enqueue it
visited[startVertex] = 1;

queue[++rear] = startVertex;

printf("BFS Traversal Order: ");

while (front != rear) {


int currentVertex = queue[++front];

printf("%d ", currentVertex);

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


if (graph[currentVertex][i] == 1 && !visited[i]) {

visited[i] = 1;

queue[++rear] = i;
}

}
}

printf("\n");

int main() {
int vertices, edges;
// Input the number of vertices

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

scanf("%d", &vertices);

if (vertices <= 0 || vertices > MAX_VERTICES) {

printf("Invalid number of vertices. Exiting...\n");


return 1;

int graph[MAX_VERTICES][MAX_VERTICES] = {0}; // Initialize the


adjacency matrix with zeros

// Input the number of edges


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

scanf("%d", &edges);

if (edges < 0 || edges > vertices * (vertices - 1) / 2) {

printf("Invalid number of edges. Exiting...\n");

return 1;
}

// Input edges and construct the adjacency matrix


for (int i = 0; i < edges; i++) {
int start, end;

printf("Input edge %d (start end): ", i + 1);

scanf("%d %d", &start, &end);

// Validate input vertices

if (start < 0 || start >= vertices || end < 0 || end >= vertices) {
printf("Invalid vertices. Try again.\n");

i--;
continue;

addEdge(graph, start, end);

// Input the starting vertex for BFS traversal


int startVertex;

printf("Input the starting vertex for BFS traversal: ");

scanf("%d", &startVertex);

// Perform BFS traversal

BFS(graph, vertices, startVertex);


return 0;
}

Output:
Input the number of vertices: 5
Input the number of edges: 6
Input edge 1 (start end): 0 1
Input edge 2 (start end): 1 2
Input edge 3 (start end): 2 3
Input edge 4 (start end): 3 4
Input edge 5 (start end): 4 0
Input edge 6 (start end): 2 4
Input the starting vertex for BFS traversal: 0
BFS Traversal Order: 0 1 4 2 3
Explanation:

In the exercise above,

 Header Files:
o #include <stdio.h>: Provides input and output functions.
o #include <stdlib.h>: Provides memory allocation and other utility
functions.
 Macro Definition:
o #define MAX_VERTICES 100: Defines the maximum number of
vertices in the graph.
 Function to Add an Edge:
o void addEdge(int graph[MAX_VERTICES][MAX_VERTICES], int
start, int end): Adds an edge between two vertices in the adjacency
matrix, indicating a connection in the graph.
 Function to Perform BFS Traversal:
o void BFS(int graph[MAX_VERTICES][MAX_VERTICES], int
vertices, int startVertex): Performs BFS traversal on the graph starting
from the specified vertex.
o Uses a queue to keep track of visited vertices and their neighbors.
o Prints the order of visited vertices.
 Main Function:
o Reads the number of vertices (vertices) and edges (edges) from the
user.
o Constructs an adjacency matrix (graph) to represent the graph.
o Takes input for edges, validates input vertices, and adds edges to the
graph.
o Takes input for the starting vertex for BFS traversal (startVertex).
o Calls the BFS function to perform BFS traversal and prints the order
of visited vertices.

Depth-First Search (DFS)

Sample Solution:

#include <stdio.h>

#define MAX_VERTICES 100

// Function to perform Depth-First Search (DFS) traversal


void DFS(int graph[MAX_VERTICES][MAX_VERTICES], int
visited[MAX_VERTICES], int vertices, int start) {

printf("%d ", start); // Print the current vertex

visited[start] = 1; // Mark the current vertex as visited

// Visit all adjacent vertices

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

if (graph[start][i] == 1 && !visited[i]) {


DFS(graph, visited, vertices, i);
}

}
}

int main() {
int vertices, edges;

// Input the number of vertices

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

scanf("%d", &vertices);

if (vertices <= 0 || vertices > MAX_VERTICES) {

printf("Invalid number of vertices. Exiting...\n");

return 1;
}

int graph[MAX_VERTICES][MAX_VERTICES] = {0}; // Initialize the


adjacency matrix with zeros

int visited[MAX_VERTICES] = {0}; // Initialize the visited array with


zeros

// Input the number of edges


printf("Enter the number of edges: ");
scanf("%d", &edges);

if (edges < 0 || edges > vertices * (vertices - 1)) {

printf("Invalid number of edges. Exiting...\n");

return 1;
}

// Input edges and construct the adjacency matrix

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

int start, end;

printf("Enter edge %d (start end): ", i + 1);


scanf("%d %d", &start, &end);

// Validate input vertices

if (start < 0 || start >= vertices || end < 0 || end >= vertices) {
printf("Invalid vertices. Try again.\n");
i--;

continue;

graph[start][end] = 1;

// For undirected graph, uncomment the following line:


// graph[end][start] = 1;

// Input the starting vertex for DFS traversal


int startVertex;

printf("Enter the starting vertex for DFS traversal: ");

scanf("%d", &startVertex);

if (startVertex < 0 || startVertex >= vertices) {


printf("Invalid starting vertex. Exiting...\n");

return 1;

printf("DFS Traversal Order: ");

DFS(graph, visited, vertices, startVertex);


return 0;
}

Output:
Enter the number of vertices: 5
Enter the number of edges: 6
Enter edge 1 (start end): 0 1
Enter edge 2 (start end): 1 2
Enter edge 3 (start end): 2 3
Enter edge 4 (start end): 3 4
Enter edge 5 (start end): 4 0
Enter edge 6 (start end): 2 4
Enter the starting vertex for DFS traversal: 2
DFS Traversal Order: 2 3 4 0 1
Explanation:

The above C exercises perform Depth-First Search (DFS) traversal on a graph


represented by an adjacency matrix. Here's a brief explanation:

 DFS Function:
o The "DFS()" function takes the graph, an array to track visited
vertices, the total number of vertices, and the starting vertex as
parameters.
o It prints the current vertex, marks it as visited, and then recursively
explores all unvisited adjacent vertices.
 Main Function:
o It takes user input for the number of vertices and edges.
o Constructs the adjacency matrix based on user input for edges.
o Takes the starting vertex for DFS traversal.
o Calls the "DFS()" function with the provided parameters.
o Outputs the DFS traversal order.
 User Input:
o The user is prompted to input the number of vertices and edges.
o For each edge, the user inputs the starting and ending vertices.
 Output:
o The program outputs the DFS traversal order starting at the specified
vertex.

You might also like