Graphic implementation in Java

I'm trying to create a graph class that uses another class. The vertex class represents all the vertices of the graph I'm not sure if I need an edge class that represents the possible connection between two vertices, because each vertex can track the other nodes it connects to But I'm not sure if that's right What's your opinion?

thank you.

Solution

You do not have to use the edge class You can use the adjacency list and still correctly represent the unweighted graph For weighted graphs, you need a way to represent the edge cost, so it is appropriate to use the edge class

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

    private static class Vertex<E> {
        E elem;
        List<Vertex<E>> neighbors;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>