

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Graphs
incomplete
2: Graph Review
incomplete
3: Adjacency List
incomplete
4: Representing Graphs
incomplete
5: Adjacent Nodes
incomplete
6: Unconnected Vertices
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Now that we have a way to add edges to a graph, it will be useful to have a helper function to get quick access to the nodes (aka vertices) that are adjacent to a given node.
Complete the adjacent_nodes(self, node) method. It takes a node (an integer) as input and returns a set of all the adjacent nodes. For example:
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 3)
graph.add_edge(2, 3)
adjacent_nodes = graph.adjacent_nodes(1)
# {0, 3}