0% found this document useful (0 votes)
20 views5 pages

Experiment 8

The document outlines an experiment to implement the Breadth First Search (BFS) algorithm using a linked list representation of a graph. It details the theory behind BFS, the algorithm steps, and provides a C program that demonstrates the implementation, including the creation of an adjacency list and the BFS traversal process. The time complexity of the method is O(V + E) and the auxiliary space complexity is also O(V + E).

Uploaded by

Kartikeya Singh
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)
20 views5 pages

Experiment 8

The document outlines an experiment to implement the Breadth First Search (BFS) algorithm using a linked list representation of a graph. It details the theory behind BFS, the algorithm steps, and provides a C program that demonstrates the implementation, including the creation of an adjacency list and the BFS traversal process. The time complexity of the method is O(V + E) and the auxiliary space complexity is also O(V + E).

Uploaded by

Kartikeya Singh
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/ 5

Experiment No.

8
Objective: To implement BFS using linked list.

Theory: The Breadth First Search (BFS) traversal is an algorithm, which is used to
visit all of the nodes of a given graph. In this traversal algorithm one node is
selected and then all of the adjacent nodes are visited one by one. After
completing all of the adjacent vertices, it moves further to check another vertices
and checks its adjacent vertices again. This process will continue until all nodes
are visited.

Here, are important rules for using BFS algorithm:

 A queue (FIFO-First in First Out) data structure is used by BFS.


 You mark any node in the graph as root and start traversing the data from
it.
 BFS traverses all the nodes in the graph and keeps dropping them as
completed.
 BFS visits an adjacent unvisited node, marks it as done, and inserts it into a
queue.
 Removes the previous vertex from the queue in case no adjacent vertex is
found.
 BFS algorithm iterates until all the vertices in the graph are successfully
traversed and marked as completed.
 There are no loops caused by BFS during the traversing of data from any
node.

Consider the following graph:


Traversal of the above graph Using BFS will be: 1-2-3-5-4.

According to BFS traversal method, first step is to visit any vertex, so we have
visited 1. Next step is to explore that visited vertex, that means we have to visit
adjacent vertex of 1, so we have visited 2,3 and 5. Again we have to visit any
unvisited vertex, so we have visited 4.

Algorithm:

1. Initialization:
1.1. Create an empty adjacency list adjList with size equal to the number of
vertices.
1.2. For each vertex, initialize its adjacency list as empty.

2. Add Edges:
2.1. For each edge (u, v) in edges[], do the following:
2.2. Create a new node with value v.
2.3. Insert this new node at the beginning of the adjacency list of vertex u.

3. Breadth-First Search (BFS):


3.1. Create a visited array with size equal to the number of vertices,
initialized to 0 (unvisited).
3.2. Create an empty queue queue.
3.3. Mark the startNode as visited (visited[startNode] = 1).
3.4. Enqueue startNode into queue.
3.5. While queue is not empty, repeat the following steps:
 Dequeue a vertex currentNode from queue.
 Print currentNode.
 For each adjacent vertex neighbor of currentNode in
adjList[currentNode]:
 If neighbor is not visited, mark it as visited (visited[neighbor] = 1).
 Enqueue neighbor into queue.
4. End of BFS Traversal
Program:

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

#define MAX_VERTICES 100

// Structure to represent a node in adjacency list


struct Node {
int data;
struct Node* next;
};

// Function to create a new node


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

// Function to add an edge to the graph


void addEdge(struct Node* adjList[], int u, int v)
{
struct Node* newNode = createNode(v);
newNode->next = adjList[u];
adjList[u] = newNode;
}

// Function to perform Breadth First Search on a graph


// represented using adjacency list
void bfs(struct Node* adjList[], int vertices,
int startNode, int visited[])
{
// Create a queue for BFS
int queue[MAX_VERTICES];
int front = 0, rear = 0;

// Mark the current node as visited and enqueue it


visited[startNode] = 1;
queue[rear++] = startNode;

// Iterate over the queue


while (front != rear) {
// Dequeue a vertex from queue and print it
int currentNode = queue[front++];
printf("%d ", currentNode);

// Get all adjacent vertices of the dequeued vertex


// currentNode If an adjacent has not been visited,
// then mark it visited and enqueue it
struct Node* temp = adjList[currentNode];
while (temp != NULL) {
int neighbor = temp->data;
if (!visited[neighbor]) {
visited[neighbor] = 1;
queue[rear++] = neighbor;
}
temp = temp->next;
}
}
}

int main()
{
// Number of vertices in the graph
int vertices = 5;
// Adjacency list representation of the graph
struct Node* adjList[vertices];
for (int i = 0; i < vertices; ++i)
adjList[i] = NULL;
// Add edges to the graph
addEdge(adjList, 0, 1);
addEdge(adjList, 0, 2);
addEdge(adjList, 1, 3);
addEdge(adjList, 1, 4);
addEdge(adjList, 2, 4);
// Mark all the vertices as not visited
int visited[vertices];
for (int i = 0; i < vertices; ++i)
visited[i] = 0;
// Perform BFS traversal starting from vertex 0
printf(
"Breadth First Traversal starting from vertex 0: ");
bfs(adjList, vertices, 0, visited);

return 0;
}

Output:
Breadth First Traversal starting from vertex 0: 0 2 1 4 3

The complexity of the above method:

Time Complexity: O(V + E) Auxiliary Space: O(V + E)

where V is the number of vertices and E is the number of edges.

You might also like