Graphing in Java

I need to create an application that uses Graphs (Data Structure), but I don’t know how to represent them, and asked if you can give me some tips.

Create Vertex and Edge class? If so, what should be their attributes?

+4
source share
4 answers

I suggest using adjacency lists for graphs.

The simplest way is probably the Vertex class, which contains a list of ArrayList<Vertex> links for adjacent vertices. This is enough to represent any graph; you do not need a separate Edge class.

You can add any other data attributes that you like in the vertex class, but the list of links is all you need.

Note that you can have either directed edges (one-way links) or undirected edges (adjacent vertices point to each other).

+11
source

You can imagine this in typical ways. See here . For instance:

+6
source

This is not entirely specific to Java. The two most common representations are adjacency matrix and list. Details here

If you need a library, JGraphT is nice

+3
source

It rather depends on your use case, but you can try to use a "graph database" like neo4j

0
source

Source: https://habr.com/ru/post/1399533/


All Articles