This video teaches fundamental graph traversal techniques including Depth First Search (DFS) and Breadth First Search (BFS) for solving grid-based problems like counting islands and capturing surrounded regions, as well as cycle detection in directed graphs using DFS with path tracking to identify circular dependencies.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
DSA(LEETCODE 150 CHALLENGE) | Beginner to Advanced
Added:Let's try to Not very different.
So let's start the today's session guys.
So I'm not able to come the last in the live in last two days because of the shifting right. Let's try to see now let's have to solve the last question Right.
So first we have number of islands.
So what is question? I think I made it this question. I solve this question within the DFS. You can right like how I solve it. uh like this question say like given you have given 2D matrix 2D grid right which represent the map of one one that means land and zero means water right so what we have to return the number of island okay and island is surrounded by the water formed by connecting all its vertical and horizontal okay adjacent land vertical horizontal we may assume that all the four edges is surrounded by okay outside that means outside from the so if you see the first test case like uh see one so one one one connected and that means this this from here to here from here to here and from here to like I hope you understand here this all are connected So there is only one line right one island and for here we have like we have this much is connected just this four this four so it's one island two island and three island yes so what you have to do just you have to do like uh go start start from the mat travel to the all the matrix That means grid and if you find the grid value is land then you have to just you have to search like how many land are connected through this point right like uh we have going first we are going to find the one now we are trying to see like how much is connected to this in the four side right from the up down left right okay So that's is this I doing I'm doing just like that like uh if it is one I have on the that mean this is the island and call the PFS like all the connected graph should be zero become zero right so I call the DFS and make that grid zero and from that grid we are traveling left right up down all the four direction right and if it is zero we have to just material or invalid right so let let me see let let me show you so it's like if you if it is out of the box this is the condition for checking the out of the box if it is what out of the box we have to just uh not take that and if it is greed is zero we have to not take and if it is one we have to again call the dfs right so we are just like doing that I'm returning the count value so this is the normal df You can use the PFS but I think the more efficient way is DFS here right?
Yes, we solve. Now, next next question is like now region. Let's see what's your question. Uh I think here given the matrix of X containing the X and O.
Okay. Capture the region that's rounded.
Okay. So connect a cell is connected to adjacent cell horizontally or vertically. Okay, horizontally or vertically. That means you have to connect this this to this right vertically. To form a region correct okay to form a region connect every zero cell. Okay. And the round region is rounded if none of the zero cell in this region are on the edge of the board.
Okay. Excess regions are completely enclosed by the X to capture the region.
Replace all X all zero with X in place and this is the original board. You do not need to enter anything. Okay.
But what I'm saying is you have to just capture it. Okay.
So how can we approach what what is connected to adjust You have traveled all the ages right like uh first row first column last row last column last row last column right and count all the zero four not a zero but four all the four and try to traverse like try to bs like see how much zero is present on the edge and how much How many O that is connected region is present right? If it is if the all the connected region is present with the edge containing four that mean they are the not the rounded line otherwise all the rounded point let's see let's see we have one here so there is nothing connected there you see the one zero is here at zero so there is nothing connected in this way that's why it's become in the final answer is it remain zero but these all are become x that's why we are doing Like if it is first row we are taking and marking digit. If it is last row uh sorry last column we are taking and if it is last that means all the condition we are closing it. Now we are taking the cube and traversing for side from the if it is not zero we have to just continue. If it is zero we marked and proceed to and like and find if it is not if it is dictated we have to mark zero else we have to mark and return just you have to return this is not so much problem if you know the basic bfs and dfs and build like right you can obviously don't like that respect.
I think I question you have given the graph right you have given graph in the node right you have to just you have to just return right okay in the point that's why I'm going right here like I'm calling from the node if it is not immediate if the node immediate we have to return the right we have to just right like from the perspective see that perspective like one one should be two and four rightfully.
We have one. So going to one calling two calling and one right this address become 1 2 and address your address we have if it is first time visited we mark to give you and you can say that like the address is first we have to just IP address and final righting.
Falling calling and returning. Right.
Let's try next.
of varation equations and pair of real numbers.
We are going to assign a given pers and a and b that representable variable.
What you have to do?
Where you must find the answer.
Okay.
The answer cannot be determined. Return.
Okay. Let's practice first one.
Okay. So like 1 / it should be 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3.0 and B / 3 it should be 3.0 0. Right now a / c. That means we have a / c that means 3 / b / okay by What should be six? Okay.
What's 1.5?
Okay.
So what we have to do here? Okay.
We have to vector. Okay.
We have a string vector / 2 and / k should be what? 1.
Okay. Fine. That was good. Now let's see a / b and b / 6 also also divide and 3ide by that's going to happen right you are you are That's not going to happen. That's not going to happen because I just b a divide bid.
Can I do like that?
Okay.
It's continuously just 2 P by A into P by Okay.
So what we have to do like one of those graphical the graphical way.
Sorry.
Okay.
Okay. Hello.
A / 2. Okay. And B should be 1 by Z. Okay. Now we have we have now we have B / C should be 3.
Okay.
is one.
Now the question we have we have to find like a b right c2 a we can go like c2. So we have 6 into 6 into a b= 3 into 8 into 3 and 2.
Okay. So to see first question to C A will not take a parent to see yes I find it can I take like can I do like using the DSL Yes.
Can I You can do it. Can I put Yes, you can go.
Can I go? Yes, you can go.
Now from C to B and from C2, right? So let's try to see let's try to be right.
This is another max or and work and so let's try to see using DFS right what what I'm trying to say like let's try to see unordered map like un unordered unordered map Take value. End value. Okay. End value.
And here now the second one.
Okay. Question. Now get it.
Okay. So, we have to take a string, right? And we have to take here a string.
This do one thing we have to take a vector here string and the double double.
Now how can we approach?
We have we have to just find the sign. I what I want to Okay. So it should be the equation side.
Equation side. Right. Now what can be your first like 10.
Okay. Now we have we have four string right. So take one a what? This would be like an equation like this much equation.
Equation I and equation I should be okay zero and the string B should be one. Equation I and equation I and one. Right? Now what we have to do just find the double value double what should be values values I correct now we have two questions first first dot post at two values B and the menus values. And the next question instead JSON dot a dot post could be the B could be the A and 1 / B.
Right?
We have to list within the graph. This is the graph. Now we have to take a vector vector vector double double answer and you have to just what what should be explain.
Yeah.
string just we have we have double b should be helper x x should be zero and x should be 1 one and the red is now we take away and you have to just return the return the answer right now how to write on the right we have a string a string a string This is the target value. So force spring force target and anything else.
If this if you can't find this you have to return. is for the target is not found.
Right? The target is not found. If the target is not found, you have to return.
Yes. is not found. You have to just put back to the DSA here. for here like uh if the if the target if the like uh if the x is called function like if the if the adjacent found src that means not found not found or JSON dot adjac Just one thing here for Can I pass it just like that? I don't think so.
Let's do one thing.
Let's do one thing.
I'm Okay. Now the question next with x0 and x1 and x.
What should be the one double?
Now the question is how can we have we first see like first map like map A to 2 and B A 1 by 2 B C B C B 3 and C C P 1 by 3 again next one nothing extra A right. So now A to go A to B. Okay. I'm going to A to B that means taking the value into two. Take the value that mean 1= 2 2.
Now going from B from B we are going to A so it's not we can't go to the parents it's not so we can't go to parents right so we are taking we can't go to the parent okay parent we can't we have to we have to we have to use that right we have And now this is six. Next click on the six.
If the target target is you have done the 6 minute This is three question.
If target you have to just return the for adjacent target.
one target. What should be written here? We find target.
Just return one. Okay. Okay. Let's return 1.0 1.0 1.0 Okay.
Now we have we have two double okay this will be what 1 okay 1 1 x that means yes not if the neighbor we have we ask like a I just remove just try to remove it from just try to remove just take only the pair right.
So if the P if the driver never should be the first this should be the second parenthe into equal to no discussion into what uh never put second dot second.
Okay. Second helper helper help first and target and parent will be what?
Okay.
Right.
You have to take a parent here. What is the parent here?
Here are lower character right making mistake somewhere.
All right.
From the map, we can't use the pair anymore.
back.
There is no I just like that I don't think so. You have to just you have to just like that. I don't like You have to use you have to take a vector right taking all the vector Uh back getting the vector input both input. Now we have to hear something else like what you subscri This is the vector.
This is good.
So let me see first any second particles and I get this question and X1.
Okay. So, this giving the wrong answer one one we have to call like we are calling like one like we are calling from one to find the direct Okay.
If again this right answer right answer and what this mistake 1 A to C, right? A= Yes. A to C. A C, right? So, A= to B. Oh, okay. Okay.
We have B2. B2.
So and just build a build a map right just again we have we have like a should be and many other ways no should be a then we have 1 by 1 by 2 1 by 2 and this should be c then we We have three.
Yes. Three. And any other?
No. Any other? Any other?
No. Any other? No.
Okay. And this is so C to B.
Now B to A. We have B to A. Let's take B to A. So it's like into yes target is fine is fine then we have yes it's like 1 into 1 by 2 into for your life.
Don't go anywhere, right?
You have right state somewhere.
Okay.
Okay.
should be the target and also so if that is fine if the file it has register If the target is fine, if it if the target is fine, right? Target is fine.
You have no return.
Okay.
Yeah, I think Okay.
The target is fine.
We have the function and the target and the target and and the capacity right if it is if it is if it is greater than 1.0 Z that means the target should be fine right that mean.
So we have to just do one thing return.
evidence not We have going we have to target.
Okay.
A to B, B to A, right? We have to find we can find this for we have to return the right.
If you find return, you have to continue the back. Next thing here like if you use the backing like not return one returning the true value here returning the true value here if you find that value okay let's do let's take a double value here setting here like except answer like. So if it is the target is found answer that what and just return the okay and what we have to do if else what will be done here first and the target target Just one second.
So we have we have that if you have to If it is not enough what value the question that value into what value into what value do Good. Okay. Good.
Good. 1.0. Good. And just remove the double here.
And this and it might be good. Now let's try to see this.
Okay.
Now why are you giving the wrong answer why should be if it is not present then we have to use -1 what should be any target second.
But they do not have to help us.
You're not falling from I think that's not bad.
Give me Father, very happy.
Now this target and just like that.
If it is return that means we can't find the connection between the yes if it return false we can't find the name connection so we have to return the And now the unign And one what should be what should the A and A. What should be the value?
Repeat into a ting I do like a can I find a I don't think she can find it a A2 If we take a by b and if we take c by a then we can not find h we can't find right a by b a by doing just like else.
If the sentence else.
Try to feel like an I think just like so insert removable group is third.
If it is See here.
Okay, that's all good.
adjacent fun.
Okay. Thank you.
visited.
Anything else? No.
Next model string string string the so we have to run that first practice.
target.
You have to find right you have to find like this should be contained then we have to adjent adjent found it's not project now just go and just continue here.
Finally social cost rejected we have to mark in. Okay. If it is not if it is we have to continue right. If it is not visited, we have to not question.
total number of you can take you have that for example for example the pair 01 indicate that you have to A 0 in this zero like this is the famous problem of the sort Right? You have to just detect the cycle is there. So either you can use the topological sort. Right? Just like I do logical for topological or either you can use the DFS TFS or cycle detection that just like that.
So let's try let me just let me just just give you just remove all just remove all right let's try to build it so this is the question total you have taken and you have the area which could be right indicate that you must take you must take you first step like last step uh P B P B P B P B P B P B P B P B P B P B P first. Okay, if you want to have a graph like that 1 0 0 1 Yeah, this is You can't just We have to do this new x and what x right now if save there. So you have to just don't do it now.
Now return the DFS the DFS. What should be the DFS content? DFS content. What should be the contain? So it should be practic value uh vegetated repeated B and false right and it should be the it should contain the part B and false. Now we have to take like we have to call from node zero node zero node to take a parent. Can I take a parent?
You have to get right because already you have to get to the detail and the return then this must this one will fra Vector visited vector vector and direction. Now we have dated node and n should be right both should be true.
Now go to call the neighbor adjacent adjacent node.
If it is unvisited if it is unvisited unvisited of you have to call the driver function function. What will be fun? neighbor and the path and you if you find the root we have to return to else what we have to do as what what else if the path is true if path is you have to return the return What should we do? If the cycle is detected, then you have to put what you have to return the proof.
You have to return the proof. At the last you have to return the cycle detection. That means not detected. Okay. Sorry. Edge is now edge.
And right there what is this? What is this?
Where I use the vector widget path What are you saying?
Okay, let us go.
cycle detection. Why should I explain should be written in vector?
Yes, this should be written or return vector. Why? Why are you making me like why you you are start talking nonsense talking you start talking to the much so we have to just we have to just apply the topological sort just like I in the previous I have solved this problem right. I solve this problem and previously I solve this problem not present just here I sol this can finish but they change the quality What we have to do like you have to just do like topological sort right we have to do like a topological sort just like that and the previous solution of this like uh this one let us see I solve this Okay fine.
So you want not want that much you won't want this but so what we have to do we have to just copy from here from here it just do minor changes. Now you are good to go.
I just any you don't know this.
Okay. I think I make a mistake here.
I use the topological right.
Okay. I made a mistake here. My function that mean I change I say like this function should be this.
So I I think we have finished activator right and this is if there is for This can go with anyone here in affected parts development learning development. that much.
Okay.
So this is the famous cycle detection using right. So what what we have to do uh in the undirected undirected graph if we detect the cycle we have to just use the visited nodes right like uh if we again we get that node we we show that there is cycle but in directed graph there there might be a condition like we there is a complete different node complete different like like 1 2 2 3 and 3 2 4 Okay. 5 to 3. This right now question like we have 1 2 2 3 market all three to four all visited. There is no sacrificing part is already digited that mean that's not we have to maintain another vector like path right that's not the vit but the path should matter here right path should be that's that's why we are doing like path and n should be vited If the united we call that function if it is the path in that direction the path is already legited that means we have found right we are again this and path is also legited that mean there there is detection right else we have to just path for right that means we travel all one path one direction direct direction there is no cycle. That means we mark that all the from the one node we can't go to any cycle. You have to just mark that part.
False.
1 2 3 4 false the uh we can't go anywhere. Can't go anywhere. Can't go anywhere. In the last we can't go anywhere. We have to just false return. We can't go anywhere.
False return. False return. False return.
We can't go there.
Yes.
solve this problem.
Let's try to solve it backing problem and problem.
Okay, I have solved this problem.
Uh I think I solved this problem in previous ABC and you have to start from the zero write the zero and this now you have to just like we have to that mean uh just do like takea 1 a b a By like one from here, right?
A another four d a e from one like your two we are making like 2 three right two three. So taking one from two a from from two we take a and from D we are matching all this like D E F again from B E F again C D E F try to combine all the characters and all three letter or the four letters of the next right this this this question is this question this question n probleming nq I need to end that no string the solution should be. So this would be the this would be the famous problem of the if you if you solve the hand problem you can solve it both are same problem just you have to just return here like how many and the hand problem and your problem you have to create that one right you have to create one that all the should bear the same name Right? Like you have to place all the right and for this like for this you have to find out how many position we can how many solution we can.
Now there is n that mean uh 9 into 9.
Now how we proceed like this? Love and beauty all right.
Okay.
4 1 2 3 4 1 2 3 4 Yes. 4 in the morning.
Thank you.
What we have to do like so is right. You have to just filter the cell three and print out.
Okay. Now call the call the help of function with integer value with integer value. Right? With the integer value.
Now what we have to do?
We have row number.
What we have to do for it? The screen should be not present in the same row and not the same column and not the adjacent the right not this column. Can I do Can I do like column?
Can I use column number? Yeah.
Just call if then you have to return the one. Okay.
So what we have to just call the uh like in solver int row vector mode.
If = n minus one, you have to return uh 4 0 1 2 3.
No, not last.
Yeah, it's no should be last. We have done one.
Now else four column zero column and column ++ if is valid that means we are presenting the mean we are presenting can I take here this is This will be not. This should be not.
This should be not. Okay. Fine. Fine.
Fine.
And column.
We have to mark that number C plus.
Yes.
Let's try to see like how can we approach like what what you are trying to see. Uh we have like a string a string s as a string s vector string as no you want to say like a string the string board the string and and and column right column and return the helper. Solver function. Solver function with the with the row hero and the board and right and just call that same helper fun what in row. Okay. And vector explain in bold and print. Now this you have to give in the row. In the row you have to we have to return one. Else you have to call value column should be and column plus if is valid.
If it isn't valid row row column hold if it is valid you have to mark sorry row and column Queen win queen helper solver solver solver what solver what solver what should be solver what should be solver right what should be the solver row + one and column from here.
Gold what? and and what code row and column and final one.
Now in method int and the vector a string code for example Just you have to travel All right, bro.
Simple board R and C.
If the R and if it is row row if it is R I= Okay the same the hold I I and See this queen?
Yes. You have to see you have that that means that means you have to take the two pointer ex R should be R and C should be C. Now what we have to do here are rater sorry greater than z and then less less than we have to check if if the if the if the four and since queen you have to return the false and rise and again we have to do like while r should be r greater than equal to and cateral.
You have to change like the board the board R and C is equal to we have to return the statement else we have to just return the statement see this much is passing the test cases or not let's try to say okay so you are saying you can't understand that my solution sir what are you doing you can't understand there is Let's pray.
Never tell me.
Four.
Right.
This step.
I think this not right. Next question.
It's a combination of IS.
and this question.
Just like that. We have to solve this question.
Maximize the number of accusation in S.
Convert the continuous.
Okay. Fine. Fine. Fine. Fine. Fine.
All zero.
Okay. So maximum one like we have like one one Yes.
If it if it's one That's not I think the con is 1 one 1 0 1 1 1 0 and it's like a force.
It's never sir. I solve this problem. Yes. today yesterday I think there's nothing that much you have to just find sum right if the sum is zero you get that value this is this all the standard for right all those standard form right and these all are the standard for like to solve it.
I saw question.
How can we approach?
How can we close? Like uh we have 1 1 0 1 1 0 1 1 0 0 Let's start.
Can I take like not surface.
Hello.
A I a - Y a I + 1 + max Right. So like one one project right here.
Take one only. Take it.
Yes. Yes. Yes.
Zero.
That means we have if it is one we can go to the next one.
What we have to do? What we have to do?
You have to starting index score all the ones.
Starting index all the ones. All the ones the starting index this much. Let's try the same space as 1 0 0 0 1 0 0 1 6 0 4 1 Bless It's That means that means what?
That makes sense.
Just one 1 4 4 4 4 If we take this much.
Okay.
Okay.
Keep repeating.
We have 0 1 1 0 0 1 1 and 1 2 3 4 5 6 7 8. Now we have 2 three six. We have we have one. So 1 6 we have 1 - 1 and we have one and now what we have to do what we have Is it that much? That's now Uh we are taking that much 6 - 4 taking that one right.
Okay. Now six we have to give like a six index starting you are taking like starting not starting a starting you have to take like a starting to end like group. You have to take starting from here. Starting ending start only one right 1 0 0 1 1 0 0 1 1.
Now we have to get 1 2 3 4 5 6 7 8 9 10 11 12 13 14. So it's like 2 three. It's like 6 9. It's like take that much.
If you take that much this much and what should be the ide different people.
14 14 - 14 - 6 1 2 3 4 5 3 7 8 9 Now - 15 + 1 0.
Now we are taking from here 9 - 2 + 1 8 + 14 - 9 14 - 9 from here this Yes, we are taking 14 15 - 5 3 - 3 - 1 from here we from here 6 - let's try to let's try to see right now let's try to say we have to If I come here group SP and SP right and group SP do what? 0.
uh you have to take a c=0 if the s = 0 else we have to take i and ying and and and= + 1 and now whatever.
You have to know what you want.
Go dot post dot post back and I for you have to Just it.
What?
What? What?
What should be the end?
Now we have to take and max.
That's we have starting with S and I here we are first one So as 5 Okay.
Yes, that's all right.
And next one plus one. Now, now starting from the starting, right?
This one. This must be the starting start like left should be what? In minus what?
In minus previous one, right? And right should be what?
Right should be what? Right.
In right next and answer and max will be and MX will be maximum of max of left or right.
You have to return max answer. Sorry, Max.
Let's try and see.
Started the group started also started limit. That's it.
Continue.
1 comma 1 0 2 0 0 1 and 1 by one or the other one should present that we are wants to present like so we can't take like one can't do like we have to take like one this much we have to do Let's end the session here, right?
Let's solve it now. Try to solve Yeah, I like myself.
Let's end the session here.
Okay. Okay.
Sorry. Let's end the session here.
Right.
Last time.
Thank you.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

One Must Imagine Sisyphus Happy
vlogbrothers
61K views•2026-07-21

Future of Taylor Farms
maighstirtarot5385
11K views•2026-07-21

The Downfall of OnePlus!
techwiser
65K views•2026-07-21

My Friend Locked Up The Engine On His K-Swapped Bug...
boostedboiz
128K views•2026-07-21