23-2. Graphs as a Python Class

 

Before we go on with writing functions for graphs, we have a first go at a Python graph class implementation.

If you look at the following listing of our class, you can see in the init-method that we use a dictionary "self._graph_dict" for storing the vertices and their corresponding adjacent vertices.

 

Output:

Let's calculate the list of all the vertices and the list of all the edges of our graph:

Output:

We add a vertex and and edge to the graph:

Output:

Output: