CyberV_ASR / VideoMMMU_ASR_large /Engineering /validation_Computer_Science_12.mp4.txt
marinero4972's picture
Upload 301 files
0a5fd4d verified
raw
history blame
10.2 kB
[0.02s -> 8.34s] In this lesson we will study about pre and post visited times. Sometimes these are also called start or end times.
[10.38s -> 15.06s] And sometimes you may also see this arrival.
[15.60s -> 25.95s] and departure times of different vertices when we do dfs so just a brief recap when we do dfs we start from one node
[25.95s -> 38.54s] and go as deep as possible for example if we start from a we will go to b from b we will we will go to c and from c there is no way to go anywhere else so we will come back to b
[38.54s -> 52.38s] again we have no option so we will come back to a then from a we will see what are different paths so this we have already taken so we will not take it this we have not taken but it goes to a vertex which we have visited so we will not visit it
[52.38s -> 63.86s] this is incoming then next is d so we will go to d and then from d there are two paths take one of them so let's say we take e and then we take f
[63.86s -> 76.08s] this way we visit and this is called DFS so here we will add a few a couple of more values while doing the DFS so we will do DFS just as our earlier method
[76.08s -> 89.30s] only thing is that the first time we visit a vertex we record its time so in the beginning time will be zero so starting node we will discover at time equal to zero
[89.58s -> 91.92s] then we will go to next node
[92.50s -> 106.51s] let's say we took this path so we reached b at time equal to 1 and every time we reach a node or we depart from a node we increment the time then from b we went to c so we reached here at time 2
[106.51s -> 118.29s] and we have not finished with b yet we are still discovering from here onwards similarly for a and these have not yet been discovered so their times are empty
[118.45s -> 132.96s] then from c there is no way to go so we will finish with this node and we will never come to it again so we return from it at time 3 then we come back to b from b there is no way to go so we come back to a
[132.96s -> 140.24s] from a we will not take this path already visited d is there so we reach d at
[140.91s -> 154.34s] 4 you can also increment time in between but let's keep if there are 5 vertices or here in this case 6 then exactly we should have start from 0 and go till 11.
[154.34s -> 158.38s] So exactly 12 values, 2 for each.
[159.09s -> 172.29s] unnecessarily incrementing the count that will not impact anything but let's keep it simple so we return from c at 3 then we came to b from b there is no way to go so we return from b
[172.29s -> 183.89s] At 4, we will never come back here again. Then we reach A. From A, there is still some undiscovered path. So we will come to D at time 5.
[184.78s -> 197.44s] Then we will go to E at time 6. Then from E we will neither go to A nor to C. These are already visited. So we return from here at 7. Then we come back to D.
[197.44s -> 200.11s] Then we go to f at time 8.
[200.37s -> 214.34s] and we return at time 9 and then from we reach d and from here we return at time 10 and then we come back to a and there is no other option so we we are done with a also
[214.34s -> 222.83s] at time 11. so you see timings are from 0 to 11 that is 12 values and we have six nodes
[223.12s -> 234.77s] So, these are called pre-visit times and post-visit times or these are also called start end times, arrival departure times, whatever you may like, you may call it.
[234.77s -> 246.43s] uh it it will be used in a lot of problems that we will solve later now one of the application would be to classifying the tree edges so when you
[246.43s -> 258.42s] do a d dfs traversal of a graph then you only take a few edges you don't take all the edges let's see we came here then here
[258.99s -> 271.01s] Then this one, this one and this one. These are the only five edges that we took. Other edges we did not took. So these edges we will call tree edges.
[271.01s -> 281.10s] Then this one is going from ancestor to descendant. So we will call them forward edge. Similarly descendant to ancestor will be back edge.
[281.10s -> 294.29s] and we will also have crossh the remaining one like e to c it's neither ancestor nor descendant so we will see all of this later and we will see that we can use this pre and post widget times
[294.29s -> 306.98s] for classifying edges into these four categories and another use of this would be that you can do topological sort using this so just look at the departure times forget about arrival times
[306.98s -> 319.57s] and sort them in descending order so maximum is a then next is d then next one is f
[320.82s -> 326.61s] then we have E and then we have
[326.93s -> 341.49s] b and c so you will see that this is a topological order and all the edges will go in left to right direction so let's draw them a to b this direction
[342.32s -> 348.11s] A to D this direction, D to E this direction.
[348.43s -> 360.94s] you can verify that all the edges will go from left to right if we sort them in descending order of departure times so these we will see in separate lessons here the topic is to
[360.94s -> 373.70s] find out the pre and post wasted times which we already roughly saw how to calculate so how we will do it in code so let's see the earlier dfs code this is our plain dfs code we start
[373.70s -> 385.54s] dfs on a graph g and there is a notion of starting vertex so what we do we level it as visited then we look at all its adjacent edges and if
[385.54s -> 398.58s] and edge is going to a vertex which is not visited then we trigger again a dfs on that node so what extra is required here we start dfs from here so its time is zero
[398.93s -> 412.08s] then we will go to this so its adjacent edges are this one 0 1 and 0 3 and both are going to unlisted nodes so we will take one of them let's say this at time 1
[412.14s -> 423.60s] here again this call will be made only one edge is outgoing and it's going to unlisted node so we come here at time two from here there is nowhere to go so we return from here
[424.08s -> 438.90s] So when we mark a node as visited, first time we write, let me write in different color, pre of that node s equal to whatever time is currently.
[439.25s -> 450.38s] and then also increment the time after storing it and in the beginning time will be zero before starting the tfs from any node
[450.74s -> 463.76s] So S will get a time of 0, this one. Then it will call DFS on 1. So when DFS of 1 is called, again 1 will be marked wasted.
[463.76s -> 477.78s] storing the time it was incremented so one will get a time of one then it will call dfs on two so two will get a pre of two and again it will call it and after
[477.78s -> 490.03s] we have done for all the nodes or all the paths going out from a given vertex we are done with everything so we return from this so then we record the post time
[497.10s -> 505.65s] and that's all these are the two lines that you need to add and obviously you need to add these time equal to zero some global counter
[506.16s -> 516.88s] so before starting just when you visit it you record the time the time at which we reached here and when this all the recursion terminates finally here
[516.98s -> 530.37s] and we go back to the calling function then also record the time so these are basically the two timings so from 3 it returns at time 3 then at 4 then we come here at time 5
[530.37s -> 544.78s] then we go here at time 6 then we return from here at 7 then 8 and finally 9 here we have 5 nodes so we have times from 0 to 9 that is 10 values
[544.98s -> 559.62s] So let's write the code for this. So I will modify it for C++ and it should be very trivial to do it for Java and Python. For those of you who are writing in Java and Python, we had already seen the code for DFS in our earlier lessons.
[559.62s -> 574.06s] So these are very trivial changes. So this was our earlier code and it's the same graph. So this is our recursive DFS. So what we need to do here.
[574.54s -> 576.27s] Pre is...
[576.56s -> 590.24s] equal to time++. We have not defined time, we have not defined pre, we will do that and when we are done with it, we write post s equal to time++.
[590.24s -> 604.34s] we have made the changes only thing required is to define these so what we will do we will keep a counter time and in the constructor we will initialize it to zero
[607.89s -> 612.21s] And what else we need? We need pre and post. So let's pass it here.
[641.81s -> 654.13s] So this will be of again the same size number of vertices since for each vertex we will have a pre-visit time and also for each vertex we will have a post-visit time.
[657.26s -> 666.16s] and we need to pass it here and similarly here
[669.74s -> 674.00s] So let's run it if there is any compilation issues here.
[675.18s -> 688.69s] So there is no compilation issue. It just prints the order in which the vertices are visited. So let's say we want to also know the actual timings for each vertex. So what we will do.
[688.69s -> 698.06s] It is just for our visualization purpose that it correctly calculates the time or not. So you can print it.
[709.58s -> 720.82s] Let's print the index also, which will be the vertex number. Here we have vertices labeled from 0 to mv-1. So that makes...
[721.17s -> 725.17s] Coding simpler here. Then pre I.
[733.84s -> 748.38s] this is just for our purpose only change we did is this one we added this line before just when we visit a node and we add this line post visit time when we are done with this node that's
[748.38s -> 761.33s] that's two line now let's print it so you see for zero it's zero and nine start and end time so for zero it's zero and nine
[762.06s -> 773.97s] or it may vary for these two depending on whether we took this path or this path for one it's one four one four it matches again
[774.96s -> 784.46s] for 2 it's 2 3 which was same there for 3 it's 5 8 for 3 it's 5 8 and for 4 it's 6 7
[785.04s -> 798.30s] so for 4867 so that exactly matches what we had seen so this is how you can calculate pre and post widget times in further lessons we will need to add these for our
[798.30s -> 804.11s] other applications related to DFS. We will also see DFS tree and edge classification.