views:

454

answers:

6

The graph is very large but undirected. Edges are unweighted..

In my implementation, I have to find the vertex with max degree and do deletion on both vertexes and edges...

Linked list? ArrayList? Map?

Which one is better for my implementation?

+3  A: 

From the above suggested the answer would be

Map with LinkedList...

Your datastructure could be like this(varies according to your requirement)...

Map<?, List<?>>
<Node-name, List-of-nodes-connected-to-it>

Basically, Graphs are best implemented with the help of HASHING and the above datastructure helps a lot in that..

Ritz
This is not true, hashing isn't always best, sometimes an Adjacency Matrix is the best structure. It depends on the algorithm.
Nick Fortescue
Also, not all maps use Hashing, for example TreeMap doesn't.
Nick Fortescue
thankyou for the comment :-)
Ritz
What do you mean by 'depends on the algorithm'? Can give any example?
Any algorithm which has a critical stage finding if two vertexes are adjacent. This is O(1) in a matrix and O(min(A,B)) in a list where A and B are the degree of the vertex
Nick Fortescue
A: 

You could also have a look at specifically-designed libraries, like JUNG

Valentin Rocher
+1  A: 

If your algorithm requires looking up on max degree, then you want a data structure ordered by degree, something like a PriorityQueue would be good.

Then you need to store the graph itself. For deletion quickly I'd recommend something like a Sparse Array structure. If you have to use java.util data structures, then a HashMap from vertexes to the list of connected vertexes offers O(1) deletion.

If you could use 3rd party libraries, then there are a list of answers here of which JGraph and JUNG seem most popular.

Nick Fortescue
+5  A: 

The two fundamental data structures for representing graphs are the adjacency list and the adjacency matrix, see http://en.wikipedia.org/wiki/Adjacency%5Flist and http://en.wikipedia.org/wiki/Adjacency%5Fmatrix The articles also discuss the pros and cons of those two structures.

Chris
Wow, there you go.
gWiz
A: 

Depends on what other requirements you have. A naive, simple approach could be

class Node
{
  List<Node> edges;
  int id;
}

where you'd have a List of all the nodes in the graph. The problem is this can become inconsistent; e.g. node A might be in node B's edges list, but node B might not be in node A's list. To get around this, you could model it as such:

class Edge
{
  Node incidentA;
  Node incidentB;
}

class Node
{
  int id;
}

Again, you'd have List and List of all the edges and nodes in the system. Of course analyzing this data structure would be done in a very different way than in the other approach.

gWiz
A: 

My suggestion would be to store the vertexes in a priority queue. That way you can have very fast access to the vertex with the highest degree. As for how to implement the vertexes, I would store each neighboring vertex in some kind of set data-structure such as a HashSet or a TreeSet to be able to remove stuff efficiently. I wouldn't represent the edges explicitly, it's not needed.

Code, something along the lines of:

class Graph {

  PriorityQueue<Node> vertexes;

  public Graph() {
    vertexes = new PriorityQueue<Vertex>(10,new Vertex());
  }

  public Vertex maxDegreeVertex() {
    return vertexes.peek();
  }

  ...

}

class Vertex implements Comparator<Vertex> {
  HashSet<Vertex> edges;

  public Vertex() {
    edges = new HashSet<Vertex>();
  }

  public compare(Vertex v1, Vertex v2) {
    v2.edges.size().compareTo(v1.edges.size());
  }

  ...

}

Hope this helps.

svenningsson
Thanks for the code..I never use PriorityQueue before, is it easy to do deletion??
Yep, it's real simple. If you want to remove the element with highest priority at the same time at retrieving it, just use the poll method. Removing elements in general is done using the remove method. Just google "priority queue java" for the documentation.
svenningsson
Got it, thanks!One more question, what is the purpose for putting method compare in vertex class?
Oh, it's a bit of a hack I suppose but it makes it easier to implement the compare method. I wanted to feed the priority queue my own custom made comparator. And in order to implement it I needed access to the number of neighbors of a vertex. Since I didn't want to clutter the code snippet with that kind of stuff I just put the compare method in the Vertex class. But you'd be better off implementing is somewhere else, perhaps as an anonymous class created when you create the PriorityQueue.Hope this helps. Good luck!
svenningsson
Vertex should implement Comparable instead of Comparator, to define the natural order. That way you don't have to specify the comparator.
Christoffer Hammarström