The Last Algorithms Course You'll Need

Implement Dijkstra's Shortest Path



The Last Algorithms Course You'll Need

Check out a free preview of the full The Last Algorithms Course You'll Need course

The "Implement Dijkstra's Shortest Path" Lesson is part of the full, The Last Algorithms Course You'll Need course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through implementing and testing a version of Dijkstra's shortest path in the kata machine.


Transcript from the "Implement Dijkstra's Shortest Path" Lesson

>> So let's go back to our Kata machine and let's go down to Dijkstra's. So this is a DijkstraList. It looks like we're gonna be using an adjacency list, how great is that, and we have to do this. Yes, I have built this before. This is not like I'm gonna come up here and just do this for the first time, that would be just silly of me.

So we can use this as kind of our general guide, right? But we do need to implement a couple things, right, which you can imagine has unvisited, is gonna be something we're gonna need to implement. And also get lowest unvisited is also something we're gonna need to implement.

Now, something I am gonna do is I'm going to intentionally implement it not the fastest. So for all the people in the chat, they're gonna be like, [SOUND]. Don't do that, let it go, okay? That way, everyone can enjoy the beautiful surprise at the end. So of course, the first thing we need is a seen list again.

Cuz again, you don't have a seen list if you use the right data structure, but in this case, we aren't using the right data structure. New array is going to be, of course, the arrays length. It's an adjacency list, which means it represents each one of the nodes and their connection to other nodes.

