Java图形:无法正确实现加权无向图的addEdge()方法
我使用Java书籍中的以下代码来实现加权的unidrected图版本 JavaJava图形:无法正确实现加权无向图的addEdge()方法,java,data-structures,graph,Java,Data Structures,Graph,我使用Java书籍中的以下代码来实现加权的unidrected图版本 Java public class Vertex<T> { /** * Client-supplied vertex information. */ protected T info; /** * Neighbors of this vertex. */ protected List<Neighbor> nei
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;
}
}
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();
}
}
import java.util.ArrayList;
公共类DirGraph{
/**
*顶点实例数组(顶点信息和邻接列表)
*/
受保护的数组列表;
/**
*初始化默认初始顶点容量的新有向图实例。
*/
公共DirGraph(){
AdjList=新的ArrayList();
}
/**
*初始化给定初始顶点容量的新有向图实例。
*
*@param vertexCap初始容量(顶点数)。
*/
公共方向图(int VERTEXAP){
adjlists=新阵列列表(vertexCap);
}
/**
*返回此图中的顶点数。
*
*@返回此图中的顶点数。
*/
公共int numberoftexts(){
返回adjlists.size();
}
/**
*将顶点添加到此图形。
*
*@要添加的参数顶点。
*@分配给图中此顶点的返回编号。
*/
公共int addVertex(T顶点){
如果(!containsVertex(顶点)){
添加(新顶点(顶点));
}
返回adjlists.size()-1;
}
/**
*说明此图是否包含给定顶点。
*
*要在此图中搜索的@param顶点。
*@如果给定顶点在此图中,则返回True,否则返回false。
*/
公共布尔containsVertex(T顶点){
返回AdjList.indexOf(新顶点(顶点))!=-1;
}
/**
*返回给定顶点的内部顶点编号。
*
*@param vertex需要内部编号的顶点。
*@返回指定给给定顶点的内部编号,-1(如果该顶点不在此图中)。
*/
公共int顶点编号(T顶点){
返回adjlists.indexOf(新顶点(顶点));
}
/**
*返回客户端提供的与给定内部顶点编号关联的顶点信息。
*
*@param vertexNumber内部顶点编号。
*@return关联客户端提供的顶点信息。
*/
公共T顶点信息(int顶点编号){
Vertex v=AdjList.get(vertexNumber);
返回v.info;
}
/**
*指示从给定顶点(内部编号)到另一个顶点(相邻顶点)是否存在边。
*
*@param vertexNumber顶点的内部编号。
*@param nbr查找边缘的邻居。
*@如果有边缘,返回True,否则返回false。
*/
公共布尔包含sedge(整数顶点编号,邻居编号){
Vertex v=AdjList.get(vertexNumber);
返回v.邻居包含(nbr);
}
/**
*将给定顶点(内部编号)的边添加到另一个顶点(相邻顶点)。注意:如果
*此顶点已经有一条到该邻居的边,此方法将返回,而不会返回
*做任何事情。换句话说,不支持多条边。
*
*@param vertexNumber顶点的内部编号。
*@param nbr向其添加边的邻居。
*/
公共无效添加(整数顶点编号,相邻编号){
Vertex fromVertex=AdjList.get(vertexNumber);
如果(!fromVertex.neights.contains(nbr)){
fromVertex.neights.add(nbr);
}
}
/**
*返回给定顶点的第一个邻居。
*
*@param vertexNumber顶点的内部编号。
*@返回给定顶点的第一个邻居,如果没有邻居,则为空。
*/
公共邻居firstNeighbor(int vertexNumber){
Vertex v=AdjList.get(vertexNumber);
返回v.neights.first();
}
/**
*返回给定顶点的下一个邻居。
*
*@param vertexNumber顶点的内部编号。
*@return给定顶点的下一个邻居,相对于先前调用
*first()或next();如果到达邻居列表的末尾,则为null。
*/
公共邻居nextNeighbor(int vertexNumber){
Vertex v=AdjList.get(vertexNumber);
返回v.next();
}
/**
*清除此图表中的所有顶点和边。
*/
公共空间清除(){
adj.clear();
}
}
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));
}
}
public类weightedUnderGraph扩展了UnderGraph{
公共权重ndirgraph(){
超级();
}
公共权重ndirgraph(int vertexCap){
超级(vertexCap);
}
公共无效添加(整数顶点编号、相邻编号、整数权重){
Vertex fromVertex=AdjList.get(vertexNumber);
如果(!fromVertex.neights.contains(nbr)){
fromVertex.neights.add(nbr);
}
}
公共静态void main(字符串[]args){
//创建新图形
WeightedUnderGraph myGraph=新的WeightedUnderGraph();
System.out.println(myGraph.numberoftexts());
//创建顶点并将其添加到图形中
顶点vWinnipeg=新顶点(“温尼伯”);
myGraph.addVertex(vWinnipeg);
//试验
System.out.println(myGraph.numberoftexts());
//创建邻居并将其添加到顶点
邻居nHalifax=新邻居(1500);
myGraph.addEdge(0,nHalifax,500);
邻居nCalgary=新邻居(1500);
myGraph.addEdge(0,nCalgary,600);
//打印出顶点的信息
System.out.println(myGraph.vertexinfo(0));
}
}
当我打印出顶点的信息时,我没有得到顶点的邻域列表。它只是打印出顶点内存对象的数据。如何获取顶点对象?sry。添加了类。那么,类
Vertex
中的info
字段是T类型的
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));
}
}