Breadth First Search or BFS for a Graph 您所在的位置:网站首页 bfs大学 Breadth First Search or BFS for a Graph

Breadth First Search or BFS for a Graph

2024-07-11 21:25| 来源: 网络整理| 查看: 265

Breadth First Search (BFS) is a fundamental graph traversal algorithm . It involves visiting all the connected nodes of a graph in a level-by-level manner. In this article, we will look into the concept of BFS and how it can be applied to graphs effectively

Table of Content

Breadth First Search or BFS for a Graph Relation between BFS for Graph and BFS for Tree Breadth First Search (BFS) for a Graph Algorithm How Does the BFS Algorithm Work? Implementation of BFS for Graph using Adjacency List Complexity Of Breadth-First Search (BFS) Algorithm Applications of BFS in Graphs Problems on Breadth First Search or BFS for a Graph FAQs on Breadth First Search (BFS) for a Graph Breadth First Search (BFS) for a Graph:

Breadth First Search (BFS) is a graph traversal algorithm that explores all the vertices in a graph at the current depth before moving on to the vertices at the next depth level. It starts at a specified vertex and visits all its neighbors before moving on to the next level of neighbors. BFS is commonly used in algorithms for pathfinding, connected components, and shortest path problems in graphs.

Relation between BFS for Graph and BFS for Tree:

Breadth-First Traversal (BFS) for a graph is similar to the Breadth-First Traversal of a tree .

The only catch here is, that, unlike trees , graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we divide the vertices into two categories:

Visited and Not visited.

A boolean visited array is used to mark the visited vertices. For simplicity, it is assumed that all vertices are reachable from the starting vertex. BFS uses a queue data structure for traversal.

Breadth First Search (BFS) for a Graph Algorithm:

Let鈥檚 discuss the algorithm for the BFS:

Initialization: Enqueue the starting node into a queue and mark it as visited. Exploration: While the queue is not empty: Dequeue a node from the queue and visit it (e.g., print its value). For each unvisited neighbor of the dequeued node: Enqueue the neighbor into the queue. Mark the neighbor as visited. Termination: Repeat step 2 until the queue is empty.

This algorithm ensures that all nodes in the graph are visited in a breadth-first manner, starting from the starting node.

How Does the BFS Algorithm Work?

Starting from the root, all the nodes at a particular level are visited first and then the nodes of the next level are traversed till all the nodes are visited.

To do this a queue is used. All the adjacent unvisited nodes of the current level are pushed into the queue and the nodes of the current level are marked visited and popped from the queue.

Illustration:

Let us understand the working of the algorithm with the help of the following example.

Step1: Initially queue and visited arrays are empty.

Queue and visited arrays are empty initially.

Step2: Push node 0 into queue and mark it visited.

Push node 0 into queue and mark it visited.

Push node 0 into queue and mark it visited.

Step 3: Remove node 0 from the front of queue and visit the unvisited neighbours and push them into queue.

Remove node 0 from the front of queue and visited the unvisited neighbours and push into queue.

Remove node 0 from the front of queue and visited the unvisited neighbours and push into queue.

Step 4: Remove node 1 from the front of queue and visit the unvisited neighbours and push them into queue.

Remove node 1 from the front of queue and visited the unvisited neighbours and push

Remove node 1 from the front of queue and visited the unvisited neighbours and push

Step 5: Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue.

Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue.

Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue.

Step 6: Remove node 3 from the front of queue and visit the unvisited neighbours and push them into queue. As we can see that every neighbours of node 3 is visited, so move to the next node that are in the front of the queue.

Remove node 3 from the front of queue and visit the unvisited neighbours and push them into queue.

Remove node 3 from the front of queue and visit the unvisited neighbours and push them into queue.

Steps 7: Remove node 4 from the front of queue and visit the unvisited neighbours and push them into queue. As we can see that every neighbours of node 4 are visited, so move to the next node that is in the front of the queue.

Remove node 4 from the front of queue and and visit the unvisited neighbours and push ithem into queue.

Remove node 4 from the front of queue and visit the unvisited neighbours and push them into queue.

Now, Queue becomes empty, So, terminate these process of iteration.

To deepen your understanding of BFS and other essential algorithms, consider enrolling in our comprehensive course, Tech Interview 101 – From DSA to System Design . This course covers data structures and algorithms from basic to advanced levels , providing you with the skills needed to excel in technical exams and interviews. Building a strong foundation in these concepts is crucial for your success in the tech industry.

mplementation of BFS for Graph using Adjacency List: C++ #include #include #include using namespace std; // Function to perform Breadth First Search on a graph // represented using adjacency list void bfs(vector& adjList, int startNode, vector& visited) { // Create a queue for BFS queue q; // Mark the current node as visited and enqueue it visited[startNode] = true; q.push(startNode); // Iterate over the queue while (!q.empty()) { // Dequeue a vertex from queue and print it int currentNode = q.front(); q.pop(); cout https://media.geeksforgeeks.org/auth/avatar.png GeeksforGeeks Improve Next Article C Program for Breadth First Search or BFS for a Graph Please Login to comment...


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有