PROBLEMS ON BFS/ DFS:-
- no. of provinces ✅components
- rotten oranges ⭐✅multi source BFS
- no. of islands ✅BFS/ DFS
- flood fill ✅BFS/ DFS
- no. of enclaves ⭐✅multi source BFS/DFS
- no. of distinct islands ⭐✅shape storing BFS/ DFS
- surrounded regions ✅multi source BFS/DFS
- 0/1 Matrix ⭐⭐✅multi source BFS
- word ladder-1 , 2
----------------------------------------------------------------------------------------------------------------------------
1. No. of provinces
count total components of a graph
time- O(N + V+2E) space: O(N)
-----------------------------------------------------------------------------------------------------------------------------
2. Rotten oranges
MULTI SOURCE BFS
time: (n*m) space: (n*m)
it can be seen that oranges are being rottened in a BFS fashion, with each time current level of oranges rottening neighbor oranges at same time...since we need minimum time so BFS is preferred here
------------
No. of starting points = no. of rotten oranges at start
-----------
Here we will create a visited 2d-matrix
-----------
1. initialize a queue & a visited matrix with all rotten oranges at start, along with time 0
2. pick q.front element and update time from this element
3. see its fresh & non visited neighbors , and put them in queue with incrementing time by 1 & mark them as visited
4. repeat till queue is non empty--------------
---------------------------------------------------------------------------------------------------------------------------
3. No. of islands
time- n^2 space- n^2
a grid of size NxM consists of '0's (Water) and ‘1's(Land). Find the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically or diagonally i.e., in all 8 directions.
------------------------------------
example- below grid has 1 island
------------------------------------
No. of islands = no. of graph components
-----------------------------------------------------------------------------------------------------------------------------
BFS
-----------------------------------------------------------------------------------------------------------------------
count no. of islands such that in a group of islands there should not be any island on boundary
Method-1: flood fill wale mein hi adjustments krdo using if else (ki boundary wale ni lunga)
----------------------------------------------------------------
Method-2: Multi source DFS
Flood-fill all 1s connected to the boundary (make them 0). Then, count remaining 1s (they are no. of required enclaves).
----------------------------------
using multi source BFS-
-----------------------------------------------------------------------------------------------------------------------------
NOTE-
For any grid-based traversal problem, you can usually solve it by either:
-
Single-source DFS/BFS → starting from each valid unvisited cell (found by if conditions u put)
-
Multi-source DFS/BFS → starting from all valid sources at once (smartly reject invalid nodes using multi source traversal and then traverse for valid nodes)
---------------------------------------------------------------------------------------------------------------------------
6. no. of distinct islands
we need to store shape of islands in a set and then return st.count()
for this for each island, we will subtract base island coordinates from all neighbor islands of it
-----------------------------------------------------------------------------------------------------------------------------
instead of putting filter in dfs function before setting vis as true, you can instead put a filter in main function before calling dfs for boundary loops
TIME:- O(nm) space: O(nm)
-----------------------------------------------------------------------------------------------------------------------------
8. Dist of nearest cell having 0⭐⭐
binary matrix
dist calculated as (i1-i2)+(j1-j2)
if a[i][j]=0 so dist is 0 since element itself is 0
we prefer BFS here, since in BFS we move layer by layer
we will use BFS on 0s and see all 1s at dist of 1 , then at dist of 2 and so on....
---------------------------------------------------------------------------------------------------------------
Comments
Post a Comment