【发布时间】:2021-02-03 18:33:11
【问题描述】:
我的任务是创建一个“GraphOfCity”类,该类运行并解释下面包含的驱动程序代码中的方法。
每次我在 GraphOfCity 上取得进展时,我都会做错事并最终回到原点。
我创建了一个“Graph”类作为占位符,GraphOfCity 类扩展了 Graph。这大约是我所得到的。我相信我需要使用邻接矩阵来执行所有操作。
GraphOfCity 需要包含的方法:
getSize()
getNeighbors()
getDegree()
isEmpty()
addVertex()
addEdge()
printEdges()
printVertices()
deleteEdge()
我已经尝试了几个小时,但没有取得任何进展,提前非常感谢!
驱动代码:
public class testcode01 {
public static void main(String args[])
{
Graph graph01 = new GraphOfCity();
String[] city = {"Little Rock", "Fayetteville", "Bentonville", "Fort Smith", "Harrison"};
int[][] distance =
{
{0, 92, 106, 136, 67},
{92, 0, 80, 120, 152},
{106, 80, 0, 209, 175},
{136, 120, 209, 0, 95},
{67, 152, 175, 95, 0}
};
Graph graph02 = new GraphOfCity(city, distance);
graph01.getSize();
graph02.getSize();
graph01.getNeighbors("Fayetteville");
graph02.getNeighbors("Fayetteville");
graph01.getDegree("Fayetteville");
graph02.getDegree("Fayetteville");
graph01.isEmpty();
graph02.isEmpty();
graph01.addVertex("Little Rock");
graph01.addVertex("Fayetteville");
graph01.addVertex("Bentonville");
graph01.addEdge("Little Rock", "Fayetteville", 45);
graph01.addEdge("Little Rock", "Bentonville", 142);
graph01.addEdge("Fayetteville", "Bentonville", 73);
graph01.printEdges();
graph01.printVertices();
graph02.printEdges();
graph02.printVertices();
graph01.deleteEdge("Fayetteville", "Bentonville");
graph01.deleteEdge("Fayetteville", "Bentonville");
graph02.deleteEdge("Fayetteville", "Bentonville");
graph02.deleteEdge("Fayetteville", "Bentonville");
}
}
【问题讨论】:
标签: java arraylist graph adjacency-matrix