Order in which the nodes are expanded

BFS is an uninformed search method that aims to expand and examine all nodes of a graph or combination of sequences by systematically searching through every solution. In other words, it exhaustively searches the entire graph or sequence without considering the goal until it finds it. It does not use a heuristic algorithm.

From the standpoint of the algorithm, all child nodes obtained by expanding a node are added to a FIFO (i.e., First In, First Out) queue. In typical implementations, nodes that have not yet been examined for their neighbors are placed in some container (such as a queue or linked list) called "open" and then once examined are placed in the container "closed".

## Algorithm

- Enqueue the root node
- Dequeue a node and examine it
- If the element sought is found in this node, quit the search and return a result.
- Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.

- If the queue is empty, every node on the graph has been examined – quit the search and return "not found".
- If the queue is not empty, repeat from Step 2.

Note: Using a stack instead of a queue would turn this algorithm into a depth-first search.

## Pseudocode

**Input**: A graph *G* and a root *v* of G

1procedureBFS(G,v):

2 create a queueQ

3 enqueuevontoQ

4 markv

5whileQis not empty:

6t← Q.dequeue()

7iftis what we are looking for:

8 returnt

9for alledges e in G.incidentEdges(t)do

10o← G.opposite(t,e)

11ifois not marked:

12 marko

13 enqueueoontoQ

## No hay comentarios:

## Publicar un comentario