0% found this document useful (0 votes)
10 views6 pages

BFS, DFS

The document outlines a lab focused on implementing and understanding Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for graph traversal. It explains the theory behind these algorithms, their applications, and provides C code for their implementation using an adjacency list representation of a graph. The lab aims to help learners explore all nodes in a graph, useful for tasks like pathfinding and cycle detection.

Uploaded by

mahadirouja.diu
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)
10 views6 pages

BFS, DFS

The document outlines a lab focused on implementing and understanding Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for graph traversal. It explains the theory behind these algorithms, their applications, and provides C code for their implementation using an adjacency list representation of a graph. The lab aims to help learners explore all nodes in a graph, useful for tasks like pathfinding and cycle detection.

Uploaded by

mahadirouja.diu
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/ 6

Lab Name

Graph Traversal Algorithms: Depth-First Search (DFS) and Breadth-First Search (BFS)

Objective
The objective of this lab is to implement and understand the Depth-First Search (DFS) and
Breadth-First Search (BFS) algorithms. These traversal methods allow us to explore all nodes in
a graph, which is useful in various applications like finding paths, detecting cycles, and
determining connected components.

Theory
Graphs consist of nodes (vertices) connected by edges. The two primary graph traversal
techniques are:

1. Depth-First Search (DFS):


DFS explores as far as possible along each branch before backtracking, making it a
depth-oriented traversal. It can be implemented using recursion or a stack. DFS is used
in applications like cycle detection, pathfinding, and topological sorting.
2. Breadth-First Search (BFS):
BFS explores all neighbors at the current level before moving to the next level, making it
a breadth-oriented traversal. It uses a queue and is often applied to find the shortest
path in unweighted graphs and level-order traversal in tree structures.

Graph Representation - Adjacency List:


An adjacency list is used to represent the graph, where each vertex has a linked list of its
adjacent vertices. This approach is memory-efficient and effective for sparse graphs.

Code with Output

Below is the C code implementing DFS and BFS for an undirected graph.

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

#define MAX 100

struct Node {
int vertex;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
int* visited;
};

struct Queue {
int items[MAX];
int front;
int rear;
};

// Function to create a node


struct Node* createNode(int v) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}

// Function to create a graph


struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct Node*));


graph->visited = malloc(vertices * sizeof(int));

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


graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}

return graph;
}

// Function to add an edge to the graph


void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// Add edge from dest to src (since the graph is undirected)
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}

// DFS algorithm
void DFS(struct Graph* graph, int vertex) {
struct Node* adjList = graph->adjLists[vertex];
struct Node* temp = adjList;

graph->visited[vertex] = 1;
printf("%d ", vertex);

while (temp != NULL) {


int connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}

// Function to create a queue


struct Queue* createQueue() {
struct Queue* queue = malloc(sizeof(struct Queue));
queue->front = -1;
queue->rear = -1;
return queue;
}

// Check if the queue is empty


int isEmpty(struct Queue* queue) {
return queue->rear == -1;
}

// Adding elements into queue


void enqueue(struct Queue* queue, int value) {
if (queue->rear == MAX - 1)
return;
else {
if (queue->front == -1)
queue->front = 0;
queue->rear++;
queue->items[queue->rear] = value;
}
}

// Removing elements from queue


int dequeue(struct Queue* queue) {
int item;
if (isEmpty(queue)) {
item = -1;
} else {
item = queue->items[queue->front];
queue->front++;
if (queue->front > queue->rear) {
queue->front = queue->rear = -1;
}
}
return item;
}

// BFS algorithm
void BFS(struct Graph* graph, int startVertex) {
struct Queue* queue = createQueue();

graph->visited[startVertex] = 1;
enqueue(queue, startVertex);

printf("BFS traversal starting from vertex %d: ", startVertex);

while (!isEmpty(queue)) {
int currentVertex = dequeue(queue);
printf("%d ", currentVertex);

struct Node* temp = graph->adjLists[currentVertex];

while (temp) {
int adjVertex = temp->vertex;

if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(queue, adjVertex);
}
temp = temp->next;
}
}
printf("\n");
}

int main() {
struct Graph* graph = createGraph(5);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);

printf("DFS traversal starting from vertex 0: ");


DFS(graph, 0);
printf("\n");

// Reset visited array for BFS


for (int i = 0; i < graph->numVertices; i++)
graph->visited[i] = 0;

BFS(graph, 0);

return 0;
}

You might also like