Section 9.6 Implementation
Using a map, or dictionaries in Python, it is easy to implement the adjacency list. In our implementation of the Graph abstract data type we will create two classes (see Listing 9.6.1 and Listing 9.6.2),
Graph
, which holds the master list of vertices, and Vertex
, which will represent each vertex in the graph.Each
Vertex
uses a map to keep track of the vertices to which it is connected, and the weight of each edge. This map is called connectedTo
. The listing below shows the code for the Vertex
class. The constructor simply initializes the id
, which will be an integer, and the connectedTo
map. The addNeighbor
method is used add a connection from this vertex to another. The getConnections
method returns all of the vertices in the adjacency list, as represented by the connectedTo
instance variable. The getWeight
method returns the weight of the edge from this vertex to the vertex passed as a parameter.We use
operator overloading
so that when we print our Vertex using the cout <<
function we get a list of its connections, instead of an error. This function must be initialized as a friend function
within the class definition, but is required to be defined outside of the class. This is specific to operator overloading
in C++.The
Graph
class, shown in the next listing, contains a map that maps vertex names (int) to vertex objects (Vertex). In Section 9.5 this map object is represented by the shaded gray box. Graph
also provides methods for adding vertices to a graph and connecting one vertex to another. The getVertices
method returns the names of all of the vertices in the graph.Using the
Graph
and Vertex
classes just defined, the following Python session creates the graph in Figure 9.2.1. First we create six vertices numbered 0 through 5. Then we display the vertex dictionary. Notice that for each key 0 through 5 we have created an instance of a Vertex
. Next, we add the edges that connect the vertices together. Finally, a nested loop verifies that each edge in the graph is properly stored. You should check the output of the edge list at the end of this session against Figure 9.2.1.You have attempted of activities on this page.