Food for Thought: An Introduction to Dijkstra's Algorithm Using Food Delivery
Penny King & Garvey Li


email hook

Welcome to your first day as a delivery driver at OOperEats.

Food delivery relies on efficient route planning to deliver meals quickly, and it's usually something we don't have to worry about, thanks to the advanced navigation software we rely on. These tools help delivery drivers navigate through the city maze efficiently, guiding them along the fastest routes to ensure that customers' meals arrive hot and fresh at their doorsteps.

Alright, it's your turn to take the wheel and make that first delivery! But, hold on a second—looks like our map system has decided to take an unexpected break. I guess route planning really is something to worry about now. It's up to you to decide the path you want to take to your customer's destination. Good luck!

Scroll down to continue!

girl gif
girl gif

This map on the left are the different routes and areas of San Diego you can pass through to get to your customer's destination. It's a bit daunting in this format to try to find the shortest path to the customer. But don't worry! We are here to help!


Understanding the complexity of optimizing delivery routes can be challenging, especially when considering factors like distance. Furthermore, understanding route optimization problems in their pure form can be difficult.

What we will use to find the shortest path will be Dijkstra's (DYKE-strəz) algorithm. Dijkstra's algorithm is an algorithmn used to find the shortest path between nodes in a weighted graph, and was created by Edsger W. Dijkstra. This website will teach about how to apply Dijkstra's algorithmn in a directed graph using various interactive features.

Scroll down to explore!

girl gif

Try It Yourself!


We turned the map into a simple graph using circles and lines. The restaurant you are currently at is the blue dot at the left.

The pink dots are neighborhoods you can pass through on your drive.

The numbers represent how many minutes it takes to get from one neighborhood to another. And the lines are one-way roads that link different neighborhoods to each other.

This red dot is your final destination.


Try to find the fastest path possible to your destination. Create the path you think is best by clicking on the nodes. Scroll down when you are done!

Answer


Was this what you got?

Finding the shortest path is not a simple task… In fact, it can be quite tricky especially in even bigger maps to find the fastest path to the customer. How can we find the concrete shortest path to your customer in an efficient manner?

One method to do so in this situation is called Dijkstra's Algorithm

Start of Dijkstra Explanation


Dijkstra's Algorithm is very similar to the Breadth First Search algorithm, and lets us find the shortest path between a source and target node on a positively weighted graph (directed or undirected). In our explanation "u" describes any node (circle) in our map.

To start out, we define an estimated distance value– u.est for each node u. This denotes a node's estimated distance from the source node.

We already know it takes 0 minutes to get to the source node a), so a.est = 0. However, for all other neighborhoods and the final destination, we do not know the fastest time it will take yet. Therefore u.est is unknown for all other nodes, so we set them to positive infinity.

Initialize Visited and Unvisited Sets


To help us keep track of which nodes to update and which edges to look at, we'll categorize the nodes into two groups: Visited and Unvisited.

Nodes in Visited have a known shortest distance from the source node, while nodes in Unvisited do not.

With the setup done, we're ready to start!

Find the Unvisited nodes adjacent to the set of Visited nodes


The first step is to look at all the nodes in Unvisited that are 1 edge (road) away from (or adjacent to) the Visited nodes.

In the context of our map, we want to see the neighborhoods that are the closet to us by road.

In this case, those nodes are f, b, and c.

Update distance estimates (u.est)


Then, we calculate new distance values for each of the Unvisited nodes adjacent to the Visited nodes.

We do this by adding the edge weight (time to travel down the road) to the u.est value of the connected Visited node.

If the new distance value is less than the respective Unvisited node’s u.est, then we update u.est to be the calculated distance value.

For node f, infinity > 0 + 7, so f.est is now 7.

Find the Unvisited Node closest to the source

Now, the next step is to find a node with the shortest estimated path from the source node.

In this case, this will be node f.

Dijkstra's algorithm is a greedy algorithm... it optimally selects the next unvisited node with the shortest estimated distance from the source at each step.

Move the node from Unvisited to Visited


Now, we consider the node f to be visited! So we can move it to the Visited set.

Repeat!


And now, with a new "Visited" set, we repeat the last few steps again

Find the Unvisited nodes adjacent to the set of Visited nodes and update u.est


Once again, we will update any "Unvisited" nodes (u.est) that are directly connected to the nodes in "Visited"

Although we've already found node b, there is another edge leading to b from node f, so we still need to check b.est.

Since the new calculated distance for b is greater than b.est, the estimate stays the same.

Adding b to Visited set


Now we find the Unvisited node with the smallest u.est, which is node b.

Then we can add b to the Visited set, like we did previously for f.

Updating nodes c and d


Again, we find all Unvisited nodes adjacent to the Visited nodes and update u.est for those Unvisited nodes.

At this stage, we now are looking at c and d.

Since neighborhoods c and d can be updated, we will adjust their estimated value to the shortest one possible based on what we have visited.

Adding c to Visited


We have now found the shortest path between the source node and node c, so we can now add c to "Visited".

Updating node e


We find the Unvisited nodes adjacent to the Visited nodes again, and update their u.est values.

Node e can now be updated. It has a direct path from node c, so we can get e.est = c.est + edge weight = 15.

Find the Unvisited Node closest to the source.


Find the adjacent Unvisited node with the smallest u.est and move it to Visited, which for this step is node e.

Updated what nodes are adjacent to "Visited" set


Again, we find the Unvisited nodes adjacent to the Visited nodes, and update their u.est values.

In this case, this is node d. The only node left is our final destination to the customer!

Our old estimate for node d is larger than our new edge that we are currently exploring, so we can replace 21 with 20.

Adding d to Visited


And now, for the last time, we find the adjacent Unvisited node with the smallest u.est. Now there are no more edges to make updates with nor nodes to visit!

All done!


We have completed Dijkstra's Algorithm for this positively weighted and directed graph.

But wait a minute...

This doesn't look exactly like our solution from before!

As it turns out, Dijkstra's Algorithm finds the shortest path between a source node and ALL the nodes in a positively weighted graph.

So what is the fastest route from the restaurant to the final destination?


To get our solution from before, we simply only consider the path that connects the source and target nodes.

Summary

In situations such as food delivery, finding efficient routes is essential, and understanding route optimization can be very daunting and difficult to grasp.

However, by illustrating Dijkstra's algorithm through a food delivery scenario in an interactive setting, we simplify its complexity. Consequently, understanding this algorithm and how it's used in route optimization becomes more accessible. The one thing that everyone should be able to walk away from our visualization with is a better understanding of Dijkstra's Algorithm and the its applications.

While our focus has been on finding the shortest path for delivering food, the underlying lesson extends far beyond this scenario. In other contexts other than this, optimzing routes through greedy algorithms such as Dijkstra's Algorithmn saves time and resources, leading to more efficient processes in general.

Scroll down to try a different example now that you know Dijkstra's Algorithmn.

Your Turn!


Now that you are more familiar with Dijkstra's Algorithm, try finding the shortest path to your customer in this setting.

Click the nodes to move your car. Scroll down to see the answer.

Your Turn! – Solution


Was this what you got?

The red lines show the right answer. The blue lines are your previous answer.