ProjektOsmium / All-Algorithms

MIT License
12 stars 52 forks source link

Breadth First Search for a Graph in Python #77

Closed anmolgaur45 closed 4 years ago

anmolgaur45 commented 4 years ago

Breadth First Traversal (or Search) for a graph is similar to Breadth First Traversal of a tree (See method 2 of this post). The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. For simplicity, it is assumed that all vertices are reachable from the starting vertex.