Hello!

I'm having a problem with this program I want to write. I have bipartite graph and I first want to store vertex from the left side in array, and then under that every vertex I want to save all the edges, that go from that vertex to another (the number of edges varies). I've tries with ArrayList of ArrayList's but I have problems, with searching for existing vertices in the array. So if anyone knows for a better way, or knows how to realize that with ArrayList I would be very grateful.

Greets!

## 5 Replies - 2416 Views - Last Post: 04 May 2010 - 04:49 PM

##
**Replies To:** Choosing data structure

### #2

## Re: Choosing data structure

Posted 04 May 2010 - 03:12 PM

You could try designing your own Generic Graph structure with your own Edge, Vertex, and Graph classes, some of which may use List structures like LinkedList and ArrayList.

I cover custom Graph design in my tutorial on Graphs if you want to check it out.

I cover custom Graph design in my tutorial on Graphs if you want to check it out.

### #3

## Re: Choosing data structure

Posted 04 May 2010 - 04:22 PM

I had similar classes than you do, for my graph. But the problem is, that I don't know how to find out if there is all ready some vertex from set A in graphs ArrayList. I need that because then I have to retrieve that vertex from my graph, and add another connection (edge) to it, so it can have multiple connections to vertices from set B.

### #4

## Re: Choosing data structure

Posted 04 May 2010 - 04:26 PM

That's why I store a List<Vertex> (or as I refer to it in my tutorial, a List<Edge>) in my Graph class- so you can randomly acces Vertices on the Graph. All interfacing for the Graph is done through the main Graph class, with the Connector and Edge classes running around in the background making things work.

### #5

## Re: Choosing data structure

Posted 04 May 2010 - 04:43 PM

Ok, for example ... I make one vertex - vertex1 and then connect him to vertex2 and then add vertex1 to the graph. Then I add some other vertices with only one connection, and then I want to add another vertex to that vertex1 that I added before. My problem is, that I can't retrieve that vertex from my graph is I don't know the number of it's position in the ArrayList, and here is where I'm stuck.

### #6

## Re: Choosing data structure

Posted 04 May 2010 - 04:49 PM

If you have a reference to the Vertex, you can use the ArrayList indexOf() method to locate its position. I also included id attributes for my Vertices to help you search. You'll need to have some idea of what you are looking for in your Graph to find it.

Page 1 of 1