Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphs implementation in java

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.

like image 790
FranXh Avatar asked Dec 17 '22 01:12

FranXh


1 Answers

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;
    }
}
like image 176
blackcompe Avatar answered Jan 02 '23 17:01

blackcompe