我使用Java书中的以下代码来实现加权的unidrected图形版本。
Vertex.Java
public class Vertex<T> {
/**
* Client-supplied vertex information.
*/
protected T info;
/**
* Neighbors of this vertex.
*/
protected List<Neighbor> neighbors;
/**
* Initializes a new instance with vertex information.
*
* @param vertexInfo Vertex information.
*/
protected Vertex(T vertexInfo) {
info = vertexInfo;
neighbors = new List<Neighbor>();
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object other) {
if ((other != null) && (other instanceof Vertex)) {
Vertex another = (Vertex)other;
return (info.equals(another.info));
}
return false;
}
}
Neighbor.Java - &gt;存储顶点的权重和数量的对象
public class Neighbor {
/**
* Internal vertex number of neighbor.
*/
public int vertexNumber;
public int weight;
/**
* Initializes a new instance with given vertex number.
*
* @param vertexNum Vertex number.
*/
public Neighbor(int vertexNum) {
vertexNumber = vertexNum;
}
//constructor for weighted neighbour
public Neighbor(int vertexNum, int weight){
vertexNumber = vertexNum;
this.weight = weight;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object other) {
if ((other != null) && (other instanceof Neighbor)) {
Neighbor another = (Neighbor)other;
return (vertexNumber == another.vertexNumber);
}
return false;
}
}
DirGraph.Java
import java.util.ArrayList;
public class DirGraph<T> {
/**
* Array of Vertex instances (vertex info and adjacency list)
*/
protected ArrayList<Vertex<T>> adjlists;
/**
* Initializes a new directed graph instance of default initial vertex capacity.
*/
public DirGraph() {
adjlists = new ArrayList<Vertex<T>>();
}
/**
* Initializes a new directed graph instance of given initial vertex capacity.
*
* @param vertexCap Initial capacity (number of vertices).
*/
public DirGraph(int vertexCap) {
adjlists = new ArrayList<Vertex<T>>(vertexCap);
}
/**
* Returns the number of vertices in this graph.
*
* @return Number of vertices in this graph.
*/
public int numberOfVertices() {
return adjlists.size();
}
/**
* Adds a vertex to this graph.
*
* @param vertex Vertex to be added.
* @return Number assigned to this vertex in the graph.
*/
public int addVertex(T vertex) {
if (!containsVertex(vertex)) {
adjlists.add(new Vertex<T>(vertex));
}
return adjlists.size() - 1;
}
/**
* Tells whether this graph contains a given vertex or not.
*
* @param vertex Vertex to be searched for in this graph.
* @return True if the given vertex is in this graph, false otherwise.
*/
public boolean containsVertex(T vertex) {
return adjlists.indexOf(new Vertex<T>(vertex)) != -1;
}
/**
* Returns the internal vertex number for the given vertex.
*
* @param vertex Vertex for which internal number is needed.
* @return Internal number assigned to the given vertex, -1 if the vertex is not in this graph.
*/
public int vertexNumberOf(T vertex) {
return adjlists.indexOf(new Vertex<T>(vertex));
}
/**
* Returns the client-supplied vertex information associated with a given internal vertex number.
*
* @param vertexNumber Internal vertex number.
* @return Associated client-supplied vertex information.
*/
public T vertexInfoOf(int vertexNumber) {
Vertex<T> v = adjlists.get(vertexNumber);
return v.info;
}
/**
* Tells whether there is an edge from a given vertex (internal number) to another (neighbor).
*
* @param vertexNumber Internal number of vertex.
* @param nbr Neighbor to which edge is sought.
* @return True if there is an edge, false otherwise.
*/
public boolean containsEdge(int vertexNumber, Neighbor nbr) {
Vertex<T> v = adjlists.get(vertexNumber);
return v.neighbors.contains(nbr);
}
/**
* Adds an edge from a given vertex (internal number) to another (neighbor). Note: If
* this vertex already has an edge to this neighbor, this method will return without
* doing anything. In other words, multiple edges are not supported.
*
* @param vertexNumber Internal number of vertex.
* @param nbr Neighbor to which edge is added.
*/
public void addEdge(int vertexNumber, Neighbor nbr) {
Vertex<T> fromVertex = adjlists.get(vertexNumber);
if (!fromVertex.neighbors.contains(nbr)) {
fromVertex.neighbors.add(nbr);
}
}
/**
* Returns the first neighbor of a given vertex.
*
* @param vertexNumber Internal number of vertex.
* @return First neighbor of given vertex, null if there are no neighbors.
*/
public Neighbor firstNeighbor(int vertexNumber) {
Vertex<T> v = adjlists.get(vertexNumber);
return v.neighbors.first();
}
/**
* Returns the next neighbor of a given vertex.
*
* @param vertexNumber Internal number of vertex.
* @return Next neighbor of given vertex, relative to an earlier call to
* first() or or next(); null if end of neighbors list is reached.
*/
public Neighbor nextNeighbor(int vertexNumber) {
Vertex<T> v = adjlists.get(vertexNumber);
return v.neighbors.next();
}
/**
* Clears this graph of all vertices and edges.
*/
public void clear() {
adjlists.clear();
}
}
WeightedUndirGraph.Java我从超类中复制了addEdge方法并为其添加了权重。但是,当我实现代码时
public class WeightedUndirGraph<T> extends UndirGraph<T> {
public WeightedUndirGraph(){
super();
}
public WeightedUndirGraph(int vertexCap) {
super(vertexCap);
}
public void addEdge(int vertexNumber, Neighbor nbr, int weight) {
Vertex<T> fromVertex = adjlists.get(vertexNumber);
if (!fromVertex.neighbors.contains(nbr)) {
fromVertex.neighbors.add(nbr);
}
}
public static void main(String[] args){
//create new graph
WeightedUndirGraph<Object> myGraph = new WeightedUndirGraph<>();
System.out.println(myGraph.numberOfVertices());
//create a vertex and add it to graph
Vertex<Object> vWinnipeg = new Vertex<>("Winnipeg");
myGraph.addVertex(vWinnipeg);
//test
System.out.println(myGraph.numberOfVertices());
//create neighbors and add it to vertex vWinnipeg
Neighbor nHalifax = new Neighbor(1,500);
myGraph.addEdge(0, nHalifax, 500);
Neighbor nCalgary = new Neighbor(1,500);
myGraph.addEdge(0, nCalgary, 600);
//print out info of vertex
System.out.println(myGraph.vertexInfoOf(0));
}
}
当我打印出顶点vWinnipeg的信息时,我没有得到顶点的邻居列表。它只打印出顶点内存对象。