BFS(Breadth First Search) Algorithm(Part-6)

BFS(Breadth First Search) Algorithm(Part-6)

Posted By 1 year ago

BFS in Python

import collections

def bfs(graph, root):
    visited
, queue = set(), collections.deque([root])
    visited
.add(root)
   
while queue:
        vertex
= queue.popleft()
       
for neighbour in graph[vertex]:
           
if neighbour not in visited:
                visited
.add(neighbour)
                queue
.append(neighbour)


if __name__ == '__main__':
    graph
= {0: [1, 2], 1: [

Blog Topics
  • PHP (php: Hypertext preprocessor)

    8 Article
  • Agile

    2 Article
  • Freelancing

    3 Article
  • JavaScript

    7 Article
  • AngularJs

    1 Article
  • Programming Language C

    22 Article
  • Object Oriented Programming(C++)

    1 Article
  • Algorithm Design

    14 Article
  • Subject

    Python

    682 Article
  • Computer

    756 Article
  • Subject

    Technology Tips & Tricks

    3 Article