- 1 day ago
Transcript
00:00Next, we are going to talk about Cycle Detection in Directed Graphs. In Directed Graphs, there are cycles exist in
00:06which we will find out using a modified DFS approach. First of all, let's see how the cycle exists. This
00:14is my Directed Graph in which there is no cycle. In this case, I have to return false. This is
00:19my Graph in which there is a cycle exist in which I have to return true. In this graph, I
00:25will reach any point in this graph.
00:36that is why this is a cyclic directed graph
00:50First of all, let's talk about the first approach of detecting your cycle. What did I have done in my
00:57undirected graph? I used my modified BFS. I saw what my neighbor already visited.
01:10Neighbor already visited or not? Plus, I had a parent approach. Is it my parent? Or is it not my
01:18parent? In those cases, I found out what the cycle exists in my graph. But here, we will use a
01:23slightly different approach. So first, let's talk about the undirected approach we learned, modified DFS, BFS, it doesn't work in
01:34directed. For that, I will give you an example.
01:49There is no cycle, but there is no cycle in undirected.
01:53Now for the undirected graph, if I use the DFS approach, where will I start? I will start with 0.
01:590 will go to your neighbor, 1 will visit 1. 0 will go to your neighbor, 2 will visit 2.
02:062 will see, 0 will have already visited, but there will be a parent. 2 will see, 3 will see,
02:113 will visit, 3 will see, 3 will see, 3 will see, 0 will see, 0 will see, 0 will
02:15see, 3 will see, 0 will see, 0 will see, 0 will see, 0 will see, 0 will see, 0
02:16will see, 0 will see, 0 will see, 0 will see, 0 will see, 0 will see, 0 will see,
02:170 will see, 0 will see, 0 will see,
02:19From 2 to 3, the parent will be 2, but from 3, the parent will be already visited and the
02:25parent will not be. So what will I do here? I will be able to detect my cycle successfully. If
02:30I put the same logic on the director, then what will happen? I will start from 0. I will visit
02:35from 0, then I will find which neighbor is from 0. Now there is no neighbor from 0. Why? Because
02:41from 1, it is not going to 0. From 0, it is not going to 1. It is going to
02:46be like this. Who is from 0?
02:49neighbor no 2, no 3, no 1. No neighbor is not 0. Because from 0, I can't go to any
02:55node. Then I will see what the next is unvisited. I will go to 1. I will visit 1 to
03:021. Now I will talk about neighbors. 1 neighbor is 0.
03:060 is already visited. What will 1 do? Who is 1's parent? 1's parent is minus 1. Because 1 is
03:12my starting node. I have not come from 0 to 1. I have started to traverse 1 from the beginning.
03:18So 1 will see that 0 is already visited and 0 is not the parent. So what will happen in
03:24this case? 1 will say that the cycle exists. So what will 1 say? Cycle exists.
03:29Why did he say this cycle exist? Because he has a neighbor who has already visited, but he doesn't have
03:35a parent. Because one's parent is minus one. So he says that cycle exists using this DFS approach. But if
03:42you look at this graph, no cycle exists. If I go to zero, I can't go anywhere. If I go
03:48to zero, I can't go anywhere. If I go to zero, I can't go anywhere. If I go to zero,
03:50I can't go anywhere. If I go to zero, I can't go anywhere. If I go to zero, I can't
03:51go anywhere. If I go to zero, I can't go anywhere. If I go to zero, I can't go anywhere.
03:53If I go to zero, I can't go anywhere. So there is no cycle in this graph. But he says
03:57that cycle exists.
03:59this approach will be wrong. Parent with DFS approach will fail. Why will it fail? Because I will already have
04:08some neighbors who will be visited.
04:11But they will not be my parents. This is very interesting. Therefore, we will use DFS, but we will not
04:19use a parent approach. We will use a modified DFS in which we will use different approaches. First of all,
04:26let's talk about our approach. I will take two graphs. This is my graph in which there is no cycle.
04:33This is my graph in which there exists in this cycle. What would I do? My normal DFS was my
04:39DFS.
04:39I have to follow DFS and write the same code. I will do this in a small modification that I
04:46will take a stack. What will it be? It will be my recursion stack. Recursion is not an implicit stack
04:54which is already formed in the recursion time. This is my explicit stack which I have to create a different
04:59way. But I will track those nodes which are called on recursion time.
05:06Now I have tagged names here, but actually it will be a boolean type and it will work like visited.
05:13It's the work to track that the recursion is running, the calls are running on my function, the dfs function.
05:20Which node is coming? I will tell you the logic. First of all, we will discuss the process. I will
05:31solve it. Then I will write code. Then I will dry run.
05:35So the biggest logic that we have to understand is that we will write code and then when we run
05:41dry run, we will clear the picture of how we have the solution to this approach. So we will understand
05:48the logic when we write code and dry run. But then we will tell you what we want to do.
05:54What I want to do is my normal DFS. I want to keep a visited. What I want to do
06:00is track my current calls.
06:06I will show you the pseudo code. First, let's talk about the cycle graph. What will I do for this
06:12graph? I will take two arrays. One will be my visited array. In some way, I will have 0, 1,
06:192, 3, 4 indexes. And another array will be my stack.
06:25This is not the actual stack. I have made it as Boolean type. But it will track that which nodes
06:33are visited within my stack. This is also a modification to the recursion stack and visited. That means visit nodes.
06:42But I don't want to see the total global visited. I want to see that now the call is running.
06:47Now the call stack is being created within the recursion stack. Which one has been visited? Just to track this
06:52recursion stack.
06:54I am going to write my normal DFS. What was the work of DFS? How does it work? In DFS,
07:01we have a current node. I have a current node that I have to traverse. So, when I come to
07:08the beginning, I will true my visitor. The current visitor will be true. Then, I will run a loop for
07:14all neighbors. I will see all the neighbors. If my neighbor is not visited, then what do we do? We
07:24visit him.
07:25In this way, my DFS fails. I have a very small code for DFS, which I visit first in my
07:30current node. Then, the neighbors are un-visited, I visit them. But now, what do I do for my little
07:36change? As I come to my current level, I will true in my current level. I will true in my
07:44stack.
07:47In the stack, I mean the current means that the recursion stack is running when the recursion is running DFS.
07:56When my recursion stack is made, there are some notes in it. For example, when I start from 0, I
08:01have 0 called from 0, after I have 2 called from 0, I will add 2 to the recursion stack.
08:07When I went to 3, I will add 3 to the recursion stack. So, when DFS calls come from 0,
08:13I have a recursion stack for my notes. Now, what did I do here? I made a boolean array to
08:20track those notes. Just like my 3 calls came, I will do it in the stack.
08:25Now my 3 has come into this current call stack. I have just been able to show this to you.
08:32Then what do I do? I have to check for my neighbors. If my neighbor has come into a stack
08:38call, it means that my cycle exists in the graph. We have to resize it and take this code and
08:51take this code.
08:57Here I have a simple condition that if you are not visited, then you can visit. Now my condition is
09:03slightly increased. My condition says that if you are visited, then you can check what the stack exists. That means
09:12if my stack of neighbors
09:16If my neighbor is already in my stack, then the stack of neighbor will be true. In this case, I
09:22always have to return the cycle here. If it doesn't happen, then what do we do? Then check that if
09:30it is not visited, then the normal condition will come. If not visited, then I have to call this cycle.
09:37I want to visit it. I mean, I will call this cycle for my neighbor. I will visit it. If
09:43my neighbor will say that the cycle exists, then I will return it true. No, I will not perform any
09:48action. I will return it false. What will I do? I will return it false. I will return it false.
09:55I will return it false.
10:05I will summarize it again. As I will come to DFS, I will true my visited current. This time, I
10:11will true the stack. Now, when I visit my neighbors, first of all, I will see if I have a
10:19neighbor that has been in the first stack. For three, there is a neighbor that has been in the first
10:25stack. So, there is a cycle that exists here. How do you see three neighbors that have been in the
10:32first stack?
10:34I have reached 3 to the stack calls and now I am talking about 3 from 0. So this is
10:40a condition of cycle. If there is a condition of cycle, we will return to true, not my neighbor who
10:46is un-visited, and that is my stack.
10:50So in this case, I will put the next call and if the call has returned true, then the cycle
10:55exists. In the last case, what I did when I went up to the level, I had true values in
11:01my stack. When I go down to the stack call, I will return these values. So I will false this
11:08value. Now we will do this which I have written a pseudo code. I have tried to explain the logic.
11:15Now we will write this code. We will run it on both cases.
11:20In this case, there should be no cycle in this case. Then we will dry run on both graphs. What
11:26do we do? We try to run our code for this graph. What did I do? There is an empty
11:31recursion stack.
11:33This stack is completely empty at the start of the start of the start of the start of the start
11:36of the start of the start of the start of the start of the start
11:552 will also be true to our visitors and the stack will also be true to our visitors and the
12:01stack will also be true to our visitors and our neighbors will also be true to our visitors and our
12:04neighbors will also be true to our visitors and our neighbors will also be true to our visitors
12:13Now 3 will see what is the neighbor. Now the calls, like before I was at 0, I was at
12:180. If 0 called 2, then I got a 2. If 2 called 3, then I got a 3. Now
12:26I am at 3. What will 3 do? 3 will see if it is the neighbor that exists in the
12:32stack.
12:34When you check 0, you will already know that 0 has already come in the stack. So this is the
12:41condition of this cycle, so 3 will return true. 3 will return true, 2 will return true, and 0 will
12:48return true. In this case, I will have true, meaning the cycle exists. This is the same thing, if I
12:54do this graph, let's run this graph. This will become my visitor, 0, 1, 2, 3.
13:02and this will become my stack. This is my visited and this is my stack. And this will become my
13:11call stack. Now I will start with 0. What will I do with 0? I will visit myself. It will
13:16come true here and here it will come true. In the recursion stack, I will add 0. I will call
13:211 to my neighbor. As I call 1, I will visit 1. We will store true returns and here we
13:28will store true returns.
13:30Now, what will 1 do? It will call 3. If 1 has called 3, this will also be true and
13:36this will also be true. Now, 3 will see where it can go, where it can go, where it can
13:39go. So, what will it return again? It will return true. It means, it will return false. It means, there
13:45is no cycle detected here.
13:48the 3 level will return false and it will be removed from the stack. When the 3 goes back, it
13:53will be removed from the stack. Visited will be removed from the true, but it will be removed from the
13:57stack. If it is removed from the stack, it will also be removed from the false return. Now, what is
14:02it? Which neighbor is the 1 neighbor? 1 neighbor is 1, 3. It has also been removed from the stack.
14:07We will also remove 1 from the stack. 1 will be removed from the recursion stack. Visited will be removed
14:14from the true.
14:16We will come to 0. What will 0 do? We will see that 1 has been visited. Now I will
14:19visit 2's neighbor because 2 is already not visited. So I will visit 2 to 2. I will add 2
14:26to the stack. Now what will we do? We will try to visit 2 to 3. Look, 3 is already
14:34visited.
14:35In which case is already visited, we analyze three cases. In the case of three, there is no stack in
14:41three cases because it is false.
14:46three non-visited, three non-visited because three have already visited, so we won't call for three because three have
14:52already visited, so I don't have any action in this case. So what will I do? I will see that
14:57all the neighbors have visited, now we will return to the false return, two will be removed from the recurrence
15:01stack, here two will be removed, here two will be removed, here two will be removed, here two will be
15:04removed, here two will be removed, last two will be removed, zero will be removed, zero will be removed from
15:08all neighbors, so zero will be removed from the call stack, here two will be removed and return to false.
15:15No cycle exists. Now we are going to write code for this whole process. What did I do? I have
15:20made my edge class and I have taken a case with this graph which I should print and be false.
15:27This is my no cycle graph which we have done dry run. The main function is called for creating the
15:34graph. Now let's write my DFS which we will find out what my cycle is in the graph. First, I
15:40will make my public static boolean is cycle.
15:44In this cycle, I will have my array list of edge, which I will name graph. I will check for
15:52my graph if the cycle exists. In the beginning, I will make two arrays. One is visited and one is
15:59recursionary. I will give it in boolean type, is equal to new boolean of my graph.length. I will write
16:09here, boolean of my stack is equal to new boolean.
16:14mygraph.length. I have visited and stacked. Now I will run a loop. The loop will run until until I
16:23have any component that is unvisited, so I have to visit it in DFS fashion. If my graph is different,
16:30I should not exist in all of my graphs. So let's run a loop here. for int i equals 0,
16:38i less than mygraph.length, i++. I will check every time that if
16:44not visited, I will call my isCyclicUtil. In this CyclicUtil, I can create another function in which I will pass
16:52the graph, current, i, visited, and I will pass the recursion in the stack. If this isCyclicUtil returned true,
17:05I have called this function and returned me true. In that case, I will return me true. It means that
17:12my cycle exists within the graph. No, I will return false. What are we going to do next? I will
17:18complete my isCycleUtil function. I will copy and paste this same thing here. I will pass this cycle. I will
17:29add my current node in the graph. I will pass my boolean visited array.
17:35and my boolean stack array. So this way I will have 4 parameters that I have passed to isCycleUtil.
17:48Now this cycle is useful to perform my actual modified DFS. In the beginning we talked about connected components. First
17:55there is a main function which is work to scroll down on all components. And within this cycle we do
18:02our main, core work. The core work is to perform your modified DFS. What will we do in the DFS?
18:09I will do the visited of current.
18:12After that, I will add true in my stack of current. I will add true. To true this stack of
18:17current means that the recursion stack is running under my current node. Now I have to run a loop on
18:24all neighbors. I will go to graph of current.size to i++. Now I will remove my edge. e is
18:35equal to graph of current.get i.
18:40First of all, I will check what my current stack is running inside e.destination, which means my neighbor's value
18:47is true. If my recursion stack is running inside, my neighbor already exists, it means that the cycle exists. This
18:55is my cycle condition. I am going to return directly here. What should I do? No. If my node of
19:04neighbor is already un-visited,
19:06If the value of e.destination is false, then I will call this cycleUtil. I will call this cycleUtil. I
19:17will pass my graph in this cycleUtil. I will pass my neighbor to the next level. I will pass visited
19:24and my stack. If this value is true, then I have to return true.
19:31I have taken the case. The first case is where the neighbor already exists. The first case exists in the
19:36stack. The neighbor exists in the stack. I mean, I am back on the road. There is a cycle. I
19:40returned the case. If my neighbor is un-visited, then I will check what the cycle exists in there. If
19:47it has returned the true, then I will return the true. If my neighbor is already visited, then I will
19:52not do anything. But it is un-visited, then I will check what the cycle exists in there. At last,
19:57I will return the case.
19:58If I have no cycle, I will do the stack of current
20:05Now I'm going to remove the current from my stack. Because when the call goes back, my node is removed
20:11from the stack. So I'm going to do it here. I'm going to do it safe. I'm going to summarize
20:16both functions again. This is my cycle, main function, which you will check. If you get un-visited, visit it
20:21from DFS and check the cycle. For checking the cycle, I have performed DFS. Just a single change is that
20:27I have used the stack. In the beginning, add the node in the stack and remove it.
20:33we have to check if the neighbor already exists in the stack then there is a cycle and if the
20:40neighbor is unvisited, then call again DFS If the cycle will return true, then we will return true We will
20:48save it I have taken a graph example in which there is no cycle I am going to call from
20:51main function to isCyclic for my graph We will save and now run
20:59For this graph, I have a true return because this is my cycle case. I have a true return for
21:05this graph. I will show you this graph. How will my graph see? In this given graph, the first edge
21:13is going to 0 to 2. The 2 edge is going to 3. The 3 edge is going to 0.
21:19After that, there is one edge of 1 which is going to 0.
21:24I have taken an example of this graph and this graph exists in the cycle. That is why the answer
21:30is true. Now I take an example of my second graph for which I should have an answer to false.
21:35Here I have commented on the previous code. I have made a new graph. This is my second graph which
21:41we had to dry run on it. For this, I will have false. Let's save it and run it again.
21:47I have returned false value in output.
21:49Already we have dry run on two graphs. Now I am going to dry run on a third graph and
21:54explain the total process again so that it will be clear for directed graphs and how our approach is done.
22:02I have already written my code here and we are going to dry run on this graph. Ideally, I have
22:08a true return on this graph because the cycle is here. This is my cycle form within the graph. I
22:15will start with which one? First, I will make a visitor.
22:200, 1, 2, 3, 4. Then I will make a stack for 0, 1, 2, 3 and 4.
22:30This will be my visited and this will be my stack. With this, I take a recursion stack, so it
22:36will be easy to visualize the same value. We will start with zero. We will visit zero first. Because when
22:47we come, we will visit the stack inside the stack. Then I will come to one. What will I do?
22:54I will check for your neighbors.
22:58my neighbor exists, so there is no other neighbor exists in the stack. All neighbors are now un-visited, so
23:03visit them before I come to 1. I will visit them in the stack and add them in the stack.
23:09Then I will check for one neighbors. One neighbor is no one, because no one goes to the node, so
23:16I will come back to 0. So, as I went back to 0, what was the first time? 0 was
23:21the call to 1. Now, as I went back to 1, so 1 will go away from my stack.
23:31Now I have to go back to zero. If there is no other neighbor, then zero will also be removed
23:35from my stack. Now I will come to the next unvisited. Zero has been visited, one has also been visited,
23:44now two has come.
23:45I am going to start with 2. I am going to start with 2. I am going to start with
23:502. I am going to start with 2. I am going to add 2 to my recursion stack. I am
23:56going to start with 2 and I am going to visit with 2. Now I am going to check with
24:002 neighbors. The first neighbor is 1.
24:04When I check 2, I will check if 1 is already visited. 1 is already visited, but 1 exists in
24:11recursion stack? 1 doesn't exist in recursion stack, so it doesn't exist in any importance. I will check for next
24:19neighbor. Next neighbor has 3. 3 is unvisited, so we will visit it. I have called 3.
24:28Now what will you do? When you reach 3, you will see your next neighbor. What will you do? When
24:34you reach 3, you will see your next neighbor. My next neighbor will be 4. What will I do? 4
24:37is unvisited, so I will put the call. Now I will put the next call to 4, so we will
24:42put it in 4. We will do it in 4 and we will do it in 4. Now 4 will
24:46check for your next neighbor. 4 will see, the next neighbor will be 2. Then you will see that 2
24:51already exists in the stack.
25:03this is my cycle condition this is my cycle condition this is my cycle condition this is my cycle condition
25:11this is my cycle condition this is my cycle condition this is my cycle condition this is my cycle condition
25:18this is my cycle condition
25:23So we can try to dry run. The time complexity of code will be bigger than v plus e. Why?
25:28Because I have used a DFS process. If you modify DFS, you don't have extra functions. So the complexity remains
25:37that. Which is bigger than v plus e.