We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Adjacent Nodes

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.

Assignment

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}