0

I am trying to create an undirected graph in Java with a "live feed" of vertices. I have the vertices coming in from a txt file. The file is structured like this,

1 2 #connect node 1 with node 2

2 3 #connect node 1 with node 2

and so on. I have decided to create an ArrayList of ArrayList (a 2d ArrayList) to store the nodes and edges in an array list. My requirement is that I have to maintain constant time in checking if there exists a path between two edges or no. I haven't really used 2d ArrayList and any head-start from you guys would be really appreciated.

1 Answers1

0

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.

Thiago Baldim
  • 5,991
  • 2
  • 26
  • 45
  • Thanks for the reference. I guess whether would this system give me a constant time complexity when checking if an edge exists between two nodes. I was thinking of creating a hashmap which would save the nodes. Say, `Add 2 to 1` This will add an edge and internally, it would do like this: ` For object (node) A, the hashmap value would save 2 in it` I want to know if this approach is alright or no. – sjee_wizzy Jul 19 '16 at 03:52