Running graphs in java

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

Thank.

+3
source share
2 answers

You do not need to use a class Edge. You can use adjacency lists and still present an unweighted graph . For a weighted graph, you need a way to represent the value of the edge, and therefore using a class Edgewould be appropriate.

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

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

, representation . GraphPanel List<Edge> .

+2

All Articles