在 Java 中实现邻接矩阵

问题描述

我的任务是创建一个类“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,80,120,152},{106,209,175},{136,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",45);
    graph01.addEdge("Little Rock",142);
    graph01.addEdge("Fayetteville",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");

}

}

解决方法

如果没有您的 Graph 类的代码,我们将无从得知。 你在哪里卡住了?