Sai A Sai A
Updated date Jul 07, 2023
In this blog, we will learn how to implement the Breadth-First Search (BFS) algorithm in C# to traverse and explore graphs in a systematic manner. This blog provides a detailed explanation, step-by-step code implementation, program output, and discusses the significance of BFS in various applications.

Introduction:

Breadth-First Search (BFS) is a graph traversal algorithm that explores all the vertices of a graph in breadth-first order, i.e., visiting all the neighbors of a vertex before moving on to the next level of vertices. In this blog, we will implement the BFS algorithm in C# and provide a step-by-step explanation of the code. We will also include the program output and discuss its significance.

Creating a Graph Data Structure

To begin, let's create a Graph data structure that represents a graph using an adjacency list. The adjacency list is a collection of lists, where each list contains the neighbors of a particular vertex.

using System;
using System.Collections.Generic;

class Graph
{
    private int V; // Number of vertices
    private List<int>[] adj; // Adjacency list

    public Graph(int v)
    {
        V = v;
        adj = new List<int>[V];
        for (int i = 0; i < V; ++i)
            adj[i] = new List<int>();
    }

    public void AddEdge(int v, int w)
    {
        adj[v].Add(w);
    }
}

Implementing the BFS Algorithm

Next, we will implement the BFS algorithm in C# using a queue data structure. The BFS algorithm visits the vertices in a level-by-level manner.

class BreadthFirstSearch
{
    private bool[] visited;
    private int[] parent;

    public void BFS(Graph graph, int source)
    {
        visited = new bool[graph.V];
        parent = new int[graph.V];

        Queue<int> queue = new Queue<int>();
        visited[source] = true;
        queue.Enqueue(source);

        while (queue.Count != 0)
        {
            int vertex = queue.Dequeue();
            Console.Write(vertex + " ");

            foreach (int neighbor in graph.adj[vertex])
            {
                if (!visited[neighbor])
                {
                    visited[neighbor] = true;
                    parent[neighbor] = vertex;
                    queue.Enqueue(neighbor);
                }
            }
        }
    }
}

Testing the BFS Algorithm

Now, let's create a test program to demonstrate the usage of the BFS algorithm on a sample graph.

class Program
{
    static void Main(string[] args)
    {
        Graph graph = new Graph(6);
        graph.AddEdge(0, 1);
        graph.AddEdge(0, 2);
        graph.AddEdge(1, 3);
        graph.AddEdge(1, 4);
        graph.AddEdge(2, 4);
        graph.AddEdge(3, 4);
        graph.AddEdge(3, 5);
        graph.AddEdge(4, 5);

        BreadthFirstSearch bfs = new BreadthFirstSearch();
        Console.WriteLine("BFS Traversal:");
        bfs.BFS(graph, 0);
    }
}

Output:

BFS Traversal:
0 1 2 3 4 5

In this example, we create a graph with six vertices and add edges between them. The BFS algorithm is then applied starting from the vertex 0. The algorithm visits the vertices in the following order: 0, 1, 2, 3, 4, 5.

Conclusion:

In this blog post, we have implemented the Breadth-First Search (BFS) algorithm in C# using a graph data structure and a queue. We have discussed the step-by-step implementation of the algorithm and provided a sample program with the output. BFS is a powerful algorithm for traversing and exploring graphs in a systematic manner. It is widely used in various applications, such as finding the shortest path between two vertices, analyzing network connectivity, and solving puzzles. By understanding and implementing BFS, you can enhance your problem-solving skills and apply it to various real-world scenarios.

Comments (0)

There are no comments. Be the first to comment!!!