Java Reference
In-Depth Information
28.26
Example. The following statements create the graph shown in Figure 28-21, which is a portion of
the graph in Figure 28-6:
BasicGraphInterface<String> airMap = new UndirectedGraph<String>();
airMap.addVertex("Boston");
airMap.addVertex("Provincetown");
airMap.addVertex("Nantucket");
airMap.addEdge("Boston", "Provincetown");
airMap.addEdge("Boston", "Nantucket");
At this point,
airMap.getNumberOfVertices()
returns 3, and
airMap.getNumberOfEdges()
returns 2.
Question 12 What revisions to the previous Java statements are necessary to make airMap
a weighted graph?
FIGURE 28-21
A portion of the flight map in Figure 28-6
Boston
Provincetown
Nantucket
28.27
The algorithms discussed earlier in this chapter use graph operations that are not specified in the
previous interface. Although we could add these operations to the interface so the client could
implement various algorithms, such as the topological sort, we choose not to do so. Instead, meth-
ods that implement the graph algorithms will be a part of the graph class. The interface in
Listing 28-2 specifies these methods. Again, the data type of the objects that label the graph's ver-
tices are represented by the generic type T .
LISTING 28-2
An interface of operations on an existing graph
package GraphPackage;
import ADTPackage.*; // classes that implement various ADTs
/** An interface of methods that process an existing graph. */
public interface GraphAlgorithmsInterface<T>
Search WWH ::




Custom Search