## Breadth-First Search (BFS)

An algorithm for graph traversal called Breadth First Search investigates every vertex in a graph at the depth it is currently at before going on to the vertices at the next depth level. It visits each of its neighbors after beginning at a given vertex and progressing to the next level of neighbors. BFS is frequently utilized in graph-based pathfinding, connected component, and shortest path algorithms.

It is possible to extend breadth-first search to directed graphs with a specified start node as well as undirected graphs. While repeated vertex searches are frequently permitted in artificial intelligence state space search, care is usually made to avoid repetitions in theoretical study of algorithms based on breadth-first search.

Konrad Zuse developed BFS and its use for locating related components of graphs in 1945, but his Ph.D. thesis on the PlankalkÃ¼l programming language was not published until 1972. Edward F. Howell redesigned it in 1959 and used it to determine the shortest path out of a maze. C. Y. Lee then developed it into a wire routing method, which was published in 1961.

### Example of Breadth-First Search (BFS)

### Algorithm of Breadth-First Search in AI

The following are the steps that the BFS algorithm takes to explore a graph:

** Step 1:** For every node in Graph G, set THE STATE = 1 (ready state).

** Step 2:** The second step is to queue up node A and set its State to 2 (waiting condition).

** Step 3: **Until the queue is empty, repeat Steps 4 and 5.

** Step 4: **Dequeue a node N in step four. After processing it, set its processed state's THE STATE = 3.

** Step 5: **Put all of N's neighbors in order of readiness (whose THE STATE = 1) and set their State is 2.

(A state of waiting)

[LOOP END]

**Step 6:** Go Out

Features of Breadth-First Search (BFS)

** First-in-First-Out:** Because it will often produce the correct order of nodes and be faster than a priority queue, the FIFO queue is usually favored in BFS. Deeper search tree nodes are added to the rear of the queue when BFS is configured with FIFO. First to expand are the older nodes that consume more resources than the newer ones.

** Early Goal Test:** The algorithm in conventional BFS implementations keeps track of the states it has reached throughout the search. Nevertheless, it maintains only the set of reached states that permit an early goal test, rather than saving all obtained states. In this test, as soon as the node is formed, it is checked to see if it satisfies the objective requirements.

* Cost-Optimal:* It always seeks to find a solution with the lowest cost, giving priority to the shortest path. As a result, BFS can find a solution as soon as it reaches a certain depth level within a search region. As a result, BFS is regarded as the most economical option.

### Application of Breadth-First Search in AI

The following are some uses for the breadth-first algorithm:

- From a particular source location, BFS can be used to determine the nearby sites.

- The BFS algorithm can be used as a traversal technique in a peer-to-peer network to locate every neighboring node.

- Web crawlers can generate web page indexes using BFS. It is among the primary algorithms for indexing web pages.

- It follows the connections connected to the page as it navigates from the original page. In this case, each webpage is viewed as a node within the graph.

- The least spanning tree and shortest path are found using BFS.

- Cheney's method of replicating the garbage collection also makes use of BFS.

- It can be applied to the Ford-Fulkerson approach to find a flow network's maximum flow.

### Time Complexity Breadth-First Search (BFS)

Depending on the data structure used to represent the graph, BFS's time complexity varies. Since the BFS algorithm searches every node and edge in the worst scenario, its time complexity is O(V+E). In a graph, O(V) is the number of vertices, while O(E) is the number of edges. O(V), where V is the number of vertices, is the expression for the space complexity of the BFS.