Breadth-First Search with examples using C#
Breadth-first search (BFS) is an algorithm for traversing or searching a graph data structure. It starts at the tree root (or some arbitrary node of a graph) and explores the neighbor nodes first, before moving to the next level of neighbors. This search algorithm is often used when traversing a graph or tree to find the shortest path from the starting node to the goal node.
Here is an example of a breadth-first search algorithm implemented in C#:
// Breadth-first search algorithm
// Graph class
public class Graph
{
// Dictionary to store the edges in the graph
Dictionary<int, List<int>> edges = new Dictionary<int, List<int>>();
// Method to add an edge to the graph
public void AddEdge(int u, int v)
{
if (!edges.ContainsKey(u))
edges.Add(u, new List<int>());
edges[u].Add(v);
}
// Method to perform breadth-first search
public List<int> BreadthFirstSearch(int start)
{
// List to store the visited nodes
List<int> visited = new List<int>();
// Queue to store the nodes to be visited
Queue<int> queue = new Queue<int>();
// Add the starting node to the queue
queue.Enqueue(start);
// Loop until the queue is empty
while (queue.Count > 0)
{
// Dequeue a node from the queue
int node = queue.Dequeue();
// If the node has not been visited
if (!visited.Contains(node))
{
// Mark the node as visited
visited.Add(node);
// Enqueue the neighbors of the node
foreach (var neighbor in edges[node])
queue.Enqueue(neighbor);
}
}
// Return the list of visited nodes
return visited;
}
}
This Graph
class uses a dictionary to store the edges in the graph, with the keys representing the starting node of the edge and the values representing the ending nodes of the edge. The BreadthFirstSearch
method performs a breadth-first search by starting at the given starting node and enqueuing its neighbors in a queue. Then, it takes the nodes off the queue and adds their neighbors who haven't been visited yet until the queue is empty. The list of visited nodes is returned at the end of the search.
Here is an example of using the BreadthFirstSearch
method:
// Create a new graph
Graph graph = new Graph();
// Add some edges to the graph
graph.AddEdge(1, 2);
graph.AddEdge(1, 3);
graph.AddEdge(1, 4);
graph.AddEdge(2, 5);
graph.AddEdge(3, 6);
graph.AddEdge(3, 7);
// Perform a breadth-first search starting at node 1
List<int> visited = graph.BreadthFirstSearch(1);
// Print the visited nodes
foreach (var node in visited)
Console.Write(node + " "); // Output: 1 2 3 4 5 6 7
Here are some pros and cons of the breadth-first search algorithm:
Pros:
- It is guaranteed to find the shortest path from the starting node to the goal node.
- It is easy to implement and understand.
Cons:
- It requires a large amount of memory to store the nodes in the queue, especially in graphs with a large number of nodes.
- It may be slower than other search algorithms, such as depth-first search, in some cases.
Overall, breadth-first search is a useful algorithm for finding the shortest path in a graph or tree. It is especially useful in scenarios where the graph is unweighted or the weights of the edges are all the same. It is not as efficient as other search algorithms in some cases, but its simplicity and guarantee of finding the shortest path make it a valuable tool in many situations.