0

A request from a noob Java beginner. I know it's a naive question, but till now I haven't found a satisfying solution. Some of the posts on stackoverflow currently either are for unweighted graphs, or involve complicated graph implementation work.

I have a set of vertices, labeled by integers 1, 2, 3, ....and some of them are linked with an undirected weighted edge (weights are integers also). I have managed to store these vertices already by using HashSet, which is convenient enough for me to manipulate (add, search, remove, etc.)

However, I have difficulty to store these edges by using built-in data structure in Java. Generally, I just want a simple built-in data structure to store edges with weight, such that I can search & select edges linked to a certain vertex quickly. I have some primary naive thoughts. For example, ArrayList<ArrayList<ArrayList>> & HashMap<int[], Integer> (thought the latter one won't work as expected, I just want to set a pair of integer of vertices for edge (a, b) as input to get its weight from the data structure) However, they are obviously inefficient to use. I don't want to use adjacency matrix since its size is large enough to be unacceptable for me.

Is such a demand feasible by simply using built-in data structure in Java? If yes, can you tell me what should I do? If not, can you come up with an implementation of graph which is easy for the work mentioned above? Thank you in advance.

  • 1
    I thing that a better approach would be to represent the graph and its components in using classes. For example define a `Node` class which has 2 attributes: a name (String) and a list of Edges. Define an `Edge` class which has 2 attributes: a `Node` and the distance to it (int). A `Graph` would be a collection of `Node`s. See an example [here](https://stackoverflow.com/a/57534732/3992939) – c0der Oct 30 '20 at 04:45
  • @c0der thank you! However, for label of vertices I just want to use integers (for convenience, since mu input are integers). When I change all String to int or Integer in class Vertex and want to create vertices like Vertex v1 = new Vertex(1); , it turns out java.lang.NoSuchMethodError: Vertex.(I)V or java.lang.NoSuchMethodError: Vertex.(Ljava/lang/Integer;)V. How to fix that? – heklmbbsna Oct 30 '20 at 05:30
  • Post [mre] of your code. A new post would be better because it is a different question. – c0der Oct 30 '20 at 05:34
  • @c0der what I did is basically in your code class Vertex, turn private String label; to private int label; turn public Vertex(String pageObject) to public Vertex(int pageObject), turn String getLabel() to int getLabel(), and remain other statements the same. Then when I tried to create a vertex by using Vertex v1 = new Vertex(1); in the main function, I encountered the error mentioned above. I think it's not appropriate to start a new thread since it's kinda hard to describe the problem without the context of your sample code. – heklmbbsna Oct 30 '20 at 05:46
  • Please do not describe your code. Post it. https://repl.it/@c0derrepo/LoneKeyTruetype#Main.java – c0der Oct 30 '20 at 05:48
  • @c0der repl.it/@AlbertHe/GraphTest#Main.java here it is. It seems to work fine on the online platform, but it cannot work on my pc vs code, so I think probably it's my own issue, not code's. The platform took a period of time to execute the program, I hope that's only because of some network issue. – heklmbbsna Oct 30 '20 at 06:06
  • Does the linked code compile on your platform ? – c0der Oct 30 '20 at 06:09
  • @c0der compile successfully but raise exception mentioned above – heklmbbsna Oct 30 '20 at 06:14

1 Answers1

0

Honestly, I find I want to use tuples often in Java, and there isn't a built in tuple class. I wind up making my own - it's straightforward generics.

Stephen B.
  • 396
  • 2
  • 19