1
+ '''
2
+ Graph traversal means visiting every vertex and edge exactly once in a well-defined order.
3
+ While using certain graph algorithms, you must ensure that each vertex of the graph is visited exactly once.
4
+ The order in which the vertices are visited are important and may depend upon the algorithm or question that
5
+ you are solving.
6
+
7
+ During a traversal, it is important that you track which vertices have been visited.
8
+ The most common way of tracking vertices is to mark them.
9
+ Breadth First Search (BFS)
10
+
11
+ There are many ways to traverse graphs. BFS is the most commonly used approach.
12
+ BFS is a traversing algorithm where you should start traversing from a selected node (source or starting node)
13
+ and traverse the graph layerwise thus exploring the neighbour nodes (nodes which are directly connected to source node).
14
+ You must then move towards the next-level neighbour nodes.
15
+
16
+ As the name BFS suggests, you are required to traverse the graph breadthwise as follows:
17
+ - First move horizontally and visit all the nodes of the current layer
18
+ - Move to the next layer
19
+
20
+ Time Complexity: O(V + E)
21
+ Space Complexity: O(V)
22
+ V is number of vertices (nodes), E is number of edges
23
+ '''
0 commit comments