Graphs implementation in java

 ̄綄美尐妖づ 提交于 2019-12-12 13:33:17

问题


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?

Thank you.


回答1:


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

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

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



回答2:


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



来源:https://stackoverflow.com/questions/10255479/graphs-implementation-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!