

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to DS&A 2
incomplete
2: Dijkstra's Algorithm
incomplete
3: Dijkstra's: Get Path
incomplete
4: Weighted Graphs
incomplete
5: Next Nearest Node
incomplete
6: Dijkstra's Algorithm
incomplete
7: Dijkstra's Algorithm Review
incomplete
8: Dijkstra's Time Complexity
incomplete
9: Greedy Algorithms
incomplete
10: Dijkstra's vs. DFS
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Testing of the Mappy app revealed that both breadth-first search (BFS) and depth-first search (DFS) were too slow for finding optimal routes.
Let's build a faster solution using Dijkstra's algorithm!
First, we'll need a helper function that will be essential later. The get_path(dest: str, predecessors: dict[str, str]) -> list[str] function has two inputs:
dest: A string representing the label of the destination node.predecessors: A dictionary of node: node, where each node is mapped to its "predecessor" in the path. For example, "Vegas": "Philadelphia" indicates that Philadelphia leads to Vegas in our path.This function returns a list of nodes representing the path that was taken through the graph by following the predecessors.
get_path FunctionStarting at the dest node, traverse the predecessors dictionary backwards, building the final path as you go.
For example, given:
dest = "York"predecessors = {"York": "London", "Hampshire": "Manchester", "London": "Hampshire"}The final path would be: ["Manchester", "Hampshire", "London", "York"]