Java: how to represent graphs?

前端 未结 12 1481
感动是毒
感动是毒 2020-12-02 12:17

I\'m implementing some algorithms to teach myself about graphs and how to work with them. What would you recommend is the best way to do that in Java? I was thinking somethi

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 13:00

    Time ago I had the same problem and did my own implementation. What I suggest you is to implement another class: Edge. Then, a Vertex will have a List of Edge.

    public class Edge {
        private Node a, b;
        private directionEnum direction;     // AB, BA or both
        private int weight;
        ...
    }
    

    It worked for me. But maybe is so simple. There is this library that maybe can help you if you look into its code: http://jgrapht.sourceforge.net/

提交回复
热议问题