

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
We mentioned earlier that testing of the Mappy app showed that simpler search algorithms like BFS and DFS are too slow for finding optimal routes.
Let's prove that for ourselves! We'll use two algorithms to find the shortest path from a source to a destination in the same graph: one of them Dijkstra's, the other DFS-based. And we'll count how many nodes each has to visit in order to find that path.
To make things easier, nearly complete implementations of both algorithms are provided. You'll just need to add counting of node visits.
Both functions, dijkstra and dfs_path, take the same arguments and should have the same return types.
There are three arguments:
graph – An adjacency dictionary, i.e. a mapping of nodes to their direct destinations, also indicating the cost/distance of each link. For example, if the only direct destination from Austin is Miami, with a cost of 40, it will be represented as "Austin": {"Miami": 40}. The Python type of graph would be dict[str, dict[str, int]].src – The source node (str).dest – The destination node (str).And the return should consist of two values:
Add the code to count node visits in both the dijkstra and dfs_path functions. Here are suggested steps (the same for each function):
Once you have tests passing, look closely at the output. What do you notice?