 Using a greedy algorithm, we found a path from A to H with distance 29, but can we do better? And more importantly, can we describe how we do it? And so in 1959, Dutch computer scientist Hezger Dijkstra described a better solution. Dijkstra's approach is based on a useful idea in graph theory and life. It's easier to know where you've come from than where you're going. We'll break Dijkstra's algorithm into two concepts. First, recording the distance of the shortest path from A node to the destination and tracking the nodes we've checked. To apply Dijkstra's algorithm, we'll start at the destination, which is distance 0 from the destination. Next, we'll find the distances from all adjacent nodes to the destination and record. And then expand from the nodes for which we have distances. And if we get to a node where we've already found a distance, we'll compare our distances and, if we have a shorter route, replace. And once we've checked all edges leading to a node, we'll mark it as visited. So let's apply Dijkstra's algorithm. Since we want to get to H, we'll keep track of the shortest distance to H. If we're at H itself, the distance is 0, so we'll mark that. Now there are three paths that lead to H. D to H, distance 8. E to H, distance 10. And G to H, distance 15. Now we'll record some information here. First, since these are all the edges to H, we can mark H as visited. In effect, we'll ignore H from this point forward. The other thing we'll do is we'll label each node with its distance to H, and you can think about this as a signpost. If you get to a node, the label tells you how far you have to go until your destination. Now we note that the shortest distance to H is through D, so we'll start there. To get to D, we have to travel from B. So the path from B to H has a distance of 12 plus 8 or 20, and we'll record that. And since all the edges to D have been considered, we can mark it as visited and ignore it hereafter. Now again, you can think about these labels as how far you have to go from a vertex to the destination. And if we want to find the minimum distance to H, we would probably want to start as close as we can. And so the smallest unvisited vertex is E with distance 10. So let's start there. Now to get to E, you have to travel from either B or C. If you go from B, the distance to H through E will be 6 plus 10, 16. And what's important here is that this is smaller than the current amount, 20. And so we replace it. And so now the sign saying distance to H at B now reads 16. Also, the original path we had to H, because it's a longer path, we can eliminate it from consideration entirely. We can ignore this path from now on. And again, the way we might look at this is the following. Once we get to B, we can get to H by either going through D or through E. But going through E is shorter, so we'll ignore the path through D. Now the other way we could get to E is by starting at C. And so the path from C to E to H has length 8 plus 10, 18. So we mark it. And since we've considered all edges to E, we mark E as visited. So again, we're trying to find the shortest route to H. And so the smallest unvisited vertex is G with distance 15. Now to get to H through G, we can start at C, which will cost us 15 plus 15 or 30. But notice that we've already found a path from C to H that has cost 18. So we don't need this alternate route, and we can ignore the edge between G and C. The other way I could have gone to H through G is by starting at F. And so we could also get to H from F, costing us 4 plus 5 or 19. And since we've considered all edges leading into G, we can ignore it and we'll mark it off. Now the smallest unvisited vertex is B. And to get to B, we can go from either A or C. And that tells us the path from A has distance 15 plus 16, 31. Meanwhile the path from C has distance 7 plus 16 or 23. Now since we already have a path from C of length 18, we're not going to take the longer path. And again, we've considered all the edges into B, so we can mark it as visited. And we continue. The smallest unvisited vertex is C with 18. In other words, if you can get to C, you can get to H with just 18 more units. Now to get to C, we can either go from A or from F. From A, the path length will be 6 plus 18 or 24. This is shorter than the existing path from A through B, E and H, and so we replace it. Now from F, the path will have length 12 plus 18, that's 30. And again, notice that F already has a path to H, which is shorter, so we'll ignore this path. We've also considered all edges leading into F, C and A, so we can mark them as visited. And notice that we visited all the vertices and found the shortest path from a vertex to our destination. And so we know that no shorter paths exist and that the shortest path from A to H has length 24.