Preface :
Our ultimate goal is to develop graph algorithms and use them in application. We need tools to create a graph and operations to access and update its properties. We do this in two steps. The Graph interface defines methods that describe graph operations. The DiGraph class implements the interface and enables us to input the vertices and edges and display the key graph attributes.
The Graph Interface :
The Graph interface defines all of the methods you need to build a graph. The interface applies to both undirected and directed graphs. The sheer number of methods in the interface is deceiving. With a closer look, you will see similarities with the interfaces we developed for the list, set and map structures. Just remember, we must deal with vertices, edges and weights. Rather than a simple add() method, the Graph interface has addVertex() and addEdge(). The same is true for remove(), contains() and size(). The following is the Graph interface :
Two methods need to be highlighted. Graph-scanning algorithms require that we provide from vertex along a variable number of edges to a designated vertex. The method getNeighbors() returns all of the adjacen vertices as a Set. An iterator on the set provides access to adjacent vertices. A class that implements the Graph interface does not have an iterator. We use the collection view concept from maps. The method vertexSet() returns a Set reference with methods that allow us to access and update the backing graph collection.
The methods in the Graph interface are a template for implementing directed or undirected graphs. However, there will be differences in implementation. For instance, addEdge(v1, v2, w) in an undirected graph makes v2 a neighbor of v1 and v1 a neighbor of v2 (Below figure-a). In a digraph, the method only makes v2 a neighbor of v1 (below figure-b). The same type of difference occurs with the method removeEdge().
The DiGraph Class :
The DiGraph class implements the Graph interface and adds other methods that are useful in applications. A constructor creates an empty graph. The methods inDegree() and outDegree() are special methods that access properties that are unique to a digraph.
Building the graph vertex by vertex with addVertex() and edge by edge with addEdge() would be a tedious task. The class provides the static method readGraph(), which builds a graph whose vertices are strings. The method inputs the vertices, edges and weights for the graph from a textfile whose name is passed as an argument :
public static DiGraph readGraph(String filename) throws FileNotFoundException;
The format treats the vertices and edges separately. Input begins with the number of vertices, followed by a list of the vertex numbers. Each vertex name is a string :
For the edges, begin with their number, followed by a sequence of triples where the first two entries are the source and destination vertices of an edge and the third entry is the weight of that edge. For nonweighted graphs, assign the weight of an edge as 1 :
The method toString() provides a representation of a graph. For each vertex, the method gives the list of adjacent vertices along with the weight of the corresponding edge. The information for each vertex also includes in its in-degree and out-degree. Output is in sorted order of vertices :
Output format :
Our ultimate goal is to develop graph algorithms and use them in application. We need tools to create a graph and operations to access and update its properties. We do this in two steps. The Graph interface defines methods that describe graph operations. The DiGraph class implements the interface and enables us to input the vertices and edges and display the key graph attributes.
The Graph Interface :
The Graph interface defines all of the methods you need to build a graph. The interface applies to both undirected and directed graphs. The sheer number of methods in the interface is deceiving. With a closer look, you will see similarities with the interfaces we developed for the list, set and map structures. Just remember, we must deal with vertices, edges and weights. Rather than a simple add() method, the Graph interface has addVertex() and addEdge(). The same is true for remove(), contains() and size(). The following is the Graph interface :
Two methods need to be highlighted. Graph-scanning algorithms require that we provide from vertex along a variable number of edges to a designated vertex. The method getNeighbors() returns all of the adjacen vertices as a Set. An iterator on the set provides access to adjacent vertices. A class that implements the Graph interface does not have an iterator. We use the collection view concept from maps. The method vertexSet() returns a Set reference with methods that allow us to access and update the backing graph collection.
The methods in the Graph interface are a template for implementing directed or undirected graphs. However, there will be differences in implementation. For instance, addEdge(v1, v2, w) in an undirected graph makes v2 a neighbor of v1 and v1 a neighbor of v2 (Below figure-a). In a digraph, the method only makes v2 a neighbor of v1 (below figure-b). The same type of difference occurs with the method removeEdge().
The DiGraph Class :
The DiGraph class implements the Graph interface and adds other methods that are useful in applications. A constructor creates an empty graph. The methods inDegree() and outDegree() are special methods that access properties that are unique to a digraph.
Building the graph vertex by vertex with addVertex() and edge by edge with addEdge() would be a tedious task. The class provides the static method readGraph(), which builds a graph whose vertices are strings. The method inputs the vertices, edges and weights for the graph from a textfile whose name is passed as an argument :
public static DiGraph
The format treats the vertices and edges separately. Input begins with the number of vertices, followed by a list of the vertex numbers. Each vertex name is a string :
For the edges, begin with their number, followed by a sequence of triples where the first two entries are the source and destination vertices of an edge and the third entry is the weight of that edge. For nonweighted graphs, assign the weight of an edge as 1 :
The method toString() provides a representation of a graph. For each vertex, the method gives the list of adjacent vertices along with the weight of the corresponding edge. The information for each vertex also includes in its in-degree and out-degree. Output is in sorted order of vertices :
Output format :
Supplement :
* [ Data Structures with Java ] Section 24.2 - Example 24.1
This message was edited 5 times. Last update was at 28/03/2011 10:12:47
沒有留言:
張貼留言