【问题标题】:Implementing an Adjacency Matrix in Java在 Java 中实现邻接矩阵
【发布时间】: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


    【解决方案1】:

    如果没有您的 Graph 类的代码,我们将无法提供太多信息。 你在哪里卡住了?

    【讨论】:

    • 这就是给出的所有代码。我创建了另一个“图形”类作为占位符,并使 GraphOfCity 扩展图形。在此之后,我尝试了一些不同的方法来让 GraphOfCity 类朝着正确的方向前进,但是我对邻接矩阵的实现以及如何实现所有请求的方法非常模糊。谢谢!
    猜你喜欢
    • 2017-10-13
    • 2014-05-04
    • 2017-07-24
    • 1970-01-01
    • 2012-12-09
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多