Here is my point of view of working with graphs and my experience with them.
First
A graph is a graph, not an array, not a matrix, not of these. Why am I saying that? Because the structure of a graph could be really, really big if you want to represent the relation in a matrix. The growth of a matrix to represent graphs could run exponentially.
Works with objects
One type of representation that you can do is with java objects. Create two types of objects. One will be the Node and the other will be de edge.
The Node object should have one array of edges.
Like this: (I'm not a java developer so here is in python)
class Node:
def __init__(self, value): #here is the constructor
self.value = obj #here we are setting the identification of node
self.edges = []
def setEdges(self, edge)
self.edges.append(edge)
#do other stuffs
The edge will be like this:
class Edge:
def __init__(self, node1, node2, value):
self.value = value
self.nodes = [node1, node2]
node1.setEdges(self) #self is the same as this in java
node2.setEdges(self)
What we have here now? We have one object that will store the edges that it has, and other object that stores just a value and set his relations. That it works to set the relation. So how to use this in the main script?
The main Script
Here is going to be easy. Look to an example:
me = Node('Me')
you = Node('You')
So, how to set the relation?
Edge(me, you, 'StackOverflow')
This is the edge that explicits the relation of me and you by the StackOverflow.
Here you will be able to see in each of objects the array of edges that they have like this:
me.edges
This will return the array of Edges objects, and with this edge you can find what type of relation you have, and with whom.
But... How this edge will not be distroyed by the gb?
Simple, both Node classes has references to the Edge class. If none of them has relations to the edge class the gb will consume it.
By Ref in Java
I don't remember how set an object by Ref in Java. I didn't programming in Java since the college. So to this thing works right check the By Ref in java, due to Python byRef is implicit.
So with this implementation you can take small arrays, and you can make sure that your nodes and adges will exists with relations.