 What's up guys my is Michael and welcome to my YouTube channel today. We're gonna go over this problem called network Delay problem on the code question network Delay time So you're given a ray in this case. It's gonna be Let me make it bigger. So you give an array to let's say I'll show you the starting values to two one one and Then two three one and three four one So this is your ray, it's a 2d array by the way as you can see the 2d 2d Let me make this larger. Okay, so Cross this out. Okay So you give an array and then you have a starting value. Okay, okay, which is your starting signal to send from the starting node K K which is the starting node to send your signal to okay, so each of these values in your array Represents the a certain value in your graph. So the two the first value represents the starting node second one weapon represents the destination and The third one represents the weight so If I were to draw this graph out To has a No, two has a starting value two and then it's gonna. Can it be connected to one and It's gonna have a way to one No, two also has a destination value of three So I'm gonna draw two to three and it has a weight of one also No, three has a starting value three right and then it's gonna have a destination value four So I'm gonna draw three to four and then it has a weight of one So the question is given a node given a node starting node Okay What will if what would be the time would take to? Pretty much to send a signal from the starting node to and reach all the values in the graph So in this case if no two were to send a signal That means that It would send a signal After one second one and three would get the signal one one and three right after one second one and three would get the signal But that's not the end of the Not all the nodes receive the signal yet from three it's gonna send a signal to four after another second So that this total seconds the output from a signal from two From no two Is going to be two seconds total output Two seconds to get to all nodes to get to all nodes the reason why is because After one second to what would only send the signal the values to one and three after another second three is gonna send a signal to four so The time would take to get from two to all the values in the graph would be Two seconds because it would be one plus one which is two Okay, so the output is two seconds. I already put that here output two seconds. Okay, but what if our graph is different? You can't just add up the the values of The distance here, and I'll tell you why let's say I have no two Let's say we're given a completely different graph like this It's like a spider right and they all have weights of one You cannot just add up all the weights in this graph because Just if you were to add up all the weights in the graph here You would have it would say the output would be four seconds But that's actually not the time it would take from from a signal from two to get to all the nodes It would actually just take one second And the reason is after one second if two were to send a signal to all its neighbors one three four and five After one second, they would all have the they would all have the values of the signal They would all have the signal from the value to they would have received it After time one second because all these weights represent the seconds they would take for To reach to the to all them after one second they two would have two would send a signal to all these values And that would be it So it's not just adding up all the nodes all the weights in every single Value here, and I'm gonna give you another thing. It's also not just getting the greatest The greatest value in your in your In your graph, let's say I have two and I point let's say let's say the graph is just one link list Let's say two let's say it takes four seconds from two to go to one Five seconds from one to go to three and then one second from one for three to go to four It's not just that if you say it's the greatest value in your graph That would give out five, but that's not true in order for two to reach all the values in the in this In this graph It would take a time of four plus five plus one, which is ten And the reason why it would take four plus five plus one to reach it because It would take all these it would take Ten seconds from a signal from two to reach all the value to all the nodes in this graph See all the nodes are two one three four would take ten seconds to reach it So it's not just five. It can't it's not just the greatest value you see in your current current The location you're at so yeah, that wouldn't work either So you might be thinking how do I solve this problem? Well a way to do this is actually to use Something that I have not gone over in a long time is depth first search What we're going to do is we are going to record the time that it takes from distance at the node Where the so we're going to maintain a array distance for each node, right The earliest that arrives at the node When we visit it We will get the weight of that value, right if this current weight is a if this current weight is The fastest at the current node, then we have to call depth first search on that node and Then after that it would exit From from that node and then we'll pretty much We'll call depth first search on that node and then we'll get all will be able to reach all the values in the in the graph Okay, and then you would maintain the distance at the node So I'm going to show you guys how to do it in a few minutes Maybe I should coat it up. Okay. Maybe I should actually go over how it works on paper and then I'll do it Code it up. I'm gonna get a drink of water first All right guys, so I'm gonna explain the solution The DFS solution so what we're gonna do is We are we're actually going to maintain a map that maps each value with its corresponding neighbors And we do this in order to look up the neighbors really quickly. So in the beginning, we're gonna create a map So what are the neighbors of two two? It's one and three. So we're gonna do a Map to So we're gonna convert this array into a map. So Two is gonna have a pair of one one It's also gonna have a value of three one Three is gonna have value of four one So we do this in order to maintain the to look up the neighbors faster Okay, so that's what they do first then what they do is they create a distance array Up size and N plus one. So n is the number of nodes. We're gonna do nodes plus one. We do this because we want to index at One so we're gonna do zero one two three four. So yeah It's gonna be size of five because we want index at one and they set everything infinity first Okay, so that's the integer under sewer max, okay so now now we're gonna set the the distance at K we're gonna set that to be zero. So what is K? K is a K is our initial node that we're sending the signal from we're gonna start that to have value of zero So at two we're gonna zero Then we're gonna keep track a variable called time Time we're gonna start at zero and every time we run DFS we are going to add the weights the weight to time and we're gonna do that in order to Check if we need to change it or not change our distance array So for every neighbor that we see So first for every neighbor that we see in this case our starting node is two the neighbors one and three So we're gonna go loop through here these values Is my distance at one Is a greater so my distance at one a distance at one is if distance is greater than a time our accumulator over DFS plus The weight at one so the weight so In our case distance at one is infinity and it is greater than Then zero plus our weight. So our weight is the time it takes To get to one is one. So yeah, if any is greater than zero plus one So what are we gonna do? We're going to set Our distance at one is gonna be zero plus one So distance at one is gonna be equal to zero plus one, which is a one So we're gonna change this into one And that's gonna be the time it takes to travel to one, right? Then we're gonna call DFS on One so on one This time we're going to keep a pass-in time plus the weight That we we just went went through Because that's the keep track of the in the case of the second case of going all the way down in the total time So we're gonna pass in one time plus weight, which is a Which is one and We're gonna call check all the neighbors at one. There are no neighbors one. So it stops there We got a check goes through three now, okay, so now is three's Is three is a distance at three greater than The time so now because we've jumped back to here the time we're gonna start at zero now. So yeah time zero plus Value one so yes, it is because three is infinity which is greater than zero plus one and That is the case. So we're gonna set our distance at three is gonna have a value of one zero plus one Okay Then what are we gonna do that? We're going to run DFS on three and passing in one our time of one. Yeah, so When you pass DFS at three it's gonna go through all the values the neighbors of three so in this case it's gonna be four is Is distance at four Greater than the time our time is gonna be one because That was a time from the previous Value it took from the previous to the previous node. So yeah times one plus The current the weight of the current time that takes from three to four which is one Yes, it is because infinity is greater than two So what are you gonna do? We're gonna set the distance at four is going to equal to two So four is gonna have distance to and we're gonna run DFS on four and four has no neighbors In your map. So we're done there and then after that we get out of the help of the DFS helper and Then we're gonna go through all our distances in our distance array And then we get the maximum distance in our case is four not four two in our case of two and then Yeah, if if one of them is still in infinity Well, then yeah, that's that means it's not possible, but in our case we're looping from one to four If one of them is still infinity, it's not possible, but it's not like one of there's It's gonna get the maximum value. So in this case, it'll be two and then we return to So yeah, that's how you that's how you do this problem I'm going to code this solution now and I hope you guys follow along with me while I code it up and Yeah Hope you guys a couple you guys understood this problem All right, so I'm gonna go over the solution So first we got to maintain the we're gonna convert our array to like pairs for each neighbors right To do that I'll use a order unordered map and it's gonna be integer representing each node and then the I'm gonna have another unordered map You could use pair, but I'm gonna use another unordered map and I'll call this Okay, this is gonna represent Each pairs so two is gonna have one one. It's also gonna have three one three is gonna have four one. Okay This is just to look up the neighbors faster So we're gonna go through every value in our array So Times that I is gonna represent each array. So you see here one ray two ray Three ray, right? So I'm gonna do So times that I zero represents the starting node See starting node zero It's gonna be Two one is gonna be one two is gonna be one, right? This is So times that I add zero representing the starting node I'm gonna make that my graph at times of a zero And I'm gonna make it into a pair is gonna equal to the pair of times that I One and times that I add two. Okay Because I'm using unordered map. I don't actually have to make a pair. I could just set this graph at Times of I one equal to the value So I'm gonna do that here. This is basically just pairing it up. Okay, so times that I don't mind the weird syntax, but This is basically what it's doing Okay, this is this is just pairing up each each value with its corresponding to Destination and wait, that's what it's doing. So like two is gonna have destination one and wait one It's also going to have destination three wait one stuff like that. So that's what this is doing. Okay Okay, so that's what we did that here Then I need to create a vector This is gonna represent distance and it's going to have and plus one I'm gonna go through all my values of my and I'm gonna go from one to end And I'm gonna set distance at I is going to set to infinity in this case. It's gonna integer underscore max Okay, so now once we got that What I'm what we're gonna do is we're going to set distance at K Which is the starting node is going to equal to zero and We're also gonna keep track of the time the total time to pass in which is gonna be the total time Uh Representing how much we further we have to go down. So like if I'm at three total time Previous is gonna be one plus the next one, right? So all time is gonna be cool start zero. Okay Okay, so then I'm gonna call DFS I'm not gonna put anything yet. We'll write DFS soon. But yeah, I'm gonna call DFS Pass in total time and Yeah, passing total time and K and stuff like that Then DFS After this, I'm gonna go through for I equals zero. I lessen distance and I the And I plus plus so I'm gonna start I Go to N and If the distance I gotta get the max value, so I'm gonna get the this is gonna be We have to get the max Now a network time in the end. So I'm gonna call this one max time Zero we're gonna do This max time is gonna equal to max of this each distance and max time So this is gonna get the maximum value for the maximum distance value in our distance, right? if Max time is equal to integer underscore max We return negative one because that means it's not possible. That means they're still in infinity Otherwise, we're gonna return Max time. I'm gonna make this into a ternary operator. So return max time equal integer max Question mark if they equal that we're gonna return negative one otherwise return max time So I'm gonna make this that's what does that's what this one line statement does, okay? so We'll get rid of that So we got rid of that Yeah, now we have to write our DFS So how are we gonna write DFS? Void DFS How to pass in this no, I need to pass in my graph. So I'm gonna pass in my graph We'll call this ampersand in order to I need to pass in my distance Vector did I pass it? Yeah, I gotta pass this in Pass this in I forget parentheses, yeah What else we have to pass and we have to pass in the time because the time changes time Let's see what else we have to pass in. Oh, yeah, I gotta do brackets here Passing distance we have past time Yeah, I think that's it Okay I'll see what else I have to pass in when I keep going but hold up. Let me see. Oh, yeah I also have to pass in the value that I'm starting which is K Let's call it start So I don't have to be Okay, so We're gonna go through all the values of The neighbors of graph at K. So I'm gonna do auto for every value, which is a neighbor I'll call it for every neighbor at a graph at K Graph at start So for every neighbor at graph at start We got to check if if Start okay, wait if If neighbor dot first So neighbor dot first is gonna be this value Right to know that we're at so if distance at neighbor dot first is greater than Time plus neighbor dot second So neighbor dot first is the The destination node right for each neighbor, right? So if the distance at the destination node is greater than the time Plus the weight that it takes to go to that node That's what this if statement is doing then we're going to set distance at neighbor first is going to equal time plus neighbor that second Then we're gonna call DFS on Same thing Neighbor dot first, I think yeah, we're gonna call the DFS on neighbor dot first Okay, so we're gonna pass in graph distance Time is gonna be time plus neighbor dot second Right because we're changing time Start is gonna be neighbor dot first Yeah, and I think that's it Pretty much it Yeah, that's it And we're gonna let's run the code and see if it works off. I forgot this We got a pass in graph Distance what else did I do time time? Start which is okay And it got accepted So yeah, that's how you do the DFS version I'm gonna go over the priority queue version now Which is a little more complicated because you have to run dexter's algorithm So I'll go over that now in the sheet of paper next Okay, we're gonna go over the same thing but this time with dexter's with he So this is different a little different We're going to maintain a visited That's gonna have the size of one two three four plus one so it's gonna be five They're all gonna have false And we're gonna have a count that represents the number of nodes that we've seen already So that's gonna be zero We are also going to have a priority queue So to do that the priority queue is going to be a min heap. So it's sorted in order um Also the we're gonna have a map the same map Representing these pairs. So I'm gonna do that here one one three one Three is four one. Okay so that's that that's a map and Let's see we have a priority queue. I'll call the heap here. I mean he puts on the bottom Put me keep on the bottom. Okay? So first we're gonna push Our starting value which is k2 So k is equal to two we're gonna push value to zero into our heap Okay, oh Yeah, we also have to have a result result is that value we're gonna return Okay, while the heap is not empty and while we haven't seen all the nodes in our graph yet We're gonna pop it. So we're gonna pop this to and Is did we visit this already? We did not visit We have not visited to yet because it's false. So what we're gonna do is we're gonna keep increase our count to become What? Accounts gonna become one. Yeah, and then we're gonna set it to true. So now we're gonna visit to so two is now true Now What are you now? Okay, so we're now so the result our result we're gonna set that equal to the second value of our heap Heaps second value, which is zero. Okay? um then if if the if the neighbors Are greater than zero, so we're gonna go through every neighbor. Okay, we're now gonna go through every neighbor So what are the neighbors of two one and three? Okay? So the neighbors of two is one and three We have not visited to So whichever one we haven't visited we're gonna push it onto our heap so we haven't visited one yet, so we're gonna push one and We have to push one and node second plus the previous neighbor second so node second was This value the result which is Yeah, zero. So we're gonna push Node second plus neighbor second so neighbor second is one plus No second was zero. So it's gonna push one one one. We're also gonna push three because it's the second neighbor and one plus The node second which is gonna be one also. So because one plus zero is gonna be one. Okay, so we do that You know all the neighbors are done We go back to the top of our heap is this empty No, it's not empty We're gonna pop the value of one one off of our heap And we're going to check if it's visited. We have not visited one yet So we increase our count to become two and then we're going to set the Value of one visit is equal to true and our result is gonna be the second value of this So this is gonna be result. So it's gonna be one Now we're going to check if one has any neighbors. It does not So you don't do anything there? Yeah, so that's it Then we come back to the top Is it empty? No, it's not yet. We haven't visited all the nodes yet. So we're gonna pop The next value three one did we visit three yet? We did not so we're gonna increase our count by another one So now count equals a three and we're gonna set three to be true Okay, so now once you set three to be true our result is now going to equal to this value These value which is one These weight which is going to be one then If the neighbors is greater than zero we're gonna go through all the neighbors of three so the all neighbors of three is Four we're going to push four's four's value, which is going to be push push four and then One plus the result of the previous one which is two all right you guys The note we're gonna go to four we're gonna add this with the previous result three yeah We push that Yeah, that's done. I'll go back to the top Is our count Is a heap empty It's not empty yet So we're gonna pop this value off for two and then we're going to check if we visited already Did we visit four? No, so we're gonna increase our count to become four and then we're gonna set it to true our Results now going to equal this value two and then after that We check all the neighbors of two all neighbors of four. There's none. So that's it and Then the loop ends there because now our count is now equal to four so now because our count equals a four We just returned result, which is two Now we turn Okay, so that's basically how you do this problem. I basically went over the whole code. I'm gonna code it up and then I'll Yeah, that's basically it. All right guys, I'm gonna code this solution up So first We're gonna do int n equal to times outside. This is to Make sure that the size is right Now it's just for for variable sake. Okay, I'm gonna go I to up to n I've lost plus I Need to create a map. So I'm gonna do an unordered map And it's gonna be integer instead. I'm gonna use a vector pair cuz like why not Call this graph, I don't know. Okay, this is gonna be a pair So I need a pair Okay Now I'm gonna do graph at times at I Dot push back I'm gonna push back the pair of times at I At zero Okay, this is that I add zero Push back One and then times that I push back to Okay, so after we push back those we have to create our heap And a visited so I'm gonna do a visited Boolean It's gonna be a five vector back there bull visited and have n plus one Okay Let's see what else we have to do. Yeah, I did that. Oh, yeah, fine. Okay, so I got to create a comparator for my heap Compare is equal to So we had a compare pair one and pair two so first pair two second We're gonna return first at Zero is greater. Oh, okay. We're gonna make it a min heap. So we're gonna be second Return second dot first is less than first dot first, okay, you know what I'll call this Value one Okay, X and Y. I'm sorry. I have to use X and Y Return Y dot first is Less than X dot first Yeah, what offers doesn't ask that first. We had these have to be ampersands Now we got to create our heap or priority queue. So priority queue. It's gonna be min heaps. Oh in Fudge cakes in yeah, so it's gonna be pair Paravents and then it's gonna be vector pair events Yeah, okay dex seal type, this is the convert the comparator to become the actual comparison type so now min heap comparator Okay, so now we pass that in now. Um, oh Yeah, okay, so we got a push min heap dot push Okay, and then zero then we got a while. Oh, yeah, I got to do a result into result and Into count equals zero. Okay, then while min heap dot side is not empty and Count is not equal to n Then I'm gonna do is We got to get the top value so Yeah, okay, so auto First Val, I don't know is it gonna equal to Min heap top Then we're gonna do pop the min heaps top value And what do we have to oh, yeah, okay? Yeah, okay, so then now we have to check if visited at first Val first is equal to false we got to increase our count To make sure we don't visit it then we're gonna do visited at First Val first is equal to true after that we need to Yeah, okay, we're gonna set our result and equal to first Val Dot second, okay, so that gets the second pair of this right the actual weight First Val dot second is the weight. Okay, maybe I shouldn't call first valve Let's just call it. Maybe I should cut top top Val, okay I'll call top Val top Val It's less confusing top Val top Val then Yeah, if the count at Top Val is greater than zero so that means it has neighbors We're gonna loop through from Dictionary of node dot first. Okay. Yeah, so yeah Yeah, that's what you do. So if if graph dot count of Top Val dot second. Oh Yeah, no, no top Val dot first If it's greater than zero then we're gonna do we're gonna go through every neighbor So auto neighbor How do you spell neighbor auto neighbor Yeah auto neighbor graph at Top Val dot first Okay, so we're gonna have to go through every neighbor of first Val and then we're going to push the if it's not visible we'll push first and then Dot second, okay, so if not visited Top Val dot first we're going to push pair of top Val dot second Top Val that first Oh neighbor, okay neighbor dot second neighbor dot second and then we're gonna push We're gonna push neighbor dot second neighbor dot second plus node at second Okay, so result top about that second so plus top Val dot second plus neighbor dot second Okay, and then after the this whole thing ends we're going to Check if count is equal to n and if it is then we're gonna so if count is equal to end We're gonna return We need to return result Otherwise we return Otherwise we return a Negative one. Yeah, submit it. Let's see if it works Whoops typo typo. Oh My god, we got to wait a few seconds Something's not right priority queue and or what? This is right vector vector pair This is right though comp Dexel type Dexel type, this is right Maybe if I call it cmp auto cmp Const pair pair in one X pair Y oh second oh My gosh Dexel type Dexel type Yeah Vector pair int int early queue pair int int vector pair int It should be right Auto comp. I did it right. I'm pretty sure that's right Return p1 that p2 dot seconds less than p1. Oh my gosh. Try it again Dexel type comp. Oh wait, did I forgot a semicolon? Oh, I forgot a semicolon Whoops my bad Wrong answer. What is what is the problem? Okay? Really? What what is the problem actually? For to why is the wrong answer? Why not second is less than x dot second should be right Okay, why is this wrong answer count is not equal to and And Count plus plus top value that first Equal the true result equals top value that second if dictionary dot count No dot for top value that first for auto neighbor Dictionary and no graph at top that first if it's not visited top that first. Oh It's neighbor my bad Your neighbor's not visit not top out. Okay, something's still not up. What is wrong? Visited neighbor dot first if it's not visited neighbor dot first. We're gonna push Neighbor dot second neighbor dot first. Whoops Top valve second neighbor dot second. Okay. Yeah, okay now it should work Yeah, now it works. Okay, so Yeah That's basically got the code This comparison Comparator compares the second value. So the weights of the second value To the weights of each graph. So that's what this does. It's a min heap comparing the weights This is pushing all the values to making each pair That's what we did there to make it easier. This pushes the K value first the starting value the result to count To keep track of what we're returning result is the one returning. Oh, so yeah We we pop the first value off and then we check if it's visited already if it's not We increase count and then we're going to set it visited to true Then we set our result to be the weight of the second So the the sec dot second is the weight. Remember we split each value to the weights, right? one and one Repair these two up VW. So these are the weights of each cost. So result we said to original this to the weights If the neighbors are greater than zero Go through every neighbor We if we haven't visited we're gonna push the neighbor and the weight plus the second Yeah The weight that we're going to plus the actual second Onto it and yeah, that's basically how you would do it with Dexter's algorithm. Hope you guys enjoyed this video Raycom subscribe and check you guys later