Breadth-First Search
Breadth-First Search
1. BFS in Graph
(1) Summary
Create adjacent list or adjacent matrix (prefer adjacent list since it is more space efficient) to build the graph mode given the relations
If each node can only be visited once, we should use an boolean array to check if the node has been visited
Pop a node from queue, and iterate over the node's neighbors and put them to the queue
(2) Template
2. BFS in Matrix
(1) Summary
Every cell in the matrix will be visited only once using BFS
(2) Template
3. Time & Space Complexity
Time: O(V) , where V is the number of nodes in the graph
Space: O(V)
4. Questions Category
Last updated
Was this helpful?