7.3. The Graph Abstract Data TypeΒΆ
The graph abstract data type (ADT) is defined as follows:
Graph()creates a new, empty graph.add_vertex(vert)adds an instance ofVertexto the graph.add_edge(from_vert, to_vert)Adds a new, directed edge to the graph that connects two vertices.add_edge(from_vert, to_vert)Adds a new, directed edge to the graph that connects two vertices.get_vertex(vertkey)finds the vertex in the graph namedvertkey.get_vertices()returns the list of all vertices in the graph.inreturnsTruefor a statement of the formvertex in graph, if the given vertex is in the graph,Falseotherwise.
Beginning with the formal definition for a graph there are several ways we can implement the graph ADT in Python. We will see that there are trade-offs in using different representations to implement the ADT described above. There are two well-known implementations of a graph, the adjacency matrix and the adjacency list. We will explain both of these options, and then implement one as a Python class.