Breadth First Search (BFS)
<aside> 💡 Breadth First Search (BFS) is a graph traversal algorithm that traverse a graph closest to a given starting node. The traversal can also be though of as going through the graph in a level-by-level basis.
</aside>
<aside> 💡 BFS works by pushing nodes to be visited in a queue. When a node is visted, it is marked as visited and any edges the node has to other nodes are added to the queue.
BFS Algorithm Steps
</aside>
def bfs (graph, node) :
visited = []
queue = []
#Add starting node to queues.
visited.append(node)
queue.append(node)
#While the queue isn't empty, pop + print the node.
while queue:
s = queue.pop(0)
print(s, end = " ")
for n in graph[s]:
if n not in visited:
visited.append (n)
queue.append (n)
<aside> 💡 The Time Complexity of Breadth First Search is $O(V+E)$. At worst case, we will visit every node and explore every edge.
</aside>