Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Java: The Interactive Edition

Section 7.3 The Graph Abstract Data Type

The graph abstract data type is defined as a collection of vertices and edges. Vertices may be either connected to each other or isolated. Edges join two vertices and may be weighted.
  • Graph() creates a new empty graph.
  • addVertex(vert) adds an instance of Vertex to the graph.
  • addEdge(fromVert, toVert) adds a new directed edge to the graph that connects two vertices.
  • addEdge(fromVert, toVert, weight) adds a new weighted directed edge to the graph that connects two vertices.
  • getVertex(vertKey) finds the vertex in the graph named vertKey.
  • getVertexList() returns the list of all vertices in the graph.
  • getVertexKeys() returns a Set of all the vertex keys in the graph.
  • containsVertex(vertex) returns true if the given vertex is in the graph, false otherwise.
Now that we have looked at the definition for the graph ADT, there are several ways we can implement it in Java. 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 Java class.
You have attempted of activities on this page.