Breadth First Search (BFS) Algorithm

hi guys, on this beautiful day we’ll talk about Breadth-First Search

Definition of Breadth-First Search Algorithm (BFS)

Breadth-First Search: is an algorithm that traverses and searches in trees and graphs using recursion and queue data str…


This content originally appeared on DEV Community and was authored by Aya Bouchiha

hi guys, on this beautiful day we'll talk about Breadth-First Search

Definition of Breadth-First Search Algorithm (BFS)

Breadth-First Search: is an algorithm that traverses and searches in trees and graphs using recursion and queue data structure, this algorithm comes to avoid processing a node more than once.

Time complexity and Space complexity of BFS

Space complexlity O(V)
Time complexity O(V+E)

Breadth-First Search applications

  • Google maps
  • Cheney's algorithm
  • Search Engines
  • Peer to Peer Networking
  • Ford-Fulkerson algorithm

Breadth-First Search approach

  • Initialize a queue

  • Mark the node as visited by inserting it to a list

  • Insert the vertices into the queue

  • While len(queue) > 0:

    • Delete the first item of the queue
    • mark as visited and push all adjacents unvisited nodes of the deleted Item.

Breadth-First Search implementation in python

more details...

If you are not familiar with python, I recommend you check this series

# CODE FROM https://favtutor.com/blogs/breadth-first-search-python

graph = {
  '5' : ['3','7'],
  '3' : ['2', '4'],
  '7' : ['8'],
  '2' : [],
  '4' : ['8'],
  '8' : []
}

visitedNodes = [] 
queue = []   

def bfs(visitedList: list, graph: dict, node):
    visitedList.append(node)
    queue.append(node)

    while queue:        
        m = queue.pop(0) 
        print (m, end = "\t") 
        for adjacent in graph[m]:
            if adjacent not in visitedList:
                visitedList.append(adjacent)
                queue.append(adjacent)

bfs(visitedNodes, graph, '5') # 5 3 7 2 4 8

References and useful resources

Have a nice day!!


This content originally appeared on DEV Community and was authored by Aya Bouchiha


Print Share Comment Cite Upload Translate Updates
APA

Aya Bouchiha | Sciencx (2021-07-09T23:09:42+00:00) Breadth First Search (BFS) Algorithm. Retrieved from https://www.scien.cx/2021/07/09/breadth-first-search-bfs-algorithm/

MLA
" » Breadth First Search (BFS) Algorithm." Aya Bouchiha | Sciencx - Friday July 9, 2021, https://www.scien.cx/2021/07/09/breadth-first-search-bfs-algorithm/
HARVARD
Aya Bouchiha | Sciencx Friday July 9, 2021 » Breadth First Search (BFS) Algorithm., viewed ,<https://www.scien.cx/2021/07/09/breadth-first-search-bfs-algorithm/>
VANCOUVER
Aya Bouchiha | Sciencx - » Breadth First Search (BFS) Algorithm. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/09/breadth-first-search-bfs-algorithm/
CHICAGO
" » Breadth First Search (BFS) Algorithm." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/07/09/breadth-first-search-bfs-algorithm/
IEEE
" » Breadth First Search (BFS) Algorithm." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/07/09/breadth-first-search-bfs-algorithm/. [Accessed: ]
rf:citation
» Breadth First Search (BFS) Algorithm | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/07/09/breadth-first-search-bfs-algorithm/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.