Learn

We’ve built a class to store information and connections between individual vertices, but we need another class that keeps track of the big picture.

Our Graph class will track all the vertices and handle higher level concerns like whether the graph is directed, requiring edges to have a set direction, or undirected, allowing bi-directional movement across edges.

We’ll start by giving Graph the functionality to add vertices. We’ll use an internal .graph_dict property to store every vertex by its value pointing to the vertex instance itself.

We want to do the following:

grand_central = Vertex("Grand Central Station") railway = Graph() print(railway.graph_dict) # {} railway.add_vertex(grand_central) print(railway.graph_dict) # {"Grand Central Station": grand_central}

Instructions

1.

In graph.py, define a Graph class with an __init__() method that takes self and directed as arguments.

directed should default to False.

Set directed as a property on self and set self.graph_dict to be an empty dictionary.

2.

In graph.py, define the .add_vertex() method on Graph. It should take self and vertex as arguments. Within the method, print “Adding “ + vertex.value.

3.

After the print statement, modify self.graph_dict so it has a key of the vertex‘s value pointing to the vertex itself.

4.

Uncomment the code to call .add_vertex() on the railway instance and pass grand_central as the argument.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?