So array length fill(false), awesome, seen(source). We don't even need to do that, we'll do it in the algorithm, awesome. Then we need to go const dist = new Array(arr.length.fill(infinity), right? That means it's just super large number, right? It's unreachable from this point, you don't have to use infinity.

But for checking sake, and maybe this is a really large graph that generates a really large value, best probably use infinity, right? All right, so let's go dists[source] = 0, right? This is the smallest distance possible. We're already at the source, therefore awesome, all right? We don't know any other distance yet.

We just simply know the distance to the source, which is always going to be 0. And so now, of course, we do the greatest thing ever. Let's create a, we don't actually have to create it. We'll do the Lord's loop, a do while. Now, we don't have to use a do while.

In fact, I'm not gonna use the Lord's loop for this one, we're actually just gonna use this. Remember we had two functions in here and I intentionally made them into functions, hasUnvisited and getLowestUnvisited. And the reason why I did that is I wanna hide the data structure that is better.

So we will go like this, hasUnvisited, and I'm gonna pass in seen, scenery, right? Sounds good? We could technically also pass a dists array, and so that way it's unseen, but it's distances infinity. Then we go, hey, we can't see this thing. So that makes sense? All right, then we also need to get current, which is going to be getLowestUnvisited, right?

And that will be seen and dists again. All right, so we need to implement these two based on the data structures we have currently chosen to use. So let's go up here and go function hasUnvisited, it's gonna take in a Boolean array, correct? Yes okay, awesome scene is gonna be a Boolean array.

I always sometimes confuse it with the chicken square, which is boiling, don't do that. All right, so seen array, and then of course dists, which is going to be a number array, correct? Awesome, and let's do a Boolean right here. A boiling, we're gonna get some chicken juices in here.

All right, so we have that one. And then let's also do getLowestUnvisited, again, exact same stuff. And we're going to need to return something out this time though. We're gonna want to return out our index that is the slowest unvisited item. All right, we're going to assume in this function that if we're calling it, there is 1.

Make sense? Awesome, great, good, let's go. So hasUnvisited, this should be pretty easy, has = false. Look at that, it's has. Why not, for( let i =0, i has to be less than seen. Actually, you know what, we finally have a chance to do this. Let's go, seen.some, whether it's true or not, whether it's seen and its index.

So if it's not seen and it's dist, it's less than infinity. I believe that should work. I always forget about infinity. Let's try this, node is infinity less than infinity. [SOUND] It's not less than infinity, good. So if this thing is less than infinity, we should be good, right?

That number is obviously less than infinity, perfect. Okay, we found something that does work, perfect. I'm glad they didn't be like, well ,what's your LF of your infinity? I'm glad we don't have to deal with that cuz that would have been harder to do. So there we go, we have our unvisited, we're gonna visit every single scene, and see is there something that's false and its distance is less than infinity.

Perfect, so now let's go like this, idx = 0 or we'll go -1. And we'll go let lowestDistance =, gosh, let's do infinity. Yeah, look at that infinity. So now we're gonna walk through all of our distances and see, hey, what is the lowest distance, and it has to be an unseen node.

Makes sense? Awesome, for let i = 0, i have to be less than seen.length, ++i. If we've already seen this one, well, we can kinda continue, yeah? Yeah, awesome, good. Great answer everybody. If dists[i] is less than infinity, and, well, actually we don't even need to do that.

We kinda make the assumption that no matter, what when we're calling this function, this above case is true. Therefore, this case will eventually happen, it will always be true. So if lowest distance is greater than, if it's greater than this distance, we have found a distance that is smaller, so we're gonna use that one.

So lowestDistance = dists[i]. Fantastic, let's do this. And at that point, all we need to do is return out the index in which that happened. So index = i, that's the lowest index, right? We return out the lowest index. Fantastic, we found it, we found the one that's gonna do that.

Again, we walk through every single one of these and then return out the lowest one. All right, so now we're at this beautiful point, we've implemented these two. So now the only thing left we have to do is do the rest of this stuff, which isn't too hard.

We need to say to the seen, we need to go through every single edge. Long as we haven't seen this edge, we calculate the distance, we technically don't even need to do this haven't seen the edge, because we will be able to use the distance formula to not update.

Either way, it doesn't really matter. If we haven't seen this edge, we'll calculate its distance, which is based on my distance plus the distance to that edge. If it's less than its best known distance, we're gonna push both the previous and the dists array with new values. Sounds good?

Great, all right, so seen[curr] = true. We have now officially seen this, isn't it just beautiful? Are we just happy we've seen this? It's like Top Gun, it's super good. Actually I haven't seen Top Gun, I'm really excited to see it. All right, so now we need to go through every single edge.

I forget, what are we using for edges in here? We are using a weighted adjacency list, which, of course, can be a 2D graph edge. So let's go here and go const adjs =, is it graph? What did I call this thing? Array, terrible naming. All right, so this should be able to give us the list of our edges.

So now we can go through each edge. Let i = 0, i has to be less than adjs.length: ++ i. Let's go, and now we can grab out the edge. So first, if seen edge, const edge = adjs[i], so if, if we've seen this one, we're gonna continue.

Wow, that was good spelling. Please tell me you like that spelling, that was. All right, so now we need to calculate its distance. Remember its distance isn't just simply the weight of the edge, but it's the weight of the edge plus my distance. So from the source, everything has a 0 plus their weight.

Then we see the next closest one and we adjust all the weights, then we see the next closest one, adjust all the weights. So let's see what we got here. So we go dist = dists from our current item, plus the edge.weight. That is the dist to this node from the node we're at.

Make sense? And so if this dist is less than the known dists of the edge, then we need to update. So we can just go dists[] equals the new smaller disk, and our previous has a new parent of current. Don't I have to call it pre, what did I call it?

I didn't even do a previous. How are you gonna know our way back? We won't, man. All right, there we go, and that should really be the end of it. We'll keep going until we have nothing left to look at. Every single time we pop something off, we mark it as seen, update the possible shortest distance that is known.

And then just do that over and over again, getting the lowest possible distance each time. All right, so now all we have left is to just walk our distance backwards and we're done. Again, let me look at the return, my goodness, look at this, I'm a genius. This time we actually just simply did a number array.

So as long as there is a pathway from our source node to our sink, we'll have an array with values in it. If there's not, it's just an empty array, fantastic. It's almost as if I've updated this. Okay, yeah, that's the only error, perfect. So let's do that one more time.

Again, exact same thing. const out is a number array and it's an empty array. Let current equals our needle, or, in this case, I called it our sink, where we want to go to. And now, we do that same while loop one last time. While previous current does not equal -1, push it into our path, out.push[curr].

And then update our current to the parent that got us to this point, curr = prev[curr]. All right, there we go, we're just walking one parent back at a time. Remember, the previous is who was the last person to update our distance to the shortest known path. At this point, we need to do the exact same trick we did before.

We don't include the source in this kind of roll up, so we're gonna have to do [source].concat, of course, with something else. Or if we wanna be a little cheeky about it, we could always do one of these, out.push(source), and then just simply out.reverse. Look at that, a little different this time, kinda fun to do.

It's always good to change things up, because that way if you get it wrong, you can always blame it on the fact that you changed up live as opposed to just sticking to the script. All right, awesome, this is a lot of code? We did 63 lines of code, including whitespace.

And so let's see, did we actually get this thing right? npx jest Dij How was that? It feels good, I'm not gonna lie to you, that feels good.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now