

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
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Welcome to Data Structures and Algorithms, Part 2! In this course, we'll cover more of the advanced data structures and algorithms that we didn't have a chance to address in Part 1.
Throughout this course we'll be building parts of "Mappy" – an app like Google Maps or Apple Maps that people can use to find directions on their smartphones.
We'll start this course with some advanced use cases of graph data structures; remember, a graph represents a set of nodes and the edges between them.
In code, a graph can be represented with an adjacency list. For example, the graph shown above can be expressed like this:
| 0 | connects with | 1 | 4 | ||
| 1 | connects with | 0 | 2 | 3 | 4 |
| 2 | connects with | 1 | 3 | ||
| 3 | connects with | 1 | 2 | 4 | |
| 4 | connects with | 0 | 1 | 3 |
Complete the __init__ and add_edge methods.
__init__(self) -> NoneThe constructor should create an empty dictionary called graph as a data member.
add_edge(self, u: str, v: str) -> Noneadd_edge takes two nodes as inputs, and should add an edge to the adjacency list (the dictionary).
The dictionary maps nodes to a set of all other nodes they share an edge with. For example, in JSON form it would look like this:
{
"0": [1, 4],
"1": [0, 2, 3, 4],
"2": [1, 3],
"3": [1, 2, 4],
"4": [0, 1, 3]
}
Use these steps: