java java中的图形实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10255479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:15:12  来源:igfitidea点击:

Graphs implementation in java

javagraphimplementation

提问by FranXh

I am trying to create a Graph class that uses another class, the Vertex class to represent all the vertices of the graph. I am not sure if I need an Edge class that will represent the possible connections between two vertices, because every vertex can keep track of the other nodes it is connected to. But I am not sure if this is correct. What do you think?

我正在尝试创建一个 Graph 类,该类使用另一个类 Vertex 类来表示图形的所有顶点。我不确定是否需要一个 Edge 类来表示两个顶点之间可能的连接,因为每个顶点都可以跟踪它所连接的其他节点。但我不确定这是否正确。你怎么认为?

Thank you.

谢谢你。

回答by blackcompe

You don't have to use an Edgeclass. You can use adjacency lists and still represent an unweightedgraph correctly. For a weighted graph you need a way to represent edge costs, and thus using an Edgeclass would be appropriate.

您不必使用Edge类。您可以使用邻接列表并仍然正确表示未加权的图。对于加权图,您需要一种表示边成本的方法,因此使用Edge类是合适的。

class Graph<E> {
    private List<Vertex<E>> vertices;

    private static class Vertex<E> {
        E elem;
        List<Vertex<E>> neighbors;
    }
}

回答by trashgod

Typically, a representationis chosen based in its suitability to the intended use. In this simple example, GraphPaneluses nothing more than a List<Edge>as its model.

通常,根据其对预期用途的适用性来选择表示。在这个简单的示例中,仅GraphPanel使用 aList<Edge>作为其模型。