I have to make an application that uses Graphs (Data Structure) but I don’t know how to represent them, and was asking if you can give me some hints.
Should I create a class Vertex and Edge? If yes, what should be their attributes?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I suggest using adjacency lists for graphs.
The simplest way is probably to make a
Vertexclass, which contains anArrayList<Vertex>list of links to adjacent vertexes. This is sufficient to represent any graph, you don’t need a separateEdgeclass.You can add whatever other data attributes you like to the vertex class, but the list of links is all you strictly need.
Note that you can have either directed edges (one-way links) or undirected edges (adjacent vertices point back to each other).