1、WeightedEdge: 包含两个int数据成员vertex1 and vertex2 ,以及一个Object类型的数据成员weight. public class Edge{ int vertex1; int vertex2; } public WeightedEdge extends Edge{ Object weight; } 2.用到的数据结构: // data members int n; // number of vertices int e; // number of edges Object [][] a; // adjacency array null stands for node absent . an integer stands for weight 3. ADT public abstract class Graph { // ADT methods public abstract int vertices(); public abstract int edges(); public abstract boolean existsEdge(int i, int j); public abstract void putEdge(Object theEdge); public abstract void removeEdge(int i, int j); public abstract int degree(int i); public abstract int inDegree(int i); public abstract int outDegree(int i); } 具体实现如下: /** adjacency matrix representation of a weighted digraph */ package dataStructures; import java.util.*; import utilities.*; public class AdjacencyWDigraph extends Graph { // data members int n; // number of vertices int e; // number of edges Object [][] a; // adjacency array // constructors public AdjacencyWDigraph(int theVertices) { // validate theVertices if (theVertices < 0) throw new IllegalArgumentException ("number of vertices must be >= 0"); n = theVertices; a = new Object [n + 1] [n + 1]; // default values are e = 0 and a[i][j] = null } // default is a 0 vertex graph public AdjacencyWDigraph() {this(0);} // Graph methods /** @return number of vertices */ public int vertices() {return n;} /** @return number of edges */ public int edges() {return e;} /** @return true iff (i,j) is an edge */ public boolean existsEdge(int i, int j) { if (i < 1 || j < 1 || i > n || j > n || a[i][j] == null) return false; else return true; } /** put edge e into the digraph; if the edge is already * there, update its weight to e.weight * @throws IllegalArgumentException when theEdge is invalid */ public void putEdge(Object theEdge) { WeightedEdge edge = (WeightedEdge) theEdge; int v1 = edge.vertex1; int v2 = edge.vertex2; if (v1 < 1 || v2 < 1 || v1 > n || v2 > n || v1 == v2) throw new IllegalArgumentException ("(" + v1 + "," + v2 + ") is not a permissible edge"); if (a[v1][v2] == null) // new edge e++; a[v1][v2] = edge.weight; } /** remove the edge (i,j) */ public void removeEdge(int i, int j) { if( i >= 1 && j >= 1 && i <= n && j<= n && a[i][j] != null){ a [i][j] = null; e--; } } /** this method is undefined for directed graphs */ public int degree(int i) {throw new NoSuchMethodError();} /** @return out-degree of vertex i * @throws IllegalArgumentException when i is not a valid vertex */ public int outDegree(int i) { if (i < 1 || i > n) throw new IllegalArgumentException("no vertex " + i); // count out edges from vertex i int sum = 0; for (int j = 1; j <= n; j++) if (a[i][j] != null) sum++; return sum; } /** @return in-degree of vertex i * @throws IllegalArgumentException when i is not a valid vertex*/ public int inDegree(int i) { if (i < 1 || i > n) throw new IllegalArgumentException("no vertex " + i); // count in edges at vertex i int sum = 0; for (int j = 1; j <= n; j++) if (a[j][i] != null) sum++; return sum; } /** output the adjacency matrix */ public void output() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) System.out.print(a[i][j] + " "); System.out.println(); } } /** create and return an iterator for vertex i * @throws IllegalArgumentException when i is an invalid vertex */ public Iterator iterator(int i) { if (i < 1 || i > n) throw new IllegalArgumentException("no vertex " + i); return new VertexIterator(i); } private class VertexIterator implements Iterator { // data members private int v; // the vertex being iterated private int nextVertex; // constructor public VertexIterator(int i) { v = i; // find first adjacent vertex for (int j = 1; j <= n; j++) if (a[v][j] != null) { nextVertex = j; return; } // no edge out of vertex i nextVertex = n + 1; } // methods /** @return true iff there is a next vertex */ public boolean hasNext() {return nextVertex <= n;} /** @return next adjacent vertex and edge weight * @throws NoSuchElementException * when there is no next vertex */ public Object next() { if (nextVertex <= n) { int u = nextVertex; // find next adjacent vertex for (int j = u + 1; j <= n; j++) if (a[v][j] != null) { nextVertex = j; return new WeightedEdgeNode(u, a[v][u]); } // no next adjacent vertex for v nextVertex = n + 1; return new WeightedEdgeNode(u, a[v][u]); } else throw new NoSuchElementException ("no next vertex"); } /** unsupported method */ public void remove() {throw new UnsupportedOperationException();} } /** test program for Graph methods */ public static void main(String [] args) { AdjacencyWDigraph g = new AdjacencyWDigraph(4); System.out.println("Edges = " + g.edges()); System.out.println(); g.putEdge(new WeightedEdge(2, 4, new Integer(1))); g.putEdge(new WeightedEdge(1, 3, new Integer(2))); g.putEdge(new WeightedEdge(2, 1, new Integer(3))); g.putEdge(new WeightedEdge(1, 4, new Integer(4))); g.putEdge(new WeightedEdge(4, 2, new Integer(5))); System.out.println("The graph is"); g.output(); System.out.println(); g.removeEdge(2,1); System.out.println("The graph after deleting (2,1) is"); g.output(); System.out.println(); System.out.println("existsEdge(3,1) = " + g.existsEdge(3,1)); System.out.println("existsEdge(1,3) = " + g.existsEdge(1,3)); System.out.println("inDegree(3) = " + g.inDegree(3)); System.out.println("outDegree(1) = " + g.outDegree(1)); System.out.println("Edges = " + g.edges()); System.out.println(); // test iterator Iterator iter = g.iterator(4); System.out.println("Edges out of vertex 4 are"); while (iter.hasNext()) { WeightedEdgeNode w = (WeightedEdgeNode) iter.next(); System.out.println("(4, " + w + ")"); } } }